From 717e03cd4a5a7936c9faa8f9679322bd9a8dc113 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Wed, 12 Dec 2018 17:55:34 +0900 Subject: [PATCH] add find-get overload functions --- toml/get.hpp | 232 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 228 insertions(+), 4 deletions(-) diff --git a/toml/get.hpp b/toml/get.hpp index a27947e..1820585 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -265,17 +265,161 @@ T get(const toml::value& v) return map; } +// ============================================================================ +// find and get + +template +decltype(::toml::get(std::declval())) +get(const toml::table& tab, const toml::key& ky, + std::string tablename = "unknown table") +{ + if(tab.count(ky) == 0) + { + throw std::out_of_range(concat_to_string("[error] key \"", ky, + "\" not found in ", tablename)); + } + return ::toml::get(tab.at(ky)); +} +template +decltype(::toml::get(std::declval<::toml::value&>())) +get(toml::table& tab, const toml::key& ky, + std::string tablename = "unknown table") +{ + if(tab.count(ky) == 0) + { + throw std::out_of_range(concat_to_string("[error] key \"", ky, + "\" not found in ", tablename)); + } + return ::toml::get(tab[ky]); +} +template +decltype(::toml::get(std::declval<::toml::value&&>())) +get(toml::table&& tab, const toml::key& ky, + std::string tablename = "unknown table") +{ + if(tab.count(ky) == 0) + { + throw std::out_of_range(concat_to_string("[error] key \"", ky, + "\" not found in ", tablename)); + } + return ::toml::get(std::move(tab[ky])); +} + +template +decltype(::toml::get(std::declval())) +get(const toml::value& v, const toml::key& ky) +{ + const auto& tab = ::toml::get(v); + if(tab.count(ky) == 0) + { + throw std::out_of_range(detail::format_error_for_value(v, + concat_to_string("[error] key \"", ky, "\" not found"), + concat_to_string("in this table"))); + } + return ::toml::get(tab.at(ky)); +} +template +decltype(::toml::get(std::declval<::toml::value&>())) +get(toml::value& v, const toml::key& ky) +{ + auto& tab = ::toml::get(v); + if(tab.count(ky) == 0) + { + throw std::out_of_range(detail::format_error_for_value(v, + concat_to_string("[error] key \"", ky, "\" not found"), + concat_to_string("in this table"))); + } + return ::toml::get(tab.at(ky)); +} +template +decltype(::toml::get(std::declval<::toml::value&&>())) +get(toml::value&& v, const toml::key& ky) +{ + auto tab = ::toml::get(std::move(v)); + if(tab.count(ky) == 0) + { + throw std::out_of_range(detail::format_error_for_value(v, + concat_to_string("[error] key \"", ky, "\" not found"), + concat_to_string("in this table"))); + } + return ::toml::get(std::move(tab[ky])); +} + + // ============================================================================ // get_or template -inline typename std::remove_cv::type>::type -get_or(const toml::Table& tab, const toml::key& ky, T&& opt) +decltype(::toml::get::type>::type>( + std::declval())) +get_or(const toml::value& v, T&& opt) +{ + try + { + return get::type>::type>(v); + } + catch(...) + { + return std::forward(opt); + } +} +template +decltype(::toml::get::type>::type>( + std::declval())) +get_or(toml::value& v, T&& opt) +{ + try + { + return get::type>::type>(v); + } + catch(...) + { + return std::forward(opt); + } +} +template +decltype(::toml::get::type>::type>( + std::declval())) +get_or(toml::value&& v, T&& opt) +{ + try + { + return get::type>::type>(std::move(v)); + } + catch(...) + { + return std::forward(opt); + } +} + +template +auto get_or(const toml::table& tab, const toml::key& ky, T&& opt) + -> decltype(get_or(std::declval(), std::forward(opt))) { if(tab.count(ky) == 0) {return std::forward(opt);} - return get::type>::type>(tab.find(ky)->second); + return ::toml::get_or(tab.at(ky), std::forward(opt)); } +template +auto get_or(toml::table& tab, const toml::key& ky, T&& opt) + -> decltype(get_or(std::declval(), std::forward(opt))) +{ + if(tab.count(ky) == 0) {return std::forward(opt);} + return ::toml::get_or(tab[ky], std::forward(opt)); +} +template +auto get_or(toml::table&& tab, const toml::key& ky, T&& opt) + -> decltype(get_or(std::declval(), std::forward(opt))) +{ + if(tab.count(ky) == 0) {return std::forward(opt);} + return ::toml::get_or(std::move(tab[ky]), std::forward(opt)); +} + // ============================================================================ // expect @@ -320,5 +464,85 @@ auto expect(toml::value&& v) } } +template +auto expect(const toml::value& v, const toml::key& k) + -> result(v, k)), std::string> +{ + try + { + return ok(get(v, k)); + } + catch(const std::exception& e) + { + return err(e.what()); + } +} +template +auto expect(toml::value& v, const toml::key& k) + -> result(v, k)), std::string> +{ + try + { + return ok(get(v, k)); + } + catch(const std::exception& e) + { + return err(e.what()); + } +} +template +auto expect(toml::value&& v, const toml::key& k) + -> result(std::move(v), k)), std::string> +{ + try + { + return ok(get(std::move(v), k)); + } + catch(const std::exception& e) + { + return err(e.what()); + } +} + +template +auto expect(const toml::table& t, const toml::key& k, std::string tn) + -> result(t, k, std::move(tn))), std::string> +{ + try + { + return ok(get(t, k, std::move(tn))); + } + catch(const std::exception& e) + { + return err(e.what()); + } +} +template +auto expect(toml::table& t, const toml::key& k, std::string tn) + -> result(t, k, std::move(tn))), std::string> +{ + try + { + return ok(get(t, k, std::move(tn))); + } + catch(const std::exception& e) + { + return err(e.what()); + } +} +template +auto expect(toml::table&& t, const toml::key& k, std::string tn) + -> result(std::move(t), k, std::move(tn))), std::string> +{ + try + { + return ok(get(std::move(t), k, std::move(tn))); + } + catch(const std::exception& e) + { + return err(e.what()); + } +} + } // toml #endif// TOML11_GET