From a4d0189df38e3105ecabd92f9d7c73a1f414b8da Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 28 Jun 2024 00:18:46 +0900 Subject: [PATCH] refactor: move key_cast to utility --- include/toml11/find.hpp | 50 ++------------------------------------ include/toml11/utility.hpp | 41 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 48 deletions(-) diff --git a/include/toml11/find.hpp b/include/toml11/find.hpp index e06442e..e8ac847 100644 --- a/include/toml11/find.hpp +++ b/include/toml11/find.hpp @@ -4,6 +4,7 @@ #include #include "get.hpp" +#include "utility.hpp" #include "value.hpp" #if defined(TOML11_HAS_STRING_VIEW) @@ -101,54 +102,7 @@ find(basic_value&& v, const std::size_t idx) } // -------------------------------------------------------------------------- -// toml::find(toml::value, toml::key, Ts&& ... keys) - -namespace detail -{ - -// It suppresses warnings by -Wsign-conversion when we pass integer literal -// to toml::find. integer literal `0` is deduced as an int, and will be -// converted to std::size_t. This causes sign-conversion. - -template -std::size_t key_cast(const std::size_t& v) noexcept -{ - return v; -} -template -cxx::enable_if_t>::value, std::size_t> -key_cast(const T& v) noexcept -{ - return static_cast(v); -} - -// for string-like (string, string literal, string_view) - -template -typename basic_value::key_type const& -key_cast(const typename basic_value::key_type& v) noexcept -{ - return v; -} -template -typename basic_value::key_type -key_cast(const typename basic_value::key_type::value_type* v) -{ - return typename basic_value::key_type(v); -} -#if defined(TOML11_HAS_STRING_VIEW) -template -typename basic_value::key_type -key_cast(const std::string_view v) -{ - return typename basic_value::key_type(v); -} -#endif // string_view - -} // detail - -// ---------------------------------------------------------------------------- -// find(v, keys...) +// toml::find(toml::value, toml::key, Ts&& ... keys) w/o conversion template cxx::enable_if_t::value, basic_value> const& diff --git a/include/toml11/utility.hpp b/include/toml11/utility.hpp index 186d7f4..559fd86 100644 --- a/include/toml11/utility.hpp +++ b/include/toml11/utility.hpp @@ -99,6 +99,47 @@ inline std::string make_string(std::size_t len, char c) return std::string(len, c); } +// --------------------------------------------------------------------------- + +// It suppresses warnings by -Wsign-conversion when we pass integer literal +// to toml::find. integer literal `0` is deduced as an int, and will be +// converted to std::size_t. This causes sign-conversion. + +template +std::size_t key_cast(const std::size_t& v) noexcept +{ + return v; +} +template +cxx::enable_if_t>::value, std::size_t> +key_cast(const T& v) noexcept +{ + return static_cast(v); +} + +// for string-like (string, string literal, string_view) + +template +typename basic_value::key_type const& +key_cast(const typename basic_value::key_type& v) noexcept +{ + return v; +} +template +typename basic_value::key_type +key_cast(const typename basic_value::key_type::value_type* v) +{ + return typename basic_value::key_type(v); +} +#if defined(TOML11_HAS_STRING_VIEW) +template +typename basic_value::key_type +key_cast(const std::string_view v) +{ + return typename basic_value::key_type(v); +} +#endif // string_view + } // namespace detail } // namespace toml #endif // TOML11_UTILITY_HPP