diff --git a/docs/md/core.md b/docs/md/core.md index 49090400b..58ca50cb7 100644 --- a/docs/md/core.md +++ b/docs/md/core.md @@ -491,6 +491,18 @@ auto info = entt::type_id(); These are the information made available by this object: +* The index associated with a given type: + + ```cpp + auto idx = entt::type_id().index(); + ``` + + This is also an alias for the following: + + ```cpp + auto idx = entt::type_index>>::value(); + ``` + * The hash value associated with a given type: ```cpp @@ -500,7 +512,7 @@ These are the information made available by this object: This is also an alias for the following: ```cpp - auto hash = entt::type_hash::value(); + auto hash = entt::type_hash>>::value(); ``` * The name associated with a given type: @@ -512,7 +524,7 @@ These are the information made available by this object: This is also an alias for the following: ```cpp - auto name = entt::type_name::value(); + auto name = entt::type_name>>::value(); ``` Where all accessed features are available at compile-time, the `type_info` class diff --git a/src/entt/core/type_info.hpp b/src/entt/core/type_info.hpp index 9fe05e6b2..20e1e2f4a 100644 --- a/src/entt/core/type_info.hpp +++ b/src/entt/core/type_info.hpp @@ -151,7 +151,8 @@ struct type_name final { struct type_info final { /*! @brief Default constructor. */ constexpr type_info() ENTT_NOEXCEPT - : identifier{}, + : seq{}, + identifier{}, alias{} {} @@ -166,7 +167,8 @@ struct type_info final { */ template constexpr type_info(std::in_place_type_t) ENTT_NOEXCEPT - : identifier{type_hash>>::value()}, + : seq{type_index>>::value()}, + identifier{type_hash>>::value()}, alias{type_name>>::value()} {} @@ -190,6 +192,14 @@ struct type_info final { return alias.data() != nullptr; } + /** + * @brief Type index. + * @return Type index. + */ + [[nodiscard]] constexpr id_type index() const ENTT_NOEXCEPT { + return seq; + } + /** * @brief Type hash. * @return Type hash. @@ -216,6 +226,7 @@ struct type_info final { } private: + id_type seq; id_type identifier; std::string_view alias; }; diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 61b7d1eac..89f909b83 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -183,16 +183,14 @@ public: * empty and thus invalid element otherwise. */ poly_storage & storage(const type_info info) { - auto it = std::find_if(pools.begin(), pools.end(), [info](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == info.hash(); }); - ENTT_ASSERT(it != pools.end(), "Storage not available"); - return it->poly; + ENTT_ASSERT(info.index() < pools.size() && pools[info.index()].poly, "Storage not available"); + return pools[info.index()].poly; } /*! @copydoc storage */ const poly_storage & storage(const type_info info) const { - auto it = std::find_if(pools.cbegin(), pools.cend(), [info](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == info.hash(); }); - ENTT_ASSERT(it != pools.cend(), "Storage not available"); - return it->poly; + ENTT_ASSERT(info.index() < pools.size() && pools[info.index()].poly, "Storage not available"); + return pools[info.index()].poly; } /** diff --git a/test/entt/core/type_info.cpp b/test/entt/core/type_info.cpp index 44f629a32..5982dc1c9 100644 --- a/test/entt/core/type_info.cpp +++ b/test/entt/core/type_info.cpp @@ -54,7 +54,7 @@ TEST(TypeInfo, Functionalities) { ASSERT_EQ(entt::type_id(), entt::type_id()); ASSERT_EQ(entt::type_id(), entt::type_id()); - auto info = entt::type_id(); + auto info = entt::type_id(); const auto unnamed = entt::type_id(); entt::type_info empty{}; @@ -62,6 +62,7 @@ TEST(TypeInfo, Functionalities) { ASSERT_TRUE(info == info); ASSERT_FALSE(info != info); + ASSERT_EQ(info.index(), entt::type_index::value()); ASSERT_EQ(info.hash(), entt::type_hash::value()); ASSERT_EQ(info.name(), entt::type_name::value());