diff --git a/include/toml11/find.hpp b/include/toml11/find.hpp index 560db2a..1d0658d 100644 --- a/include/toml11/find.hpp +++ b/include/toml11/find.hpp @@ -100,6 +100,95 @@ find(basic_value&& v, const std::size_t idx) return basic_value(std::move(v.at(idx))); } +// -------------------------------------------------------------------------- +// find> + +#if defined(TOML11_HAS_OPTIONAL) +template +cxx::enable_if_t::value, T> +find(const basic_value& v, const typename basic_value::key_type& ky) +{ + if(v.contains(ky)) + { + return ::toml::get(v.at(ky)); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value, T> +find(basic_value& v, const typename basic_value::key_type& ky) +{ + if(v.contains(ky)) + { + return ::toml::get(v.at(ky)); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value, T> +find(basic_value&& v, const typename basic_value::key_type& ky) +{ + if(v.contains(ky)) + { + return ::toml::get(std::move(v.at(ky))); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(const basic_value& v, const K& k) +{ + if(static_cast(k) < v.size()) + { + return ::toml::get(v.at(static_cast(k))); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value& v, const K& k) +{ + if(static_cast(k) < v.size()) + { + return ::toml::get(v.at(static_cast(k))); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value&& v, const K& k) +{ + if(static_cast(k) < v.size()) + { + return ::toml::get(std::move(v.at(static_cast(k)))); + } + else + { + return std::nullopt; + } +} +#endif // optional + // -------------------------------------------------------------------------- // toml::find(toml::value, toml::key, Ts&& ... keys) @@ -191,6 +280,88 @@ find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) return find(std::move(v.at(detail::key_cast(k1))), detail::key_cast(k2), ks...); } +#if defined(TOML11_HAS_OPTIONAL) +template +cxx::enable_if_t::value, T> +find(const basic_value& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) +{ + if(v.contains(k1)) + { + return find(v.at(k1), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value, T> +find(basic_value& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) +{ + if(v.contains(k1)) + { + return find(v.at(k1), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value, T> +find(basic_value&& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) +{ + if(v.contains(k1)) + { + return find(v.at(k1), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(const basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + if(static_cast(k1) < v.size()) + { + return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + if(static_cast(k1) < v.size()) + { + return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + if(static_cast(k1) < v.size()) + { + return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +#endif // optional + // =========================================================================== // find_or(value, key, fallback) diff --git a/include/toml11/traits.hpp b/include/toml11/traits.hpp index 7a8832f..7a34fbb 100644 --- a/include/toml11/traits.hpp +++ b/include/toml11/traits.hpp @@ -17,6 +17,10 @@ #include #endif +#if defined(TOML11_HAS_OPTIONAL) +#include +#endif + namespace toml { template @@ -161,6 +165,16 @@ struct is_std_tuple_impl> : std::true_type{}; template using is_std_tuple = is_std_tuple_impl>; +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if __has_include() +template struct is_std_optional_impl : std::false_type{}; +template +struct is_std_optional_impl> : std::true_type{}; +template +using is_std_optional = is_std_optional_impl>; +# endif // +#endif // > C++17 + template struct is_std_array_impl : std::false_type{}; template struct is_std_array_impl> : std::true_type{};