From cf240daf6306d5f5183f7513b165841d9c76f26f Mon Sep 17 00:00:00 2001 From: beder Date: Sun, 11 Sep 2011 23:18:19 -0500 Subject: [PATCH] Added reading/writing std::list --- include/yaml-cpp/node/convert.h | 26 ++++++++++++++++++++++++-- test/new-api/nodetests.cpp | 17 +++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index f5dfc58..7950148 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -8,8 +8,10 @@ #include "yaml-cpp/node/node.h" #include "yaml-cpp/node/iterator.h" +#include #include #include +#include namespace YAML { @@ -83,8 +85,8 @@ namespace YAML struct convert > { static Node encode(const std::vector& rhs) { Node node(NodeType::Sequence); - for(std::size_t i=0;i::const_iterator it=rhs.begin();it!=rhs.end();++it) + node.append(*it); return node; } @@ -98,6 +100,26 @@ namespace YAML return true; } }; + + template + struct convert > { + static Node encode(const std::list& rhs) { + Node node(NodeType::Sequence); + for(typename std::list::const_iterator it=rhs.begin();it!=rhs.end();++it) + node.append(*it); + return node; + } + + static bool decode(const Node& node, std::list& 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()); + return true; + } + }; } #endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/test/new-api/nodetests.cpp b/test/new-api/nodetests.cpp index 5b1ca51..921e983 100644 --- a/test/new-api/nodetests.cpp +++ b/test/new-api/nodetests.cpp @@ -132,6 +132,22 @@ namespace Test YAML_ASSERT(node["primes"].as >() == primes); return true; } + + TEST StdList() + { + std::list 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 >() == primes); + return true; + } } void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) { @@ -165,6 +181,7 @@ namespace Test RunNodeTest(&Node::MapIteratorWithUndefinedValues, "map iterator with undefined values", passed, total); RunNodeTest(&Node::SimpleSubkeys, "simple subkey", passed, total); RunNodeTest(&Node::StdVector, "std::vector", passed, total); + RunNodeTest(&Node::StdList, "std::list", passed, total); std::cout << "Node tests: " << passed << "/" << total << " passed\n"; return passed == total;