Browse Source

group: split group handler functions

Michele Caini 3 years ago
parent
commit
945dc40937
2 changed files with 37 additions and 23 deletions
  1. 22 8
      src/entt/entity/group.hpp
  2. 15 15
      src/entt/entity/registry.hpp

+ 22 - 8
src/entt/entity/group.hpp

@@ -126,16 +126,23 @@ public:
             +[]([[maybe_unused]] const id_type ctype) noexcept { return ((ctype == entt::type_hash<typename Exclude::value_type>::value()) || ...); }},
             +[]([[maybe_unused]] const id_type ctype) noexcept { return ((ctype == entt::type_hash<typename Exclude::value_type>::value()) || ...); }},
           pools{&opool..., &gpool...}, filter{&epool...}, len{} {}
           pools{&opool..., &gpool...}, filter{&epool...}, len{} {}
 
 
-    template<bool Expected>
-    void push_if(const entity_type entt) {
+    void push_on_construct(const entity_type entt) {
         if(std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
         if(std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
-           && std::apply([entt](auto *...cpool) { return (Expected == (0u + ... + cpool->contains(entt))); }, filter)
+           && std::apply([entt](auto *...cpool) { return (!cpool->contains(entt) && ...); }, filter)
            && !(std::get<0>(pools)->index(entt) < len)) {
            && !(std::get<0>(pools)->index(entt) < len)) {
             swap_elements(std::index_sequence_for<Owned...>{}, len++, entt);
             swap_elements(std::index_sequence_for<Owned...>{}, len++, entt);
         }
         }
     }
     }
 
 
-    void remove_if(const entity_type entt) {
+    void push_on_destroy(const entity_type entt) {
+        if(std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
+           && std::apply([entt](auto *...cpool) { return (0u + ... + cpool->contains(entt)) == 1u; }, filter)
+           && !(std::get<0>(pools)->index(entt) < len)) {
+            swap_elements(std::index_sequence_for<Owned...>{}, len++, entt);
+        }
+    }
+
+    void remove(const entity_type entt) {
         if(std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < len)) {
         if(std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < len)) {
             swap_elements(std::index_sequence_for<Owned...>{}, --len, entt);
             swap_elements(std::index_sequence_for<Owned...>{}, --len, entt);
         }
         }
@@ -169,16 +176,23 @@ public:
             +[]([[maybe_unused]] const id_type ctype) noexcept { return ((ctype == entt::type_hash<typename Exclude::value_type>::value()) || ...); }},
             +[]([[maybe_unused]] const id_type ctype) noexcept { return ((ctype == entt::type_hash<typename Exclude::value_type>::value()) || ...); }},
           pools{&gpool...}, filter{&epool...}, elem{alloc} {}
           pools{&gpool...}, filter{&epool...}, elem{alloc} {}
 
 
-    template<bool Expected>
-    void push_if(const entity_type entt) {
+    void push_on_construct(const entity_type entt) {
+        if(std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
+           && std::apply([entt](auto *...cpool) { return (!cpool->contains(entt) && ...); }, filter)
+           && !elem.contains(entt)) {
+            elem.push(entt);
+        }
+    }
+
+    void push_on_destroy(const entity_type entt) {
         if(std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
         if(std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
-           && std::apply([entt](auto *...cpool) { return (Expected == (0u + ... + cpool->contains(entt))); }, filter)
+           && std::apply([entt](auto *...cpool) { return (0u + ... + cpool->contains(entt)) == 1u; }, filter)
            && !elem.contains(entt)) {
            && !elem.contains(entt)) {
             elem.push(entt);
             elem.push(entt);
         }
         }
     }
     }
 
 
-    void remove_if(const entity_type entt) {
+    void remove(const entity_type entt) {
         elem.remove(entt);
         elem.remove(entt);
     }
     }
 
 

+ 15 - 15
src/entt/entity/registry.hpp

@@ -1240,21 +1240,21 @@ public:
                 }
                 }
             }
             }
 
 
