If does not have name return empty string

This commit is contained in:
terik23 2019-03-27 18:53:35 +05:00
parent 5e50eccae1
commit 0767865559
3 changed files with 36 additions and 8 deletions

View file

@ -77,7 +77,7 @@ struct Long {
LL ll; LL ll;
}; };
enum class Color { RED = -10, GREEN, BLUE }; enum class Color { RED, GREEN, BLUE };
SomeStruct structvar; SomeStruct structvar;
Long othervar; Long othervar;

View file

@ -136,7 +136,7 @@ template <typename T>
constexpr auto prefix = sizeof("class std::basic_string_view<char,struct std::char_traits<char> > __cdecl nameof::detail::nameof_type_impl<struct nameof::detail::identity<") - 1; constexpr auto prefix = sizeof("class std::basic_string_view<char,struct std::char_traits<char> > __cdecl nameof::detail::nameof_type_impl<struct nameof::detail::identity<") - 1;
constexpr auto suffix = sizeof(">>(void) noexcept") - 1; constexpr auto suffix = sizeof(">>(void) noexcept") - 1;
#else #else
return "nameof_type::unsupported_compiler"; return {}; // Unsupported compiler.
#endif #endif
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) #if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
@ -156,6 +156,7 @@ template <typename T>
while (name.back() == ' ') { while (name.back() == ' ') {
name.remove_suffix(1); name.remove_suffix(1);
} }
return name; return name;
#endif #endif
} }
@ -173,7 +174,7 @@ template <auto V>
std::string_view name{__FUNCSIG__}; std::string_view name{__FUNCSIG__};
constexpr auto suffix = sizeof(">(void) noexcept") - 1; constexpr auto suffix = sizeof(">(void) noexcept") - 1;
#else #else
return "nameof_enum::unsupported_compiler"; return {}; // Unsupported compiler.
#endif #endif
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER)
@ -184,7 +185,12 @@ template <auto V>
break; break;
} }
} }
return name;
if (name.front() >= '0' && name.front() <= '9') {
return {}; // Enum variable does not have name.
} else {
return name;
}
#endif #endif
} }
@ -193,7 +199,7 @@ struct nameof_enum_impl_t final {
[[nodiscard]] constexpr std::string_view operator()(int value) const noexcept { [[nodiscard]] constexpr std::string_view operator()(int value) const noexcept {
static_assert(std::is_enum_v<E>); static_assert(std::is_enum_v<E>);
if constexpr (V > std::numeric_limits<std::underlying_type_t<E>>::max()) { if constexpr (V > std::numeric_limits<std::underlying_type_t<E>>::max()) {
return "nameof_enum::out_of_range"; return {}; // Enum variable out of range.
} }
switch (value - V) { switch (value - V) {
@ -223,7 +229,7 @@ template <typename E>
struct nameof_enum_impl_t<E, NAMEOF_ENUM_MAX_SEARCH_DEPTH> final { struct nameof_enum_impl_t<E, NAMEOF_ENUM_MAX_SEARCH_DEPTH> final {
[[nodiscard]] constexpr std::string_view operator()(int) const noexcept { [[nodiscard]] constexpr std::string_view operator()(int) const noexcept {
static_assert(std::is_enum_v<E>); static_assert(std::is_enum_v<E>);
return "nameof_enum::out_of_range"; return {}; // Enum variable out of range NAMEOF_ENUM_MAX_SEARCH_DEPTH.
} }
}; };

View file

@ -23,6 +23,7 @@
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include <catch.hpp> #include <catch.hpp>
#define NAMEOF_ENUM_MAX_SEARCH_DEPTH 120
#include <nameof.hpp> #include <nameof.hpp>
#include <string> #include <string>
@ -67,9 +68,9 @@ struct Long {
LL ll; 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; SomeStruct struct_var;
Long othervar; Long othervar;
@ -223,6 +224,13 @@ TEST_CASE("NAMEOF_ENUM") {
REQUIRE(NAMEOF_ENUM(Directions::Right) == "Right"); REQUIRE(NAMEOF_ENUM(Directions::Right) == "Right");
REQUIRE(NAMEOF_ENUM(directions) == "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 #endif
} }
@ -240,6 +248,13 @@ TEST_CASE("nameof::nameof_enum<T>(value)") {
REQUIRE(nameof::nameof_enum(Directions::Right) == "Right"); REQUIRE(nameof::nameof_enum(Directions::Right) == "Right");
REQUIRE(nameof::nameof_enum(directions) == "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 #endif
} }
@ -257,6 +272,13 @@ TEST_CASE("nameof::nameof_enum<value>()") {
REQUIRE(nameof::nameof_enum<Directions::Right>() == "Right"); REQUIRE(nameof::nameof_enum<Directions::Right>() == "Right");
REQUIRE(nameof::nameof_enum<directions>() == "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 #endif
} }