Просмотр исходного кода

registry: visit/ctx return type info objects and not only the hash values (breaking changes)

Michele Caini 5 лет назад
Родитель
Сommit
81311ddd03
3 измененных файлов с 36 добавлено и 35 удалено
  1. 21 20
      src/entt/entity/registry.hpp
  2. 1 1
      test/entt/entity/handle.cpp
  3. 14 14
      test/entt/entity/registry.cpp

+ 21 - 20
src/entt/entity/registry.hpp

@@ -43,7 +43,7 @@ class basic_registry {
     using traits_type = entt_traits<Entity>;
 
     struct pool_data {
-        id_type type_hash{};
+        type_info info{};
         std::unique_ptr<sparse_set<Entity>> pool{};
         void(* erase)(sparse_set<Entity> &, basic_registry &, const Entity);
     };
@@ -99,7 +99,7 @@ class basic_registry {
     };
 
     struct variable_data {
-        id_type type_hash;
+        type_info info;
         std::unique_ptr<void, void(*)(void *)> value;
     };
 
@@ -112,7 +112,7 @@ class basic_registry {
         }
         
         if(auto &&pdata = pools[index]; !pdata.pool) {
-            pdata.type_hash = type_hash<Component>::value();
+            pdata.info = type_id<Component>();
             pdata.pool.reset(new pool_t<Entity, Component>());
             pdata.erase = +[](sparse_set<Entity> &cpool, basic_registry &owner, const Entity entt) {
                 static_cast<pool_t<Entity, Component> &>(cpool).erase(owner, entt);
@@ -1148,12 +1148,12 @@ public:
         std::vector<const sparse_set<Entity> *> filter(std::distance(from, to));
 
         std::transform(first, last, component.begin(), [this](const auto ctype) {
-            const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.pool && pdata.type_hash == ctype; });
+            const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.pool && pdata.info.hash() == ctype; });
             return it == pools.cend() ? nullptr : it->pool.get();
         });
 
         std::transform(from, to, filter.begin(), [this](const auto ctype) {
-            const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.pool && pdata.type_hash == ctype; });
+            const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.pool && pdata.info.hash() == ctype; });
             return it == pools.cend() ? nullptr : it->pool.get();
         });
 
@@ -1438,17 +1438,17 @@ public:
     }
 
     /**
-     * @brief Visits an entity and returns the types for its components.
+     * @brief Visits an entity and returns the type info for its components.
      *
      * The signature of the function should be equivalent to the following:
      *
      * @code{.cpp}
-     * void(const id_type);
+     * void(const type_info);
      * @endcode
      *
      * Returned identifiers are those of the components owned by the entity.
      *
-     * @sa type_hash
+     * @sa type_info
      *
      * @warning
      * It's not specified whether a component attached to or removed from the
@@ -1462,23 +1462,23 @@ public:
     void visit(entity_type entity, Func func) const {
         for(auto pos = pools.size(); pos; --pos) {
             if(const auto &pdata = pools[pos-1]; pdata.pool && pdata.pool->contains(entity)) {
-                func(pdata.type_hash);
+                func(pdata.info);
             }
         }
     }
 
     /**
-     * @brief Visits a registry and returns the types for its components.
+     * @brief Visits a registry and returns the type info for its components.
      *
      * The signature of the function should be equivalent to the following:
      *
      * @code{.cpp}
-     * void(const id_type);
+     * void(const type_info);
      * @endcode
      *
      * Returned identifiers are those of the components managed by the registry.
      *
-     * @sa type_hash
+     * @sa type_info
      *
      * @warning
      * It's not specified whether a component for which a pool is created during
@@ -1491,7 +1491,7 @@ public:
     void visit(Func func) const {
         for(auto pos = pools.size(); pos; --pos) {
             if(const auto &pdata = pools[pos-1]; pdata.pool) {
-                func(pdata.type_hash);
+                func(pdata.info);
             }
         }
     }
@@ -1510,7 +1510,7 @@ public:
     template<typename Type, typename... Args>
     Type & set(Args &&... args) {
         unset<Type>();
-        vars.push_back(variable_data{type_hash<Type>::value(), { new Type{std::forward<Args>(args)...}, [](void *instance) { delete static_cast<Type *>(instance); } }});
+        vars.push_back(variable_data{type_id<Type>(), { new Type{std::forward<Args>(args)...}, [](void *instance) { delete static_cast<Type *>(instance); } }});
         return *static_cast<Type *>(vars.back().value.get());
     }
 
@@ -1521,7 +1521,7 @@ public:
     template<typename Type>
     void unset() {
         vars.erase(std::remove_if(vars.begin(), vars.end(), [](auto &&var) {
-            return var.type_hash == type_hash<Type>::value();
+            return var.info.hash() == type_hash<Type>::value();
         }), vars.end());
     }
 
@@ -1550,7 +1550,7 @@ public:
      */
     template<typename Type>
     [[nodiscard]] const Type * try_ctx() const {
-        auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&var) { return var.type_hash == type_hash<Type>::value(); });
+        auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&var) { return var.info.hash() == type_hash<Type>::value(); });
         return it == vars.cend() ? nullptr : static_cast<const Type *>(it->value.get());
     }
 
