From 4a668c97a353e997102fe5b2a7c5dc83397204d7 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 2 Dec 2020 10:45:41 +0100 Subject: [PATCH] type_info: get around a nasty bug of clang < 9 when using libstdc++ --- src/entt/config/config.h | 16 +++------- src/entt/core/type_info.hpp | 63 ++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/entt/config/config.h b/src/entt/config/config.h index 20d9c0323..291c781f7 100644 --- a/src/entt/config/config.h +++ b/src/entt/config/config.h @@ -42,20 +42,14 @@ #ifndef ENTT_STANDARD_CPP -# if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8) -# define ENTT_PRETTY_FUNCTION_CONSTEXPR -# define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__ -# define ENTT_PRETTY_FUNCTION_PREFIX '=' -# define ENTT_PRETTY_FUNCTION_SUFFIX ']' -# elif defined __GNUC__ -# define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__ -# define ENTT_PRETTY_FUNCTION_PREFIX '=' -# define ENTT_PRETTY_FUNCTION_SUFFIX ']' -# elif defined _MSC_VER -# define ENTT_PRETTY_FUNCTION_CONSTEXPR +# if defined _MSC_VER # define ENTT_PRETTY_FUNCTION __FUNCSIG__ # define ENTT_PRETTY_FUNCTION_PREFIX '<' # define ENTT_PRETTY_FUNCTION_SUFFIX '>' +# elif defined __clang__ || defined __GNUC__ +# define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__ +# define ENTT_PRETTY_FUNCTION_PREFIX '=' +# define ENTT_PRETTY_FUNCTION_SUFFIX ']' # endif #endif diff --git a/src/entt/core/type_info.hpp b/src/entt/core/type_info.hpp index aa871de94..212f0e9d2 100644 --- a/src/entt/core/type_info.hpp +++ b/src/entt/core/type_info.hpp @@ -31,7 +31,7 @@ struct ENTT_API type_seq final { template -[[nodiscard]] constexpr auto type_name() ENTT_NOEXCEPT { +[[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT { #if defined ENTT_PRETTY_FUNCTION std::string_view pretty_function{ENTT_PRETTY_FUNCTION}; auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1); @@ -43,6 +43,37 @@ template } +template().find_first_of('.')> +[[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT { + constexpr auto value = stripped_type_name(); + return value; +} + + +template +[[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT { + static const auto value = stripped_type_name(); + return value; +} + + +template().find_first_of('.')> +[[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT { + constexpr auto stripped = stripped_type_name(); + constexpr auto value = hashed_string::value(stripped.data(), stripped.size()); + return value; +} + + +template +[[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT { + static const auto value = [](const auto stripped) { + return hashed_string::value(stripped.data(), stripped.size()); + }(stripped_type_name()); + return value; +} + + } @@ -79,22 +110,14 @@ struct type_hash final { * @brief Returns the numeric representation of a given type. * @return The numeric representation of the given type. */ -#if defined ENTT_PRETTY_FUNCTION_CONSTEXPR +#if defined ENTT_PRETTY_FUNCTION [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT { - constexpr auto stripped = internal::type_name(); - constexpr auto value = hashed_string::value(stripped.data(), stripped.size()); - return value; - } -#elif defined ENTT_PRETTY_FUNCTION - [[nodiscard]] static id_type value() ENTT_NOEXCEPT { - static const auto value = [](const auto stripped) { return hashed_string::value(stripped.data(), stripped.size()); }(internal::type_name()); - return value; - } + return internal::type_hash(0); #else - [[nodiscard]] static id_type value() ENTT_NOEXCEPT { + [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT { return type_seq::value(); - } #endif + } }; @@ -108,21 +131,9 @@ struct type_name final { * @brief Returns the name of a given type. * @return The name of the given type. */ -#if defined ENTT_PRETTY_FUNCTION_CONSTEXPR [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT { - constexpr auto value = internal::type_name(); - return value; + return internal::type_name(0); } -#elif defined ENTT_PRETTY_FUNCTION - [[nodiscard]] static std::string_view value() ENTT_NOEXCEPT { - static const auto value = internal::type_name(); - return value; - } -#else - [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT { - return internal::type_name(); - } -#endif };