diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index aea0f4886..dbba30932 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -568,6 +568,24 @@ public: return found; } + /** + * @brief Checks if an entity owns the given tag. + * + * Syntactic sugar for the following snippet: + * + * @code{.cpp} + * has() && attachee() == 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 + bool has(tag_t, entity_type entity) const ENTT_NOEXCEPT { + return has() && attachee() == entity; + } + /** * @brief Checks if an entity has all the given components. * diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 6621123f8..14193d341 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -364,6 +364,7 @@ TEST(DefaultRegistry, AttachSetRemoveTags) { registry.assign(entt::tag_t{}, entity, 42); ASSERT_TRUE(registry.has()); + ASSERT_TRUE(registry.has(entt::tag_t{}, entity)); ASSERT_EQ(registry.get(), 42); ASSERT_EQ(cregistry.get(), 42); ASSERT_EQ(registry.attachee(), entity); @@ -371,6 +372,7 @@ TEST(DefaultRegistry, AttachSetRemoveTags) { registry.replace(entt::tag_t{}, 3); ASSERT_TRUE(registry.has()); + ASSERT_TRUE(registry.has(entt::tag_t{}, entity)); ASSERT_EQ(registry.get(), 3); ASSERT_EQ(cregistry.get(), 3); ASSERT_EQ(registry.attachee(), entity); @@ -379,6 +381,8 @@ TEST(DefaultRegistry, AttachSetRemoveTags) { registry.move(other); ASSERT_TRUE(registry.has()); + ASSERT_FALSE(registry.has(entt::tag_t{}, entity)); + ASSERT_TRUE(registry.has(entt::tag_t{}, other)); ASSERT_EQ(registry.get(), 3); ASSERT_EQ(cregistry.get(), 3); ASSERT_EQ(registry.attachee(), other); @@ -386,11 +390,15 @@ TEST(DefaultRegistry, AttachSetRemoveTags) { registry.remove(); ASSERT_FALSE(registry.has()); + ASSERT_FALSE(registry.has(entt::tag_t{}, entity)); + ASSERT_FALSE(registry.has(entt::tag_t{}, other)); registry.assign(entt::tag_t{}, entity, 42); registry.destroy(entity); ASSERT_FALSE(registry.has()); + ASSERT_FALSE(registry.has(entt::tag_t{}, entity)); + ASSERT_FALSE(registry.has(entt::tag_t{}, other)); } TEST(DefaultRegistry, StandardView) {