Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
d1e4c264
Commit
d1e4c264
authored
Jan 12, 2012
by
Jesse Beder
Browse files
Added default parameters for the as<> function (new API)
parent
ddc578db
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
15 deletions
+81
-15
include/yaml-cpp/node/impl.h
include/yaml-cpp/node/impl.h
+66
-15
include/yaml-cpp/node/node.h
include/yaml-cpp/node/node.h
+2
-0
test/new-api/nodetests.cpp
test/new-api/nodetests.cpp
+13
-0
No files found.
include/yaml-cpp/node/impl.h
View file @
d1e4c264
...
@@ -65,26 +65,77 @@ namespace YAML
...
@@ -65,26 +65,77 @@ namespace YAML
}
}
// access
// access
// template helpers
template
<
typename
T
,
typename
S
>
struct
as_if
{
explicit
as_if
(
const
Node
&
node_
)
:
node
(
node_
)
{}
const
Node
&
node
;
const
T
operator
()(
const
S
&
fallback
)
const
{
if
(
!
node
.
m_pNode
)
return
fallback
;
T
t
;
if
(
convert
<
T
>::
decode
(
node
,
t
))
return
t
;
return
fallback
;
}
};
template
<
typename
S
>
struct
as_if
<
std
::
string
,
S
>
{
explicit
as_if
(
const
Node
&
node_
)
:
node
(
node_
)
{}
const
Node
&
node
;
const
std
::
string
operator
()(
const
S
&
fallback
)
const
{
if
(
node
.
Type
()
!=
NodeType
::
Scalar
)
return
fallback
;
return
node
.
Scalar
();
}
};
template
<
typename
T
>
struct
as_if
<
T
,
void
>
{
explicit
as_if
(
const
Node
&
node_
)
:
node
(
node_
)
{}
const
Node
&
node
;
const
T
operator
()()
const
{
if
(
!
node
.
m_pNode
)
throw
std
::
runtime_error
(
"Unable to convert to type"
);
T
t
;
if
(
convert
<
T
>::
decode
(
node
,
t
))
return
t
;
throw
std
::
runtime_error
(
"Unable to convert to type"
);
}
};
template
<
>
struct
as_if
<
std
::
string
,
void
>
{
explicit
as_if
(
const
Node
&
node_
)
:
node
(
node_
)
{}
const
Node
&
node
;
const
std
::
string
operator
()()
const
{
if
(
node
.
Type
()
!=
NodeType
::
Scalar
)
throw
std
::
runtime_error
(
"Unable to convert to string, not a scalar"
);
return
node
.
Scalar
();
}
};
// access functions
template
<
typename
T
>
template
<
typename
T
>
inline
const
T
Node
::
as
()
const
inline
const
T
Node
::
as
()
const
{
{
if
(
!
m_pNode
)
return
as_if
<
T
,
void
>
(
*
this
)();
throw
std
::
runtime_error
(
"Unable to convert to type"
);
T
t
;
if
(
convert
<
T
>::
decode
(
*
this
,
t
))
return
t
;
throw
std
::
runtime_error
(
"Unable to convert to type"
);
}
template
<
>
inline
const
std
::
string
Node
::
as
()
const
{
if
(
Type
()
!=
NodeType
::
Scalar
)
throw
std
::
runtime_error
(
"Unable to convert to string, not a scalar"
);
return
Scalar
();
}
}
template
<
typename
T
,
typename
S
>
inline
const
T
Node
::
as
(
const
S
&
fallback
)
const
{
return
as_if
<
T
,
S
>
(
*
this
)(
fallback
);
}
inline
const
std
::
string
&
Node
::
Scalar
()
const
inline
const
std
::
string
&
Node
::
Scalar
()
const
{
{
return
m_pNode
?
m_pNode
->
scalar
()
:
detail
::
node_data
::
empty_scalar
;
return
m_pNode
?
m_pNode
->
scalar
()
:
detail
::
node_data
::
empty_scalar
;
...
...
include/yaml-cpp/node/node.h
View file @
d1e4c264
...
@@ -22,6 +22,7 @@ namespace YAML
...
@@ -22,6 +22,7 @@ namespace YAML
friend
class
NodeEvents
;
friend
class
NodeEvents
;
friend
class
detail
::
node_data
;
friend
class
detail
::
node_data
;
template
<
typename
>
friend
class
detail
::
iterator_base
;
template
<
typename
>
friend
class
detail
::
iterator_base
;
template
<
typename
T
,
typename
S
>
friend
struct
as_if
;
Node
();
Node
();
explicit
Node
(
NodeType
::
value
type
);
explicit
Node
(
NodeType
::
value
type
);
...
@@ -43,6 +44,7 @@ namespace YAML
...
@@ -43,6 +44,7 @@ namespace YAML
// access
// access
template
<
typename
T
>
const
T
as
()
const
;
template
<
typename
T
>
const
T
as
()
const
;
template
<
typename
T
,
typename
S
>
const
T
as
(
const
S
&
fallback
)
const
;
const
std
::
string
&
Scalar
()
const
;
const
std
::
string
&
Scalar
()
const
;
const
std
::
string
&
Tag
()
const
;
const
std
::
string
&
Tag
()
const
;
void
SetTag
(
const
std
::
string
&
tag
);
void
SetTag
(
const
std
::
string
&
tag
);
...
...
test/new-api/nodetests.cpp
View file @
d1e4c264
...
@@ -286,6 +286,18 @@ namespace Test
...
@@ -286,6 +286,18 @@ namespace Test
node
=
YAML
::
Node
();
node
=
YAML
::
Node
();
return
true
;
return
true
;
}
}
TEST
FallbackValues
()
{
YAML
::
Node
node
=
YAML
::
Load
(
"foo: bar
\n
x: 2"
);
YAML_ASSERT
(
node
[
"foo"
].
as
<
std
::
string
>
()
==
"bar"
);
YAML_ASSERT
(
node
[
"foo"
].
as
<
std
::
string
>
(
"hello"
)
==
"bar"
);
YAML_ASSERT
(
node
[
"baz"
].
as
<
std
::
string
>
(
"hello"
)
==
"hello"
);
YAML_ASSERT
(
node
[
"x"
].
as
<
int
>
()
==
2
);
YAML_ASSERT
(
node
[
"x"
].
as
<
int
>
(
5
)
==
2
);
YAML_ASSERT
(
node
[
"y"
].
as
<
int
>
(
5
)
==
5
);
return
true
;
}
}
}
void
RunNodeTest
(
TEST
(
*
test
)(),
const
std
::
string
&
name
,
int
&
passed
,
int
&
total
)
{
void
RunNodeTest
(
TEST
(
*
test
)(),
const
std
::
string
&
name
,
int
&
passed
,
int
&
total
)
{
...
@@ -332,6 +344,7 @@ namespace Test
...
@@ -332,6 +344,7 @@ namespace Test
RunNodeTest
(
&
Node
::
Bool
,
"bool"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
Bool
,
"bool"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
AutoBoolConversion
,
"auto bool conversion"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
AutoBoolConversion
,
"auto bool conversion"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
Reassign
,
"reassign"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
Reassign
,
"reassign"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
FallbackValues
,
"fallback values"
,
passed
,
total
);
std
::
cout
<<
"Node tests: "
<<
passed
<<
"/"
<<
total
<<
" passed
\n
"
;
std
::
cout
<<
"Node tests: "
<<
passed
<<
"/"
<<
total
<<
" passed
\n
"
;
return
passed
==
total
;
return
passed
==
total
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment