Added reading/writing std::vector

This commit is contained in:
Jesse Beder
2011-09-11 23:14:52 -05:00
parent 9e62bf8349
commit f22f38f7f2
3 changed files with 65 additions and 12 deletions

View File

@@ -7,6 +7,7 @@
#include "yaml-cpp/node/node.h" #include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/iterator.h"
#include <map> #include <map>
#include <sstream> #include <sstream>
@@ -19,10 +20,10 @@ namespace YAML
return Node(rhs); return Node(rhs);
} }
static bool decode(const Node& value, std::string& rhs) { static bool decode(const Node& node, std::string& rhs) {
if(value.Type() != NodeType::Scalar) if(node.Type() != NodeType::Scalar)
return false; return false;
rhs = value.scalar(); rhs = node.scalar();
return true; return true;
} }
}; };
@@ -36,10 +37,10 @@ namespace YAML
return Node(stream.str());\ return Node(stream.str());\
}\ }\
\ \
static bool decode(const Node& value, type& rhs) {\ static bool decode(const Node& node, type& rhs) {\
if(value.Type() != NodeType::Scalar)\ if(node.Type() != NodeType::Scalar)\
return false;\ return false;\
std::stringstream stream(value.scalar());\ std::stringstream stream(node.scalar());\
stream >> rhs;\ stream >> rhs;\
return !!stream;\ return !!stream;\
}\ }\
@@ -66,17 +67,37 @@ namespace YAML
template<typename K, typename V> template<typename K, typename V>
struct convert<std::map<K, V> > { struct convert<std::map<K, V> > {
static Node encode(const std::map<K, V>& rhs) { static Node encode(const std::map<K, V>& rhs) {
Node value(NodeType::Map); Node node(NodeType::Map);
for(typename std::map<K, V>::const_iterator it=rhs.begin();it!=rhs.end();++it) for(typename std::map<K, V>::const_iterator it=rhs.begin();it!=rhs.end();++it)
value[it->first] = it->second; node[it->first] = it->second;
return value; return node;
} }
static bool decode(const Node& value, std::map<K, V>& rhs) { static bool decode(const Node& node, std::map<K, V>& rhs) {
rhs.clear(); rhs.clear();
return false; return false;
} }
}; };
template<typename T>
struct convert<std::vector<T> > {
static Node encode(const std::vector<T>& rhs) {
Node node(NodeType::Sequence);
for(std::size_t i=0;i<rhs.size();i++)
node.append(rhs[i]);
return node;
}
static bool decode(const Node& node, std::vector<T>& 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<T>());
return true;
}
};
} }
#endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -46,9 +46,9 @@ namespace YAML
value_type dereference() const { value_type dereference() const {
const typename base_type::value_type& v = *this->base(); const typename base_type::value_type& v = *this->base();
if(v.pNode) if(v.pNode)
return value_type(Value(*v, m_pMemory)); return value_type(Node(*v, m_pMemory));
if(v.first && v.second) 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(); return value_type();
} }

View File

@@ -102,6 +102,36 @@ namespace Test
YAML_ASSERT(count == 1); YAML_ASSERT(count == 1);
return true; 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<std::string>() == "12345");
YAML_ASSERT(node["device"]["name"].as<std::string>() == "iPhone");
YAML_ASSERT(node["device"]["os"].as<std::string>() == "4.0");
YAML_ASSERT(node["username"].as<std::string>() == "monkey");
return true;
}
TEST StdVector()
{
std::vector<int> 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<std::vector<int> >() == primes);
return true;
}
} }
void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) { 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::SimpleMap, "simple map", passed, total);
RunNodeTest(&Node::MapWithUndefinedValues, "map with undefined values", passed, total); RunNodeTest(&Node::MapWithUndefinedValues, "map with undefined values", passed, total);
RunNodeTest(&Node::MapIteratorWithUndefinedValues, "map iterator 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"; std::cout << "Node tests: " << passed << "/" << total << " passed\n";
return passed == total; return passed == total;