Browse Source

group: wrap the len of owning groups to avoid changing it by mistake

Michele Caini 3 years ago
parent
commit
88dac318e5
1 changed files with 16 additions and 13 deletions
  1. 16 13
      src/entt/entity/group.hpp

+ 16 - 13
src/entt/entity/group.hpp

@@ -117,22 +117,25 @@ public:
     void push_if(const entity_type entt) {
         if(std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
            && std::apply([entt](auto *...cpool) { return (Expected == (0u + ... + cpool->contains(entt))); }, filter)
-           && !(std::get<0>(pools)->index(entt) < current)) {
-            swap_elements(std::index_sequence_for<Owned...>{}, current++, entt);
+           && !(std::get<0>(pools)->index(entt) < len)) {
+            swap_elements(std::index_sequence_for<Owned...>{}, len++, entt);
         }
     }
 
     void remove_if(const entity_type entt) {
-        if(std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < current)) {
-            swap_elements(std::index_sequence_for<Owned...>{}, --current, entt);
+        if(std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < len)) {
+            swap_elements(std::index_sequence_for<Owned...>{}, --len, entt);
         }
     }
 
-    std::size_t current{};
+    [[nodiscard]] std::size_t size() const noexcept {
+        return len;
+    }
 
 private:
     std::tuple<Owned *..., Get *...> pools;
     std::tuple<Exclude *...> filter;
+    std::size_t len{};
 };
 
 template<typename... Get, typename... Exclude>
@@ -693,7 +696,7 @@ public:
      * @return Number of entities that that are part of the group.
      */
     [[nodiscard]] size_type size() const noexcept {
-        return *this ? descriptor->current : size_type{};
+        return *this ? descriptor->size() : size_type{};
     }
 
     /**
@@ -701,7 +704,7 @@ public:
      * @return True if the group is empty, false otherwise.
      */
     [[nodiscard]] bool empty() const noexcept {
-        return !*this || !descriptor->current;
+        return !*this || !descriptor->size();
     }
 
     /**
@@ -713,7 +716,7 @@ public:
      * @return An iterator to the first entity of the group.
      */
     [[nodiscard]] iterator begin() const noexcept {
-        return *this ? (handle().base_type::end() - descriptor->current) : iterator{};
+        return *this ? (handle().base_type::end() - descriptor->size()) : iterator{};
     }
 
     /**
@@ -754,7 +757,7 @@ public:
      * reversed group.
      */
     [[nodiscard]] reverse_iterator rend() const noexcept {
-        return *this ? (handle().base_type::rbegin() + descriptor->current) : reverse_iterator{};
+        return *this ? (handle().base_type::rbegin() + descriptor->size()) : reverse_iterator{};
     }
 
     /**
@@ -811,7 +814,7 @@ public:
      * @return True if the group contains the given entity, false otherwise.
      */
     [[nodiscard]] bool contains(const entity_type entt) const noexcept {
-        return *this && handle().contains(entt) && (handle().index(entt) < (descriptor->current));
+        return *this && handle().contains(entt) && (handle().index(entt) < (descriptor->size()));
     }
 
     /**
@@ -931,7 +934,7 @@ public:
     void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const {
         if constexpr(sizeof...(Type) == 0) {
             static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
-            std::get<0>(pools)->sort_n(descriptor->current, std::move(compare), std::move(algo), std::forward<Args>(args)...);
+            std::get<0>(pools)->sort_n(descriptor->size(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
         } else {
             auto comp = [this, &compare](const entity_type lhs, const entity_type rhs) {
                 if constexpr(sizeof...(Type) == 1) {
@@ -941,11 +944,11 @@ public:
                 }
             };
 
-            std::get<0>(pools)->sort_n(descriptor->current, std::move(comp), std::move(algo), std::forward<Args>(args)...);
+            std::get<0>(pools)->sort_n(descriptor->size(), std::move(comp), std::move(algo), std::forward<Args>(args)...);
         }
 
         auto cb = [this](auto *head, auto *...other) {
-            for(auto next = descriptor->current; next; --next) {
+            for(auto next = descriptor->size(); next; --next) {
                 const auto pos = next - 1;
                 [[maybe_unused]] const auto entt = head->data()[pos];
                 (other->swap_elements(other->data()[pos], entt), ...);