diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 312281f..e415275 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -15,6 +15,11 @@ #include #include +#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) +#include +#define YAML_CPP_HAS_DECODE_DISPATCHER +#endif + namespace YAML { inline Node::Node() : m_isValid(true), m_invalidKey{}, m_pMemory(nullptr), m_pNode(nullptr) {} @@ -88,6 +93,18 @@ inline NodeType::value Node::Type() const { // access // template helpers +#ifdef YAML_CPP_HAS_DECODE_DISPATCHER +template +struct decode_dispatcher { + static std::optional dispatch(const Node& node) { + T t; + if (convert::decode(node, t)) + return {t}; + return std::nullopt; + } +}; +#endif + template struct as_if { explicit as_if(const Node& node_) : node(node_) {} @@ -97,10 +114,14 @@ struct as_if { if (!node.m_pNode) return fallback; +#ifdef YAML_CPP_HAS_DECODE_DISPATCHER + return decode_dispatcher::dispatch(node).value_or(fallback); +#else T t; if (convert::decode(node, t)) return t; return fallback; +#endif } }; @@ -127,9 +148,15 @@ struct as_if { if (!node.m_pNode) throw TypedBadConversion(node.Mark()); +#ifdef YAML_CPP_HAS_DECODE_DISPATCHER + auto t = decode_dispatcher::dispatch(node); + if (t) + return *t; +#else T t; if (convert::decode(node, t)) return t; +#endif throw TypedBadConversion(node.Mark()); } };