Browse Source

registry: added range remove

Michele Caini 6 years ago
parent
commit
6dbbb265d1
3 changed files with 51 additions and 7 deletions
  1. 1 2
      TODO
  2. 22 5
      src/entt/entity/registry.hpp
  3. 28 0
      test/entt/entity/registry.cpp

+ 1 - 2
TODO

@@ -31,7 +31,6 @@
   - entity for each component
   - opaque get
   - and so on (I'm lazy) :)
+* registry::each to iterate all components of an entity
 * named types: almost-stable index optimization for direct access to pools, no more linear searches
   - can implicitly generate types for meta benefit from a similar approach?
-  * registry::each to iterate all components of an entity
-* multi component registry::remove and some others?

+ 22 - 5
src/entt/entity/registry.hpp

@@ -1,4 +1,4 @@
-#ifndef ENTT_ENTITY_REGISTRY_HPP
+#ifndef ENTT_ENTITY_REGISTRY_HPP
 #define ENTT_ENTITY_REGISTRY_HPP
 
 
@@ -640,8 +640,8 @@ public:
      * @sa destroy
      *
      * @tparam It Type of input iterator.
-     * @param first An iterator to the first element of the range to destroy.
-     * @param last An iterator past the last element of the range to destroy.
+     * @param first An iterator to the first element of the range of entities.
+     * @param last An iterator past the last element of the range of entities.
      */
     template<typename It>
     void destroy(It first, It last) {
@@ -683,8 +683,8 @@ public:
      * @tparam Component Type of component to create.
      * @tparam It Type of input iterator.
      * @tparam Args Types of arguments to use to construct the component.
-     * @param first An iterator to the first element of the range to assign.
-     * @param last An iterator past the last element of the range to assign.
+     * @param first An iterator to the first element of the range of entities.
+     * @param last An iterator past the last element of the range of entities.
      * @param args Parameters to use to initialize the component.
      * @return An iterator to the list of components just created.
      */
@@ -714,6 +714,23 @@ public:
         (assure<Component>()->remove(*this, entity), ...);
     }
 
+    /**
+     * @brief Removes the given components from all the entities in a range.
+     *
+     * @see remove
+     *
+     * @tparam Component Types of components to remove.
+     * @tparam It Type of input iterator.
+     * @param first An iterator to the first element of the range of entities.
+     * @param last An iterator past the last element of the range of entities.
+     */
+    template<typename... Component, typename It>
+    void remove(It first, It last) {
+        ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }));
+        // useless this-> used to suppress a warning with clang
+        std::for_each(first, last, [this](const auto entity) { this->remove<Component...>(entity); });
+    }
+
     /**
      * @brief Checks if an entity has all the given components.
      *

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

@@ -1099,6 +1099,34 @@ TEST(Registry, RangeAssign) {
     ASSERT_EQ(registry.get<float>(*(++view.begin())), 1.f);
 }
 
+TEST(Registry, RangeRemove) {
+    entt::registry registry;
+
+    const auto e0 = registry.create();
+    const auto e1 = registry.create();
+    const auto e2 = registry.create();
+
+    registry.assign<int>(e0);
+    registry.assign<char>(e0);
+    registry.assign<double>(e0);
+
+    registry.assign<int>(e1);
+    registry.assign<char>(e1);
+
+    registry.assign<int>(e2);
+
+    ASSERT_TRUE(registry.has<int>(e0));
+    ASSERT_TRUE(registry.has<int>(e1));
+    ASSERT_TRUE(registry.has<int>(e2));
+
+    const auto view = registry.view<int, char>();
+    registry.remove<int>(view.begin(), view.end());
+
+    ASSERT_FALSE(registry.has<int>(e0));
+    ASSERT_FALSE(registry.has<int>(e1));
+    ASSERT_TRUE(registry.has<int>(e2));
+}
+
 TEST(Registry, CreateManyEntitiesAtOnce) {
     entt::registry registry;
     entt::entity entities[3];