From d5adfe8c7db01e78354e0c32b6e0d6901bce04a0 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 28 Sep 2019 17:01:45 +0900 Subject: [PATCH] refactor: use as_xxx instead of cast because cast() requires `template` specifier inside a template function. it makes code long. --- toml/get.hpp | 55 +++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/toml/get.hpp b/toml/get.hpp index 2fd9296..fedabc1 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -90,7 +90,7 @@ inline detail::enable_if_t::value, T> get(const basic_value& v) { - return static_cast(v.template cast()); + return static_cast(v.as_integer()); } // ============================================================================ @@ -105,7 +105,7 @@ inline detail::enable_if_t::value, T> get(const basic_value& v) { - return static_cast(v.template cast()); + return static_cast(v.as_floating()); } // ============================================================================ @@ -117,7 +117,7 @@ template::value, std::string>& get(basic_value& v) { - return v.template cast().str; + return v.as_string().str; } template::value, std::string> const& get(const basic_value& v) { - return v.template cast().str; + return v.as_string().str; } template::value, std::string>&& get(basic_value&& v) { - return std::move(v.template cast().str); + return std::move(v.as_string().str); } // ============================================================================ @@ -145,7 +145,7 @@ template::value, std::string_view> get(const basic_value& v) { - return std::string_view(v.template cast().str); + return std::string_view(v.as_string().str); } #endif @@ -158,7 +158,7 @@ inline detail::enable_if_t::value, T> get(const basic_value& v) { return std::chrono::duration_cast( - std::chrono::nanoseconds(v.template cast())); + std::chrono::nanoseconds(v.as_local_time())); } // ============================================================================ @@ -174,18 +174,15 @@ get(const basic_value& v) { case value_t::local_date: { - return std::chrono::system_clock::time_point( - v.template cast()); + return std::chrono::system_clock::time_point(v.as_local_date()); } case value_t::local_datetime: { - return std::chrono::system_clock::time_point( - v.template cast()); + return std::chrono::system_clock::time_point(v.as_local_datetime()); } case value_t::offset_datetime: { - return std::chrono::system_clock::time_point( - v.template cast()); + return std::chrono::system_clock::time_point(v.as_offset_datetime()); } default: { @@ -276,7 +273,7 @@ detail::enable_if_t& v) { using value_type = typename T::value_type; - const auto& ar = v.template cast(); + const auto& ar = v.as_array(); T container; container.resize(ar.size()); std::transform(ar.cbegin(), ar.cend(), container.begin(), @@ -298,7 +295,7 @@ detail::enable_if_t& v) { using value_type = typename T::value_type; - const auto& ar = v.template cast(); + const auto& ar = v.as_array(); T container; if(ar.size() != container.size()) @@ -325,7 +322,7 @@ get(const basic_value& v) using first_type = typename T::first_type; using second_type = typename T::second_type; - const auto& ar = v.template cast(); + const auto& ar = v.as_array(); if(ar.size() != 2) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -356,7 +353,7 @@ template::value, T> get(const basic_value& v) { - const auto& ar = v.template cast(); + const auto& ar = v.as_array(); if(ar.size() != std::tuple_size::value) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -388,7 +385,7 @@ get(const basic_value& v) "toml::get only supports map type of which key_type is " "convertible from std::string."); T map; - for(const auto& kv : v.template cast()) + for(const auto& kv : v.as_table()) { map[key_type(kv.first)] = ::toml::get(kv.second); } @@ -421,7 +418,7 @@ T get(const basic_value& v) } // ============================================================================ -// find and get +// find // ---------------------------------------------------------------------------- // these overloads do not require to set T. and returns value itself. @@ -429,7 +426,7 @@ template class M, template class V> basic_value const& find(const basic_value& v, const key& ky) { - const auto& tab = v.template cast(); + const auto& tab = v.as_table(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -443,7 +440,7 @@ template class M, template class V> basic_value& find(basic_value& v, const key& ky) { - auto& tab = v.template cast(); + auto& tab = v.as_table(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -457,7 +454,7 @@ template class M, template class V> basic_value&& find(basic_value&& v, const key& ky) { - auto& tab = v.template cast(); + auto& tab = v.as_table(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -476,7 +473,7 @@ template(std::declval const&>())) find(const basic_value& v, const key& ky) { - const auto& tab = v.template cast(); + const auto& tab = v.as_table(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -492,7 +489,7 @@ template(std::declval&>())) find(basic_value& v, const key& ky) { - auto& tab = v.template cast(); + auto& tab = v.as_table(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -508,7 +505,7 @@ template(std::declval&&>())) find(basic_value&& v, const key& ky) { - auto& tab = v.template cast(); + auto& tab = v.as_table(); if(tab.count(ky) == 0) { throw std::out_of_range(detail::format_underline(concat_to_string( @@ -657,7 +654,7 @@ get_or(const basic_value& v, const T& opt) { try { - return v.template cast().str; + return v.as_string().str; } catch(...) { @@ -671,7 +668,7 @@ get_or(basic_value& v, T& opt) { try { - return v.template cast().str; + return v.as_string().str; } catch(...) { @@ -686,7 +683,7 @@ get_or(basic_value&& v, T&& opt) { try { - return std::move(v.template cast().str); + return std::move(v.as_string().str); } catch(...) { @@ -705,7 +702,7 @@ get_or(const basic_value& v, T&& opt) { try { - return std::move(v.template cast().str); + return std::move(v.as_string().str); } catch(...) {