Ver código fonte

registry::has accepts multiple components

Michele Caini 9 anos atrás
pai
commit
b4d99cf037
3 arquivos alterados com 9 adições e 3 exclusões
  1. 1 1
      README.md
  2. 6 2
      src/registry.hpp
  3. 2 0
      test/registry.cpp

+ 1 - 1
README.md

@@ -172,7 +172,7 @@ Once you have created a registry, the followings are the exposed member function
 * `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.
-* `has<Component>(entity)`: returns `true` if the entity has the given component, `false` otherwise.
+* `has<Components...>(entity)`: returns `true` if the entity has the given components, `false` otherwise.
 * `get<Component>(entity)`: returns a reference to the given component for the entity (undefined behaviour if the entity has not the component).
 * `replace<Component>(entity, args...)`: replaces the given component for the entity, using `args...` to create the new component.
 * `clone(entity)`: clones an entity and all its components, then returns the new entity identifier.

+ 6 - 2
src/registry.hpp

@@ -301,9 +301,13 @@ public:
         pool.template destroy<Comp>(entity);
     }
 
-    template<typename Comp>
+    template<typename... Comp>
     bool has(entity_type entity) const noexcept {
-        return pool.template has<Comp>(entity);
+        using accumulator_type = bool[];
+        bool all = true;
+        accumulator_type accumulator = { true, (all = all && pool.template has<Comp>(entity))... };
+        (void)accumulator;
+        return all;
     }
 
     template<typename Comp>

+ 2 - 0
test/registry.cpp

@@ -25,6 +25,7 @@ TEST(DefaultRegistry, Functionalities) {
     ASSERT_TRUE(registry.has<int>(e2));
     ASSERT_FALSE(registry.has<char>(e1));
     ASSERT_TRUE(registry.has<char>(e2));
+    ASSERT_TRUE((registry.has<int, char>(e2)));
 
     ASSERT_EQ(registry.assign<int>(e1, 42), 42);
     ASSERT_EQ(registry.assign<char>(e1, 'c'), 'c');
@@ -35,6 +36,7 @@ TEST(DefaultRegistry, Functionalities) {
     ASSERT_FALSE(registry.has<int>(e2));
     ASSERT_TRUE(registry.has<char>(e1));
     ASSERT_FALSE(registry.has<char>(e2));
+    ASSERT_TRUE((registry.has<int, char>(e1)));
 
     registry_type::entity_type e3 = registry.clone(e1);