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
4ae4cb73
Unverified
Commit
4ae4cb73
authored
Sep 20, 2022
by
sfalmo
Committed by
GitHub
Sep 20, 2022
Browse files
Support conversion for std::valarray (#956)
parent
97ebcf03
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
0 deletions
+42
-0
include/yaml-cpp/node/convert.h
include/yaml-cpp/node/convert.h
+32
-0
test/node/node_test.cpp
test/node/node_test.cpp
+10
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
4ae4cb73
...
...
@@ -15,6 +15,7 @@
#include <unordered_map>
#include <sstream>
#include <type_traits>
#include <valarray>
#include <vector>
#include "yaml-cpp/binary.h"
...
...
@@ -363,6 +364,37 @@ struct convert<std::array<T, N>> {
}
};
// std::valarray
template
<
typename
T
>
struct
convert
<
std
::
valarray
<
T
>>
{
static
Node
encode
(
const
std
::
valarray
<
T
>&
rhs
)
{
Node
node
(
NodeType
::
Sequence
);
for
(
const
auto
&
element
:
rhs
)
{
node
.
push_back
(
element
);
}
return
node
;
}
static
bool
decode
(
const
Node
&
node
,
std
::
valarray
<
T
>&
rhs
)
{
if
(
!
node
.
IsSequence
())
{
return
false
;
}
rhs
.
resize
(
node
.
size
());
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
;
}
};
// std::pair
template
<
typename
T
,
typename
U
>
struct
convert
<
std
::
pair
<
T
,
U
>>
{
...
...
test/node/node_test.cpp
View file @
4ae4cb73
...
...
@@ -352,6 +352,16 @@ TEST(NodeTest, StdArrayWrongSize) {
(
node
[
"evens"
].
as
<
std
::
array
<
int
,
5
>>
()),
ErrorMsg
::
BAD_CONVERSION
);
}
TEST
(
NodeTest
,
StdValrray
)
{
std
::
valarray
<
int
>
evens
{{
2
,
4
,
6
,
8
,
10
}};
Node
node
;
node
[
"evens"
]
=
evens
;
std
::
valarray
<
int
>
actualEvens
=
node
[
"evens"
].
as
<
std
::
valarray
<
int
>>
();
for
(
int
i
=
0
;
i
<
evens
.
size
();
++
i
)
{
EXPECT_EQ
(
evens
[
i
],
actualEvens
[
i
]);
}
}
TEST
(
NodeTest
,
StdVector
)
{
std
::
vector
<
int
>
primes
;
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