added has for tag + entity to test ownership

This commit is contained in:
Michele Caini
2018-05-02 17:07:20 +02:00
parent 7ba479c9c9
commit b89f39d78c
2 changed files with 26 additions and 0 deletions

View File

@@ -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<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.
*

View File

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