From f34639ac844763483abab5323fcd703a4ae2ff3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=BA=E5=8F=B2=E8=89=B2=E7=AC=9B?= <48883004+cishisedis@users.noreply.github.com> Date: Tue, 26 Mar 2019 02:48:03 +0100 Subject: [PATCH] make use of convert::decode_optional --- include/yaml-cpp/node/impl.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 8346f40..e3e086d 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -13,6 +13,11 @@ #include "yaml-cpp/node/iterator.h" #include "yaml-cpp/node/node.h" #include +#include + +#ifdef __cpp_lib_optional +#include +#endif namespace YAML { inline Node::Node() : m_isValid(true), m_pMemory(nullptr), m_pNode(nullptr) {} @@ -115,7 +120,8 @@ struct as_if { explicit as_if(const Node& node_) : node(node_) {} const Node& node; - T operator()() const { + template + auto operator()() const -> typename std::tuple_element<1, std::tuple::decode), T>>::type { if (!node.m_pNode) throw TypedBadConversion(node.Mark()); @@ -124,6 +130,19 @@ struct as_if { return t; throw TypedBadConversion(node.Mark()); } + +#ifdef __cpp_lib_optional + template + auto operator()() const -> typename std::tuple_element<1, std::tuple::decode_optional), T>>::type { + if (!node.m_pNode) + throw TypedBadConversion(node.Mark()); + + std::optional t = convert::decode_optional(node); + if (t) + return *t; + throw TypedBadConversion(node.Mark()); + } +#endif }; template <>