From f2522c2cc0579b11bc4b4ad27906f61ed4b1de5d Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 14 Jun 2023 02:44:15 +0400 Subject: [PATCH] * add sep param to nameof_enum_flag * fix noexcept --- doc/reference.md | 1 + include/nameof.hpp | 50 +++++++++++++++++----------------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/doc/reference.md b/doc/reference.md index c374d7e..524e629 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -211,6 +211,7 @@ flag = AnimalFlags::CanFly | AnimalFlags::Endangered; NAMEOF_ENUM_FLAG(flag) -> "CanFly|Endangered" nameof_enum_flag(flag) -> "CanFly|Endangered" + nameof_enum_flag(flag, '$') -> "CanFly$Endangered" NAMEOF_ENUM(HasClaws | CanFly) -> "" nameof_enum(HasClaws | CanFly) -> "" diff --git a/include/nameof.hpp b/include/nameof.hpp index 5bb8864..2991602 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -47,15 +47,15 @@ #include #if !defined(NAMEOF_USING_ALIAS_STRING) -#include +# include #endif #if !defined(NAMEOF_USING_ALIAS_STRING_VIEW) -#include +# include #endif #if __has_include() -#include -#include +# include +# include #endif #if defined(__clang__) @@ -174,7 +174,6 @@ static_assert(NAMEOF_ENUM_RANGE_MAX > NAMEOF_ENUM_RANGE_MIN, "NAMEOF_ENUM_RANGE_ template constexpr string_view enum_name(E) noexcept { static_assert(std::is_enum_v, "nameof::customize::enum_name requires enum type."); - return {}; } @@ -413,7 +412,6 @@ std::basic_ostream& operator<<(std::basic_ostream& o for (const auto c : srt) { os.put(c); } - return os; } @@ -853,11 +851,11 @@ using enable_if_has_short_name_t = std::enable_if_t && !std: template constexpr auto n() noexcept { -# if defined(_MSC_VER) && !defined(__clang__) +#if defined(_MSC_VER) && !defined(__clang__) [[maybe_unused]] constexpr auto custom_name = customize::type_name(); #else [[maybe_unused]] constexpr auto custom_name = customize::type_name(); -# endif +#endif if constexpr (custom_name.empty() && nameof_type_supported::value) { #if defined(__clang__) @@ -870,7 +868,6 @@ constexpr auto n() noexcept { constexpr auto name = string_view{}; #endif return cstring{name}; - } else { return cstring{custom_name}; } @@ -927,16 +924,15 @@ string nameof_short_type_rtti(const char* tn) { } #else template -string nameof_type_rtti(const char* tn) noexcept { +string nameof_type_rtti(const char* tn) { static_assert(nameof_type_rtti_supported::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); const auto name = string_view{tn}; assert(!name.empty() && "Type does not have a name."); - return {name.data(), name.size()}; } template -string nameof_full_type_rtti(const char* tn) noexcept { +string nameof_full_type_rtti(const char* tn) { static_assert(nameof_type_rtti_supported::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); auto name = string{tn}; assert(!name.empty() && "Type does not have a name."); @@ -952,16 +948,14 @@ string nameof_full_type_rtti(const char* tn) noexcept { if constexpr (std::is_rvalue_reference_v) { name.append("&&"); } - return name; } template = 0> -string nameof_short_type_rtti(const char* tn) noexcept { +string nameof_short_type_rtti(const char* tn) { static_assert(nameof_type_rtti_supported::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); const auto name = pretty_name(tn); assert(!name.empty() && "Type does not have a short name."); - return {name.data(), name.size()}; } #endif @@ -1011,9 +1005,9 @@ constexpr auto get_member_name() noexcept { constexpr bool is_defined = sizeof(decltype(get_base_type(V))) != 0; static_assert(is_defined, "nameof::nameof_member member name can use only if the struct is already fully defined. Please use NAMEOF macro, or separate definition and declaration."); if constexpr (is_defined) { - return n::value.f.*V)>(); + return n::value.f.*V)>(); } else { - return ""; + return ""; } } } @@ -1083,8 +1077,8 @@ template static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); static_assert(detail::count_v > 0, "nameof::nameof_enum requires enum implementation and valid max and min."); - const bool valid = static_cast(value) >= static_cast(detail::min_v) && static_cast(value) <= static_cast(detail::max_v); - if (const auto i = static_cast(value) - detail::min_v; valid) { + if (static_cast(value) >= static_cast(detail::min_v) && static_cast(value) <= static_cast(detail::max_v)) { + const auto i = static_cast(value) - detail::min_v; if constexpr (detail::is_sparse_v) { if (const auto idx = detail::indexes_v[i]; idx != detail::invalid_index_v) { return detail::strings_v[idx]; @@ -1093,7 +1087,6 @@ template return detail::strings_v[static_cast(i)]; } } - return {}; // Value out of range. } @@ -1101,17 +1094,18 @@ template template [[nodiscard]] auto nameof_enum_or(E value, string_view default_value) -> detail::enable_if_enum_t { using D = std::decay_t; + static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum_or unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); + static_assert(detail::count_v > 0, "nameof::nameof_enum_or requires enum implementation and valid max and min."); if (auto v = nameof_enum(value); !v.empty()) { return string{v.data(), v.size()}; } - return string{default_value.data(), default_value.size()}; } // Obtains name of enum-flags variable. template -[[nodiscard]] auto nameof_enum_flag(E value) -> detail::enable_if_enum_t { +[[nodiscard]] auto nameof_enum_flag(E value, char sep = '|') -> detail::enable_if_enum_t { using D = std::decay_t; using U = std::underlying_type_t; static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum_flag unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); @@ -1125,7 +1119,7 @@ template if (const auto n = detail::strings_v[i]; n != nullptr) { check_value |= v; if (!name.empty()) { - name.append(1, '|'); + name.append(1, sep); } name.append(n); } else { @@ -1134,11 +1128,9 @@ template } } - const bool valid = check_value != 0 && check_value == static_cast(value); - if (valid) { + if (check_value != 0 && check_value == static_cast(value)) { return name; } - return {}; // Invalid value or out of range. } @@ -1150,7 +1142,6 @@ template static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); constexpr string_view name = detail::enum_name_v; static_assert(!name.empty(), "Enum value does not have a name."); - return name; } @@ -1161,7 +1152,6 @@ template using U = detail::identity>; constexpr string_view name = detail::type_name_v; static_assert(!name.empty(), "Type does not have a name."); - return name; } @@ -1172,7 +1162,6 @@ template using U = detail::identity; constexpr string_view name = detail::type_name_v; static_assert(!name.empty(), "Type does not have a full name."); - return name; } @@ -1183,7 +1172,6 @@ template using U = detail::identity>; constexpr string_view name = detail::pretty_name(detail::type_name_v); static_assert(!name.empty(), "Type does not have a short name."); - return name; } @@ -1193,7 +1181,6 @@ template static_assert(detail::nameof_member_supported::value, "nameof::nameof_member unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); constexpr string_view name = detail::member_name_v; static_assert(!name.empty(), "Member does not have a name."); - return name; } @@ -1203,7 +1190,6 @@ template static_assert(detail::nameof_pointer_supported::value, "nameof::nameof_pointer unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); constexpr string_view name = detail::pointer_name_v; static_assert(!name.empty(), "Pointer does not have a name."); - return name; }