Explorar o código

updated docs + added reset<Component> to the registry

Michele Caini %!s(int64=9) %!d(string=hai) anos
pai
achega
67e41e79f6
Modificáronse 5 ficheiros con 63 adicións e 22 borrados
  1. 13 10
      README.md
  2. 5 0
      src/component_pool.hpp
  3. 17 12
      src/registry.hpp
  4. 13 0
      test/component_pool.cpp
  5. 15 0
      test/registry.cpp

+ 13 - 10
README.md

@@ -157,9 +157,10 @@ Once you have created a registry, the followings are the exposed member function
 
 * `size`: returns the number of entities still alive.
 * `capacity`: returns the maximum number of entities created till now.
+* `empty<Component>`: returns `true` if at least an instance of `Component` exists, `false` otherwise.
 * `empty`: returns `true` if all the entities have been destroyed, `false` otherwise.
-* `create`: creates a new entity and returns it, no components assigned.
 * `create<Components...>`: creates a new entity and assigns it the given components, then returns the entity.
+* `create`: creates a new entity and returns it, no components assigned.
 * `destroy`: destroys the entity and all its components.
 * `assign<Component>(entity, args...)`: assigns the given component to the entity and uses `args...` to initialize it.
 * `remove<Component>(entity)`: removes the given component from the entity.
@@ -169,6 +170,7 @@ Once you have created a registry, the followings are the exposed member function
 * `clone(entity)`: clones an entity and all its components, then returns the new entity identifier.
 * `copy<Component>(from, to)`: copies a component from an entity to another one (both the entities must already have been assigned the component, undefined behaviour otherwise).
 * `copy(from, to)`: copies all the components and their contents from an entity to another one (comoonents are created or destroyed if needed).
+* `reset<Component>()`: destroys all the instances of `Component`.
 * `reset()`: resets the pool and destroys all the entities and their components.
 * `view<Components...>()`: gets a view of the entities that have the given components (see below for further details).
 
@@ -267,15 +269,16 @@ Even thoug the underlying pool doesn't store the components separately, the regi
 specific actions (like `destroy` or `copy`). That's why they must be explicitly specified.<br/>
 A generic pool should expose at least the following memeber functions:
 
-* `template<typename Comp> bool empty() const noexcept;`
-* `template<typename Comp> size_type capacity() const noexcept;`
-* `template<typename Comp> size_type size() const noexcept;`
-* `template<typename Comp> const entity_type * entities() const noexcept;`
-* `template<typename Comp> bool has(entity_type entity) const noexcept;`
-* `template<typename Comp> const Comp & get(entity_type entity) const noexcept;`
-* `template<typename Comp> Comp & get(entity_type entity) noexcept;`
-* `template<typename Comp, typename... Args> Comp & construct(entity_type entity, Args&&... args);`
-* `template<typename Comp> void destroy(entity_type entity);`
+* `template<typename Component> bool empty() const noexcept;`
+* `template<typename Component> size_type capacity() const noexcept;`
+* `template<typename Component> size_type size() const noexcept;`
+* `template<typename Component> const entity_type * entities() const noexcept;`
+* `template<typename Component> bool has(entity_type entity) const noexcept;`
+* `template<typename Component> const Comp & get(entity_type entity) const noexcept;`
+* `template<typename Component> Comp & get(entity_type entity) noexcept;`
+* `template<typename Component, typename... Args> Comp & construct(entity_type entity, Args&&... args);`
+* `template<typename Component> void destroy(entity_type entity);`
+* `template<typename Component> void reset();`
 * `void reset();`
 
 Good luck. If you come out with a more performant components pool, do not forget to make a PR so that I can add it to

+ 5 - 0
src/component_pool.hpp

@@ -176,6 +176,11 @@ struct  ComponentPool final {
         std::get<ComponentPool<Comp>>(pools).destroy(entity);
     }
 
+    template<typename Comp>
+    void reset() {
+        std::get<ComponentPool<Comp>>(pools).reset();
+    }
+
     void reset() {
         using accumulator_type = int[];
         std::get<ComponentPool<Component>>(pools).reset();

+ 17 - 12
src/registry.hpp

@@ -252,13 +252,22 @@ public:
         return count;
     }
 
+    template<typename Comp>
     bool empty() const noexcept {
-        return available.size() == count;
+        return pool.template empty<Comp>();
     }
 
-    template<typename Comp>
     bool empty() const noexcept {
-        return pool.template empty<Comp>();
+        return available.size() == count;
+    }
+
+    template<typename... Comp>
+    entity_type create() noexcept {
+        using accumulator_type = int[];
+        auto entity = create();
+        accumulator_type accumulator = { 0, (assign<Comp>(entity), 0)... };
+        (void)accumulator;
+        return entity;
     }
 
     entity_type create() noexcept {
@@ -274,15 +283,6 @@ public:
         return entity;
     }
 
-    template<typename... Comp>
-    entity_type create() noexcept {
-        using accumulator_type = int[];
-        auto entity = create();
-        accumulator_type accumulator = { 0, (assign<Comp>(entity), 0)... };
-        (void)accumulator;
-        return entity;
-    }
-
     void destroy(entity_type entity) {
         using accumulator_type = int[];
         accumulator_type accumulator = { 0, (destroy<Components>(entity), 0)... };
@@ -339,6 +339,11 @@ public:
         (void)accumulator;
     }
 
+    template<typename Comp>
+    void reset() {
+        pool.reset<Comp>();
+    }
+
     void reset() {
         available.clear();
         count = 0;

+ 13 - 0
test/component_pool.cpp

@@ -148,5 +148,18 @@ TEST(ComponentPool, EntitiesReset) {
     ASSERT_EQ(pool.entities<int>()[1], typename pool_type::entity_type{1});
     ASSERT_EQ(pool.entities<int>()[2], typename pool_type::entity_type{3});
 
+    ASSERT_EQ(pool.construct<char>(0, 'c'), 'c');
+
+    ASSERT_FALSE(pool.empty<int>());
+    ASSERT_FALSE(pool.empty<char>());
+
+    ASSERT_NO_THROW(pool.reset<char>());
+
+    ASSERT_FALSE(pool.empty<int>());
+    ASSERT_TRUE(pool.empty<char>());
+
     ASSERT_NO_THROW(pool.reset());
+
+    ASSERT_TRUE(pool.empty<int>());
+    ASSERT_TRUE(pool.empty<char>());
 }

+ 15 - 0
test/registry.cpp

@@ -78,6 +78,21 @@ TEST(DefaultRegistry, Functionalities) {
     ASSERT_EQ(registry.size(), registry_type::size_type{0});
     ASSERT_EQ(registry.capacity(), registry_type::size_type{0});
     ASSERT_TRUE(registry.empty());
+
+    registry.create<int, char>();
+
+    ASSERT_FALSE(registry.empty<int>());
+    ASSERT_FALSE(registry.empty<char>());
+
+    ASSERT_NO_THROW(registry.reset<int>());
+
+    ASSERT_TRUE(registry.empty<int>());
+    ASSERT_FALSE(registry.empty<char>());
+
+    ASSERT_NO_THROW(registry.reset());
+
+    ASSERT_TRUE(registry.empty<int>());
+    ASSERT_TRUE(registry.empty<char>());
 }
 
 TEST(DefaultRegistry, ViewSingleComponent) {