// _ _ __ _____ // | \ | | / _| / ____|_ _ // | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_ // | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _| // | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_| // |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____| // https://github.com/Neargye/nameof // vesion 0.5.0 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2016, 2018 - 2019 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. #pragma once #include #include #include #if !defined(NAMEOF_ENUM_MAX_SEARCH_DEPTH) # define NAMEOF_ENUM_MAX_SEARCH_DEPTH 64 #endif namespace nameof { namespace detail { template 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_bracket(char s) noexcept { return s == ')' || s == '}' || s == '>' || s == '(' || s == '{' || s == '<'; } constexpr std::string_view pretty_name(std::string_view name, bool with_suffix) { 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])) { ++s; continue; } if (name[i - 1] == ')' || name[i - 1] == '}') { ++h; ++s; continue; } else if (name[i - 1] == '(' || 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 (is_lexeme(name[i - 1])) { name.remove_prefix(i); break; } } name.remove_suffix(with_suffix ? 0 : s); return name; } template constexpr int nameof_enum_impl_() { #if defined(__clang__) return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::nameof_enum_impl_() [T = ") - sizeof("]") + 1; #elif defined(__GNUC__) return sizeof(__PRETTY_FUNCTION__) - sizeof("constexpr int nameof::detail::nameof_enum_impl_() [with T = ") - sizeof("]") + 1; #elif defined(_MSC_VER) return sizeof(__FUNCSIG__) - sizeof("int __cdecl nameof::detail::nameof_enum_impl_<") - sizeof(">(void)") + 1; #else return 0; #endif } template constexpr std::string_view nameof_enum_impl() { #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 suffix = sizeof("]") - 1; return {str + prefix, size - prefix - suffix}; #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 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 suffix = sizeof(">(void)") - 1; return {str + prefix, size - prefix - suffix}; #else return {}; #endif } template struct nameof_enum_t { constexpr std::string_view operator()(int value) const { return (value - I == 0) ? nameof_enum_impl() : (value - I == 1) ? nameof_enum_impl() : (value - I == 2) ? nameof_enum_impl() : (value - I == 3) ? nameof_enum_impl() : (value - I == 4) ? nameof_enum_impl() : (value - I == 5) ? nameof_enum_impl() : (value - I == 6) ? nameof_enum_impl() : (value - I == 7) ? nameof_enum_impl() : nameof_enum_t{}(value); } }; template struct nameof_enum_t { constexpr std::string_view operator()(int) const { return {"nameof_enum::out_of_range"}; } }; constexpr std::string_view nameof_type_impl_(std::string_view name) { #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 constexpr std::string_view nameof_type_impl() { #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_type_impl() [T = nameof::detail::identity<") - 1; const auto suffix = sizeof(">]") - 1; return nameof_type_impl_({str + prefix, size - prefix - suffix}); #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_type_impl() [with T = nameof::detail::identity<") - 1; const auto suffix = sizeof(">; std::string_view = std::basic_string_view]") - 1; return nameof_type_impl_({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_type_impl>(void)") - 1; return nameof_type_impl_({str + prefix, size - prefix - suffix}); #else return {}; #endif } } // namespace detail template ::value>::type> constexpr std::string_view nameof(std::string_view name, bool with_suffix = false) { return detail::pretty_name(name, with_suffix); } template ::type>::value>::type> constexpr std::string_view nameof_enum(T value) { constexpr auto s = std::is_signed::type>::type>::value; const auto name = detail::nameof_enum_t::type, s ? -NAMEOF_ENUM_MAX_SEARCH_DEPTH : 0>{}(static_cast(value)); #if defined(__clang__) || defined(_MSC_VER) return detail::pretty_name(name, false); #elif defined(__GNUC__) return name; #else return {}; #endif } template constexpr std::string_view nameof_type() { return detail::nameof_type_impl>(); } template constexpr std::string_view nameof_raw(std::string_view name) { return name; } } // namespace nameof // Used to obtain the simple (unqualified) string name of a variable, member, function, enum, macros. #define NAMEOF(...) ::nameof::nameof(#__VA_ARGS__, false) // Used to obtain the full string name of a variable, member, function, enum, macros. #define NAMEOF_FULL(...) ::nameof::nameof(#__VA_ARGS__, true) // Used to obtain the raw string name of a variable, member, function, enum, macros. #define NAMEOF_RAW(...) ::nameof::nameof_raw(#__VA_ARGS__) // Used to obtain the simple (unqualified) string name of a enum variable. #define NAMEOF_ENUM(...) ::nameof::nameof_enum(__VA_ARGS__) // Used to obtain the string name of a type. #define NAMEOF_TYPE(...) ::nameof::nameof_type() #define NAMEOF_TYPE_T(...) ::nameof::nameof_type<__VA_ARGS__>()