From 725d915ba98414e9364d684b2163a90bf69c2df6 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 2 Jun 2019 17:31:49 +0900 Subject: [PATCH] feat(WIP): update toml::get --- toml/get.hpp | 155 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 62 deletions(-) diff --git a/toml/get.hpp b/toml/get.hpp index cf2cec4..0f5ff92 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -10,50 +10,60 @@ namespace toml { +// C++14 alias +template +using enable_if_t = typename std::enable_if::type; + // ============================================================================ // exact toml::* type -template::value, std::nullptr_t>::type = nullptr> -inline T& get(value& v) +template class M, template class V> +enable_if_t>::value, T> & +get(basic_value& v) { - return v.cast::value>(); + return v.template cast>::value>(); } -template::value, std::nullptr_t>::type = nullptr> -inline T const& get(const value& v) +template class M, template class V> +enable_if_t>::value, T> const& +get(const basic_value& v) { - return v.cast::value>(); + return v.template cast>::value>(); } -template::value, std::nullptr_t>::type = nullptr> -inline T&& get(value&& v) +template class M, template class V> +enable_if_t>::value, T> && +get(basic_value&& v) { - return std::move(v.cast::value>()); + return v.template cast>::value>(); } // ============================================================================ // T == toml::value; identity transformation. -template::value, std::nullptr_t>::type = nullptr> -inline T& get(value& v) +template class M, template class V> +inline enable_if_t>::value, T>& +get(basic_value& v) { return v; } -template::value, std::nullptr_t>::type = nullptr> -inline T const& get(const value& v) +template class M, template class V> +inline enable_if_t>::value, T> const& +get(const basic_value& v) { return v; } -template::value, std::nullptr_t>::type = nullptr> -inline T&& get(value&& v) +template class M, template class V> +inline enable_if_t>::value, T> && +get(basic_value&& v) { return std::move(v); } @@ -61,104 +71,125 @@ inline T&& get(value&& v) // ============================================================================ // integer convertible from toml::Integer -template class M, template class V> +inline enable_if_t, // T is integral detail::negation>, // but not bool - detail::negation> // but not toml::integer - >::value, std::nullptr_t>::type = nullptr> -inline T get(const value& v) + detail::negation< // but not toml::integer + detail::is_exact_toml_type>> + >::value, T> +get(const basic_value& v) { - return static_cast(v.cast()); + return static_cast(v.template cast()); } // ============================================================================ // floating point convertible from toml::Float -template class M, template class V> +inline enable_if_t, // T is floating_point - detail::negation> // but not toml::Float - >::value, std::nullptr_t>::type = nullptr> -inline T get(const value& v) + detail::negation< // but not toml::floating + detail::is_exact_toml_type>> + >::value, T> +get(const basic_value& v) { - return static_cast(v.cast()); + return static_cast(v.template cast()); } // ============================================================================ // std::string; toml uses its own toml::string, but it should be convertible to // std::string seamlessly -template::value, std::nullptr_t>::type = nullptr> -inline std::string& get(value& v) +template class M, template class V> +inline enable_if_t::value, std::string>& +get(basic_value& v) { - return v.cast().str; + return v.template cast().str; } -template::value, std::nullptr_t>::type = nullptr> -inline std::string const& get(const value& v) +template class M, template class V> +inline enable_if_t::value, std::string> const& +get(const basic_value& v) { - return v.cast().str; + return v.template cast().str; } -template::value, std::nullptr_t>::type = nullptr> -inline std::string get(value&& v) +template class M, template class V> +inline enable_if_t::value, std::string> const& +get(basic_value&& v) { - return std::move(v.cast().str); + return std::move(v.template cast().str); } // ============================================================================ // std::string_view #if __cplusplus >= 201703L -template::value, std::nullptr_t>::type = nullptr> -inline std::string_view get(const value& v) +template class M, template class V> +inline enable_if_t::value, std::string_view> +get(const basic_value& v) { - return std::string_view(v.cast().str); + return std::string_view(v.template cast().str); } #endif // ============================================================================ // std::chrono::duration from toml::local_time. -template::value, std::nullptr_t>::type = nullptr> -inline T get(const value& v) +template class M, template class V> +inline enable_if_t::value, T> +get(const basic_value& v) { return std::chrono::duration_cast( - std::chrono::nanoseconds(v.cast())); + std::chrono::nanoseconds(v.template cast())); } // ============================================================================ // std::chrono::system_clock::time_point from toml::datetime variants -template::value, - std::nullptr_t>::type = nullptr> -inline T get(const value& v) +template class M, template class V> +inline enable_if_t< + std::is_same::value, T> +get(const basic_value& v) { switch(v.type()) { - case value_t::LocalDate: + case value_t::local_date: { return std::chrono::system_clock::time_point( - v.cast()); + v.template cast()); } - case value_t::LocalDatetime: + case value_t::local_datetime: { return std::chrono::system_clock::time_point( - v.cast()); + v.template cast()); + } + case value_t::offset_datetime: + { + return std::chrono::system_clock::time_point( + v.template cast()); } default: { - return std::chrono::system_clock::time_point( - v.cast()); + throw type_error(detail::format_underline("[error] toml::value " + "bad_cast to std::chrono::system_clock::time_point", { + {std::addressof(detail::get_region(v)), + concat_to_string("the actual type is ", v.type())} + })); } } } +/* // ============================================================================ // forward declaration to use this recursively. ignore this and go ahead. @@ -768,6 +799,6 @@ result expect(const toml::table& t, const toml::key& k, return err(e.what()); } } - +*/ } // toml #endif// TOML11_GET