From def82b534b1e0ed0d0395b034c8ec96c6d2b45e6 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 20 Mar 2023 08:11:42 +0100 Subject: [PATCH] group: index based get --- src/entt/entity/group.hpp | 58 ++++++++++++++++++++++++++++-------- test/entt/entity/group.cpp | 60 ++++++++++++++++++++++++++------------ 2 files changed, 87 insertions(+), 31 deletions(-) diff --git a/src/entt/entity/group.hpp b/src/entt/entity/group.hpp index 99ef9e5f7..8404fae1c 100644 --- a/src/entt/entity/group.hpp +++ b/src/entt/entity/group.hpp @@ -494,18 +494,35 @@ public: * Attempting to use an entity that doesn't belong to the group results in * undefined behavior. * - * @tparam Type Types of components to get. + * @tparam Type Type of the component to get. + * @tparam Other Other types of components to get. * @param entt A valid identifier. * @return The components assigned to the entity. */ - template + template [[nodiscard]] decltype(auto) get(const entity_type entt) const { - if constexpr(sizeof...(Type) == 0) { + return get, index_of...>(entt); + } + + /** + * @brief Returns the components assigned to the given entity. + * + * @warning + * Attempting to use an entity that doesn't belong to the groups results in + * undefined behavior. + * + * @tparam Index Indexes of the components to get. + * @param entt A valid identifier. + * @return The components assigned to the entity. + */ + template + [[nodiscard]] decltype(auto) get(const entity_type entt) const { + if constexpr(sizeof...(Index) == 0) { return std::apply([entt](auto *...curr) { return std::tuple_cat(curr->get_as_tuple(entt)...); }, pools()); - } else if constexpr(sizeof...(Type) == 1) { - return (storage>().get(entt), ...); + } else if constexpr(sizeof...(Index) == 1) { + return (storage().get(entt), ...); } else { - return std::tuple_cat(storage>().get_as_tuple(entt)...); + return std::tuple_cat(storage().get_as_tuple(entt)...); } } @@ -883,18 +900,35 @@ public: * Attempting to use an entity that doesn't belong to the group results in * undefined behavior. * - * @tparam Type Types of components to get. + * @tparam Type Type of the component to get. + * @tparam Other Other types of components to get. * @param entt A valid identifier. * @return The components assigned to the entity. */ - template + template [[nodiscard]] decltype(auto) get(const entity_type entt) const { - if constexpr(sizeof...(Type) == 0) { + return get, index_of...>(entt); + } + + /** + * @brief Returns the components assigned to the given entity. + * + * @warning + * Attempting to use an entity that doesn't belong to the groups results in + * undefined behavior. + * + * @tparam Index Indexes of the components to get. + * @param entt A valid identifier. + * @return The components assigned to the entity. + */ + template + [[nodiscard]] decltype(auto) get(const entity_type entt) const { + if constexpr(sizeof...(Index) == 0) { return std::apply([entt](auto *...curr) { return std::tuple_cat(curr->get_as_tuple(entt)...); }, pools()); - } else if constexpr(sizeof...(Type) == 1) { - return (storage>().get(entt), ...); + } else if constexpr(sizeof...(Index) == 1) { + return (storage().get(entt), ...); } else { - return std::tuple_cat(storage>().get_as_tuple(entt)...); + return std::tuple_cat(storage().get_as_tuple(entt)...); } } diff --git a/test/entt/entity/group.cpp b/test/entt/entity/group.cpp index de3bec7b2..beec1bb82 100644 --- a/test/entt/entity/group.cpp +++ b/test/entt/entity/group.cpp @@ -56,7 +56,7 @@ TEST(NonOwningGroup, Functionalities) { for(auto entity: group) { ASSERT_EQ(std::get<0>(cgroup.get(entity)), 42); ASSERT_EQ(std::get<1>(group.get(entity)), '2'); - ASSERT_EQ(cgroup.get(entity), '2'); + ASSERT_EQ(cgroup.get<1>(entity), '2'); } ASSERT_EQ(group.handle().data()[0u], e1); @@ -276,9 +276,9 @@ TEST(NonOwningGroup, Sort) { ASSERT_EQ(group.handle().data()[1u], e1); ASSERT_EQ(group.handle().data()[2u], e2); - ASSERT_EQ((group.get(e0)), (std::make_tuple(0, 0u))); - ASSERT_EQ((group.get(e1)), (std::make_tuple(1, 1u))); - ASSERT_EQ((group.get(e2)), (std::make_tuple(2, 2u))); + ASSERT_EQ((group.get<0, 1>(e0)), (std::make_tuple(0, 0u))); + ASSERT_EQ((group.get<0, 1>(e1)), (std::make_tuple(1, 1u))); + ASSERT_EQ((group.get<0, 1>(e2)), (std::make_tuple(2, 2u))); ASSERT_FALSE(group.contains(e3)); @@ -329,7 +329,7 @@ TEST(NonOwningGroup, SortAsAPool) { group.sort(); ASSERT_EQ((group.get(e0)), (std::make_tuple(0, 0u))); - ASSERT_EQ((group.get(e1)), (std::make_tuple(1, 1u))); + ASSERT_EQ((group.get<0, 1>(e1)), (std::make_tuple(1, 1u))); ASSERT_EQ((group.get(e2)), (std::make_tuple(2, 2u))); ASSERT_FALSE(group.contains(e3)); @@ -387,9 +387,18 @@ TEST(NonOwningGroup, ConstNonConstAndAllInBetween) { ASSERT_EQ(group.size(), 1u); + static_assert(std::is_same_v({})), int &>); static_assert(std::is_same_v({})), int &>); + + static_assert(std::is_same_v({})), void>); + static_assert(std::is_same_v({})), void>); + + static_assert(std::is_same_v({})), const char &>); static_assert(std::is_same_v({})), const char &>); - static_assert(std::is_same_v({})), std::tuple>); + + static_assert(std::is_same_v({})), std::tuple>); + static_assert(std::is_same_v({})), std::tuple>); + static_assert(std::is_same_v>); static_assert(std::is_same_v)), decltype(std::as_const(registry).group_if_exists(entt::get))>); @@ -478,7 +487,7 @@ TEST(NonOwningGroup, ExcludedComponents) { if(entity == e0) { ASSERT_EQ(group.get(e0), 0); } else if(entity == e2) { - ASSERT_EQ(group.get(e2), 2); + ASSERT_EQ(group.get<0>(e2), 2); } } @@ -496,7 +505,7 @@ TEST(NonOwningGroup, ExcludedComponents) { if(entity == e1) { ASSERT_EQ(group.get(e1), 1); } else if(entity == e3) { - ASSERT_EQ(group.get(e3), 3); + ASSERT_EQ(group.get<0>(e3), 3); } } } @@ -786,7 +795,7 @@ TEST(OwningGroup, Functionalities) { for(auto entity: group) { ASSERT_EQ(std::get<0>(cgroup.get(entity)), 42); ASSERT_EQ(std::get<1>(group.get(entity)), '2'); - ASSERT_EQ(cgroup.get(entity), '2'); + ASSERT_EQ(cgroup.get<1>(entity), '2'); } ASSERT_EQ(group.handle().data()[0u], e1); @@ -975,7 +984,7 @@ TEST(OwningGroup, SortOrdered) { registry.emplace(entities[4], 2); group.sort([&group](const entt::entity lhs, const entt::entity rhs) { - return group.get(lhs).value < group.get(rhs).value; + return group.get(lhs).value < group.get<0>(rhs).value; }); ASSERT_EQ(group.handle().data()[0u], entities[0]); @@ -995,7 +1004,7 @@ TEST(OwningGroup, SortOrdered) { ASSERT_EQ(group.storage().raw()[0u][2u], 'c'); ASSERT_EQ((group.get(entities[0])), (std::make_tuple(boxed_int{12}, 'a'))); - ASSERT_EQ((group.get(entities[1])), (std::make_tuple(boxed_int{9}, 'b'))); + ASSERT_EQ((group.get<0, 1>(entities[1])), (std::make_tuple(boxed_int{9}, 'b'))); ASSERT_EQ((group.get(entities[2])), (std::make_tuple(boxed_int{6}, 'c'))); ASSERT_FALSE(group.contains(entities[3])); @@ -1042,7 +1051,7 @@ TEST(OwningGroup, SortReverse) { ASSERT_EQ(group.storage().raw()[0u][2u], 'a'); ASSERT_EQ((group.get(entities[0])), (std::make_tuple(boxed_int{6}, 'a'))); - ASSERT_EQ((group.get(entities[1])), (std::make_tuple(boxed_int{9}, 'b'))); + ASSERT_EQ((group.get<0, 1>(entities[1])), (std::make_tuple(boxed_int{9}, 'b'))); ASSERT_EQ((group.get(entities[2])), (std::make_tuple(boxed_int{12}, 'c'))); ASSERT_FALSE(group.contains(entities[3])); @@ -1097,9 +1106,9 @@ TEST(OwningGroup, SortUnordered) { ASSERT_EQ(group.storage().raw()[0u][6u].value, 5); ASSERT_EQ(group.get(group.handle().data()[0u]), 'e'); - ASSERT_EQ(group.get(group.handle().data()[1u]), 'd'); + ASSERT_EQ(group.get<1>(group.handle().data()[1u]), 'd'); ASSERT_EQ(group.get(group.handle().data()[2u]), 'c'); - ASSERT_EQ(group.get(group.handle().data()[3u]), 'b'); + ASSERT_EQ(group.get<1>(group.handle().data()[3u]), 'b'); ASSERT_EQ(group.get(group.handle().data()[4u]), 'a'); ASSERT_FALSE(group.contains(entities[5])); @@ -1136,9 +1145,9 @@ TEST(OwningGroup, SortWithExclusionList) { ASSERT_EQ(group.storage().raw()[0u][3u].value, 0); ASSERT_EQ(group.get(entities[0]).value, 0); - ASSERT_EQ(group.get(entities[1]).value, 1); + ASSERT_EQ(group.get<0>(entities[1]).value, 1); ASSERT_EQ(group.get(entities[3]).value, 3); - ASSERT_EQ(group.get(entities[4]).value, 4); + ASSERT_EQ(group.get<0>(entities[4]).value, 4); ASSERT_FALSE(group.contains(entities[2])); } @@ -1192,11 +1201,24 @@ TEST(OwningGroup, ConstNonConstAndAllInBetween) { ASSERT_EQ(group.size(), 1u); + static_assert(std::is_same_v({})), int &>); static_assert(std::is_same_v({})), int &>); + + static_assert(std::is_same_v({})), const char &>); static_assert(std::is_same_v({})), const char &>); + + static_assert(std::is_same_v({})), void>); + static_assert(std::is_same_v({})), void>); + + static_assert(std::is_same_v({})), double &>); static_assert(std::is_same_v({})), double &>); + + static_assert(std::is_same_v({})), const float &>); static_assert(std::is_same_v({})), const float &>); - static_assert(std::is_same_v({})), std::tuple>); + + static_assert(std::is_same_v({})), std::tuple>); + static_assert(std::is_same_v({})), std::tuple>); + static_assert(std::is_same_v>); static_assert(std::is_same_v(entt::get)), decltype(std::as_const(registry).group_if_exists(entt::get))>); @@ -1289,7 +1311,7 @@ TEST(OwningGroup, ExcludedComponents) { if(entity == e0) { ASSERT_EQ(group.get(e0), 0); } else if(entity == e2) { - ASSERT_EQ(group.get(e2), 2); + ASSERT_EQ(group.get<0>(e2), 2); } } @@ -1307,7 +1329,7 @@ TEST(OwningGroup, ExcludedComponents) { if(entity == e1) { ASSERT_EQ(group.get(e1), 1); } else if(entity == e3) { - ASSERT_EQ(group.get(e3), 3); + ASSERT_EQ(group.get<0>(e3), 3); } } }