From d3529f8cd016238c7e9e0239d3745be40079c59b Mon Sep 17 00:00:00 2001 From: terik23 Date: Sun, 24 Mar 2019 20:15:14 +0500 Subject: [PATCH] wip improve --- README.md | 5 +++ example/example.cpp | 2 + include/nameof.hpp | 100 ++++++++++++++++++++------------------------ test/test.cpp | 28 +++++++++++++ 4 files changed, 80 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index ab28dbd..9169840 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,9 @@ nameof::nameof_enum(c) -> "RED" constexpr auto cx_name = NAMEOF_ENUM(c); static_assert("RED" == cx_name); + +constexpr auto cx_color = Color::RED; +nameof::nameof_enum() -> "RED" ``` * Name of type @@ -107,6 +110,8 @@ NAMEOF_RAW(std::string) -> "std::string" auto c = Color::RED; NAMEOF_ENUM(c) -> "(Color)0" nameof::nameof_enum(c) -> "(Color)0" + +NAMEOF(Color::RED) -> "RED" ``` * Spaces and Tabs ignored diff --git a/example/example.cpp b/example/example.cpp index 92618ab..9cbd05f 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -92,6 +92,8 @@ int main() { auto color = Color::RED; std::cout << NAMEOF_ENUM(color) << std::endl; // RED std::cout << nameof::nameof_enum(color) << std::endl; // RED + constexpr auto cx_color = Color::RED; + std::cout << nameof::nameof_enum() << std::endl; // RED // Variable name. std::cout << NAMEOF(structvar) << std::endl; // structvar diff --git a/include/nameof.hpp b/include/nameof.hpp index f9ae57a..40a2b61 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -121,17 +121,45 @@ struct identity final { return name; } -template -[[nodiscard]] constexpr int nameof_enum_impl_() noexcept { +template +[[nodiscard]] constexpr std::string_view nameof_type_impl() noexcept { #if defined(__clang__) - return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::nameof_enum_impl_() [E = ") - sizeof("]") + 1; + constexpr auto str = __PRETTY_FUNCTION__; + constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; + constexpr auto prefix = sizeof("std::string_view nameof::detail::nameof_type_impl() [T = nameof::detail::identity<") - 1; + constexpr auto suffix = sizeof(">]") - 1; #elif defined(__GNUC__) - return sizeof(__PRETTY_FUNCTION__) - sizeof("constexpr int nameof::detail::nameof_enum_impl_() [with E = ") - sizeof("]") + 1; + constexpr auto str = __PRETTY_FUNCTION__; + constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; + constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_type_impl() [with T = nameof::detail::identity<") - 1; + constexpr auto suffix = sizeof(">; std::string_view = std::basic_string_view]") - 1; #elif defined(_MSC_VER) - return sizeof(__FUNCSIG__) - sizeof("int __cdecl nameof::detail::nameof_enum_impl_<") - sizeof(">(void) noexcept") + 1; + constexpr auto str = __FUNCSIG__; + constexpr auto size = sizeof(__FUNCSIG__) - 1; + constexpr auto prefix = sizeof("class std::basic_string_view > __cdecl nameof::detail::nameof_type_impl>(void) noexcept") - 1; #else - return 0; + constexpr auto str = "nameof_type::unsupported_compiler"; + constexpr auto size = sizeof("nameof_type::unsupported_compiler") - 1; + constexpr auto prefix = 0; + constexpr auto suffix = 0; #endif + + std::string_view name{str + prefix, size - prefix - suffix}; + if (name.size() > sizeof("enum") && name[0] == 'e' && name[1] == 'n' && name[2] == 'u' && name[3] == 'm' && name[4] == ' ') { + name.remove_prefix(sizeof("enum")); + } + if (name.size() > sizeof("class") && name[0] == 'c' && name[1] == 'l' && name[2] == 'a' && name[3] == 's' && name[4] == 's' && name[5] == ' ') { + name.remove_prefix(sizeof("class")); + } + if (name.size() > sizeof("struct") && name[0] == 's' && name[1] == 't' && name[2] == 'r' && name[3] == 'u' && name[4] == 'c' && name[5] == 't' && name[6] == ' ') { + name.remove_prefix(sizeof("struct")); + } + while (name.back() == ' ') { + name.remove_suffix(1); + } + + return name; } template @@ -139,21 +167,19 @@ template #if defined(__clang__) constexpr auto str = __PRETTY_FUNCTION__; constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; - constexpr auto prefix = sizeof("std::string_view nameof::detail::nameof_enum_impl() [E = ") + nameof_enum_impl_() + sizeof("; V = ") - 2; constexpr auto suffix = sizeof("]") - 1; - return detail::pretty_name({str + prefix, size - prefix - suffix}, false); + return detail::pretty_name({str, size - suffix}, false); #elif defined(__GNUC__) constexpr auto str = __PRETTY_FUNCTION__; constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; - constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_enum_impl_() + sizeof("; V = "); + constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_type_impl>().length() + sizeof("; V = "); constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; return {str + prefix, size - prefix - suffix}; #elif defined(_MSC_VER) constexpr auto str = __FUNCSIG__; constexpr auto size = sizeof(__FUNCSIG__) - 1; - constexpr auto prefix = sizeof("class std::basic_string_view > __cdecl nameof::detail::nameof_enum_impl<") + nameof_enum_impl_(); constexpr auto suffix = sizeof(">(void) noexcept") - 1; - return detail::pretty_name({str + prefix, size - prefix - suffix}, false); + return detail::pretty_name({str, size - suffix}, false); #else return {"nameof_enum::unsupported_compiler"}; #endif @@ -196,50 +222,6 @@ struct nameof_enum_t final { } }; -[[nodiscard]] constexpr std::string_view nameof_type_impl_(std::string_view name) noexcept { -#if defined(_MSC_VER) - if (name.size() > sizeof("enum") && name[0] == 'e' && name[1] == 'n' && name[2] == 'u' && name[3] == 'm' && name[4] == ' ') { - name.remove_prefix(sizeof("enum")); - } - if (name.size() > sizeof("class") && name[0] == 'c' && name[1] == 'l' && name[2] == 'a' && name[3] == 's' && name[4] == 's' && name[5] == ' ') { - name.remove_prefix(sizeof("class")); - } - if (name.size() > sizeof("struct") && name[0] == 's' && name[1] == 't' && name[2] == 'r' && name[3] == 'u' && name[4] == 'c' && name[5] == 't' && name[6] == ' ') { - name.remove_prefix(sizeof("struct")); - } -#endif - while (name.back() == ' ') { - name.remove_suffix(1); - } - - return name; -} - -template -[[nodiscard]] constexpr std::string_view nameof_type_impl() noexcept { -#if defined(__clang__) - constexpr auto str = __PRETTY_FUNCTION__; - constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; - constexpr auto prefix = sizeof("std::string_view nameof::detail::nameof_type_impl() [T = nameof::detail::identity<") - 1; - constexpr auto suffix = sizeof(">]") - 1; - return nameof_type_impl_({str + prefix, size - prefix - suffix}); -#elif defined(__GNUC__) - constexpr auto str = __PRETTY_FUNCTION__; - constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; - constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_type_impl() [with T = nameof::detail::identity<") - 1; - constexpr auto suffix = sizeof(">; std::string_view = std::basic_string_view]") - 1; - return nameof_type_impl_({str + prefix, size - prefix - suffix}); -#elif defined(_MSC_VER) - constexpr auto str = __FUNCSIG__; - constexpr auto size = sizeof(__FUNCSIG__) - 1; - constexpr auto prefix = sizeof("class std::basic_string_view > __cdecl nameof::detail::nameof_type_impl>(void) noexcept") - 1; - return nameof_type_impl_({str + prefix, size - prefix - suffix}); -#else - return {"nameof_type::unsupported_compiler"}; -#endif -} - template >> [[nodiscard]] constexpr std::string_view nameof_impl(std::string_view name, bool with_suffix) noexcept { return detail::pretty_name(name, with_suffix); @@ -251,6 +233,7 @@ template >> } // namespace detail +// nameof_enum used to obtain the simple (unqualified) string name of enum variable. template >>> [[nodiscard]] constexpr std::string_view nameof_enum(T value) noexcept { constexpr bool s = std::is_signed_v>>; @@ -258,6 +241,13 @@ template return detail::nameof_enum_t, min>{}(static_cast(value)); } +// nameof_enum used to obtain the simple (unqualified) string name of static storage enum variable. +template >>> +[[nodiscard]] constexpr std::string_view nameof_enum() noexcept { + return detail::nameof_enum_impl(); +} + +// nameof_type used to obtain the string name of type. template [[nodiscard]] constexpr std::string_view nameof_type() noexcept { return detail::nameof_type_impl>(); diff --git a/test/test.cpp b/test/test.cpp index c27354d..73ca6aa 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -265,6 +265,34 @@ TEST_CASE("nameof::nameof_enum(value)") { #endif } +TEST_CASE("nameof::nameof_enum()") { + constexpr Color color_ = Color::BLUE; + constexpr Color m[3] = {Color::RED, Color::GREEN, Color::BLUE}; +#if defined(__clang__) || defined(_MSC_VER) + REQUIRE(nameof::nameof_enum() == "RED"); + REQUIRE(nameof::nameof_enum() == "RED"); + + REQUIRE(nameof::nameof_enum() == "BLUE"); + REQUIRE(nameof::nameof_enum() == "BLUE"); + + REQUIRE(nameof::nameof_enum() == "GREEN"); + + REQUIRE(nameof::nameof_enum() == "Right"); + REQUIRE(nameof::nameof_enum() == "Right"); +#elif defined(__GNUC__) + REQUIRE(nameof::nameof_enum() == "(Color)-1"); + REQUIRE(nameof::nameof_enum() == "(Color)-1"); + + REQUIRE(nameof::nameof_enum() == "(Color)1"); + REQUIRE(nameof::nameof_enum() == "(Color)1"); + + REQUIRE(nameof::nameof_enum() == "(Color)0"); + + REQUIRE(nameof::nameof_enum() == "(Directions)2"); + REQUIRE(nameof::nameof_enum() == "(Directions)2"); +#endif +} + TEST_CASE("NAMEOF_TYPE") { #if defined(__clang__) REQUIRE(NAMEOF_TYPE(struct_var) == "SomeStruct");