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
bce601f2
Unverified
Commit
bce601f2
authored
Jan 06, 2022
by
Chen
Committed by
GitHub
Jan 05, 2022
Browse files
Support the conversion for std::unordered_map (#932)
parent
d8d94190
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
0 deletions
+56
-0
include/yaml-cpp/node/convert.h
include/yaml-cpp/node/convert.h
+27
-0
test/node/node_test.cpp
test/node/node_test.cpp
+29
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
bce601f2
...
...
@@ -12,6 +12,7 @@
#include <limits>
#include <list>
#include <map>
#include <unordered_map>
#include <sstream>
#include <type_traits>
#include <vector>
...
...
@@ -251,6 +252,32 @@ struct convert<std::map<K, V, C, A>> {
}
};
// std::unordered_map
template
<
typename
K
,
typename
V
,
typename
H
,
typename
P
,
typename
A
>
struct
convert
<
std
::
unordered_map
<
K
,
V
,
H
,
P
,
A
>>
{
static
Node
encode
(
const
std
::
unordered_map
<
K
,
V
,
H
,
P
,
A
>&
rhs
)
{
Node
node
(
NodeType
::
Map
);
for
(
const
auto
&
element
:
rhs
)
node
.
force_insert
(
element
.
first
,
element
.
second
);
return
node
;
}
static
bool
decode
(
const
Node
&
node
,
std
::
unordered_map
<
K
,
V
,
H
,
P
,
A
>&
rhs
)
{
if
(
!
node
.
IsMap
())
return
false
;
rhs
.
clear
();
for
(
const
auto
&
element
:
node
)
#if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3:
rhs
[
element
.
first
.
template
as
<
K
>()]
=
element
.
second
.
template
as
<
V
>();
#else
rhs
[
element
.
first
.
as
<
K
>
()]
=
element
.
second
.
as
<
V
>
();
#endif
return
true
;
}
};
// std::vector
template
<
typename
T
,
typename
A
>
struct
convert
<
std
::
vector
<
T
,
A
>>
{
...
...
test/node/node_test.cpp
View file @
bce601f2
...
...
@@ -40,6 +40,7 @@ class CustomAllocator : public std::allocator<T> {
template
<
class
T
>
using
CustomVector
=
std
::
vector
<
T
,
CustomAllocator
<
T
>>
;
template
<
class
T
>
using
CustomList
=
std
::
list
<
T
,
CustomAllocator
<
T
>>
;
template
<
class
K
,
class
V
,
class
C
=
std
::
less
<
K
>
>
using
CustomMap
=
std
::
map
<
K
,
V
,
C
,
CustomAllocator
<
std
::
pair
<
const
K
,
V
>>>
;
template
<
class
K
,
class
V
,
class
H
=
std
::
hash
<
K
>,
class
P
=
std
::
equal_to
<
K
>>
using
CustomUnorderedMap
=
std
::
unordered_map
<
K
,
V
,
H
,
P
,
CustomAllocator
<
std
::
pair
<
const
K
,
V
>>>
;
}
// anonymous namespace
...
...
@@ -435,6 +436,34 @@ TEST(NodeTest, StdMapWithCustomAllocator) {
EXPECT_EQ
(
squares
,
actualSquares
);
}
TEST
(
NodeTest
,
StdUnorderedMap
)
{
std
::
unordered_map
<
int
,
int
>
squares
;
squares
[
0
]
=
0
;
squares
[
1
]
=
1
;
squares
[
2
]
=
4
;
squares
[
3
]
=
9
;
squares
[
4
]
=
16
;
Node
node
;
node
[
"squares"
]
=
squares
;
std
::
unordered_map
<
int
,
int
>
actualSquares
=
node
[
"squares"
].
as
<
std
::
unordered_map
<
int
,
int
>>
();
EXPECT_EQ
(
squares
,
actualSquares
);
}
TEST
(
NodeTest
,
StdUnorderedMapWithCustomAllocator
)
{
CustomUnorderedMap
<
int
,
int
>
squares
;
squares
[
0
]
=
0
;
squares
[
1
]
=
1
;
squares
[
2
]
=
4
;
squares
[
3
]
=
9
;
squares
[
4
]
=
16
;
Node
node
;
node
[
"squares"
]
=
squares
;
CustomUnorderedMap
<
int
,
int
>
actualSquares
=
node
[
"squares"
].
as
<
CustomUnorderedMap
<
int
,
int
>>
();
EXPECT_EQ
(
squares
,
actualSquares
);
}
TEST
(
NodeTest
,
StdPair
)
{
std
::
pair
<
int
,
std
::
string
>
p
;
p
.
first
=
5
;
...
...
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