Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions include/yaml-cpp/node/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <list>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <type_traits>
#include <valarray>
Expand Down Expand Up @@ -300,6 +301,32 @@ struct convert<std::unordered_map<K, V, H, P, A>> {
}
};

// std::unordered_set
template <typename T, typename H, typename P, typename A>
struct convert<std::unordered_set<T, H, P, A>> {
static Node encode(const std::unordered_set<T, H, P, A>& rhs) {
Node node(NodeType::Sequence);
for (const auto& element : rhs)
node.push_back(element);
return node;
}

static bool decode(const Node& node, std::unordered_set<T, H, P, A>& rhs) {
if (!node.IsSequence())
return false;

rhs.clear();
for (const auto& element : node)
#if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3:
rhs.insert(element.template as<T>());
#else
rhs.insert(element.as<T>());
#endif
return true;
}
};

// std::vector
template <typename T, typename A>
struct convert<std::vector<T, A>> {
Expand Down
29 changes: 29 additions & 0 deletions test/node/node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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>>>;
template <class K, class H=std::hash<K>, class P=std::equal_to<K>> using CustomUnorderedSet = std::unordered_set<K,H,P,CustomAllocator<K>>;

} // anonymous namespace

Expand Down Expand Up @@ -532,6 +533,34 @@ TEST(NodeTest, StdUnorderedMapWithCustomAllocator) {
EXPECT_EQ(squares, actualSquares);
}

TEST(NodeTest, StdUnorderedSet) {
std::unordered_set<int> primes;
primes.insert(2);
primes.insert(3);
primes.insert(5);
primes.insert(7);
primes.insert(11);
primes.insert(13);

Node node;
node["primes"] = primes;
EXPECT_EQ(primes, node["primes"].as<std::unordered_set<int>>());
}

TEST(NodeTest, StdUnorderedSetWithCustomAllocator) {
CustomUnorderedSet<int> primes;
primes.insert(2);
primes.insert(3);
primes.insert(5);
primes.insert(7);
primes.insert(11);
primes.insert(13);

Node node;
node["primes"] = primes;
EXPECT_EQ(primes, node["primes"].as<CustomUnorderedSet<int>>());
}

TEST(NodeTest, StdPair) {
std::pair<int, std::string> p;
p.first = 5;
Expand Down
Loading