@@ -1586,17 +1586,18 @@ public:
     }
 
     /**
-     * @brief Visits a registry and returns the types for its context variables.
+     * @brief Visits a registry and returns the type info for its context
+     * variables.
      *
      * The signature of the function should be equivalent to the following:
      *
      * @code{.cpp}
-     * void(const id_type);
+     * void(const type_info);
      * @endcode
      *
      * Returned identifiers are those of the context variables currently set.
      *
-     * @sa type_hash
+     * @sa type_info
      *
      * @warning
      * It's not specified whether a context variable created during the visit is
@@ -1608,7 +1609,7 @@ public:
     template<typename Func>
     void ctx(Func func) const {
         for(auto pos = vars.size(); pos; --pos) {
-            func(vars[pos-1].type_hash);
+            func(vars[pos-1].info);
         }
     }
 

+ 1 - 1
test/entt/entity/handle.cpp

@@ -67,7 +67,7 @@ TEST(BasicHandle, Component) {
     ASSERT_TRUE(registry.empty<char>());
     ASSERT_EQ(0u, handle.remove_if_exists<char>());
 
-    handle.visit([](auto id) { ASSERT_EQ(entt::type_hash<int>::value(), id); });
+    handle.visit([](auto info) { ASSERT_EQ(entt::type_hash<int>::value(), info.hash()); });
 
     ASSERT_TRUE((handle.any<int, char>()));
     ASSERT_FALSE((handle.has<int, char>()));

+ 14 - 14
test/entt/entity/registry.cpp

@@ -63,8 +63,8 @@ TEST(Registry, Context) {
 
     auto count = 0;
 
-    registry.ctx([&count](const auto var) {
-        ASSERT_EQ(var, entt::type_hash<char>::value());
+    registry.ctx([&count](auto info) {
+        ASSERT_EQ(info.hash(), entt::type_hash<char>::value());
         ++count;
     });
 
@@ -1435,30 +1435,30 @@ TEST(Registry, Visit) {
 
     bool hasType[3]{};
 
-    registry.visit([&hasType](const auto component) {
-        hasType[0] = hasType[0] || (component == entt::type_hash<int>::value());
-        hasType[1] = hasType[1] || (component == entt::type_hash<double>::value());
-        hasType[2] = hasType[2] || (component == entt::type_hash<char>::value());
+    registry.visit([&hasType](auto info) {
+        hasType[0] = hasType[0] || (info.hash() == entt::type_hash<int>::value());
+        hasType[1] = hasType[1] || (info.hash() == entt::type_hash<double>::value());
+        hasType[2] = hasType[2] || (info.hash() == entt::type_hash<char>::value());
     });
 
     ASSERT_TRUE(hasType[0] && hasType[1] && hasType[2]);
 
     hasType[0] = hasType[1] = hasType[2] = false;
 
-    registry.visit(entity, [&hasType](const auto component) {
-        hasType[0] = hasType[0] || (component == entt::type_hash<int>::value());
-        hasType[1] = hasType[1] || (component == entt::type_hash<double>::value());
-        hasType[2] = hasType[2] || (component == entt::type_hash<char>::value());
+    registry.visit(entity, [&hasType](auto info) {
+        hasType[0] = hasType[0] || (info.hash() == entt::type_hash<int>::value());
+        hasType[1] = hasType[1] || (info.hash() == entt::type_hash<double>::value());
+        hasType[2] = hasType[2] || (info.hash() == entt::type_hash<char>::value());
     });
 
     ASSERT_TRUE(hasType[0] && !hasType[1] && hasType[2]);
 
     hasType[0] = hasType[2] = false;
 
-    registry.visit(other, [&hasType](const auto component) {
-        hasType[0] = hasType[0] || (component == entt::type_hash<int>::value());
-        hasType[1] = hasType[1] || (component == entt::type_hash<double>::value());
-        hasType[2] = hasType[2] || (component == entt::type_hash<char>::value());
+    registry.visit(other, [&hasType](auto info) {
+        hasType[0] = hasType[0] || (info.hash() == entt::type_hash<int>::value());
+        hasType[1] = hasType[1] || (info.hash() == entt::type_hash<double>::value());
+        hasType[2] = hasType[2] || (info.hash() == entt::type_hash<char>::value());
     });
 
     ASSERT_TRUE(!hasType[0] && hasType[1] && !hasType[2]);