1
0
Эх сурвалжийг харах

registry: removed ::orphans, use ::each/::orphan instead (no way to further optimize it)

Michele Caini 4 жил өмнө
parent
commit
12d177a6c8

+ 12 - 17
docs/md/entity.md

@@ -2038,30 +2038,25 @@ registry.each([](auto entity) {
 });
 ```
 
-It returns to the caller all the entities that are still in use.<br/>
 As a rule of thumb, consider using a view or a group if the goal is to iterate
 entities that have a determinate set of components. These tools are usually much
-faster than combining this function with a bunch of custom tests.<br/>
-In all the other cases, this is the way to go.
-
-There exists also another member function to use to retrieve orphans. An orphan
-is an entity that is still in use and has no assigned components.<br/>
-The signature of the function is the same of `each`:
+faster than combining the `each` function with a bunch of custom tests.<br/>
+In all the other cases, this is the way to go. For example, it's possible to
+combine `each` with the `orphan` member function to clean up orphan entities
+(that is, entities that are still in use and have no assigned components):
 
 ```cpp
-registry.orphans([](auto entity) {
-    // ...
+registry.each([&registry](auto entity) {
+    if(registry.orphan(entity)) {
+        registry.release(entity);
+    }
 });
 ```
 
-To test the _orphanity_ of a single entity, use the member function `orphan`
-instead. It accepts a valid entity identifer as an argument and returns true in
-case the entity is an orphan, false otherwise.
-
-In general, all these functions can result in poor performance.<br/>
-`each` is fairly slow because of some checks it performs on each and every
-entity. For similar reasons, `orphans` can be even slower. Both functions should
-not be used frequently to avoid the risk of a performance hit.
+In general, iterating all entities can result in poor performance. It should not
+be done frequently to avoid the risk of a performance hit.<br/>
+However, it can be convenient when initializing an editor or to reclaim pending
+identifiers.
 
 ## What is allowed and what is not
 

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

@@ -900,29 +900,6 @@ public:
         return std::none_of(pools.cbegin(), pools.cend(), [entity](auto &&curr) { return curr.second->contains(entity); });
     }
 
-    /**
-     * @brief Iterates orphans and applies them the given function object.
-     *
-     * The signature of the function should be equivalent to the following:
-     *
-     * @code{.cpp}
-     * void(const Entity);
-     * @endcode
-     *
-     * This function can be very slow and should not be used frequently.
-     *
-     * @tparam Func Type of the function object to invoke.
-     * @param func A valid function object.
-     */
-    template<typename Func>
-    void orphans(Func func) const {
-        each([this, &func](const auto entity) {
-            if(orphan(entity)) {
-                func(entity);
-            }
-        });
-    }
-
     /**
      * @brief Returns a sink object for the given component.
      *

+ 8 - 4
src/entt/entity/snapshot.hpp

@@ -263,8 +263,10 @@ public:
      * @return A valid loader to continue restoring data.
      */
     const basic_snapshot_loader &orphans() const {
-        reg->orphans([this](const auto entt) {
-            reg->release(entt);
+        reg->each([this](const auto entt) {
+            if(reg->orphan(entt)) {
+                reg->release(entt);
+            }
         });
 
         return *this;
@@ -516,8 +518,10 @@ public:
      * @return A non-const reference to this loader.
      */
     basic_continuous_loader &orphans() {
-        reg->orphans([this](const auto entt) {
-            reg->release(entt);
+        reg->each([this](const auto entt) {
+            if(reg->orphan(entt)) {
+                reg->release(entt);
+            }
         });
 
         return *this;

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

@@ -781,30 +781,22 @@ TEST(Registry, Each) {
 
 TEST(Registry, Orphans) {
     entt::registry registry;
-    entt::registry::size_type tot{};
     entt::entity entities[3u]{};
 
     registry.create(std::begin(entities), std::end(entities));
     registry.emplace<int>(entities[0u]);
     registry.emplace<int>(entities[2u]);
 
-    registry.orphans([&](auto) { ++tot; });
-
-    ASSERT_EQ(tot, 1u);
+    registry.each([&](const auto entt) {
+        ASSERT_TRUE(entt != entities[1u] || registry.orphan(entt));
+    });
 
     registry.erase<int>(entities[0u]);
     registry.erase<int>(entities[2u]);
 
-    tot = {};
-    registry.orphans([&](auto) { ++tot; });
-
-    ASSERT_EQ(tot, 3u);
-
-    registry.clear();
-    tot = {};
-
-    registry.orphans([&](auto) { ++tot; });
-    ASSERT_EQ(tot, 0u);
+    registry.each([&](const auto entt) {
+        ASSERT_TRUE(registry.orphan(entt));
+    });
 }
 
 TEST(Registry, View) {