diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 97b1e13..7acda3e 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -24,7 +24,7 @@ namespace YAML } static bool decode(const Node& node, std::string& rhs) { - if(node.Type() != NodeType::Scalar) + if(!node.IsScalar()) return false; rhs = node.Scalar(); return true; @@ -38,7 +38,7 @@ namespace YAML } static bool decode(const Node& node, _Null& /* rhs */) { - return node.Type() == NodeType::Null; + return node.IsNull(); } }; @@ -77,6 +77,16 @@ namespace YAML YAML_DEFINE_CONVERT_STREAMABLE(long double); #undef YAML_DEFINE_CONVERT_STREAMABLE + + // bool + template<> + struct convert { + static Node encode(bool rhs) { + return rhs ? Node("true") : Node("false"); + } + + static bool decode(const Node& node, bool& rhs); + }; // std::map template @@ -89,7 +99,7 @@ namespace YAML } static bool decode(const Node& node, std::map& rhs) { - if(node.Type() != NodeType::Map) + if(!node.IsMap()) return false; rhs.clear(); @@ -110,7 +120,7 @@ namespace YAML } static bool decode(const Node& node, std::vector& rhs) { - if(node.Type() != NodeType::Sequence) + if(!node.IsSequence()) return false; rhs.clear(); @@ -131,7 +141,7 @@ namespace YAML } static bool decode(const Node& node, std::list& rhs) { - if(node.Type() != NodeType::Sequence) + if(!node.IsSequence()) return false; rhs.clear(); diff --git a/src/node/convert.cpp b/src/node/convert.cpp index 7b1d7a5..dc715f7 100644 --- a/src/node/convert.cpp +++ b/src/node/convert.cpp @@ -1,5 +1,83 @@ #include "yaml-cpp/node/convert.h" +#include "yaml-cpp/node/impl.h" +#include + +namespace +{ + // we're not gonna mess with the mess that is all the isupper/etc. functions + bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; } + bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; } + char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; } + + std::string tolower(const std::string& str) + { + std::string s(str); + std::transform(s.begin(), s.end(), s.begin(), ToLower); + return s; + } + + template + bool IsEntirely(const std::string& str, T func) + { + for(std::size_t i=0;i::decode(const Node& node, bool& rhs) { + if(!node.IsScalar()) + return false; + + // we can't use iostream bool extraction operators as they don't + // recognize all possible values in the table below (taken from + // http://yaml.org/type/bool.html) + static const struct { + std::string truename, falsename; + } names[] = { + { "y", "n" }, + { "yes", "no" }, + { "true", "false" }, + { "on", "off" }, + }; + + if(!IsFlexibleCase(node.Scalar())) + return false; + + for(unsigned i=0;i() == false); + return true; + } } void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) { @@ -302,6 +311,7 @@ namespace Test RunNodeTest(&Node::SelfReferenceMap, "self reference map", passed, total); RunNodeTest(&Node::TempMapVariable, "temp map variable", passed, total); RunNodeTest(&Node::TempMapVariableAlias, "temp map variable alias", passed, total); + RunNodeTest(&Node::Bool, "bool", passed, total); std::cout << "Node tests: " << passed << "/" << total << " passed\n"; return passed == total;