// _ _ __ _____ // | \ | | / _| / ____|_ _ // | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_ // | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _| // | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_| // |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____| // https://github.com/Neargye/nameof // version 0.10.0 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2016, 2018 - 2021 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #ifndef NEARGYE_NAMEOF_HPP #define NEARGYE_NAMEOF_HPP #define NAMEOF_VERSION_MAJOR 0 #define NAMEOF_VERSION_MINOR 10 #define NAMEOF_VERSION_PATCH 0 #include #include #include #include #include #include #include #include #include #if !defined(NAMEOF_USING_ALIAS_STRING) #include #endif #if !defined(NAMEOF_USING_ALIAS_STRING_VIEW) #include #endif #if __has_include() #include #include #endif #if defined(__clang__) # pragma clang diagnostic push #elif defined(__GNUC__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wstringop-overflow" // Missing terminating nul 'enum_name_v'. #elif defined(_MSC_VER) # pragma warning(push) # pragma warning(disable : 26495) // Variable 'cstring::chars_' is uninitialized. # pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. # pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call. #endif // Checks nameof_type compiler compatibility. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && _MSC_VER >= 1910 # undef NAMEOF_TYPE_SUPPORTED # define NAMEOF_TYPE_SUPPORTED 1 #endif // Checks nameof_type_rtti compiler compatibility. #if defined(__clang__) # if __has_feature(cxx_rtti) # undef NAMEOF_TYPE_RTTI_SUPPORTED # define NAMEOF_TYPE_RTTI_SUPPORTED 1 # endif #elif defined(__GNUC__) # if defined(__GXX_RTTI) # undef NAMEOF_TYPE_RTTI_SUPPORTED # define NAMEOF_TYPE_RTTI_SUPPORTED 1 # endif #elif defined(_MSC_VER) # if defined(_CPPRTTI) # undef NAMEOF_TYPE_RTTI_SUPPORTED # define NAMEOF_TYPE_RTTI_SUPPORTED 1 # endif #endif // Checks nameof_member compiler compatibility. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 # undef NAMEOF_MEMBER_SUPPORTED # define NAMEOF_MEMBER_SUPPORTED 1 #endif // Checks nameof_enum compiler compatibility. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 # undef NAMEOF_ENUM_SUPPORTED # define NAMEOF_ENUM_SUPPORTED 1 #endif // Checks nameof_enum compiler aliases compatibility. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920 # undef NAMEOF_ENUM_SUPPORTED_ALIASES # define NAMEOF_ENUM_SUPPORTED_ALIASES 1 #endif // Enum value must be greater or equals than NAMEOF_ENUM_RANGE_MIN. By default NAMEOF_ENUM_RANGE_MIN = -128. // If need another min range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN. #if !defined(NAMEOF_ENUM_RANGE_MIN) # define NAMEOF_ENUM_RANGE_MIN -128 #endif // Enum value must be less or equals than NAMEOF_ENUM_RANGE_MAX. By default NAMEOF_ENUM_RANGE_MAX = 128. // If need another max range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MAX. #if !defined(NAMEOF_ENUM_RANGE_MAX) # define NAMEOF_ENUM_RANGE_MAX 128 #endif namespace nameof { // If need another string_view type, define the macro NAMEOF_USING_ALIAS_STRING_VIEW. #if defined(NAMEOF_USING_ALIAS_STRING_VIEW) NAMEOF_USING_ALIAS_STRING_VIEW #else using std::string_view; #endif // If need another string type, define the macro NAMEOF_USING_ALIAS_STRING. #if defined(NAMEOF_USING_ALIAS_STRING) NAMEOF_USING_ALIAS_STRING #else using std::string; #endif namespace customize { // Enum value must be in range [NAMEOF_ENUM_RANGE_MIN, NAMEOF_ENUM_RANGE_MAX]. By default NAMEOF_ENUM_RANGE_MIN = -128, NAMEOF_ENUM_RANGE_MAX = 128. // If need another range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN and NAMEOF_ENUM_RANGE_MAX. // If need another range for specific enum type, add specialization enum_range for necessary enum type. template struct enum_range { static_assert(std::is_enum_v, "nameof::customize::enum_range requires enum type."); inline static constexpr int min = NAMEOF_ENUM_RANGE_MIN; inline static constexpr int max = NAMEOF_ENUM_RANGE_MAX; static_assert(max > min, "nameof::customize::enum_range requires max > min."); }; static_assert(NAMEOF_ENUM_RANGE_MIN <= 0, "NAMEOF_ENUM_RANGE_MIN must be less or equals than 0."); static_assert(NAMEOF_ENUM_RANGE_MIN > (std::numeric_limits::min)(), "NAMEOF_ENUM_RANGE_MIN must be greater than INT16_MIN."); static_assert(NAMEOF_ENUM_RANGE_MAX > 0, "NAMEOF_ENUM_RANGE_MAX must be greater than 0."); static_assert(NAMEOF_ENUM_RANGE_MAX < (std::numeric_limits::max)(), "NAMEOF_ENUM_RANGE_MAX must be less than INT16_MAX."); static_assert(NAMEOF_ENUM_RANGE_MAX > NAMEOF_ENUM_RANGE_MIN, "NAMEOF_ENUM_RANGE_MAX must be greater than NAMEOF_ENUM_RANGE_MIN."); // If need custom names for enum, add specialization enum_name for necessary enum type. template constexpr string_view enum_name(E) noexcept { static_assert(std::is_enum_v, "nameof::customize::enum_name requires enum type."); return {}; } // If need custom name for type, add specialization type_name for necessary type. template constexpr string_view type_name() noexcept { return {}; } // If need custom name for member, add specialization member_name for necessary type. template constexpr string_view member_name() noexcept { return {}; } } // namespace nameof::customize template class [[nodiscard]] cstring { public: using value_type = const char; using size_type = std::size_t; using difference_type = std::ptrdiff_t; using pointer = const char*; using const_pointer = const char*; using reference = const char&; using const_reference = const char&; using iterator = const char*; using const_iterator = const char*; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; constexpr explicit cstring(string_view str) noexcept : cstring{str, std::make_index_sequence{}} { assert(str.size() > 0 && str.size() == N); } constexpr cstring() = delete; constexpr cstring(const cstring&) = default; constexpr cstring(cstring&&) = default; ~cstring() = default; cstring& operator=(const cstring&) = default; cstring& operator=(cstring&&) = default; [[nodiscard]] constexpr const_pointer data() const noexcept { return chars_; } [[nodiscard]] constexpr size_type size() const noexcept { return N; } [[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); } [[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); } [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); } [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); } [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return end(); } [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return begin(); } [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); } [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); } [[nodiscard]] constexpr const_reference operator[](size_type i) const noexcept { return assert(i < size()), chars_[i]; } [[nodiscard]] constexpr const_reference front() const noexcept { return chars_[0]; } [[nodiscard]] constexpr const_reference back() const noexcept { return chars_[N]; } [[nodiscard]] constexpr size_type length() const noexcept { return size(); } [[nodiscard]] constexpr bool empty() const noexcept { return false; } [[nodiscard]] constexpr int compare(string_view str) const noexcept { return string_view{data(), size()}.compare(str); } [[nodiscard]] constexpr const char* c_str() const noexcept { return data(); } [[nodiscard]] string str() const { return {begin(), end()}; } [[nodiscard]] constexpr operator string_view() const noexcept { return {data(), size()}; } [[nodiscard]] constexpr explicit operator const_pointer() const noexcept { return data(); } [[nodiscard]] explicit operator string() const { return {begin(), end()}; } private: template constexpr cstring(string_view str, std::index_sequence) noexcept : chars_{str[I]..., '\0'} {} char chars_[N + 1]; }; template [[nodiscard]] constexpr bool operator==(const cstring& lhs, string_view rhs) noexcept { return lhs.compare(rhs) == 0; } template [[nodiscard]] constexpr bool operator==(string_view lhs, const cstring& rhs) noexcept { return lhs.compare(rhs) == 0; } template [[nodiscard]] constexpr bool operator!=(const cstring& lhs, string_view rhs) noexcept { return lhs.compare(rhs) != 0; } template [[nodiscard]] constexpr bool operator!=(string_view lhs, const cstring& rhs) noexcept { return lhs.compare(rhs) != 0; } template [[nodiscard]] constexpr bool operator>(const cstring& lhs, string_view rhs) noexcept { return lhs.compare(rhs) > 0; } template [[nodiscard]] constexpr bool operator>(string_view lhs, const cstring& rhs) noexcept { return lhs.compare(rhs) > 0; } template [[nodiscard]] constexpr bool operator>=(const cstring& lhs, string_view rhs) noexcept { return lhs.compare(rhs) >= 0; } template [[nodiscard]] constexpr bool operator>=(string_view lhs, const cstring& rhs) noexcept { return lhs.compare(rhs) >= 0; } template [[nodiscard]] constexpr bool operator<(const cstring& lhs, string_view rhs) noexcept { return lhs.compare(rhs) < 0; } template [[nodiscard]] constexpr bool operator<(string_view lhs, const cstring& rhs) noexcept { return lhs.compare(rhs) < 0; } template [[nodiscard]] constexpr bool operator<=(const cstring& lhs, string_view rhs) noexcept { return lhs.compare(rhs) <= 0; } template [[nodiscard]] constexpr bool operator<=(string_view lhs, const cstring& rhs) noexcept { return lhs.compare(rhs) <= 0; } template std::basic_ostream& operator<<(std::basic_ostream& os, const cstring& srt) { for (const auto c : srt) { os.put(c); } return os; } namespace detail { constexpr string_view pretty_name(string_view name, bool remove_suffix = true) noexcept { if (name.size() >= 1 && (name[0] == '"' || name[0] == '\'')) { return {}; // Narrow multibyte string literal. } else if (name.size() >= 2 && name[0] == 'R' && (name[1] == '"' || name[1] == '\'')) { return {}; // Raw string literal. } else if (name.size() >= 2 && name[0] == 'L' && (name[1] == '"' || name[1] == '\'')) { return {}; // Wide string literal. } else if (name.size() >= 2 && name[0] == 'U' && (name[1] == '"' || name[1] == '\'')) { return {}; // UTF-32 encoded string literal. } else if (name.size() >= 2 && name[0] == 'u' && (name[1] == '"' || name[1] == '\'')) { return {}; // UTF-16 encoded string literal. } else if (name.size() >= 3 && name[0] == 'u' && name[1] == '8' && (name[2] == '"' || name[2] == '\'')) { return {}; // UTF-8 encoded string literal. } else if (name.size() >= 1 && (name[0] >= '0' && name[0] <= '9')) { return {}; // Invalid name. } for (std::size_t i = name.size(), h = 0, s = 0; i > 0; --i) { if (name[i - 1] == ')') { ++h; ++s; continue; } else if (name[i - 1] == '(') { --h; ++s; continue; } if (h == 0) { name.remove_suffix(s); break; } else { ++s; continue; } } std::size_t s = 0; for (std::size_t i = name.size(), h = 0; i > 0; --i) { if (name[i - 1] == '>') { ++h; ++s; continue; } else if (name[i - 1] == '<') { --h; ++s; continue; } if (h == 0) { break; } else { ++s; continue; } } for (std::size_t i = name.size() - s; i > 0; --i) { if (!((name[i - 1] >= '0' && name[i - 1] <= '9') || (name[i - 1] >= 'a' && name[i - 1] <= 'z') || (name[i - 1] >= 'A' && name[i - 1] <= 'Z') || (name[i - 1] == '_'))) { name.remove_prefix(i); break; } } if (remove_suffix) { name.remove_suffix(s); } if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') || (name.front() >= 'A' && name.front() <= 'Z') || (name.front() == '_'))) { return name; } return {}; // Invalid name. } template constexpr std::array, N> to_array(T (&a)[N], std::index_sequence) { return {{a[I]...}}; } template constexpr bool cmp_less(L lhs, R rhs) noexcept { static_assert(std::is_integral_v && std::is_integral_v, "nameof::detail::cmp_less requires integral type."); if constexpr (std::is_signed_v == std::is_signed_v) { // If same signedness (both signed or both unsigned). return lhs < rhs; } else if constexpr (std::is_signed_v) { // If 'right' is negative, then result is 'false', otherwise cast & compare. return rhs > 0 && lhs < static_cast>(rhs); } else { // If 'left' is negative, then result is 'true', otherwise cast & compare. return lhs < 0 || static_cast>(lhs) < rhs; } } template constexpr I log2(I value) noexcept { static_assert(std::is_integral_v, "nameof::detail::log2 requires integral type."); auto ret = I{0}; for (; value > I{1}; value >>= I{1}, ++ret) {} return ret; } template struct nameof_enum_supported #if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED || defined(NAMEOF_ENUM_NO_CHECK_SUPPORT) : std::true_type {}; #else : std::false_type {}; #endif template using enable_if_enum_t = std::enable_if_t>, R>; template inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; template constexpr auto n() noexcept { static_assert(is_enum_v, "nameof::detail::n requires enum type."); constexpr auto custom_name = customize::enum_name(V); if constexpr (custom_name.empty()) { static_cast(custom_name); #if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED # if defined(__clang__) || defined(__GNUC__) constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); # elif defined(_MSC_VER) constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); # endif if constexpr (name.size() > 0) { return cstring{name}; } else { return string_view{}; } #else return string_view{}; // Unsupported compiler. #endif } else { return cstring{custom_name}; } } template inline constexpr auto enum_name_v = n(); template constexpr bool is_valid() noexcept { static_assert(is_enum_v, "nameof::detail::is_valid requires enum type."); return n(V)>().size() != 0; } template > constexpr E value(std::size_t i) noexcept { static_assert(is_enum_v, "nameof::detail::value requires enum type."); if constexpr (IsFlags) { return static_cast(U{1} << static_cast(static_cast(i) + O)); } else { return static_cast(static_cast(i) + O); } } template > constexpr int reflected_min() noexcept { static_assert(is_enum_v, "nameof::detail::reflected_min requires enum type."); if constexpr (IsFlags) { return 0; } else { constexpr auto lhs = customize::enum_range::min; static_assert(lhs > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); constexpr auto rhs = (std::numeric_limits::min)(); if constexpr (cmp_less(lhs, rhs)) { return rhs; } else { static_assert(!is_valid(0)>(), "nameof::enum_range detects enum value smaller than min range size."); return lhs; } } } template > constexpr int reflected_max() noexcept { static_assert(is_enum_v, "nameof::detail::reflected_max requires enum type."); if constexpr (IsFlags) { return std::numeric_limits::digits - 1; } else { constexpr auto lhs = customize::enum_range::max; static_assert(lhs < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT16_MAX."); constexpr auto rhs = (std::numeric_limits::max)(); if constexpr (cmp_less(lhs, rhs)) { static_assert(!is_valid(0)>(), "nameof::enum_range detects enum value larger than max range size."); return lhs; } else { return rhs; } } } template inline constexpr auto reflected_min_v = reflected_min(); template inline constexpr auto reflected_max_v = reflected_max(); template constexpr std::size_t values_count(const bool (&valid)[N]) noexcept { auto count = std::size_t{0}; for (std::size_t i = 0; i < N; ++i) { if (valid[i]) { ++count; } } return count; } template constexpr auto values(std::index_sequence) noexcept { static_assert(is_enum_v, "nameof::detail::values requires enum type."); constexpr bool valid[sizeof...(I)] = {is_valid(I)>()...}; constexpr std::size_t count = values_count(valid); if constexpr (count > 0) { E values[count] = {}; for (std::size_t i = 0, v = 0; v < count; ++i) { if (valid[i]) { values[v++] = value(i); } } return to_array(values, std::make_index_sequence{}); } else { return std::array{}; } } template > constexpr auto values() noexcept { static_assert(is_enum_v, "nameof::detail::values requires enum type."); constexpr auto min = reflected_min_v; constexpr auto max = reflected_max_v; constexpr auto range_size = max - min + 1; static_assert(range_size > 0, "nameof::enum_range requires valid size."); static_assert(range_size < (std::numeric_limits::max)(), "nameof::enum_range requires valid size."); return values>(std::make_index_sequence{}); } template inline constexpr auto values_v = values(); template > using values_t = decltype((values_v)); template inline constexpr auto count_v = values_v.size(); template > inline constexpr auto min_v = (count_v > 0) ? static_cast(values_v.front()) : U{0}; template > inline constexpr auto max_v = (count_v > 0) ? static_cast(values_v.back()) : U{0}; template > constexpr std::size_t range_size() noexcept { static_assert(is_enum_v, "nameof::detail::range_size requires enum type."); constexpr auto max = IsFlags ? log2(max_v) : max_v; constexpr auto min = IsFlags ? log2(min_v) : min_v; constexpr auto range_size = max - min + U{1}; static_assert(range_size > 0, "nameof::enum_range requires valid size."); static_assert(range_size < (std::numeric_limits::max)(), "nameof::enum_range requires valid size."); return static_cast(range_size); } template inline constexpr auto range_size_v = range_size(); template using index_t = std::conditional_t < (std::numeric_limits::max)(), std::uint8_t, std::uint16_t>; template inline constexpr auto invalid_index_v = (std::numeric_limits>::max)(); template constexpr auto indexes(std::index_sequence) noexcept { static_assert(is_enum_v, "nameof::detail::indexes requires enum type."); constexpr auto min = IsFlags ? log2(min_v) : min_v; [[maybe_unused]] auto i = index_t{0}; return std::array{{(is_valid(I)>() ? i++ : invalid_index_v)...}}; } template inline constexpr auto indexes_v = indexes(std::make_index_sequence>{}); template > constexpr bool is_sparse() noexcept { static_assert(is_enum_v, "nameof::detail::is_sparse requires enum type."); if constexpr (IsFlags) { return (sizeof(const char*) * range_size_v) > (sizeof(E) * count_v + sizeof(const char*) * count_v); } else { return (sizeof(const char*) * range_size_v) > (sizeof(index_t) * range_size_v + sizeof(const char*) * count_v); } } template inline constexpr bool is_sparse_v = is_sparse(); template > [[nodiscard]] constexpr E get_value(std::size_t i) noexcept { static_assert(is_enum_v, "nameof::detail::strings requires enum type."); if constexpr (is_sparse_v) { return values_v[i]; } else { constexpr auto min = IsFlags ? log2(min_v) : min_v; return value(i); } } template constexpr auto strings(std::index_sequence) noexcept { static_assert(is_enum_v, "nameof::detail::strings requires enum type."); return std::array{{enum_name_v(I)>.data()...}}; } template constexpr auto strings() noexcept { static_assert(is_enum_v, "nameof::detail::strings requires enum type."); constexpr auto count = is_sparse_v ? count_v : range_size_v; return strings(std::make_index_sequence{}); } template inline static constexpr auto strings_v = strings(); template struct nameof_type_supported #if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT) : std::true_type {}; #else : std::false_type {}; #endif template struct nameof_type_rtti_supported #if defined(NAMEOF_TYPE_RTTI_SUPPORTED) && NAMEOF_TYPE_RTTI_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT) : std::true_type {}; #else : std::false_type {}; #endif template struct nameof_member_supported #if defined(NAMEOF_MEMBER_SUPPORTED) && NAMEOF_MEMBER_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT) : std::true_type {}; #else : std::false_type {}; #endif #if defined(_MSC_VER) && !defined(__clang__) template struct identity { using type = T; }; #else template using identity = T; #endif template using remove_cvref_t = std::remove_cv_t>; template using enable_if_has_short_name_t = std::enable_if_t && !std::is_pointer_v, R>; template constexpr auto n() noexcept { # if defined(_MSC_VER) && !defined(__clang__) constexpr auto custom_name = customize::type_name(); #else constexpr auto custom_name = customize::type_name(); # endif if constexpr (custom_name.empty()) { static_cast(custom_name); #if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED # if defined(__clang__) constexpr string_view name{__PRETTY_FUNCTION__ + 31, sizeof(__PRETTY_FUNCTION__) - 34}; # elif defined(__GNUC__) constexpr string_view name{__PRETTY_FUNCTION__ + 46, sizeof(__PRETTY_FUNCTION__) - 49}; # elif defined(_MSC_VER) constexpr string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)}; # endif return cstring{name}; #else return string_view{}; // Unsupported compiler. #endif } else { return cstring{custom_name}; } } template inline constexpr auto type_name_v = n(); #if __has_include() template 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 dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr); const auto name = string{dmg}; std::free(dmg); assert(name.size() > 0 && "Type does not have a name."); return name; } template 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)."); const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr); auto name = string{dmg}; std::free(dmg); assert(name.size() > 0 && "Type does not have a name."); if constexpr (std::is_const_v>) { name = "const " + name; } if constexpr (std::is_volatile_v>) { name = "volatile " + name; } if constexpr (std::is_lvalue_reference_v) { name += '&'; } if constexpr (std::is_rvalue_reference_v) { name += "&&"; } return name; } template = 0> 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 dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr); const auto name = string{pretty_name(dmg)}; std::free(dmg); assert(name.size() > 0 && "Type does not have a short name."); return name; } #else template string nameof_type_rtti(const char* tn) noexcept { 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.size() > 0 && "Type does not have a name."); return {name.begin(), name.end()}; } template string nameof_full_type_rtti(const char* tn) noexcept { 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.size() > 0 && "Type does not have a name."); if constexpr (std::is_const_v>) { name = "const " + name; } if constexpr (std::is_volatile_v>) { name = "volatile " + name; } if constexpr (std::is_lvalue_reference_v) { name += '&'; } if constexpr (std::is_rvalue_reference_v) { name += "&&"; } return name; } template = 0> string nameof_short_type_rtti(const char* tn) noexcept { 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.size() > 0 && "Type does not have a short name."); return {name.begin(), name.end()}; } #endif template constexpr auto n() noexcept { constexpr auto custom_name = customize::member_name(); if constexpr (custom_name.empty()) { static_cast(custom_name); #if defined(NAMEOF_MEMBER_SUPPORTED) && NAMEOF_MEMBER_SUPPORTED constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); return cstring{name}; #else return string_view{}; // Unsupported compiler. #endif } else { return cstring{custom_name}; } } template inline constexpr auto member_name_v = n(); } // namespace nameof::detail // Checks is nameof_type supported compiler. inline constexpr bool is_nameof_type_supported = detail::nameof_type_supported::value; // Checks is nameof_type_rtti supported compiler. inline constexpr bool is_nameof_type_rtti_supported = detail::nameof_type_rtti_supported::value; // Checks is nameof_member supported compiler. inline constexpr bool is_nameof_member_supported = detail::nameof_member_supported::value; // Checks is nameof_enum supported compiler. inline constexpr bool is_nameof_enum_supported = detail::nameof_enum_supported::value; // Obtains simple (unqualified) name of enum variable. template [[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> 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 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 constexpr (detail::is_sparse_v) { if (const auto idx = detail::indexes_v[i]; idx != detail::invalid_index_v) { return detail::strings_v[idx]; } } else { return detail::strings_v[i]; } } assert(valid && "enum variable does not have a name."); return {}; // Value out of range. } // Obtains simple (unqualified) name of enum-flags variable. template [[nodiscard]] auto nameof_enum_flag(E value) -> 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)."); static_assert(detail::count_v > 0, "nameof::nameof_enum_flag requires enum-flags implementation."); constexpr auto size = detail::is_sparse_v ? detail::count_v : detail::range_size_v; string name; auto check_value = U{0}; for (std::size_t i = 0; i < size; ++i) { if (const auto v = static_cast(detail::get_value(i)); (static_cast(value) & v) != 0) { if (const auto n = detail::strings_v[i]; n != nullptr) { check_value |= v; if (!name.empty()) { name.append(1, '|'); } name.append(n); } else { return {}; // Value out of range. } } } const bool valid = check_value != 0 && check_value == static_cast(value); if (valid) { return name; } assert(valid && "enum-flags variable does not have a name."); return {}; // Invalid value or out of range. } // Obtains simple (unqualified) name of static storage enum variable. // This version is much lighter on the compile times and is not restricted to the enum_range limitation. template [[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t { using D = std::decay_t; 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.size() > 0, "Enum value does not have a name."); return name; } // Obtains name of type, reference and cv-qualifiers are ignored. template [[nodiscard]] constexpr string_view nameof_type() noexcept { static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); using U = detail::identity>; constexpr string_view name = detail::type_name_v; static_assert(name.size() > 0, "Type does not have a name."); return name; } // Obtains full name of type, with reference and cv-qualifiers. template [[nodiscard]] constexpr string_view nameof_full_type() noexcept { static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); using U = detail::identity; constexpr string_view name = detail::type_name_v; static_assert(name.size() > 0, "Type does not have a full name."); return name; } // Obtains short name of type. template [[nodiscard]] constexpr auto nameof_short_type() noexcept -> detail::enable_if_has_short_name_t { static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); using U = detail::identity>; constexpr string_view name = detail::pretty_name(detail::type_name_v); static_assert(name.size() > 0, "Type does not have a short name."); return name; } // Obtains name of member. template [[nodiscard]] constexpr auto nameof_member() noexcept -> std::enable_if_t, string_view> { static_assert(detail::nameof_member_supported::value, "nameof::nameof_memder unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); constexpr string_view name = detail::member_name_v; static_assert(name.size() > 0, "Member does not have a name."); return name; } } // namespace nameof // Obtains name of variable, function, macro. #define NAMEOF(...) []() constexpr noexcept { \ ::std::void_t(); \ constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__); \ static_assert(_name.size() > 0, "Expression does not have a name."); \ constexpr auto _size = _name.size(); \ constexpr auto _nameof = ::nameof::cstring<_size>{_name}; \ return _nameof; }() // Obtains full name of variable, function, macro. #define NAMEOF_FULL(...) []() constexpr noexcept { \ ::std::void_t(); \ constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__, false); \ static_assert(_name.size() > 0, "Expression does not have a name."); \ constexpr auto _size = _name.size(); \ constexpr auto _nameof_full = ::nameof::cstring<_size>{_name}; \ return _nameof_full; }() // Obtains raw name of variable, function, macro. #define NAMEOF_RAW(...) []() constexpr noexcept { \ ::std::void_t(); \ constexpr auto _name = ::nameof::string_view{#__VA_ARGS__}; \ static_assert(_name.size() > 0, "Expression does not have a name."); \ constexpr auto _size = _name.size(); \ constexpr auto _nameof_raw = ::nameof::cstring<_size>{_name}; \ return _nameof_raw; }() // Obtains name of enum variable. #define NAMEOF_ENUM(...) ::nameof::nameof_enum<::std::decay_t>(__VA_ARGS__) // Obtains name of static storage enum variable. // This version is much lighter on the compile times and is not restricted to the enum_range limitation. #define NAMEOF_ENUM_CONST(...) ::nameof::nameof_enum<__VA_ARGS__>() // Obtains name of enum-flags variable. #define NAMEOF_ENUM_FLAG(...) ::nameof::nameof_enum_flag<::std::decay_t>(__VA_ARGS__) // Obtains type name, reference and cv-qualifiers are ignored. #define NAMEOF_TYPE(...) ::nameof::nameof_type<__VA_ARGS__>() // Obtains type name of expression, reference and cv-qualifiers are ignored. #define NAMEOF_TYPE_EXPR(...) ::nameof::nameof_type() // Obtains full type name, with reference and cv-qualifiers. #define NAMEOF_FULL_TYPE(...) ::nameof::nameof_full_type<__VA_ARGS__>() // Obtains full type name of expression, with reference and cv-qualifiers. #define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type() // Obtains short type name. #define NAMEOF_SHORT_TYPE(...) ::nameof::nameof_short_type<__VA_ARGS__>() // Obtains short type name of expression. #define NAMEOF_SHORT_TYPE_EXPR(...) ::nameof::nameof_short_type() // Obtains type name, with reference and cv-qualifiers, using RTTI. #define NAMEOF_TYPE_RTTI(...) ::nameof::detail::nameof_type_rtti<::std::void_t>(typeid(__VA_ARGS__).name()) // Obtains full type name, using RTTI. #define NAMEOF_FULL_TYPE_RTTI(...) ::nameof::detail::nameof_full_type_rtti(typeid(__VA_ARGS__).name()) // Obtains short type name, using RTTI. #define NAMEOF_SHORT_TYPE_RTTI(...) ::nameof::detail::nameof_short_type_rtti(typeid(__VA_ARGS__).name()) // Obtains name of member. #define NAMEOF_MEMBER(...) ::nameof::nameof_member<__VA_ARGS__>() #if defined(__clang__) # pragma clang diagnostic pop #elif defined(__GNUC__) # pragma GCC diagnostic pop #elif defined(_MSC_VER) # pragma warning(pop) #endif #endif // NEARGYE_NAMEOF_HPP