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
f22f38f7
"vscode:/vscode.git/clone" did not exist on "cbaa29dde0289a0c94f5f005c02e7d1b775be130"
Commit
f22f38f7
authored
Sep 11, 2011
by
Jesse Beder
Browse files
Added reading/writing std::vector
parent
9e62bf83
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
12 deletions
+65
-12
include/yaml-cpp/node/convert.h
include/yaml-cpp/node/convert.h
+31
-10
include/yaml-cpp/node/detail/iterator.h
include/yaml-cpp/node/detail/iterator.h
+2
-2
test/new-api/nodetests.cpp
test/new-api/nodetests.cpp
+32
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
f22f38f7
...
...
@@ -7,6 +7,7 @@
#include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/iterator.h"
#include <map>
#include <sstream>
...
...
@@ -19,10 +20,10 @@ namespace YAML
return
Node
(
rhs
);
}
static
bool
decode
(
const
Node
&
valu
e
,
std
::
string
&
rhs
)
{
if
(
valu
e
.
Type
()
!=
NodeType
::
Scalar
)
static
bool
decode
(
const
Node
&
nod
e
,
std
::
string
&
rhs
)
{
if
(
nod
e
.
Type
()
!=
NodeType
::
Scalar
)
return
false
;
rhs
=
valu
e
.
scalar
();
rhs
=
nod
e
.
scalar
();
return
true
;
}
};
...
...
@@ -36,10 +37,10 @@ namespace YAML
return Node(stream.str());\
}\
\
static bool decode(const Node&
valu
e, type& rhs) {\
if(
valu
e.Type() != NodeType::Scalar)\
static bool decode(const Node&
nod
e, type& rhs) {\
if(
nod
e.Type() != NodeType::Scalar)\
return false;\
std::stringstream stream(
valu
e.scalar());\
std::stringstream stream(
nod
e.scalar());\
stream >> rhs;\
return !!stream;\
}\
...
...
@@ -66,17 +67,37 @@ namespace YAML
template
<
typename
K
,
typename
V
>
struct
convert
<
std
::
map
<
K
,
V
>
>
{
static
Node
encode
(
const
std
::
map
<
K
,
V
>&
rhs
)
{
Node
valu
e
(
NodeType
::
Map
);
Node
nod
e
(
NodeType
::
Map
);
for
(
typename
std
::
map
<
K
,
V
>::
const_iterator
it
=
rhs
.
begin
();
it
!=
rhs
.
end
();
++
it
)
valu
e
[
it
->
first
]
=
it
->
second
;
return
valu
e
;
nod
e
[
it
->
first
]
=
it
->
second
;
return
nod
e
;
}
static
bool
decode
(
const
Node
&
valu
e
,
std
::
map
<
K
,
V
>&
rhs
)
{
static
bool
decode
(
const
Node
&
nod
e
,
std
::
map
<
K
,
V
>&
rhs
)
{
rhs
.
clear
();
return
false
;
}
};
template
<
typename
T
>
struct
convert
<
std
::
vector
<
T
>
>
{
static
Node
encode
(
const
std
::
vector
<
T
>&
rhs
)
{
Node
node
(
NodeType
::
Sequence
);
for
(
std
::
size_t
i
=
0
;
i
<
rhs
.
size
();
i
++
)
node
.
append
(
rhs
[
i
]);
return
node
;
}
static
bool
decode
(
const
Node
&
node
,
std
::
vector
<
T
>&
rhs
)
{
if
(
node
.
Type
()
!=
NodeType
::
Sequence
)
return
false
;
rhs
.
clear
();
for
(
const_iterator
it
=
node
.
begin
();
it
!=
node
.
end
();
++
it
)
rhs
.
push_back
(
it
->
as
<
T
>
());
return
true
;
}
};
}
#endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
include/yaml-cpp/node/detail/iterator.h
View file @
f22f38f7
...
...
@@ -46,9 +46,9 @@ namespace YAML
value_type
dereference
()
const
{
const
typename
base_type
::
value_type
&
v
=
*
this
->
base
();
if
(
v
.
pNode
)
return
value_type
(
Valu
e
(
*
v
,
m_pMemory
));
return
value_type
(
Nod
e
(
*
v
,
m_pMemory
));
if
(
v
.
first
&&
v
.
second
)
return
value_type
(
Valu
e
(
*
v
.
first
,
m_pMemory
),
Valu
e
(
*
v
.
second
,
m_pMemory
));
return
value_type
(
Nod
e
(
*
v
.
first
,
m_pMemory
),
Nod
e
(
*
v
.
second
,
m_pMemory
));
return
value_type
();
}
...
...
test/new-api/nodetests.cpp
View file @
f22f38f7
...
...
@@ -102,6 +102,36 @@ namespace Test
YAML_ASSERT
(
count
==
1
);
return
true
;
}
TEST
SimpleSubkeys
()
{
YAML
::
Node
node
;
node
[
"device"
][
"udid"
]
=
"12345"
;
node
[
"device"
][
"name"
]
=
"iPhone"
;
node
[
"device"
][
"os"
]
=
"4.0"
;
node
[
"username"
]
=
"monkey"
;
YAML_ASSERT
(
node
[
"device"
][
"udid"
].
as
<
std
::
string
>
()
==
"12345"
);
YAML_ASSERT
(
node
[
"device"
][
"name"
].
as
<
std
::
string
>
()
==
"iPhone"
);
YAML_ASSERT
(
node
[
"device"
][
"os"
].
as
<
std
::
string
>
()
==
"4.0"
);
YAML_ASSERT
(
node
[
"username"
].
as
<
std
::
string
>
()
==
"monkey"
);
return
true
;
}
TEST
StdVector
()
{
std
::
vector
<
int
>
primes
;
primes
.
push_back
(
2
);
primes
.
push_back
(
3
);
primes
.
push_back
(
5
);
primes
.
push_back
(
7
);
primes
.
push_back
(
11
);
primes
.
push_back
(
13
);
YAML
::
Node
node
;
node
[
"primes"
]
=
primes
;
YAML_ASSERT
(
node
[
"primes"
].
as
<
std
::
vector
<
int
>
>
()
==
primes
);
return
true
;
}
}
void
RunNodeTest
(
TEST
(
*
test
)(),
const
std
::
string
&
name
,
int
&
passed
,
int
&
total
)
{
...
...
@@ -133,6 +163,8 @@ namespace Test
RunNodeTest
(
&
Node
::
SimpleMap
,
"simple map"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
MapWithUndefinedValues
,
"map with undefined values"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
MapIteratorWithUndefinedValues
,
"map iterator with undefined values"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
SimpleSubkeys
,
"simple subkey"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
StdVector
,
"std::vector"
,
passed
,
total
);
std
::
cout
<<
"Node tests: "
<<
passed
<<
"/"
<<
total
<<
" passed
\n
"
;
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