1
0
Эх сурвалжийг харах

type_info: allow to construct type_info objects directly

Michele Caini 3 жил өмнө
parent
commit
2d5e3402fa

+ 13 - 13
src/entt/core/type_info.hpp

@@ -139,25 +139,25 @@ struct type_name final {
 };
 
 /*! @brief Implementation specific information about a type. */
-class type_info final {
-    template<typename Type>
-    friend const type_info &type_id() ENTT_NOEXCEPT;
-
-    template<typename Type>
-    constexpr type_info(std::in_place_type_t<Type>) ENTT_NOEXCEPT
-        : seq{type_index<std::remove_reference_t<std::remove_const_t<Type>>>::value()},
-          identifier{type_hash<std::remove_reference_t<std::remove_const_t<Type>>>::value()},
-          alias{type_name<std::remove_reference_t<std::remove_const_t<Type>>>::value()} {
-        ENTT_ASSERT(seq != 0u, "Invalid type index");
-    }
-
-public:
+struct type_info final {
     /*! @brief Default constructor. */
     constexpr type_info() ENTT_NOEXCEPT
         : seq{},
           identifier{},
           alias{} {}
 
+    /**
+     * @brief Constructs a type info object for a given type.
+     * @tparam Type Type for which to construct a type info object.
+     */
+    template<typename Type>
+    constexpr type_info(std::in_place_type_t<Type>) ENTT_NOEXCEPT
+        : seq{type_index<std::remove_cv_t<std::remove_reference_t<Type>>>::value()},
+          identifier{type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value()},
+          alias{type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()} {
+        ENTT_ASSERT(seq != 0u, "Invalid type index");
+    }
+
     /**
      * @brief Type index.
      * @return Type index.

+ 20 - 14
test/entt/core/type_info.cpp

@@ -48,21 +48,12 @@ TEST(TypeInfo, Functionalities) {
     static_assert(std::is_copy_assignable_v<entt::type_info>);
     static_assert(std::is_move_assignable_v<entt::type_info>);
 
-    const int value = 42;
+    entt::type_info info{std::in_place_type<int>};
+    entt::type_info other{std::in_place_type<void>};
 
-    ASSERT_EQ(entt::type_id(value), entt::type_id<int>());
-    ASSERT_EQ(entt::type_id(42), entt::type_id<int>());
-
-    ASSERT_EQ(entt::type_id<int>(), entt::type_id<int>());
-    ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int &&>());
-    ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int>());
-    ASSERT_NE(entt::type_id<int>(), entt::type_id<char>());
-
-    ASSERT_EQ(&entt::type_id<int>(), &entt::type_id<int>());
-    ASSERT_NE(&entt::type_id<int>(), &entt::type_id<void>());
-
-    auto info = entt::type_id<const int &>();
-    auto other = entt::type_id<void>();
+    ASSERT_EQ(info, entt::type_info{std::in_place_type<int &>});
+    ASSERT_EQ(info, entt::type_info{std::in_place_type<int &&>});
+    ASSERT_EQ(info, entt::type_info{std::in_place_type<const int &>});
 
     ASSERT_NE(info, other);
     ASSERT_TRUE(info == info);
@@ -119,3 +110,18 @@ TEST(TypeInfo, Order) {
     ASSERT_GT(lhs, rhs);
     ASSERT_GE(lhs, rhs);
 }
+
+TEST(TypeId, Functionalities) {
+    const int value = 42;
+
+    ASSERT_EQ(entt::type_id(value), entt::type_id<int>());
+    ASSERT_EQ(entt::type_id(42), entt::type_id<int>());
+
+    ASSERT_EQ(entt::type_id<int>(), entt::type_id<int>());
+    ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int &&>());
+    ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int>());
+    ASSERT_NE(entt::type_id<int>(), entt::type_id<char>());
+
+    ASSERT_EQ(&entt::type_id<int>(), &entt::type_id<int>());
+    ASSERT_NE(&entt::type_id<int>(), &entt::type_id<void>());
+}