view/groups: extended get (breaking changes)

This commit is contained in:
Michele Caini
2020-10-15 17:02:50 +02:00
parent 3e385fb884
commit 5a3e816544
4 changed files with 53 additions and 8 deletions

View File

@@ -419,10 +419,18 @@ public:
* @return The components assigned to the entity.
*/
template<typename... Component>
[[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
[[nodiscard]] decltype(auto) get(const entity_type entt) const {
ENTT_ASSERT(contains(entt));
if constexpr(sizeof...(Component) == 1) {
if constexpr(sizeof...(Component) == 0) {
return std::tuple_cat([entt](auto *cpool) {
if constexpr(is_eto_eligible_v<typename std::remove_reference_t<decltype(*cpool)>::value_type>) {
return std::make_tuple();
} else {
return std::forward_as_tuple(cpool->get(entt));
}
}(std::get<pool_type<Get> *>(pools))...);
} else if constexpr(sizeof...(Component) == 1) {
return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
} else {
return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};
@@ -987,10 +995,20 @@ public:
* @return The components assigned to the entity.
*/
template<typename... Component>
[[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
[[nodiscard]] decltype(auto) get(const entity_type entt) const {
ENTT_ASSERT(contains(entt));
if constexpr(sizeof...(Component) == 1) {
if constexpr(sizeof...(Component) == 0) {
auto filter = [entt](auto *cpool) {
if constexpr(is_eto_eligible_v<typename std::remove_reference_t<decltype(*cpool)>::value_type>) {
return std::make_tuple();
} else {
return std::forward_as_tuple(cpool->get(entt));
}
};
return std::tuple_cat(filter(std::get<pool_type<Owned> *>(pools))..., filter(std::get<pool_type<Get> *>(pools))...);
} else if constexpr(sizeof...(Component) == 1) {
return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
} else {
return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};

View File

@@ -474,8 +474,13 @@ public:
ENTT_ASSERT(contains(entt));
if constexpr(sizeof...(Comp) == 0) {
static_assert(sizeof...(Component) == 1, "Invalid component type");
return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
return std::tuple_cat([entt](auto *cpool) {
if constexpr(is_eto_eligible_v<typename std::remove_reference_t<decltype(*cpool)>::value_type>) {
return std::make_tuple();
} else {
return std::forward_as_tuple(cpool->get(entt));
}
}(std::get<pool_type<Component> *>(pools))...);
} else if constexpr(sizeof...(Comp) == 1) {
return (std::get<pool_type<Comp> *>(pools)->get(entt), ...);
} else {

View File

@@ -579,6 +579,13 @@ TEST(NonOwningGroup, SignalRace) {
ASSERT_EQ(group.size(), 1u);
}
TEST(NonOwningGroup, ExtendedGet) {
using type = decltype(std::declval<entt::registry>().group(entt::get<int, empty_type, char>).get({}));
static_assert(std::tuple_size_v<type> == 2u);
static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
}
TEST(OwningGroup, Functionalities) {
entt::registry registry;
auto group = registry.group<int>(entt::get<char>);
@@ -1265,3 +1272,10 @@ TEST(OwningGroup, PreventEarlyOptOut) {
ASSERT_EQ(i, 2);
});
}
TEST(OwningGroup, ExtendedGet) {
using type = decltype(std::declval<entt::registry>().group<int, empty_type>(entt::get<char>).get({}));
static_assert(std::tuple_size_v<type> == 2u);
static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
}

View File

@@ -1,3 +1,4 @@
#include <tuple>
#include <utility>
#include <type_traits>
#include <gtest/gtest.h>
@@ -623,7 +624,7 @@ TEST(MultiComponentView, ExcludedComponents) {
if(entity == e0) {
ASSERT_EQ(view.get<const int>(e0), 0);
} else if(entity == e2) {
ASSERT_EQ(view.get(e2), 2);
ASSERT_EQ(std::get<0>(view.get(e2)), 2);
}
}
@@ -636,7 +637,7 @@ TEST(MultiComponentView, ExcludedComponents) {
ASSERT_TRUE(entity == e1 || entity == e3);
if(entity == e1) {
ASSERT_EQ(view.get(e1), 1);
ASSERT_EQ(std::get<0>(view.get(e1)), 1);
} else if(entity == e3) {
ASSERT_EQ(view.get<const int>(e3), 3);
}
@@ -858,3 +859,10 @@ TEST(MultiComponentView, ChunkedWithExcludedComponents) {
}
});
}
TEST(MultiComponentView, ExtendedGet) {
using type = decltype(std::declval<entt::registry>().view<int, empty_type, char>().get({}));
static_assert(std::tuple_size_v<type> == 2u);
static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
}