From 16f010ef081f9a96e9cf0d5eac123b1f3bede189 Mon Sep 17 00:00:00 2001 From: neargye Date: Fri, 22 Mar 2019 13:56:01 +0500 Subject: [PATCH] more c++17, more refactoring --- include/nameof.hpp | 76 +++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 9e7012c..d8623bf 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -48,17 +48,17 @@ struct identity { using type = T; }; -constexpr bool is_lexeme(char s) noexcept { - return !((s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_'); +constexpr bool is_name_char(char s) noexcept { + return (s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_'; } -constexpr bool is_bracket(char s) noexcept { +constexpr bool is_bracket_char(char s) noexcept { return s == ')' || s == '}' || s == '>' || s == '(' || s == '{' || s == '<'; } constexpr std::string_view pretty_name(std::string_view name, bool with_suffix) noexcept { for (std::size_t i = name.size(), h = 0, s = 0; i > 0; --i) { - if (h == 0 && is_lexeme(name[i - 1]) && !is_bracket(name[i - 1])) { + if (h == 0 && !is_name_char(name[i - 1]) && !is_bracket_char(name[i - 1])) { ++s; continue; } @@ -103,7 +103,7 @@ constexpr std::string_view pretty_name(std::string_view name, bool with_suffix) } for (std::size_t i = name.size() - s; i > 0; --i) { - if (is_lexeme(name[i - 1])) { + if (!is_name_char(name[i - 1])) { name.remove_prefix(i); break; } @@ -113,12 +113,12 @@ constexpr std::string_view pretty_name(std::string_view name, bool with_suffix) return name; } -template +template constexpr int nameof_enum_impl_() noexcept { #if defined(__clang__) - return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::nameof_enum_impl_() [T = ") - sizeof("]") + 1; + return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::nameof_enum_impl_() [E = ") - sizeof("]") + 1; #elif defined(__GNUC__) - return sizeof(__PRETTY_FUNCTION__) - sizeof("constexpr int nameof::detail::nameof_enum_impl_() [with T = ") - sizeof("]") + 1; + return sizeof(__PRETTY_FUNCTION__) - sizeof("constexpr int nameof::detail::nameof_enum_impl_() [with E = ") - sizeof("]") + 1; #elif defined(_MSC_VER) return sizeof(__FUNCSIG__) - sizeof("int __cdecl nameof::detail::nameof_enum_impl_<") - sizeof(">(void) noexcept") + 1; #else @@ -126,24 +126,24 @@ constexpr int nameof_enum_impl_() noexcept { #endif } -template +template constexpr std::string_view nameof_enum_impl() noexcept { #if defined(__clang__) const auto str = __PRETTY_FUNCTION__; const auto size = sizeof(__PRETTY_FUNCTION__) - 1; - const auto prefix = sizeof("std::string_view nameof::detail::nameof_enum_impl() [T = ") + nameof_enum_impl_() + sizeof("; V = ") - 2; + const auto prefix = sizeof("std::string_view nameof::detail::nameof_enum_impl() [E = ") + nameof_enum_impl_() + sizeof("; V = ") - 2; const auto suffix = sizeof("]") - 1; return detail::pretty_name({str + prefix, size - prefix - suffix}, false); #elif defined(__GNUC__) const auto str = __PRETTY_FUNCTION__; const auto size = sizeof(__PRETTY_FUNCTION__) - 1; - const auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with T = ") + nameof_enum_impl_() + sizeof("; V = "); + const auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_enum_impl_() + sizeof("; V = "); const auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; return {str + prefix, size - prefix - suffix}; #elif defined(_MSC_VER) const auto str = __FUNCSIG__; const auto size = sizeof(__FUNCSIG__) - 1; - const auto prefix = sizeof("class std::basic_string_view > __cdecl nameof::detail::nameof_enum_impl<") + nameof_enum_impl_(); + const auto prefix = sizeof("class std::basic_string_view > __cdecl nameof::detail::nameof_enum_impl<") + nameof_enum_impl_(); const auto suffix = sizeof(">(void) noexcept") - 1; return detail::pretty_name({str + prefix, size - prefix - suffix}, false); #else @@ -151,36 +151,36 @@ constexpr std::string_view nameof_enum_impl() noexcept { #endif } -template +template struct nameof_enum_t { constexpr std::string_view operator()(int value) const noexcept { - switch (value - I) { + switch (value - V) { case 0: - return nameof_enum_impl(); + return nameof_enum_impl(); case 1: - return nameof_enum_impl(); + return nameof_enum_impl(); case 2: - return nameof_enum_impl(); + return nameof_enum_impl(); case 3: - return nameof_enum_impl(); + return nameof_enum_impl(); case 4: - return nameof_enum_impl(); + return nameof_enum_impl(); case 5: - return nameof_enum_impl(); + return nameof_enum_impl(); case 6: - return nameof_enum_impl(); + return nameof_enum_impl(); case 7: - return nameof_enum_impl(); + return nameof_enum_impl(); default: - return nameof_enum_t{}(value); + return nameof_enum_t{}(value); } } }; -template -struct nameof_enum_t { +template +struct nameof_enum_t { constexpr std::string_view operator()(int) const noexcept { - return { "nameof_enum::out_of_range" }; + return {"nameof_enum::out_of_range"}; } }; @@ -228,9 +228,8 @@ constexpr std::string_view nameof_type_impl() noexcept { #endif } -template ::value>::type> -constexpr std::string_view nameof_impl(std::string_view name, bool with_suffix = false) noexcept { +template >> +constexpr std::string_view nameof_impl(std::string_view name, bool with_suffix) noexcept { return detail::pretty_name(name, with_suffix); } @@ -241,11 +240,10 @@ constexpr std::string_view nameof_raw_impl(std::string_view name) noexcept { } // namespace detail -template ::type>::value>::type> +template >>> constexpr std::string_view nameof_enum(T value) noexcept { - constexpr auto s = std::is_signed::type>::type>::value; - return detail::nameof_enum_t::type, s ? -NAMEOF_ENUM_MAX_SEARCH_DEPTH : 0>{}(static_cast(value)); + constexpr auto s = std::is_signed_v>>; + return detail::nameof_enum_t, s ? -NAMEOF_ENUM_MAX_SEARCH_DEPTH : 0>{}(static_cast(value)); } template @@ -260,18 +258,20 @@ constexpr std::string_view nameof_type(T) noexcept { } // namespace nameof -// Used to obtain the simple (unqualified) string name of a variable, member, function, enum, macro. +// Used to obtain the simple (unqualified) string name of variable, function, enum, macro. #define NAMEOF(...) ::nameof::detail::nameof_impl(#__VA_ARGS__, false) -// Used to obtain the full string name of a variable, member, function, enum, macro. +// Used to obtain the full string name of variable, function, enum, macro. #define NAMEOF_FULL(...) ::nameof::detail::nameof_impl(#__VA_ARGS__, true) -// Used to obtain the raw string name of a variable, member, function, enum, macro. +// Used to obtain the raw string name of variable, function, enum, macro. #define NAMEOF_RAW(...) ::nameof::detail::nameof_raw_impl(#__VA_ARGS__) -// Used to obtain the simple (unqualified) string name of a enum variable. +// Used to obtain the simple (unqualified) string name of enum variable. #define NAMEOF_ENUM(...) ::nameof::nameof_enum(__VA_ARGS__) -// Used to obtain the string name of a type. +// Used to obtain the string name of variable type. #define NAMEOF_TYPE(...) ::nameof::nameof_type() + +// Used to obtain the string name of type. #define NAMEOF_TYPE_T(...) ::nameof::nameof_type<__VA_ARGS__>()