-            on_construct<std::remove_const_t<Owned>>().before(next).template connect<&handler_type::template push_if<0u>>(*handler);
-            (on_construct<std::remove_const_t<Other>>().before(next).template connect<&handler_type::template push_if<0u>>(*handler), ...);
-            (on_construct<std::remove_const_t<Get>>().before(next).template connect<&handler_type::template push_if<0u>>(*handler), ...);
-            (on_destroy<std::remove_const_t<Exclude>>().before(next).template connect<&handler_type::template push_if<1u>>(*handler), ...);
+            on_construct<std::remove_const_t<Owned>>().before(next).template connect<&handler_type::push_on_construct>(*handler);
+            (on_construct<std::remove_const_t<Other>>().before(next).template connect<&handler_type::push_on_construct>(*handler), ...);
+            (on_construct<std::remove_const_t<Get>>().before(next).template connect<&handler_type::push_on_construct>(*handler), ...);
+            (on_destroy<std::remove_const_t<Exclude>>().before(next).template connect<&handler_type::push_on_destroy>(*handler), ...);
 
 
-            on_destroy<std::remove_const_t<Owned>>().before(prev).template connect<&handler_type::remove_if>(*handler);
-            (on_destroy<std::remove_const_t<Other>>().before(prev).template connect<&handler_type::remove_if>(*handler), ...);
-            (on_destroy<std::remove_const_t<Get>>().before(prev).template connect<&handler_type::remove_if>(*handler), ...);
-            (on_construct<std::remove_const_t<Exclude>>().before(prev).template connect<&handler_type::remove_if>(*handler), ...);
+            on_destroy<std::remove_const_t<Owned>>().before(prev).template connect<&handler_type::remove>(*handler);
+            (on_destroy<std::remove_const_t<Other>>().before(prev).template connect<&handler_type::remove>(*handler), ...);
+            (on_destroy<std::remove_const_t<Get>>().before(prev).template connect<&handler_type::remove>(*handler), ...);
+            (on_construct<std::remove_const_t<Exclude>>().before(prev).template connect<&handler_type::remove>(*handler), ...);
 
 
             auto &cpool = assure<std::remove_const_t<Owned>>();
             auto &cpool = assure<std::remove_const_t<Owned>>();
 
 
             // we cannot iterate backwards because we want to leave behind valid entities in case of owned types
             // we cannot iterate backwards because we want to leave behind valid entities in case of owned types
             for(auto *first = cpool.data(), *last = first + cpool.size(); first != last; ++first) {
             for(auto *first = cpool.data(), *last = first + cpool.size(); first != last; ++first) {
-                handler->template push_if<0u>(*first);
+                handler->push_on_construct(*first);
             }
             }
         }
         }
 
 
@@ -1281,13 +1281,13 @@ public:
             groups.emplace(type_hash<handler_type>::value(), elem);
             groups.emplace(type_hash<handler_type>::value(), elem);
             handler = static_cast<handler_type *>(elem.get());
             handler = static_cast<handler_type *>(elem.get());
 
 
-            on_construct<std::remove_const_t<Get>>().template connect<&handler_type::template push_if<0u>>(*handler);
-            (on_construct<std::remove_const_t<Other>>().template connect<&handler_type::template push_if<0u>>(*handler), ...);
-            (on_destroy<std::remove_const_t<Exclude>>().template connect<&handler_type::template push_if<1u>>(*handler), ...);
+            on_construct<std::remove_const_t<Get>>().template connect<&handler_type::push_on_construct>(*handler);
+            (on_construct<std::remove_const_t<Other>>().template connect<&handler_type::push_on_construct>(*handler), ...);
+            (on_destroy<std::remove_const_t<Exclude>>().template connect<&handler_type::push_on_destroy>(*handler), ...);
 
 
-            on_destroy<std::remove_const_t<Get>>().template connect<&handler_type::remove_if>(*handler);
-            (on_destroy<std::remove_const_t<Other>>().template connect<&handler_type::remove_if>(*handler), ...);
-            (on_construct<std::remove_const_t<Exclude>>().template connect<&handler_type::remove_if>(*handler), ...);
+            on_destroy<std::remove_const_t<Get>>().template connect<&handler_type::remove>(*handler);
+            (on_destroy<std::remove_const_t<Other>>().template connect<&handler_type::remove>(*handler), ...);
+            (on_construct<std::remove_const_t<Exclude>>().template connect<&handler_type::remove>(*handler), ...);
 
 
             for(const auto entity: view<Get, Other...>(exclude<Exclude...>)) {
             for(const auto entity: view<Get, Other...>(exclude<Exclude...>)) {
                 handler->group().push(entity);
                 handler->group().push(entity);