mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Added reading/writing std::list
This commit is contained in:
@@ -8,8 +8,10 @@
|
|||||||
|
|
||||||
#include "yaml-cpp/node/node.h"
|
#include "yaml-cpp/node/node.h"
|
||||||
#include "yaml-cpp/node/iterator.h"
|
#include "yaml-cpp/node/iterator.h"
|
||||||
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
@@ -83,8 +85,8 @@ namespace YAML
|
|||||||
struct convert<std::vector<T> > {
|
struct convert<std::vector<T> > {
|
||||||
static Node encode(const std::vector<T>& rhs) {
|
static Node encode(const std::vector<T>& rhs) {
|
||||||
Node node(NodeType::Sequence);
|
Node node(NodeType::Sequence);
|
||||||
for(std::size_t i=0;i<rhs.size();i++)
|
for(typename std::vector<T>::const_iterator it=rhs.begin();it!=rhs.end();++it)
|
||||||
node.append(rhs[i]);
|
node.append(*it);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,6 +100,26 @@ namespace YAML
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct convert<std::list<T> > {
|
||||||
|
static Node encode(const std::list<T>& rhs) {
|
||||||
|
Node node(NodeType::Sequence);
|
||||||
|
for(typename std::list<T>::const_iterator it=rhs.begin();it!=rhs.end();++it)
|
||||||
|
node.append(*it);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool decode(const Node& node, std::list<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
|
||||||
|
@@ -132,6 +132,22 @@ namespace Test
|
|||||||
YAML_ASSERT(node["primes"].as<std::vector<int> >() == primes);
|
YAML_ASSERT(node["primes"].as<std::vector<int> >() == primes);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST StdList()
|
||||||
|
{
|
||||||
|
std::list<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::list<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) {
|
||||||
@@ -165,6 +181,7 @@ namespace Test
|
|||||||
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::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);
|
||||||
|
|
||||||
std::cout << "Node tests: " << passed << "/" << total << " passed\n";
|
std::cout << "Node tests: " << passed << "/" << total << " passed\n";
|
||||||
return passed == total;
|
return passed == total;
|
||||||
|
Reference in New Issue
Block a user