From bfacde30f23c7ca3d813d2db42cc4de88bb11549 Mon Sep 17 00:00:00 2001 From: Neargye Date: Sat, 1 Sep 2018 22:08:09 +0500 Subject: [PATCH] fix nameof enum --- include/nameof.hpp | 35 +++++++++++++++++++++-------------- test/test.cpp | 18 +++++++++++------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 81e66dd..bde5e31 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -42,10 +42,10 @@ #endif #if (defined(__clang__) || defined(_MSC_VER)) || (defined(__GNUC__) && __GNUC__ >= 5) -# define NAMEOF_TYPE_HAS_CONSTEXPR 1 -# define NAMEOF_TYPE_CONSTEXPR constexpr +# define NAMEOF_HAS_CONSTEXPR 1 +# define NAMEOF_CONSTEXPR constexpr #else -# define NAMEOF_TYPE_CONSTEXPR inline +# define NAMEOF_CONSTEXPR inline #endif namespace nameof { @@ -347,7 +347,7 @@ constexpr cstring NameofTypePretty(const char* str, std::size_t size, std::size_ #endif template -constexpr int NameofEnumImpl1() { +NAMEOF_CONSTEXPR int NameofEnumImpl1() { #if defined(__clang__) return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::NameofEnumImpl1() [T = ") - sizeof("]") + 1; #elif defined(__GNUC__) @@ -360,7 +360,7 @@ constexpr int NameofEnumImpl1() { } template -constexpr nameof::cstring NameofEnumImpl2() { +NAMEOF_CONSTEXPR nameof::cstring NameofEnumImpl2() { #if defined(__clang__) return {__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, @@ -383,20 +383,27 @@ constexpr nameof::cstring NameofEnumImpl2() { template struct NameofEnumImpl { - constexpr nameof::cstring operator()(T value) const noexcept { + NAMEOF_CONSTEXPR nameof::cstring operator()(T value) const { return (static_cast(value) - I == 0) - ? NameofEnumImpl2() - : NameofEnumImpl{}(value); + ? NameofEnumImpl2() + : (static_cast(value) >= 0) + ? NameofEnumImpl{}(value) + : NameofEnumImpl{}(value); } }; template struct NameofEnumImpl { - constexpr nameof::cstring operator()(T) const noexcept { return {}; } + NAMEOF_CONSTEXPR nameof::cstring operator()(T) const { return {}; } }; template -NAMEOF_TYPE_CONSTEXPR cstring NameofType() { +struct NameofEnumImpl { + NAMEOF_CONSTEXPR nameof::cstring operator()(T) const { return {}; } +}; + +template +NAMEOF_CONSTEXPR cstring NameofType() { #if defined(__clang__) return NameofTypePretty( __PRETTY_FUNCTION__, @@ -407,7 +414,7 @@ NAMEOF_TYPE_CONSTEXPR cstring NameofType() { return NameofTypePretty( __PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1, -# if defined(NAMEOF_TYPE_HAS_CONSTEXPR) +# if defined(NAMEOF_HAS_CONSTEXPR) sizeof("constexpr nameof::cstring nameof::detail::NameofType() [with T = nameof::detail::nstd::identity<") - 1, # else sizeof("nameof::cstring nameof::detail::NameofType() [with T = nameof::detail::nstd::identity<") - 1, @@ -434,7 +441,7 @@ constexpr cstring Nameof(const char* name, std::size_t size, bool with_suffix = template ::value && std::is_enum::value>::type> -constexpr cstring NameofEnum(T value) { +NAMEOF_CONSTEXPR cstring NameofEnum(T value) { #if defined(__clang__) || defined(_MSC_VER) return detail::NameofPretty(detail::NameofEnumImpl{}(value), false); #elif defined(__GNUC__) @@ -445,8 +452,8 @@ return {}; } template -NAMEOF_TYPE_CONSTEXPR cstring NameofType() { - return detail::NameofType>(); +NAMEOF_CONSTEXPR cstring NameofType() { + return true ? detail::NameofType>() : detail::NameofType>(); } template diff --git a/test/test.cpp b/test/test.cpp index b059762..5e7f53e 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -68,7 +68,7 @@ struct Long { LL ll; }; -enum class Color { RED, GREEN, BLUE }; +enum class Color { RED = -1, GREEN, BLUE }; enum Directions { Up, Down, Right, Left}; @@ -151,7 +151,7 @@ TEST_CASE("constexpr") { static_assert(cx == "RED", ""); REQUIRE(cx == "RED"); #elif defined(__GNUC__) - REQUIRE(cx == "(const Color)0"); + REQUIRE(cx == "(const Color)-1"); #endif } @@ -308,13 +308,17 @@ TEST_CASE("NAMEOF_RAW") { TEST_CASE("NAMEOF_ENUM") { # if defined(__clang__) || defined(_MSC_VER) REQUIRE(NAMEOF_ENUM(Color::RED) == "RED"); - REQUIRE(NAMEOF_ENUM(color) == "RED"); + REQUIRE(NAMEOF_ENUM(::color) == "RED"); + + auto color_ = Color::BLUE; + REQUIRE(NAMEOF_ENUM(Color::BLUE) == "BLUE"); + REQUIRE(NAMEOF_ENUM(color_) == "BLUE"); REQUIRE(NAMEOF_ENUM(Directions::Right) == "Right"); REQUIRE(NAMEOF_ENUM(directions) == "Right"); # elif defined(__GNUC__) - REQUIRE(NAMEOF_ENUM(Color::RED) == "(Color)0"); - REQUIRE(NAMEOF_ENUM(color) == "(const Color)0"); + REQUIRE(NAMEOF_ENUM(Color::RED) == "(Color)-1"); + REQUIRE(NAMEOF_ENUM(color) == "(const Color)-1"); REQUIRE(NAMEOF_ENUM(Directions::Right) == "(Directions)2"); REQUIRE(NAMEOF_ENUM(directions) == "(const Directions)2"); @@ -430,7 +434,7 @@ TEST_CASE("Spaces and Tabs ignored") { #if defined(__clang__) || defined(_MSC_VER) REQUIRE(NAMEOF_ENUM( color ) == "RED"); #elif defined(__GNUC__) - REQUIRE(NAMEOF_ENUM( color ) == "(const Color)0"); + REQUIRE(NAMEOF_ENUM( color ) == "(const Color)-1"); #endif REQUIRE(NAMEOF_TYPE( struct_var ) == "SomeStruct"); REQUIRE(NAMEOF_TYPE_T( decltype(struct_var) ) == "SomeStruct"); @@ -443,7 +447,7 @@ TEST_CASE("Spaces and Tabs ignored") { #if defined(__clang__) || defined(_MSC_VER) REQUIRE(NAMEOF_ENUM( color ) == "RED"); #elif defined(__GNUC__) - REQUIRE(NAMEOF_ENUM( color ) == "(const Color)0"); + REQUIRE(NAMEOF_ENUM( color ) == "(const Color)-1"); #endif REQUIRE(NAMEOF_TYPE( struct_var ) == "SomeStruct"); REQUIRE(NAMEOF_TYPE_T( decltype(struct_var) ) == "SomeStruct");