From 2cdcdc0f838643c210584d6ccb7cc7e27288362e Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 3 Jun 2020 19:25:30 +0500 Subject: [PATCH] add NAMEOF_TYPE_RTTI --- include/nameof.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/nameof.hpp b/include/nameof.hpp index bee0337..bd9c91d 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -43,6 +43,12 @@ #include #include +#if __has_include() +#include +#include +#include +#endif + #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable : 26495) // Variable 'nameof::cstring::chars_' is uninitialized. @@ -535,6 +541,17 @@ constexpr auto n() noexcept { template inline constexpr auto type_name_v = n(); +inline auto n(const char* tn) { +#if __has_include() + auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr); + auto r = std::string{dmg != nullptr ? dmg : tn}; + std::free(dmg); + return r; +#else + return std::string_view{tn}; +#endif +} + } // namespace nameof::detail // Checks is nameof_type supported compiler. @@ -654,6 +671,13 @@ template // Obtains string name full type of expression, with reference and cv-qualifiers. #define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type() +// Obtains string name of type using RTTI. +#if defined(__cpp_rtti) || defined(_CPPRTTI) || defined(__GXX_RTTI) +# define NAMEOF_TYPE_RTTI(...) ::nameof::detail::n(typeid(__VA_ARGS__).name()) +#else +# define NAMEOF_TYPE_RTTI(...) +#endif + #if defined(_MSC_VER) # pragma warning(pop) #endif