From 5ef9890d0cb255d8bf698edb754df2a96555201b Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 2 Jun 2019 19:22:17 +0900 Subject: [PATCH] feat: update find_or for basic_value --- toml/get.hpp | 159 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 63 deletions(-) diff --git a/toml/get.hpp b/toml/get.hpp index 416aa04..4e53d2b 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -709,96 +709,106 @@ get_or(const basic_value& v, T&& opt) } } -/* // =========================================================================== // find_or(value, key, fallback) // --------------------------------------------------------------------------- // exact types (return type can be a reference) -template::value, std::nullptr_t>::type = nullptr> -T const& find_or(const toml::value& v, const toml::key& ky, const T& opt) +template class M, template class V> +enable_if_t>::value, T> const& +find_or(const basic_value& v, const key& ky, const T& opt) { if(!v.is_table()) {return opt;} - const auto& tab = toml::get(v); + const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } -template::value, std::nullptr_t>::type = nullptr> -T& find_or(toml::value& v, const toml::key& ky, T& opt) +template class M, template class V> +enable_if_t>::value, T>& +find_or(basic_value& v, const toml::key& ky, T& opt) { if(!v.is_table()) {return opt;} - auto& tab = toml::get(v); + auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab[ky], opt); } -template::value, std::nullptr_t>::type = nullptr> -T&& find_or(toml::value&& v, const toml::key& ky, T&& opt) +template class M, template class V> +enable_if_t>::value, T>&& +find_or(basic_value&& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return opt;} - auto tab = toml::get(std::move(v)); + auto tab = std::move(v).as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(std::move(tab[ky]), std::forward(opt)); } // --------------------------------------------------------------------------- // std::string (return type can be a reference) -template::value, std::nullptr_t>::type = nullptr> -std::string const& find_or(const toml::value& v, const toml::key& ky, const T& opt) + +template class M, template class V> +enable_if_t::value, std::string> const& +find_or(const basic_value& v, const key& ky, const T& opt) { if(!v.is_table()) {return opt;} - const auto& tab = toml::get(v); + const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } -template::value, std::nullptr_t>::type = nullptr> -std::string& find_or(toml::value& v, const toml::key& ky, T& opt) +template class M, template class V> +enable_if_t::value, std::string>& +find_or(basic_value& v, const toml::key& ky, T& opt) { if(!v.is_table()) {return opt;} - auto& tab = toml::get(v); + auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} - return get_or(tab[ky], opt); + return get_or(tab.at(ky), opt); } -template::value, std::nullptr_t>::type = nullptr> -std::string find_or(toml::value&& v, const toml::key& ky, T&& opt) +template class M, template class V> +enable_if_t::value, std::string> +find_or(basic_value&& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return opt;} - auto tab = toml::get(std::move(v)); + auto tab = std::move(v).as_table(); if(tab.count(ky) == 0) {return opt;} - return get_or(std::move(tab[ky]), std::forward(opt)); + return get_or(std::move(tab.at(ky)), std::forward(opt)); } // --------------------------------------------------------------------------- // string literal (deduced as std::string) -template class M, template class V> +enable_if_t< detail::is_string_literal::type>::value, - std::nullptr_t>::type = nullptr> -std::string find_or(const toml::value& v, const toml::key& ky, T&& opt) + std::string> +find_or(const basic_value& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return opt;} - const auto& tab = toml::get(v); + const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return std::string(opt);} return get_or(tab.at(ky), std::forward(opt)); } // --------------------------------------------------------------------------- // others (require type conversion and return type cannot be lvalue reference) -template>, +template class M, template class V> +enable_if_t>>, detail::negation>, detail::negation::type>> - >::value, std::nullptr_t>::type = nullptr> -T find_or(const toml::value& v, const toml::key& ky, T&& opt) + >::value, T> +find_or(const basic_value& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return opt;} - const auto& tab = toml::get(v); + const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), std::forward(opt)); } @@ -808,25 +818,34 @@ T find_or(const toml::value& v, const toml::key& ky, T&& opt) // --------------------------------------------------------------------------- // exact types (return type can be a reference) -template::value, std::nullptr_t>::type = nullptr> -T const& find_or(const toml::table& tab, const toml::key& ky, const T& opt) +template +enable_if_t, detail::is_basic_value, + detail::is_exact_toml_type + >::value, T> const& +find_or(const Table& tab, const key& ky, const T& opt) { if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } -template::value, std::nullptr_t>::type = nullptr> -T& find_or(toml::table& tab, const toml::key& ky, T& opt) +template +enable_if_t, detail::is_basic_value, + detail::is_exact_toml_type + >::value, T>& +find_or(Table& tab, const key& ky, T& opt) { if(tab.count(ky) == 0) {return opt;} return get_or(tab[ky], opt); } -template::value, std::nullptr_t>::type = nullptr> -T&& find_or(toml::table&& tab, const toml::key& ky, T&& opt) +template +enable_if_t, detail::is_basic_value, + detail::is_exact_toml_type + >::value, T>&& +find_or(typename std::remove_reference::type&& tab, const key& ky, T&& opt) { if(tab.count(ky) == 0) {return opt;} return get_or(std::move(tab[ky]), std::forward(opt)); @@ -834,23 +853,32 @@ T&& find_or(toml::table&& tab, const toml::key& ky, T&& opt) // --------------------------------------------------------------------------- // std::string (return type can be a reference) -template::value, std::nullptr_t>::type = nullptr> -std::string const& find_or(const toml::table& tab, const toml::key& ky, const T& opt) +template +enable_if_t, detail::is_basic_value, + std::is_same + >::value, std::string> const& +find_or(const Table& tab, const key& ky, const T& opt) { if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } -template::value, std::nullptr_t>::type = nullptr> -std::string& find_or(toml::table& tab, const toml::key& ky, T& opt) +template +enable_if_t, detail::is_basic_value, + std::is_same + >::value, std::string>& +find_or(Table& tab, const key& ky, T& opt) { if(tab.count(ky) == 0) {return opt;} return get_or(tab[ky], opt); } -template::value, std::nullptr_t>::type = nullptr> -std::string find_or(toml::table&& tab, const toml::key& ky, T&& opt) +template +enable_if_t, detail::is_basic_value, + std::is_same + >::value, std::string> +find_or(Table&& tab, const key& ky, T&& opt) { if(tab.count(ky) == 0) {return opt;} return get_or(std::move(tab[ky]), std::forward(opt)); @@ -858,10 +886,13 @@ std::string find_or(toml::table&& tab, const toml::key& ky, T&& opt) // --------------------------------------------------------------------------- // string literal (deduced as std::string) -template::type>::value, - std::nullptr_t>::type = nullptr> -std::string find_or(const toml::table& tab, const toml::key& ky, T&& opt) +template +enable_if_t, + detail::is_basic_value, + detail::is_string_literal::type> + >::value, std::string> +find_or(const Table& tab, const key& ky, T&& opt) { if(tab.count(ky) == 0) {return std::string(opt);} return get_or(tab.at(ky), std::forward(opt)); @@ -869,17 +900,19 @@ std::string find_or(const toml::table& tab, const toml::key& ky, T&& opt) // --------------------------------------------------------------------------- // others (require type conversion and return type cannot be lvalue reference) -template>, +template +enable_if_t, + detail::is_basic_value, + detail::negation>, detail::negation>, detail::negation::type>> - >::value, std::nullptr_t>::type = nullptr> -T find_or(const toml::table& tab, const toml::key& ky, T&& opt) + >::value, T> +find_or(const Table& tab, const key& ky, T&& opt) { if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), std::forward(opt)); } -*/ // ============================================================================ // expect