From cf28c3fb95dac3560aef0551acd7b82c8c69444b Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 2 Jun 2019 18:36:49 +0900 Subject: [PATCH] feat: update toml::find for basic_value --- toml/get.hpp | 129 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 92 insertions(+), 37 deletions(-) diff --git a/toml/get.hpp b/toml/get.hpp index 88ec0eb..627f6aa 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -231,7 +231,7 @@ template class M, template class V> enable_if_t, // T is map - detail::negation< // but not toml::array + detail::negation< // but not toml::table detail::is_exact_toml_type>> >::value, T> get(const basic_value&); @@ -411,52 +411,55 @@ T get(const basic_value& v) return ::toml::from::from_toml(v); } -/* // ============================================================================ // find and get -template -decltype(::toml::get(std::declval())) -find(const toml::table& tab, const toml::key& ky, - std::string tablename = "unknown table") +// for toml::table. +template +enable_if_t::value, decltype( + ::toml::get(std::declval()))> +find(Table& tab, const toml::key& ky, std::string tn = "unknown table") { if(tab.count(ky) == 0) { - throw std::out_of_range(concat_to_string("[error] key \"", ky, - "\" not found in ", tablename)); + throw std::out_of_range(concat_to_string( + "[error] key \"", ky, "\" not found in ", tn)); } return ::toml::get(tab.at(ky)); } -template -decltype(::toml::get(std::declval<::toml::value&>())) -find(toml::table& tab, const toml::key& ky, - std::string tablename = "unknown table") +template +enable_if_t::value, decltype( + ::toml::get(std::declval()))> +find(Table const& tab, const toml::key& ky, std::string tn = "unknown table") { if(tab.count(ky) == 0) { - throw std::out_of_range(concat_to_string("[error] key \"", ky, - "\" not found in ", tablename)); + throw std::out_of_range(concat_to_string( + "[error] key \"", ky, "\" not found in ", tn)); } - return ::toml::get(tab[ky]); + return ::toml::get(tab.at(ky)); } -template -decltype(::toml::get(std::declval<::toml::value&&>())) -find(toml::table&& tab, const toml::key& ky, - std::string tablename = "unknown table") +template +enable_if_t::value, decltype( + ::toml::get(std::declval()))> +find(typename std::remove_reference&& tab, const toml::key& ky, + std::string tn = "unknown table") { if(tab.count(ky) == 0) { - throw std::out_of_range(concat_to_string("[error] key \"", ky, - "\" not found in ", tablename)); + throw std::out_of_range(concat_to_string( + "[error] key \"", ky, "\" not found in ", tn)); } - return ::toml::get(std::move(tab[ky])); + return ::toml::get(std::move(tab.at(ky))); } -template -decltype(::toml::get(std::declval())) -find(const toml::value& v, const toml::key& ky) +// ---------------------------------------------------------------------------- +// these overloads do not require to set T. and returns value itself. +template class M, template class V> +basic_value const& find(const basic_value& v, const key& ky) { - const auto& tab = ::toml::get(v); + const auto& tab = v.template cast(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -464,13 +467,13 @@ find(const toml::value& v, const toml::key& ky) {std::addressof(detail::get_region(v)), "in this table"} })); } - return ::toml::get(tab.at(ky)); + return tab.at(ky); } -template -decltype(::toml::get(std::declval<::toml::value&>())) -find(toml::value& v, const toml::key& ky) +template class M, template class V> +basic_value& find(basic_value& v, const key& ky) { - auto& tab = ::toml::get(v); + const auto& tab = v.template cast(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -478,13 +481,13 @@ find(toml::value& v, const toml::key& ky) {std::addressof(detail::get_region(v)), "in this table"} })); } - return ::toml::get(tab.at(ky)); + return tab.at(ky); } -template -decltype(::toml::get(std::declval<::toml::value&&>())) -find(toml::value&& v, const toml::key& ky) +template class M, template class V> +basic_value&& find(basic_value&& v, const key& ky) { - auto tab = ::toml::get(std::move(v)); + auto& tab = v.template cast(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -492,9 +495,61 @@ find(toml::value&& v, const toml::key& ky) {std::addressof(detail::get_region(v)), "in this table"} })); } - return ::toml::get(std::move(tab[ky])); + return tab.at(ky); } +// ---------------------------------------------------------------------------- +// find(value, key); + +template class M, template class V> +decltype(::toml::get(std::declval const&>())) +find(const basic_value& v, const key& ky) +{ + const auto& tab = v.template cast(); + if(tab.count(ky) == 0) + { + throw std::out_of_range(detail::format_underline(concat_to_string( + "[error] key \"", ky, "\" not found"), { + {std::addressof(detail::get_region(v)), "in this table"} + })); + } + return ::toml::get(tab.at(ky)); +} + +template class M, template class V> +decltype(::toml::get(std::declval&>())) +find(basic_value& v, const key& ky) +{ + const auto& tab = v.template cast(); + if(tab.count(ky) == 0) + { + throw std::out_of_range(detail::format_underline(concat_to_string( + "[error] key \"", ky, "\" not found"), { + {std::addressof(detail::get_region(v)), "in this table"} + })); + } + return ::toml::get(tab.at(ky)); +} + +template class M, template class V> +decltype(::toml::get(std::declval&&>())) +find(basic_value&& v, const key& ky) +{ + auto& tab = v.template cast(); + if(tab.count(ky) == 0) + { + throw std::out_of_range(detail::format_underline(concat_to_string( + "[error] key \"", ky, "\" not found"), { + {std::addressof(detail::get_region(v)), "in this table"} + })); + } + return ::toml::get(std::move(tab.at(ky))); +} + +/* // ============================================================================ // get_or(value, fallback)