From af9b21151ca704414ff82156e1a7eca98e3e62b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Sun, 2 Apr 2017 13:10:15 +0200 Subject: [PATCH] add enum class default conversions --- src/json.hpp | 25 ++++++++++--------------- src/json.hpp.re2c | 25 ++++++++++--------------- test/src/unit-conversions.cpp | 9 +++++++++ 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 90a48538..5df1bacc 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -454,12 +454,6 @@ using enable_if_t = typename std::enable_if::type; template using uncvref_t = typename std::remove_cv::type>::type; -// taken from http://stackoverflow.com/a/26936864/266378 -template -using is_unscoped_enum = - std::integral_constant::value and - std::is_enum::value>; - /* Implementation of two C++17 constructs: conjunction, negation. This is needed to avoid evaluating all the traits in a condition @@ -818,11 +812,12 @@ void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept external_constructor::construct(j, static_cast(val)); } -template::value, int> = 0> -void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept +template::value, int> = 0> +void to_json(BasicJsonType& j, EnumType e) noexcept { - external_constructor::construct(j, e); + using underlying_type = typename std::underlying_type::type; + external_constructor::construct(j, static_cast(e)); } template @@ -937,13 +932,13 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& get_arithmetic_value(j, val); } -template::value, int> = 0> -void from_json(const BasicJsonType& j, UnscopedEnumType& e) +template::value, int> = 0> +void from_json(const BasicJsonType& j, EnumType& e) { - typename std::underlying_type::type val; + typename std::underlying_type::type val; get_arithmetic_value(j, val); - e = static_cast(val); + e = static_cast(val); } template diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index d88830b9..da503495 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -454,12 +454,6 @@ using enable_if_t = typename std::enable_if::type; template using uncvref_t = typename std::remove_cv::type>::type; -// taken from http://stackoverflow.com/a/26936864/266378 -template -using is_unscoped_enum = - std::integral_constant::value and - std::is_enum::value>; - /* Implementation of two C++17 constructs: conjunction, negation. This is needed to avoid evaluating all the traits in a condition @@ -818,11 +812,12 @@ void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept external_constructor::construct(j, static_cast(val)); } -template::value, int> = 0> -void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept +template::value, int> = 0> +void to_json(BasicJsonType& j, EnumType e) noexcept { - external_constructor::construct(j, e); + using underlying_type = typename std::underlying_type::type; + external_constructor::construct(j, static_cast(e)); } template @@ -937,13 +932,13 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& get_arithmetic_value(j, val); } -template::value, int> = 0> -void from_json(const BasicJsonType& j, UnscopedEnumType& e) +template::value, int> = 0> +void from_json(const BasicJsonType& j, EnumType& e) { - typename std::underlying_type::type val; + typename std::underlying_type::type val; get_arithmetic_value(j, val); - e = static_cast(val); + e = static_cast(val); } template diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 59969bf9..0940ec51 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -917,6 +917,15 @@ TEST_CASE("value conversion") } } + SECTION("get an enum") + { + enum c_enum { value_1, value_2 }; + enum class cpp_enum { value_1, value_2 }; + + CHECK(json(value_1).get() == value_1); + CHECK(json(cpp_enum::value_1).get() == cpp_enum::value_1); + } + SECTION("more involved conversions") { SECTION("object-like STL containers")