Browse Source

added has for tag + entity to test ownership

Michele Caini 7 years ago
parent
commit
b89f39d78c
2 changed files with 26 additions and 0 deletions
  1. 18 0
      src/entt/entity/registry.hpp
  2. 8 0
      test/entt/entity/registry.cpp

+ 18 - 0
src/entt/entity/registry.hpp

@@ -568,6 +568,24 @@ public:
         return found;
         return found;
     }
     }
 
 
+    /**
+     * @brief Checks if an entity owns the given tag.
+     *
+     * Syntactic sugar for the following snippet:
+     *
+     * @code{.cpp}
+     * has<Tag>() && attachee<Tag>() == entity
+     * @endcode
+     *
+     * @tparam Tag Type of tag for which to perform the check.
+     * @param entity A valid entity identifier.
+     * @return True if the entity owns the tag, false otherwise.
+     */
+    template<typename Tag>
+    bool has(tag_t, entity_type entity) const ENTT_NOEXCEPT {
+        return has<Tag>() && attachee<Tag>() == entity;
+    }
+
     /**
     /**
      * @brief Checks if an entity has all the given components.
      * @brief Checks if an entity has all the given components.
      *
      *

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

@@ -364,6 +364,7 @@ TEST(DefaultRegistry, AttachSetRemoveTags) {
     registry.assign<int>(entt::tag_t{}, entity, 42);
     registry.assign<int>(entt::tag_t{}, entity, 42);
 
 
     ASSERT_TRUE(registry.has<int>());
     ASSERT_TRUE(registry.has<int>());
+    ASSERT_TRUE(registry.has<int>(entt::tag_t{}, entity));
     ASSERT_EQ(registry.get<int>(), 42);
     ASSERT_EQ(registry.get<int>(), 42);
     ASSERT_EQ(cregistry.get<int>(), 42);
     ASSERT_EQ(cregistry.get<int>(), 42);
     ASSERT_EQ(registry.attachee<int>(), entity);
     ASSERT_EQ(registry.attachee<int>(), entity);
@@ -371,6 +372,7 @@ TEST(DefaultRegistry, AttachSetRemoveTags) {
     registry.replace<int>(entt::tag_t{}, 3);
     registry.replace<int>(entt::tag_t{}, 3);
 
 
     ASSERT_TRUE(registry.has<int>());
     ASSERT_TRUE(registry.has<int>());
+    ASSERT_TRUE(registry.has<int>(entt::tag_t{}, entity));
     ASSERT_EQ(registry.get<int>(), 3);
     ASSERT_EQ(registry.get<int>(), 3);
     ASSERT_EQ(cregistry.get<int>(), 3);
     ASSERT_EQ(cregistry.get<int>(), 3);
     ASSERT_EQ(registry.attachee<int>(), entity);
     ASSERT_EQ(registry.attachee<int>(), entity);
@@ -379,6 +381,8 @@ TEST(DefaultRegistry, AttachSetRemoveTags) {
     registry.move<int>(other);
     registry.move<int>(other);
 
 
     ASSERT_TRUE(registry.has<int>());
     ASSERT_TRUE(registry.has<int>());
+    ASSERT_FALSE(registry.has<int>(entt::tag_t{}, entity));
+    ASSERT_TRUE(registry.has<int>(entt::tag_t{}, other));
     ASSERT_EQ(registry.get<int>(), 3);
     ASSERT_EQ(registry.get<int>(), 3);
     ASSERT_EQ(cregistry.get<int>(), 3);
     ASSERT_EQ(cregistry.get<int>(), 3);
     ASSERT_EQ(registry.attachee<int>(), other);
     ASSERT_EQ(registry.attachee<int>(), other);
@@ -386,11 +390,15 @@ TEST(DefaultRegistry, AttachSetRemoveTags) {
     registry.remove<int>();
     registry.remove<int>();
 
 
     ASSERT_FALSE(registry.has<int>());
     ASSERT_FALSE(registry.has<int>());
+    ASSERT_FALSE(registry.has<int>(entt::tag_t{}, entity));
+    ASSERT_FALSE(registry.has<int>(entt::tag_t{}, other));
 
 
     registry.assign<int>(entt::tag_t{}, entity, 42);
     registry.assign<int>(entt::tag_t{}, entity, 42);
     registry.destroy(entity);
     registry.destroy(entity);
 
 
     ASSERT_FALSE(registry.has<int>());
     ASSERT_FALSE(registry.has<int>());
+    ASSERT_FALSE(registry.has<int>(entt::tag_t{}, entity));
+    ASSERT_FALSE(registry.has<int>(entt::tag_t{}, other));
 }
 }
 
 
 TEST(DefaultRegistry, StandardView) {
 TEST(DefaultRegistry, StandardView) {