Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yaml-cpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
yaml-cpp
Commits
7c33b3cd
Commit
7c33b3cd
authored
Jun 13, 2016
by
Vincent Cogne
Committed by
Jesse Beder
Jun 12, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add convert specialization for std::array.
parent
728e26e4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
0 deletions
+59
-0
include/yaml-cpp/node/convert.h
+35
-0
test/node/node_test.cpp
+24
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
7c33b3cd
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
#pragma once
#pragma once
#endif
#endif
#include <array>
#include <limits>
#include <limits>
#include <list>
#include <list>
#include <map>
#include <map>
...
@@ -241,6 +242,40 @@ struct convert<std::list<T> > {
...
@@ -241,6 +242,40 @@ struct convert<std::list<T> > {
}
}
};
};
// std::array
template
<
typename
T
,
std
::
size_t
N
>
struct
convert
<
std
::
array
<
T
,
N
>>
{
static
Node
encode
(
const
std
::
array
<
T
,
N
>&
rhs
)
{
Node
node
(
NodeType
::
Sequence
);
for
(
const
auto
&
element
:
rhs
)
{
node
.
push_back
(
element
);
}
return
node
;
}
static
bool
decode
(
const
Node
&
node
,
std
::
array
<
T
,
N
>&
rhs
)
{
if
(
!
isNodeValid
(
node
))
{
return
false
;
}
for
(
auto
i
=
0u
;
i
<
node
.
size
();
++
i
)
{
#if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3:
rhs
[
i
]
=
node
[
i
].
template
as
<
T
>
();
#else
rhs
[
i
]
=
node
[
i
].
as
<
T
>
();
#endif
}
return
true
;
}
private
:
static
bool
isNodeValid
(
const
Node
&
node
)
{
return
node
.
IsSequence
()
&&
node
.
size
()
==
N
;
}
};
// std::pair
// std::pair
template
<
typename
T
,
typename
U
>
template
<
typename
T
,
typename
U
>
struct
convert
<
std
::
pair
<
T
,
U
>
>
{
struct
convert
<
std
::
pair
<
T
,
U
>
>
{
...
...
test/node/node_test.cpp
View file @
7c33b3cd
...
@@ -12,6 +12,14 @@
...
@@ -12,6 +12,14 @@
using
::
testing
::
AnyOf
;
using
::
testing
::
AnyOf
;
using
::
testing
::
Eq
;
using
::
testing
::
Eq
;
#define EXPECT_THROW_REPRESENTATION_EXCEPTION(statement, message) \
ASSERT_THROW(statement, RepresentationException); \
try { \
statement; \
} catch (const RepresentationException& e) { \
EXPECT_EQ(e.msg, message); \
}
namespace
YAML
{
namespace
YAML
{
namespace
{
namespace
{
TEST
(
NodeTest
,
SimpleScalar
)
{
TEST
(
NodeTest
,
SimpleScalar
)
{
...
@@ -154,6 +162,22 @@ TEST(NodeTest, SimpleSubkeys) {
...
@@ -154,6 +162,22 @@ TEST(NodeTest, SimpleSubkeys) {
EXPECT_EQ
(
"monkey"
,
node
[
"username"
].
as
<
std
::
string
>
());
EXPECT_EQ
(
"monkey"
,
node
[
"username"
].
as
<
std
::
string
>
());
}
}
TEST
(
NodeTest
,
StdArray
)
{
std
::
array
<
int
,
5
>
evens
{{
2
,
4
,
6
,
8
,
10
}};
Node
node
;
node
[
"evens"
]
=
evens
;
std
::
array
<
int
,
5
>
actualEvens
=
node
[
"evens"
].
as
<
std
::
array
<
int
,
5
>>
();
EXPECT_EQ
(
evens
,
actualEvens
);
}
TEST
(
NodeTest
,
StdArrayWrongSize
)
{
std
::
array
<
int
,
3
>
evens
{{
2
,
4
,
6
}};
Node
node
;
node
[
"evens"
]
=
evens
;
EXPECT_THROW_REPRESENTATION_EXCEPTION
((
node
[
"evens"
].
as
<
std
::
array
<
int
,
5
>>
()),
ErrorMsg
::
BAD_CONVERSION
);
}
TEST
(
NodeTest
,
StdVector
)
{
TEST
(
NodeTest
,
StdVector
)
{
std
::
vector
<
int
>
primes
;
std
::
vector
<
int
>
primes
;
primes
.
push_back
(
2
);
primes
.
push_back
(
2
);
...
...
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