From 96460a15d491bc2843ced103ca22bda5fb4dcd0c Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 20 Jul 2024 20:57:42 +0900 Subject: [PATCH] feat: support key string conversion in get --- include/toml11/get.hpp | 49 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/include/toml11/get.hpp b/include/toml11/get.hpp index 8cdad9c..3805d09 100644 --- a/include/toml11/get.hpp +++ b/include/toml11/get.hpp @@ -218,16 +218,33 @@ template cxx::enable_if_t::value, T> get(const basic_value&); -// map-like +// std::map (key is convertible from toml::value::key_type) template cxx::enable_if_t, // T is map detail::is_not_toml_type>, // but not toml::table + std::is_convertible::key_type, + typename T::key_type>, // keys are convertible cxx::negation>, // no T.from_toml() cxx::negation>, // no toml::from cxx::negation&>> >::value, T> -get(const basic_value&); +get(const basic_value& v); + +// std::map (key is not convertible from toml::value::key_type, but +// is a std::basic_string) +template +cxx::enable_if_t, // T is map + detail::is_not_toml_type>, // but not toml::table + cxx::negation::key_type, + typename T::key_type>>, // keys are NOT convertible + detail::is_1byte_std_basic_string, // is std::basic_string + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value& v); // toml::from::from_toml(v) template @@ -384,10 +401,13 @@ get(const basic_value& v) // ============================================================================ // map-like types; most likely STL map, like std::map or std::unordered_map. +// key is convertible from toml::value::key_type template cxx::enable_if_t, // T is map detail::is_not_toml_type>, // but not toml::table + std::is_convertible::key_type, + typename T::key_type>, // keys are convertible cxx::negation>, // no T.from_toml() cxx::negation>, // no toml::from cxx::negation&>> @@ -409,6 +429,31 @@ get(const basic_value& v) return m; } +// key is NOT convertible from toml::value::key_type but std::basic_string +template +cxx::enable_if_t, // T is map + detail::is_not_toml_type>, // but not toml::table + cxx::negation::key_type, + typename T::key_type>>, // keys are NOT convertible + detail::is_1byte_std_basic_string, // is std::basic_string + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value& v) +{ + using key_type = typename T::key_type; + using mapped_type = typename T::mapped_type; + + T m; + for(const auto& kv : v.as_table()) + { + m.emplace(detail::string_conv(kv.first), get(kv.second)); + } + return m; +} + // ============================================================================ // user-defined, but convertible types.