diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 12089ea..099faaf 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -66,7 +66,8 @@ template <> struct convert { static Node encode(const Node& rhs) { return rhs; } - static bool decode(const Node& node, Node& rhs) { + static bool decode(const Node& node, Node& rhs) { //FIXME, this is dangerous + throw std::runtime_error("this should not have been encountered"); rhs.reset(node); return true; } @@ -77,10 +78,10 @@ template <> struct convert { static Node encode(const std::string& rhs) { return Node(rhs); } - static std::pair decode(const Node& node) { + static std::string decode(const Node& node) { if (!node.IsScalar()) throw conversion::DecodeException(""); - return std::make_pair(true, node.Scalar()); + return node.Scalar(); } }; @@ -105,10 +106,10 @@ template <> struct convert<_Null> { static Node encode(const _Null& /* rhs */) { return Node(); } - static std::pair decode(const Node& node) { + static _Null decode(const Node& node) { if (!node.IsNull()) throw conversion::DecodeException(""); - return std::make_pair(node.IsNull(), _Null()); + return _Null(); } }; @@ -172,7 +173,7 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) { return Node(stream.str()); \ } \ \ - static std::pair decode(const Node& node) { \ + static type decode(const Node& node) { \ if (node.Type() != NodeType::Scalar) { \ throw conversion::DecodeException(""); \ } \ @@ -188,19 +189,15 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) { } \ if (std::numeric_limits::has_infinity) { \ if (conversion::IsInfinity(input)) { \ - return std::make_pair(true, \ - std::numeric_limits::infinity()); \ + return std::numeric_limits::infinity(); \ } else if (conversion::IsNegativeInfinity(input)) { \ - return std::make_pair(true, \ - negative_op std::numeric_limits::infinity()); \ + return negative_op std::numeric_limits::infinity(); \ } \ } \ \ if (std::numeric_limits::has_quiet_NaN) { \ if (conversion::IsNaN(input)) { \ - rhs = std::numeric_limits::quiet_NaN(); \ - return std::make_pair(true, \ - std::numeric_limits::quiet_NaN()); \ + return std::numeric_limits::quiet_NaN(); \ } \ } \ \ @@ -240,7 +237,7 @@ template <> struct convert { static Node encode(bool rhs) { return rhs ? Node("true") : Node("false"); } - YAML_CPP_API static std::pair decode(const Node& node); + YAML_CPP_API static bool decode(const Node& node); }; // std::map @@ -253,7 +250,7 @@ struct convert> { return node; } - static std::pair > decode(const Node& node) { + static std::map decode(const Node& node) { if (!node.IsMap()) throw conversion::DecodeException(""); @@ -265,7 +262,7 @@ struct convert> { #else rhs[element.first.as()] = element.second.as(); #endif - return std::make_pair(true, rhs); + return rhs; } }; @@ -279,7 +276,7 @@ struct convert> { return node; } - static std::pair > decode(const Node& node) { + static std::vector decode(const Node& node) { if (!node.IsSequence()) throw conversion::DecodeException(""); @@ -291,7 +288,7 @@ struct convert> { #else rhs.push_back(element.as()); #endif - return std::make_pair(true, rhs); + return rhs; } }; @@ -305,7 +302,7 @@ struct convert> { return node; } - static std::pair > decode(const Node& node) { + static std::list decode(const Node& node) { if (!node.IsSequence()) throw conversion::DecodeException(""); @@ -317,7 +314,7 @@ struct convert> { #else rhs.push_back(element.as()); #endif - return std::make_pair(true, rhs); + return rhs; } }; @@ -332,7 +329,7 @@ struct convert> { return node; } - static std::pair > decode(const Node& node) { + static std::array decode(const Node& node) { if (!isNodeValid(node)) throw conversion::DecodeException(""); @@ -345,7 +342,7 @@ struct convert> { rhs[i] = node[i].as(); #endif } - return std::make_pair(true, rhs); + return rhs; } private: @@ -364,7 +361,7 @@ struct convert> { return node; } - static std::pair > decode(const Node& node) { + static std::pair decode(const Node& node) { if (!node.IsSequence() or node.size() != 2) throw conversion::DecodeException(""); @@ -381,7 +378,7 @@ struct convert> { #else rhs.second = node[1].as(); #endif - return std::make_pair(true, rhs); + return rhs; } }; @@ -392,7 +389,7 @@ struct convert { return Node(EncodeBase64(rhs.data(), rhs.size())); } - static std::pair decode(const Node& node) { + static Binary decode(const Node& node) { if (!node.IsScalar()) throw conversion::DecodeException(""); @@ -402,7 +399,7 @@ struct convert { Binary rhs; rhs.swap(data); - return std::make_pair(true, rhs); + return rhs; } }; } diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index fe95b5d..19dc426 100644 --- a/include/yaml-cpp/node/detail/impl.h +++ b/include/yaml-cpp/node/detail/impl.h @@ -100,10 +100,7 @@ template inline bool node::equals(const T& rhs, shared_memory_holder pMemory) { try { const auto rslt = convert::decode(Node(*this, pMemory)); - if (rslt.first) { - return rslt.second == rhs; - } - return false; + return rslt == rhs; } catch (const conversion::DecodeException& e) { return false; } catch(...) { @@ -115,9 +112,7 @@ inline bool node::equals(const char* rhs, shared_memory_holder pMemory) { try { const auto rslt = convert::decode(Node(*this, std::move(pMemory))); - if (rslt.first) - return rslt.second == rhs; - return false; + return rslt == rhs; } catch (const conversion::DecodeException& e) { return false; } catch(...) { diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 50e1939..3f200a0 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -99,9 +99,7 @@ struct as_if { return fallback; try { - const auto rslt = convert::decode(node); - if (rslt.first) - return rslt.second; + return convert::decode(node); } catch (conversion::DecodeException& e) { return fallback; } catch (...) { @@ -110,6 +108,35 @@ struct as_if { } }; +//specialize for Node +template +struct as_if { + explicit as_if(const Node& node_) : node(node_) {} + const Node& node; + + Node operator()(const S& fallback) const { + if (!node.m_pNode) + return fallback; + + try { + node.reset(node); + return node; + } catch (conversion::DecodeException& e) { + return fallback; + } catch (...) { + std::rethrow_exception(std::current_exception()); + } + } +}; + + + + + + + + + template struct as_if { explicit as_if(const Node& node_) : node(node_) {} @@ -134,11 +161,7 @@ struct as_if { throw TypedBadConversion(node.Mark()); try { - auto result = convert::decode(node); - if (result.first) - return result.second; - else - throw TypedBadConversion(node.Mark()); + return convert::decode(node); } catch(const conversion::DecodeException& e) { throw TypedBadConversion(node.Mark()); } catch (...) { diff --git a/src/convert.cpp b/src/convert.cpp index 5f0b832..c299d5f 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -38,7 +38,7 @@ bool IsFlexibleCase(const std::string& str) { } // namespace namespace YAML { -std::pair convert::decode(const Node& node) { +bool convert::decode(const Node& node) { if (!node.IsScalar()) throw conversion::DecodeException(""); @@ -59,11 +59,11 @@ std::pair convert::decode(const Node& node) { for (const auto& name : names) { if (name.truename == tolower(node.Scalar())) { - return std::make_pair(true, true); + return true; } if (name.falsename == tolower(node.Scalar())) { - return std::make_pair(true, false); + return false; } }