Added overloads for parsing stl maps and vectors

This commit is contained in:
Jesse Beder 2010-03-15 04:25:17 +00:00
parent 083a97b171
commit 326899815f
3 changed files with 40 additions and 3 deletions

36
include/stlnode.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#ifndef STLNODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define STLNODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#include <vector>
#include <map>
namespace YAML
{
template <typename T>
void operator >> (const Node& node, std::vector<T>& v)
{
v.clear();
v.resize(node.size());
for(unsigned i=0;i<node.size();++i)
node[i] >> v[i];
}
template <typename K, typename V>
void operator >> (const Node& node, std::map<K, V>& m)
{
m.clear();
for(Iterator it=node.begin();it!=node.end();++it) {
K k;
V v;
it.first() >> k;
it.second() >> v;
m[k] = v;
}
}
}
#endif // STLNODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@ -6,6 +6,7 @@
#include "parser.h"
#include "node.h"
#include "stlnode.h"
#include "iterator.h"
#include "emitter.h"
#include "stlemitter.h"

View File

@ -1121,9 +1121,9 @@ namespace Test {
" 0.278";
PARSE(doc, input);
StringMap key;
key._["first"] = "Sammy";
key._["last"] = "Sosa";
std::map<std::string, std::string> key;
key["first"] = "Sammy";
key["last"] = "Sosa";
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc[key].size() == 2);
YAML_ASSERT(doc[key]["hr"] == 65);