瀏覽代碼

view/groups: extended get (breaking changes)

Michele Caini 5 年之前
父節點
當前提交
5a3e816544
共有 4 個文件被更改,包括 53 次插入8 次删除
  1. 22 4
      src/entt/entity/group.hpp
  2. 7 2
      src/entt/entity/view.hpp
  3. 14 0
      test/entt/entity/group.cpp
  4. 10 2
      test/entt/entity/view.cpp

+ 22 - 4
src/entt/entity/group.hpp

@@ -419,10 +419,18 @@ public:
      * @return The components assigned to the entity.
      * @return The components assigned to the entity.
      */
      */
     template<typename... Component>
     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));
         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), ...);
             return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
         } else {
         } else {
             return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};
             return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};
@@ -987,10 +995,20 @@ public:
      * @return The components assigned to the entity.
      * @return The components assigned to the entity.
      */
      */
     template<typename... Component>
     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));
         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), ...);
             return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
         } else {
         } else {
             return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};
             return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};

+ 7 - 2
src/entt/entity/view.hpp

@@ -474,8 +474,13 @@ public:
         ENTT_ASSERT(contains(entt));
         ENTT_ASSERT(contains(entt));
 
 
         if constexpr(sizeof...(Comp) == 0) {
         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) {
         } else if constexpr(sizeof...(Comp) == 1) {
             return (std::get<pool_type<Comp> *>(pools)->get(entt), ...);
             return (std::get<pool_type<Comp> *>(pools)->get(entt), ...);
         } else {
         } else {

+ 14 - 0
test/entt/entity/group.cpp

@@ -579,6 +579,13 @@ TEST(NonOwningGroup, SignalRace) {
     ASSERT_EQ(group.size(), 1u);
     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) {
 TEST(OwningGroup, Functionalities) {
     entt::registry registry;
     entt::registry registry;
     auto group = registry.group<int>(entt::get<char>);
     auto group = registry.group<int>(entt::get<char>);
@@ -1265,3 +1272,10 @@ TEST(OwningGroup, PreventEarlyOptOut) {
         ASSERT_EQ(i, 2);
         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 &>);
+}

+ 10 - 2
test/entt/entity/view.cpp

@@ -1,3 +1,4 @@
+#include <tuple>
 #include <utility>
 #include <utility>
 #include <type_traits>
 #include <type_traits>
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
@@ -623,7 +624,7 @@ TEST(MultiComponentView, ExcludedComponents) {
         if(entity == e0) {
         if(entity == e0) {
             ASSERT_EQ(view.get<const int>(e0), 0);
             ASSERT_EQ(view.get<const int>(e0), 0);
         } else if(entity == e2) {
         } 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);
         ASSERT_TRUE(entity == e1 || entity == e3);
 
 
         if(entity == e1) {
         if(entity == e1) {
-            ASSERT_EQ(view.get(e1), 1);
+            ASSERT_EQ(std::get<0>(view.get(e1)), 1);
         } else if(entity == e3) {
         } else if(entity == e3) {
             ASSERT_EQ(view.get<const int>(e3), 3);
             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 &>);
+}