From 076786555939009e7b7b2584d9b1d3f9cb368836 Mon Sep 17 00:00:00 2001 From: terik23 Date: Wed, 27 Mar 2019 18:53:35 +0500 Subject: [PATCH] If does not have name return empty string --- example/example.cpp | 2 +- include/nameof.hpp | 16 +++++++++++----- test/test.cpp | 26 ++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index f8608a7..cd157b2 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -77,7 +77,7 @@ struct Long { LL ll; }; -enum class Color { RED = -10, GREEN, BLUE }; +enum class Color { RED, GREEN, BLUE }; SomeStruct structvar; Long othervar; diff --git a/include/nameof.hpp b/include/nameof.hpp index 4121d26..a5e087f 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -136,7 +136,7 @@ template constexpr auto prefix = sizeof("class std::basic_string_view > __cdecl nameof::detail::nameof_type_impl>(void) noexcept") - 1; #else - return "nameof_type::unsupported_compiler"; + return {}; // Unsupported compiler. #endif #if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) @@ -156,6 +156,7 @@ template while (name.back() == ' ') { name.remove_suffix(1); } + return name; #endif } @@ -173,7 +174,7 @@ template std::string_view name{__FUNCSIG__}; constexpr auto suffix = sizeof(">(void) noexcept") - 1; #else - return "nameof_enum::unsupported_compiler"; + return {}; // Unsupported compiler. #endif #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) @@ -184,7 +185,12 @@ template break; } } - return name; + + if (name.front() >= '0' && name.front() <= '9') { + return {}; // Enum variable does not have name. + } else { + return name; + } #endif } @@ -193,7 +199,7 @@ struct nameof_enum_impl_t final { [[nodiscard]] constexpr std::string_view operator()(int value) const noexcept { static_assert(std::is_enum_v); if constexpr (V > std::numeric_limits>::max()) { - return "nameof_enum::out_of_range"; + return {}; // Enum variable out of range. } switch (value - V) { @@ -223,7 +229,7 @@ template struct nameof_enum_impl_t final { [[nodiscard]] constexpr std::string_view operator()(int) const noexcept { static_assert(std::is_enum_v); - return "nameof_enum::out_of_range"; + return {}; // Enum variable out of range NAMEOF_ENUM_MAX_SEARCH_DEPTH. } }; diff --git a/test/test.cpp b/test/test.cpp index b386a27..0c1b6b4 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -23,6 +23,7 @@ #define CATCH_CONFIG_MAIN #include +#define NAMEOF_ENUM_MAX_SEARCH_DEPTH 120 #include #include @@ -67,9 +68,9 @@ struct Long { LL ll; }; -enum class Color { RED = -1, GREEN, BLUE }; +enum class Color : int { RED = -10, GREEN = 0, BLUE = 10 }; -enum Directions { Up, Down, Right, Left }; +enum Directions : unsigned int { Up, Down, Right, Left }; SomeStruct struct_var; Long othervar; @@ -223,6 +224,13 @@ TEST_CASE("NAMEOF_ENUM") { REQUIRE(NAMEOF_ENUM(Directions::Right) == "Right"); REQUIRE(NAMEOF_ENUM(directions) == "Right"); + + REQUIRE(NAMEOF_ENUM((Color)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(NAMEOF_ENUM((Color)-100).empty()); + REQUIRE(NAMEOF_ENUM((Color)100).empty()); + REQUIRE(NAMEOF_ENUM((Directions)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(NAMEOF_ENUM((Directions)100).empty()); + REQUIRE(NAMEOF_ENUM((Directions)100).empty()); #endif } @@ -240,6 +248,13 @@ TEST_CASE("nameof::nameof_enum(value)") { REQUIRE(nameof::nameof_enum(Directions::Right) == "Right"); REQUIRE(nameof::nameof_enum(directions) == "Right"); + + REQUIRE(nameof::nameof_enum((Color)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(nameof::nameof_enum((Color)-100).empty()); + REQUIRE(nameof::nameof_enum((Color)100).empty()); + REQUIRE(nameof::nameof_enum((Directions)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(nameof::nameof_enum((Directions)100).empty()); + REQUIRE(nameof::nameof_enum((Directions)100).empty()); #endif } @@ -257,6 +272,13 @@ TEST_CASE("nameof::nameof_enum()") { REQUIRE(nameof::nameof_enum() == "Right"); REQUIRE(nameof::nameof_enum() == "Right"); + + REQUIRE(nameof::nameof_enum<(Color)NAMEOF_ENUM_MAX_SEARCH_DEPTH>().empty()); + REQUIRE(nameof::nameof_enum<(Color)-100>().empty()); + REQUIRE(nameof::nameof_enum<(Color)100>().empty()); + REQUIRE(nameof::nameof_enum<(Directions)NAMEOF_ENUM_MAX_SEARCH_DEPTH>().empty()); + REQUIRE(nameof::nameof_enum<(Directions)100>().empty()); + REQUIRE(nameof::nameof_enum<(Directions)100>().empty()); #endif }