From 2d5e3402fa59d9b52b4458a1cfb6849cdfa87f6a Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 6 Apr 2022 11:25:44 +0200 Subject: [PATCH] type_info: allow to construct type_info objects directly --- src/entt/core/type_info.hpp | 26 +++++++++++++------------- test/entt/core/type_info.cpp | 34 ++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/entt/core/type_info.hpp b/src/entt/core/type_info.hpp index 153d437de..73243f3d1 100644 --- a/src/entt/core/type_info.hpp +++ b/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 - friend const type_info &type_id() ENTT_NOEXCEPT; - - template - constexpr type_info(std::in_place_type_t) ENTT_NOEXCEPT - : seq{type_index>>::value()}, - identifier{type_hash>>::value()}, - alias{type_name>>::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 + constexpr type_info(std::in_place_type_t) ENTT_NOEXCEPT + : seq{type_index>>::value()}, + identifier{type_hash>>::value()}, + alias{type_name>>::value()} { + ENTT_ASSERT(seq != 0u, "Invalid type index"); + } + /** * @brief Type index. * @return Type index. diff --git a/test/entt/core/type_info.cpp b/test/entt/core/type_info.cpp index 8d19ac715..84f238880 100644 --- a/test/entt/core/type_info.cpp +++ b/test/entt/core/type_info.cpp @@ -48,21 +48,12 @@ TEST(TypeInfo, Functionalities) { static_assert(std::is_copy_assignable_v); static_assert(std::is_move_assignable_v); - const int value = 42; + entt::type_info info{std::in_place_type}; + entt::type_info other{std::in_place_type}; - ASSERT_EQ(entt::type_id(value), entt::type_id()); - ASSERT_EQ(entt::type_id(42), entt::type_id()); - - ASSERT_EQ(entt::type_id(), entt::type_id()); - ASSERT_EQ(entt::type_id(), entt::type_id()); - ASSERT_EQ(entt::type_id(), entt::type_id()); - ASSERT_NE(entt::type_id(), entt::type_id()); - - ASSERT_EQ(&entt::type_id(), &entt::type_id()); - ASSERT_NE(&entt::type_id(), &entt::type_id()); - - auto info = entt::type_id(); - auto other = entt::type_id(); + ASSERT_EQ(info, entt::type_info{std::in_place_type}); + ASSERT_EQ(info, entt::type_info{std::in_place_type}); + ASSERT_EQ(info, entt::type_info{std::in_place_type}); 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()); + ASSERT_EQ(entt::type_id(42), entt::type_id()); + + ASSERT_EQ(entt::type_id(), entt::type_id()); + ASSERT_EQ(entt::type_id(), entt::type_id()); + ASSERT_EQ(entt::type_id(), entt::type_id()); + ASSERT_NE(entt::type_id(), entt::type_id()); + + ASSERT_EQ(&entt::type_id(), &entt::type_id()); + ASSERT_NE(&entt::type_id(), &entt::type_id()); +}