Implemented std::map decode (and fixed bug in the Node iterator - the reference_type should be just a plain value, since it's created on-the-fly)

This commit is contained in:
Jesse Beder
2011-09-12 00:29:39 -05:00
parent b9583dde76
commit 5397a93702
3 changed files with 24 additions and 2 deletions

View File

@@ -76,8 +76,13 @@ namespace YAML
} }
static bool decode(const Node& node, std::map<K, V>& rhs) { static bool decode(const Node& node, std::map<K, V>& rhs) {
rhs.clear(); if(node.Type() != NodeType::Map)
return false; return false;
rhs.clear();
for(const_iterator it=node.begin();it!=node.end();++it)
rhs[it->first.as<K>()] = it->second.as<V>();
return true;
} }
}; };

View File

@@ -23,7 +23,8 @@ namespace YAML
iterator_base<V>, iterator_base<V>,
node_iterator, node_iterator,
V, V,
std::forward_iterator_tag> std::forward_iterator_tag,
V>
{ {
private: private:
template<typename> friend class iterator_base; template<typename> friend class iterator_base;

View File

@@ -148,6 +148,21 @@ namespace Test
YAML_ASSERT(node["primes"].as<std::list<int> >() == primes); YAML_ASSERT(node["primes"].as<std::list<int> >() == primes);
return true; return true;
} }
TEST StdMap()
{
std::map<int, int> squares;
squares[0] = 0;
squares[1] = 1;
squares[2] = 4;
squares[3] = 9;
squares[4] = 16;
YAML::Node node;
node["squares"] = squares;
YAML_ASSERT((node["squares"].as<std::map<int, int> >() == squares));
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) {
@@ -182,6 +197,7 @@ namespace Test
RunNodeTest(&Node::SimpleSubkeys, "simple subkey", passed, total); RunNodeTest(&Node::SimpleSubkeys, "simple subkey", passed, total);
RunNodeTest(&Node::StdVector, "std::vector", passed, total); RunNodeTest(&Node::StdVector, "std::vector", passed, total);
RunNodeTest(&Node::StdList, "std::list", passed, total); RunNodeTest(&Node::StdList, "std::list", passed, total);
RunNodeTest(&Node::StdMap, "std::map", passed, total);
std::cout << "Node tests: " << passed << "/" << total << " passed\n"; std::cout << "Node tests: " << passed << "/" << total << " passed\n";
return passed == total; return passed == total;