From d7662347f21ad4b81074550a40686e2e18d7a389 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 21 Mar 2020 17:44:23 +0900 Subject: [PATCH] refactor: shorten switch_cast definition by macro --- toml/value.hpp | 231 ++++++++----------------------------------------- 1 file changed, 34 insertions(+), 197 deletions(-) diff --git a/toml/value.hpp b/toml/value.hpp index f693d4c..aa88a82 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -123,205 +123,42 @@ throw_key_not_found_error(const Value& v, const key& ky) } } -// switch by `value_t` and call the corresponding `value::as_xxx()`. {{{ +// switch by `value_t` at the compile time. template struct switch_cast {}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::boolean& invoke(basic_value& v) noexcept - { - return v.as_boolean(); - } - template class T, template class A> - static ::toml::boolean const& invoke(basic_value const& v) noexcept - { - return v.as_boolean(); - } - template class T, template class A> - static ::toml::boolean&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_boolean(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::integer& invoke(basic_value& v) noexcept - { - return v.as_integer(); - } - template class T, template class A> - static ::toml::integer const& invoke(basic_value const& v) noexcept - { - return v.as_integer(); - } - template class T, template class A> - static ::toml::integer&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_integer(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::floating& invoke(basic_value& v) noexcept - { - return v.as_floating(); - } - template class T, template class A> - static ::toml::floating const& invoke(basic_value const& v) noexcept - { - return v.as_floating(); - } - template class T, template class A> - static ::toml::floating&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_floating(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::string& invoke(basic_value& v) noexcept - { - return v.as_string(); - } - template class T, template class A> - static ::toml::string const& invoke(basic_value const& v) noexcept - { - return v.as_string(); - } - template class T, template class A> - static ::toml::string&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_string(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::offset_datetime& invoke(basic_value& v) noexcept - { - return v.as_offset_datetime(); - } - template class T, template class A> - static ::toml::offset_datetime const& invoke(basic_value const& v) noexcept - { - return v.as_offset_datetime(); - } - template class T, template class A> - static ::toml::offset_datetime&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_offset_datetime(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::local_datetime& invoke(basic_value& v) noexcept - { - return v.as_local_datetime(); - } - template class T, template class A> - static ::toml::local_datetime const& invoke(basic_value const& v) noexcept - { - return v.as_local_datetime(); - } - template class T, template class A> - static ::toml::local_datetime&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_local_datetime(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::local_date& invoke(basic_value& v) noexcept - { - return v.as_local_date(); - } - template class T, template class A> - static ::toml::local_date const& invoke(basic_value const& v) noexcept - { - return v.as_local_date(); - } - template class T, template class A> - static ::toml::local_date&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_local_date(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static ::toml::local_time& invoke(basic_value& v) noexcept - { - return v.as_local_time(); - } - template class T, template class A> - static ::toml::local_time const& invoke(basic_value const& v) noexcept - { - return v.as_local_time(); - } - template class T, template class A> - static ::toml::local_time&& invoke(basic_value&& v) noexcept - { - return std::move(v).as_local_time(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static typename basic_value::array_type& - invoke(basic_value& v) noexcept - { - return v.as_array(); - } - template class T, template class A> - static typename basic_value::array_type const& - invoke(basic_value const& v) noexcept - { - return v.as_array(); - } - template class T, template class A> - static typename basic_value::array_type && - invoke(basic_value&& v) noexcept - { - return std::move(v).as_array(); - } -}; -template<> -struct switch_cast -{ - template class T, template class A> - static typename basic_value::table_type& - invoke(basic_value& v) noexcept - { - return v.as_table(); - } - template class T, template class A> - static typename basic_value::table_type const& - invoke(basic_value const& v) noexcept - { - return v.as_table(); - } - template class T, template class A> - static typename basic_value::table_type && - invoke(basic_value&& v) noexcept - { - return std::move(v).as_table(); - } -}; // }}} +#define TOML11_GENERATE_SWITCH_CASTER(TYPE) \ + template<> \ + struct switch_cast \ + { \ + template \ + static typename Value::TYPE##_type& invoke(Value& v) \ + { \ + return v.as_##TYPE(); \ + } \ + template \ + static typename Value::TYPE##_type const& invoke(const Value& v) \ + { \ + return v.as_##TYPE(); \ + } \ + template \ + static typename Value::TYPE##_type&& invoke(Value&& v) \ + { \ + return std::move(v).as_##TYPE(); \ + } \ + }; \ + /**/ +TOML11_GENERATE_SWITCH_CASTER(boolean) +TOML11_GENERATE_SWITCH_CASTER(integer) +TOML11_GENERATE_SWITCH_CASTER(floating) +TOML11_GENERATE_SWITCH_CASTER(string) +TOML11_GENERATE_SWITCH_CASTER(offset_datetime) +TOML11_GENERATE_SWITCH_CASTER(local_datetime) +TOML11_GENERATE_SWITCH_CASTER(local_date) +TOML11_GENERATE_SWITCH_CASTER(local_time) +TOML11_GENERATE_SWITCH_CASTER(array) +TOML11_GENERATE_SWITCH_CASTER(table) + +#undef TOML11_GENERATE_SWITCH_CASTER }// detail