Procházet zdrojové kódy

registry: remove the visit overload that also takes an entity

Michele Caini před 4 roky
rodič
revize
69ddf4936e

+ 5 - 3
docs/md/entity.md

@@ -1207,9 +1207,11 @@ components with entities without knowing their actual type and even initialize
 them by copy if needed:
 
 ```cpp
-registry.visit(entity, [other](auto &&storage) {
-    // create a copy of an entity component by component
-    storage.emplace(other, storage.get(entity));
+// create a copy of an entity component by component
+registry.visit([entity, other](auto &&storage) {
+    if(storage.contains(entity)) {
+        storage.emplace(other, storage.get(entity));
+    }
 });
 ```
 

+ 5 - 1
src/entt/entity/handle.hpp

@@ -272,7 +272,11 @@ struct basic_handle {
      */
     template<typename Func>
     void visit(Func &&func) const {
-        reg->visit(entt, std::forward<Func>(func));
+        reg->visit([func = std::forward<Func>(func), this](auto &&storage) {
+            if(storage.contains(entt)) {
+                func(storage);
+            }
+        });
     }
 
 private:

+ 0 - 24
src/entt/entity/registry.hpp

@@ -1295,30 +1295,6 @@ public:
         assure<To>().respect(assure<From>());
     }
 
-    /**
-     * @brief Visits an entity and returns the pools for its components.
-     *
-     * The signature of the function should be equivalent to the following:
-     *
-     * @code{.cpp}
-     * void(const basic_sparse_set<entity_type> &);
-     * @endcode
-     *
-     * Returned pools are those of the components owned by the entity.
-     *
-     * @tparam Func Type of the function object to invoke.
-     * @param entity A valid identifier.
-     * @param func A valid function object.
-     */
-    template<typename Func>
-    void visit(entity_type entity, Func func) const {
-        for(auto &&curr: pools) {
-            if(curr.second->contains(entity)) {
-                func(*curr.second);
-            }
-        }
-    }
-
     /**
      * @brief Visits a registry and returns the pools for its components.
      *

+ 0 - 22
test/entt/entity/registry.cpp

@@ -1872,28 +1872,6 @@ TEST(Registry, Visit) {
     });
 
     ASSERT_TRUE(hasType[0] && hasType[1] && hasType[2]);
-
-    hasType[0] = hasType[1] = hasType[2] = false;
-
-    registry.visit(entity, [&hasType](const auto &pool) {
-        hasType[0] = hasType[0] || (pool.type() == entt::type_id<int>());
-        hasType[1] = hasType[1] || (pool.type() == entt::type_id<double>());
-        hasType[2] = hasType[2] || (pool.type() == entt::type_id<char>());
-    });
-
-    ASSERT_TRUE(hasType[0] && !hasType[1] && hasType[2]);
-
-    hasType[0] = hasType[2] = false;
-
-    registry.visit(other, [&hasType](const auto &pool) {
-        hasType[0] = hasType[0] || (pool.type() == entt::type_id<int>());
-        hasType[1] = hasType[1] || (pool.type() == entt::type_id<double>());
-        hasType[2] = hasType[2] || (pool.type() == entt::type_id<char>());
-    });
-
-    ASSERT_TRUE(!hasType[0] && hasType[1] && !hasType[2]);
-
-    hasType[1] = false;
 }
 
 TEST(Registry, ScramblingPoolsIsAllowed) {

+ 2 - 2
test/example/entity_copy.cpp

@@ -22,9 +22,9 @@ TEST(Example, EntityCopy) {
     ASSERT_FALSE((registry.any_of<int, char, double>(dst)));
     ASSERT_FALSE(custom.contains(dst));
 
-    registry.visit(src, [src, dst](auto &&storage) {
+    registry.visit([src, dst](auto &&storage) {
         // discard chars because why not, this is just an example after all
-        if(storage.type() != entt::type_id<char>()) {
+        if(storage.type() != entt::type_id<char>() && storage.contains(src)) {
             storage.emplace(dst, storage.get(src));
         }
     });