فهرست منبع

type_info: temporarily reintroduce type_info::index for performance reasons

Michele Caini 4 سال پیش
والد
کامیت
7ab08779dc
4فایلهای تغییر یافته به همراه33 افزوده شده و 11 حذف شده
  1. 14 2
      docs/md/core.md
  2. 13 2
      src/entt/core/type_info.hpp
  3. 4 6
      src/entt/entity/registry.hpp
  4. 2 1
      test/entt/core/type_info.cpp

+ 14 - 2
docs/md/core.md

@@ -491,6 +491,18 @@ auto info = entt::type_id<a_type>();
 
 These are the information made available by this object:
 
+* The index associated with a given type:
+
+  ```cpp
+  auto idx = entt::type_id<a_type>().index();
+  ```
+
+  This is also an alias for the following:
+
+  ```cpp
+  auto idx = entt::type_index<std::remove_const_t<std::remove_reference_t<a_type>>>::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<a_type>::value();
+  auto hash = entt::type_hash<std::remove_const_t<std::remove_reference_t<a_type>>>::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<a_type>::value();
+  auto name = entt::type_name<std::remove_const_t<std::remove_reference_t<a_type>>>::value();
   ```
 
 Where all accessed features are available at compile-time, the `type_info` class

+ 13 - 2
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<typename Type>
     constexpr type_info(std::in_place_type_t<Type>) ENTT_NOEXCEPT
-        : identifier{type_hash<std::remove_reference_t<std::remove_const_t<Type>>>::value()},
+        : 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()}
     {}
 
@@ -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;
 };

+ 4 - 6
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;
     }
 
     /**

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

@@ -54,7 +54,7 @@ TEST(TypeInfo, Functionalities) {
     ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int &&>());
     ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int>());
 
-    auto info = entt::type_id<int>();
+    auto info = entt::type_id<const int &>();
     const auto unnamed = entt::type_id<float>();
     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<int>::value());
     ASSERT_EQ(info.hash(), entt::type_hash<int>::value());
     ASSERT_EQ(info.name(), entt::type_name<int>::value());