From f22f38f7f260aad80b66f5e08fa3aaab2b0f7b49 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sun, 11 Sep 2011 23:14:52 -0500 Subject: [PATCH] Added reading/writing std::vector --- include/yaml-cpp/node/convert.h | 41 +++++++++++++++++++------ include/yaml-cpp/node/detail/iterator.h | 4 +-- test/new-api/nodetests.cpp | 32 +++++++++++++++++++ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 369bf77..f5dfc58 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -7,6 +7,7 @@ #include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/iterator.h" #include #include @@ -19,10 +20,10 @@ namespace YAML return Node(rhs); } - static bool decode(const Node& value, std::string& rhs) { - if(value.Type() != NodeType::Scalar) + static bool decode(const Node& node, std::string& rhs) { + if(node.Type() != NodeType::Scalar) return false; - rhs = value.scalar(); + rhs = node.scalar(); return true; } }; @@ -36,10 +37,10 @@ namespace YAML return Node(stream.str());\ }\ \ - static bool decode(const Node& value, type& rhs) {\ - if(value.Type() != NodeType::Scalar)\ + static bool decode(const Node& node, type& rhs) {\ + if(node.Type() != NodeType::Scalar)\ return false;\ - std::stringstream stream(value.scalar());\ + std::stringstream stream(node.scalar());\ stream >> rhs;\ return !!stream;\ }\ @@ -66,17 +67,37 @@ namespace YAML template struct convert > { static Node encode(const std::map& rhs) { - Node value(NodeType::Map); + Node node(NodeType::Map); for(typename std::map::const_iterator it=rhs.begin();it!=rhs.end();++it) - value[it->first] = it->second; - return value; + node[it->first] = it->second; + return node; } - static bool decode(const Node& value, std::map& rhs) { + static bool decode(const Node& node, std::map& rhs) { rhs.clear(); return false; } }; + + template + struct convert > { + static Node encode(const std::vector& rhs) { + Node node(NodeType::Sequence); + for(std::size_t i=0;i& 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/include/yaml-cpp/node/detail/iterator.h b/include/yaml-cpp/node/detail/iterator.h index 88e31ad..13afb5d 100644 --- a/include/yaml-cpp/node/detail/iterator.h +++ b/include/yaml-cpp/node/detail/iterator.h @@ -46,9 +46,9 @@ namespace YAML value_type dereference() const { const typename base_type::value_type& v = *this->base(); if(v.pNode) - return value_type(Value(*v, m_pMemory)); + return value_type(Node(*v, m_pMemory)); if(v.first && v.second) - return value_type(Value(*v.first, m_pMemory), Value(*v.second, m_pMemory)); + return value_type(Node(*v.first, m_pMemory), Node(*v.second, m_pMemory)); return value_type(); } diff --git a/test/new-api/nodetests.cpp b/test/new-api/nodetests.cpp index 0579b62..5b1ca51 100644 --- a/test/new-api/nodetests.cpp +++ b/test/new-api/nodetests.cpp @@ -102,6 +102,36 @@ namespace Test YAML_ASSERT(count == 1); return true; } + + TEST SimpleSubkeys() + { + YAML::Node node; + node["device"]["udid"] = "12345"; + node["device"]["name"] = "iPhone"; + node["device"]["os"] = "4.0"; + node["username"] = "monkey"; + YAML_ASSERT(node["device"]["udid"].as() == "12345"); + YAML_ASSERT(node["device"]["name"].as() == "iPhone"); + YAML_ASSERT(node["device"]["os"].as() == "4.0"); + YAML_ASSERT(node["username"].as() == "monkey"); + return true; + } + + TEST StdVector() + { + std::vector 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) { @@ -133,6 +163,8 @@ namespace Test RunNodeTest(&Node::SimpleMap, "simple map", passed, total); RunNodeTest(&Node::MapWithUndefinedValues, "map with undefined values", passed, total); RunNodeTest(&Node::MapIteratorWithUndefinedValues, "map iterator with undefined values", passed, total); + RunNodeTest(&Node::SimpleSubkeys, "simple subkey", passed, total); + RunNodeTest(&Node::StdVector, "std::vector", passed, total); std::cout << "Node tests: " << passed << "/" << total << " passed\n"; return passed == total;