Bladeren bron

type_info: added ::name

Michele Caini 5 jaren geleden
bovenliggende
commit
4a305e1568
3 gewijzigde bestanden met toevoegingen van 72 en 13 verwijderingen
  1. 13 8
      src/entt/config/config.h
  2. 43 4
      src/entt/core/type_info.hpp
  3. 16 1
      test/entt/core/type_info.cpp

+ 13 - 8
src/entt/config/config.h

@@ -53,15 +53,20 @@
 
 
 #ifndef ENTT_STANDARD_CPP
-#   if defined _MSC_VER
-#      define ENTT_PRETTY_FUNCTION __FUNCSIG__
-#      define ENTT_PRETTY_FUNCTION_CONSTEXPR(...) constexpr
-#   elif defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
-#      define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
-#      define ENTT_PRETTY_FUNCTION_CONSTEXPR(...) constexpr
+#   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_CONSTEXPR(...) __VA_ARGS__
+#       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
+#       define ENTT_PRETTY_FUNCTION __FUNCSIG__
+#       define ENTT_PRETTY_FUNCTION_PREFIX '<'
+#       define ENTT_PRETTY_FUNCTION_SUFFIX '>'
 #   endif
 #endif
 

+ 43 - 4
src/entt/core/type_info.hpp

@@ -2,6 +2,7 @@
 #define ENTT_CORE_TYPE_INFO_HPP
 
 
+#include <string_view>
 #include "../config/config.h"
 #include "../core/attribute.h"
 #include "hashed_string.hpp"
@@ -28,6 +29,19 @@ struct ENTT_API type_index {
 };
 
 
+template<typename Type>
+constexpr auto 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);
+    auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
+    return value;
+#else
+    return std::string_view{};
+#endif
+}
+
+
 }
 
 
@@ -81,14 +95,19 @@ inline constexpr bool has_type_index_v = has_type_index<Type>::value;
  * @tparam Type Type for which to generate information.
  */
 template<typename Type, typename = void>
-struct ENTT_API type_info {
+struct type_info {
     /**
      * @brief Returns the numeric representation of a given type.
      * @return The numeric representation of the given type.
      */
-#if defined ENTT_PRETTY_FUNCTION
-    static ENTT_PRETTY_FUNCTION_CONSTEXPR() id_type id() ENTT_NOEXCEPT {
-        ENTT_PRETTY_FUNCTION_CONSTEXPR(static const) auto value = entt::hashed_string::value(ENTT_PRETTY_FUNCTION);
+#if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
+    static constexpr id_type id() ENTT_NOEXCEPT {
+        constexpr auto value = entt::hashed_string::value(ENTT_PRETTY_FUNCTION);
+        return value;
+    }
+#elif defined ENTT_PRETTY_FUNCTION
+    static id_type id() ENTT_NOEXCEPT {
+        static const auto value = entt::hashed_string::value(ENTT_PRETTY_FUNCTION);
         return value;
     }
 #else
@@ -96,6 +115,26 @@ struct ENTT_API type_info {
         return type_index<Type>::value();
     }
 #endif
+
+    /**
+     * @brief Returns the name of a given type.
+     * @return The name of the given type.
+     */
+#if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
+    static constexpr std::string_view name() ENTT_NOEXCEPT {
+        constexpr auto value = internal::type_name<Type>();
+        return value;
+    }
+#elif defined ENTT_PRETTY_FUNCTION
+    static std::string_view name() ENTT_NOEXCEPT {
+        static const auto value = internal::type_name<Type>();
+        return value;
+    }
+#else
+    static constexpr std::string_view name() ENTT_NOEXCEPT {
+        return internal::type_name<Type>();
+    }
+#endif
 };
 
 

+ 16 - 1
test/entt/core/type_info.cpp

@@ -1,13 +1,28 @@
+#include <string_view>
 #include <gtest/gtest.h>
 #include <entt/core/hashed_string.hpp>
 #include <entt/core/type_info.hpp>
+#include <entt/core/type_traits.hpp>
 
-TEST(TypeInfo, Functionalities) {
+TEST(TypeInfo, Id) {
     ASSERT_NE(entt::type_info<int>::id(), entt::type_info<const int>::id());
     ASSERT_NE(entt::type_info<int>::id(), entt::type_info<char>::id());
     ASSERT_EQ(entt::type_info<int>::id(), entt::type_info<int>::id());
 }
 
+TEST(TypeInfo, Name) {
+    ASSERT_EQ(entt::type_info<int>::name(), std::string_view{"int"});
+
+    ASSERT_TRUE((entt::type_info<entt::integral_constant<3>>::name() == std::string_view{"std::integral_constant<int, 3>"})
+                || (entt::type_info<entt::integral_constant<3>>::name() == std::string_view{"std::__1::integral_constant<int, 3>"})
+                || (entt::type_info<entt::integral_constant<3>>::name() == std::string_view{"struct std::integral_constant<int,3>"}))
+            << "Type name: " << entt::type_info<entt::integral_constant<3>>::name();
+
+    ASSERT_TRUE(((entt::type_info<entt::type_list<entt::type_list<int, char>, double>>::name()) == std::string_view{"entt::type_list<entt::type_list<int, char>, double>"})
+                || ((entt::type_info<entt::type_list<entt::type_list<int, char>, double>>::name()) == std::string_view{"struct entt::type_list<struct entt::type_list<int,char>,double>"}))
+            << "Type name: " << entt::type_info<entt::integral_constant<3>>::name();
+}
+
 TEST(TypeIndex, Functionalities) {
     ASSERT_EQ(entt::type_index<int>::value(), entt::type_index<int>::value());
     ASSERT_NE(entt::type_index<int>::value(), entt::type_index<char>::value());