From 7bf624de33e4167a0a9eda17ee8fc439ae7ace3c Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 3 Jun 2020 19:33:01 +0500 Subject: [PATCH] wip NAMEOF_TYPE_RTTI --- include/nameof.hpp | 9 +++++++-- test/test.cpp | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index bd9c91d..7e883a9 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -60,6 +60,11 @@ # define NAMEOF_TYPE_SUPPORTED 1 #endif +#if defined(__cpp_rtti) || defined(_CPPRTTI) || defined(__GXX_RTTI) +# undef NAMEOF_TYPE_RTTI_SUPPORTED +# define NAMEOF_TYPE_RTTI_SUPPORTED 1 +#endif + // Checks nameof_enum compiler compatibility. #if defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) # undef NAMEOF_ENUM_SUPPORTED @@ -672,10 +677,10 @@ template #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) +#if defined(NAMEOF_TYPE_RTTI_SUPPORTED) && NAMEOF_TYPE_RTTI_SUPPORTED # define NAMEOF_TYPE_RTTI(...) ::nameof::detail::n(typeid(__VA_ARGS__).name()) #else -# define NAMEOF_TYPE_RTTI(...) +# define NAMEOF_TYPE_RTTI(...) static_assert(sizeof(__VA_ARGS__) < 0, "NAMEOF_TYPE_RTTI unsupported compiler."); #endif #if defined(_MSC_VER) diff --git a/test/test.cpp b/test/test.cpp index f10aeb7..3abd25e 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -684,3 +684,18 @@ TEST_CASE("nameof::nameof_type") { } #endif + +#if defined(NAMEOF_TYPE_RTTI_SUPPORTED) && NAMEOF_TYPE_RTTI_SUPPORTED +TEST_CASE("NAMEOF_TYPE_RTTI") { +#if defined(__clang__) + REQUIRE(NAMEOF_TYPE_RTTI(std::string) == "std::__1::basic_string, std::__1::allocator >"); + REQUIRE(NAMEOF_TYPE_RTTI(Color) == "Color"); +#elif defined(_MSC_VER) + REQUIRE(NAMEOF_TYPE_RTTI(std::string) == "class std::basic_string,class std::allocator >"); + REQUIRE(NAMEOF_TYPE_RTTI(Color) == "enum Color"); +#elif defined(__GNUC__) + REQUIRE(NAMEOF_TYPE_RTTI(std::string) == "std::__cxx11::basic_string, std::allocator >"); + REQUIRE(NAMEOF_TYPE_RTTI(Color) == "Color"); +#endif +} +#endif