mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +00:00
Add std::pair conversion specialization.
This commit is contained in:
@@ -223,6 +223,38 @@ namespace YAML
|
||||
}
|
||||
};
|
||||
|
||||
// std::pair
|
||||
template<typename T, typename U>
|
||||
struct convert<std::pair<T, U> > {
|
||||
static Node encode(const std::pair<T, U>& rhs) {
|
||||
Node node(NodeType::Sequence);
|
||||
node.push_back(rhs.first);
|
||||
node.push_back(rhs.second);
|
||||
return node;
|
||||
}
|
||||
|
||||
static bool decode(const Node& node, std::pair<T, U>& rhs) {
|
||||
if(!node.IsSequence())
|
||||
return false;
|
||||
if (node.size() != 2)
|
||||
return false;
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ < 4
|
||||
//workaround for GCC 3:
|
||||
rhs.first = node[0].template as<T>();
|
||||
#else
|
||||
rhs.first = node[0].as<T>();
|
||||
#endif
|
||||
#if defined(__GNUC__) && __GNUC__ < 4
|
||||
//workaround for GCC 3:
|
||||
rhs.second = node[1].template as<U>();
|
||||
#else
|
||||
rhs.second = node[1].as<U>();
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// binary
|
||||
template<>
|
||||
struct convert<Binary> {
|
||||
|
@@ -168,7 +168,19 @@ namespace Test
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST SimpleAlias()
|
||||
TEST StdPair()
|
||||
{
|
||||
std::pair<int, std::string> p;
|
||||
p.first = 5;
|
||||
p.second = "five";
|
||||
|
||||
YAML::Node node;
|
||||
node["pair"] = p;
|
||||
YAML_ASSERT((node["pair"].as<std::pair<int, std::string> >() == p));
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST SimpleAlias()
|
||||
{
|
||||
YAML::Node node;
|
||||
node["foo"] = "value";
|
||||
@@ -463,8 +475,9 @@ namespace Test
|
||||
TEST ret;
|
||||
try {
|
||||
ret = test();
|
||||
} catch(...) {
|
||||
} catch(const std::exception& e) {
|
||||
ret.ok = false;
|
||||
ret.error = e.what();
|
||||
}
|
||||
if(ret.ok) {
|
||||
passed++;
|
||||
@@ -492,6 +505,7 @@ namespace Test
|
||||
RunNodeTest(&Node::StdVector, "std::vector", passed, total);
|
||||
RunNodeTest(&Node::StdList, "std::list", passed, total);
|
||||
RunNodeTest(&Node::StdMap, "std::map", passed, total);
|
||||
RunNodeTest(&Node::StdPair, "std::pair", passed, total);
|
||||
RunNodeTest(&Node::SimpleAlias, "simple alias", passed, total);
|
||||
RunNodeTest(&Node::AliasAsKey, "alias as key", passed, total);
|
||||
RunNodeTest(&Node::SelfReferenceSequence, "self reference sequence", passed, total);
|
||||
|
Reference in New Issue
Block a user