mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51: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
|
// binary
|
||||||
template<>
|
template<>
|
||||||
struct convert<Binary> {
|
struct convert<Binary> {
|
||||||
|
@@ -168,6 +168,18 @@ namespace Test
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
TEST SimpleAlias()
|
||||||
{
|
{
|
||||||
YAML::Node node;
|
YAML::Node node;
|
||||||
@@ -463,8 +475,9 @@ namespace Test
|
|||||||
TEST ret;
|
TEST ret;
|
||||||
try {
|
try {
|
||||||
ret = test();
|
ret = test();
|
||||||
} catch(...) {
|
} catch(const std::exception& e) {
|
||||||
ret.ok = false;
|
ret.ok = false;
|
||||||
|
ret.error = e.what();
|
||||||
}
|
}
|
||||||
if(ret.ok) {
|
if(ret.ok) {
|
||||||
passed++;
|
passed++;
|
||||||
@@ -492,6 +505,7 @@ namespace Test
|
|||||||
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);
|
RunNodeTest(&Node::StdMap, "std::map", passed, total);
|
||||||
|
RunNodeTest(&Node::StdPair, "std::pair", passed, total);
|
||||||
RunNodeTest(&Node::SimpleAlias, "simple alias", passed, total);
|
RunNodeTest(&Node::SimpleAlias, "simple alias", passed, total);
|
||||||
RunNodeTest(&Node::AliasAsKey, "alias as key", passed, total);
|
RunNodeTest(&Node::AliasAsKey, "alias as key", passed, total);
|
||||||
RunNodeTest(&Node::SelfReferenceSequence, "self reference sequence", passed, total);
|
RunNodeTest(&Node::SelfReferenceSequence, "self reference sequence", passed, total);
|
||||||
|
Reference in New Issue
Block a user