Просмотр исходного кода

view: drop empty func each support for non-empty types in single views

Michele Caini 1 год назад
Родитель
Сommit
312c2b1cdf
3 измененных файлов с 15 добавлено и 26 удалено
  1. 0 2
      TODO
  2. 10 18
      src/entt/entity/view.hpp
  3. 5 6
      test/entt/entity/view.cpp

+ 0 - 2
TODO

@@ -20,7 +20,6 @@ TODO:
 * view: update natvis as needed after the last rework, merge pools/filter in the same array, drop check (?) and turn view into a position
 * view: type-only view_iterator (dyn get/excl sizes), type-only basic_common_view (dyn get/excl sizes with pointer to array from derived)
 * combine version-mask-vs-version-bits tricks with reserved bits to allow things like enabling/disabling
-* review all // NOLINT
 * self contained entity traits to avoid explicit specializations (ie enum constants)
 * auto type info data from types if present
 * test: push sharing types further
@@ -38,7 +37,6 @@ TODO:
 * improve front (no multiple checks) and back (ie no contains) for multi-type view
 * cleanup common view from tricks to handle single swap-only and in-place, if constexpr branches
 * consider returning subrange for swap-only sparse sets or consider using filtered each for in-place storage (smilar to storage entity), cleanup views
-* stop supporting func() for view::each (nonsense)
 * exploit ref/cref in any to avoid invoking the vtable if possible
 * review meta properties and details, maybe a dense map is too much
 * self assignment support for any and meta_any

+ 10 - 18
src/entt/entity/view.hpp

@@ -1002,35 +1002,27 @@ public:
      */
     template<typename Func>
     void each(Func func) const {
-        if constexpr(is_applicable_v<Func, decltype(*storage()->each().begin())>) {
+        if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_view>().get({})))>) {
             for(const auto pack: each()) {
                 std::apply(func, pack);
             }
-        } else if constexpr(std::is_invocable_v<Func, decltype(*storage()->begin())>) {
-            if constexpr(Get::storage_policy == deletion_policy::swap_and_pop || Get::storage_policy == deletion_policy::swap_only) {
+        } else if constexpr(Get::storage_policy == deletion_policy::swap_and_pop || Get::storage_policy == deletion_policy::swap_only) {
+            if constexpr(std::is_void_v<typename Get::value_type>) {
+                for(size_type pos = base_type::size(); pos; --pos) {
+                    func();
+                }
+            } else {
                 if(const auto len = base_type::size(); len != 0u) {
                     for(auto last = storage()->end(), first = last - len; first != last; ++first) {
                         func(*first);
                     }
                 }
-            } else {
-                static_assert(Get::storage_policy == deletion_policy::in_place, "Unexpected storage policy");
-
-                for(const auto pack: each()) {
-                    func(std::get<1>(pack));
-                }
             }
         } else {
-            if constexpr(Get::storage_policy == deletion_policy::swap_and_pop || Get::storage_policy == deletion_policy::swap_only) {
-                for(size_type pos = base_type::size(); pos; --pos) {
-                    func();
-                }
-            } else {
-                static_assert(Get::storage_policy == deletion_policy::in_place, "Unexpected storage policy");
+            static_assert(Get::storage_policy == deletion_policy::in_place, "Unexpected storage policy");
 
-                for([[maybe_unused]] const auto entt: *this) {
-                    func();
-                }
+            for(const auto pack: each()) {
+                std::apply([&func](const auto, auto &&...elem) { func(std::forward<decltype(elem)>(elem)...); }, pack);
             }
         }
     }

+ 5 - 6
test/entt/entity/view.cpp

@@ -401,17 +401,16 @@ TEST(SingleStorageView, StableType) {
         ASSERT_EQ(entt, entity[1u]);
     }
 
-    view.each([&entity](const auto entt, const auto &) {
+    view.each([&](const auto entt, const auto &elem) {
+        ASSERT_EQ(elem, view->get(entity[1u]));
         ASSERT_EQ(entt, entity[1u]);
     });
 
-    view.each([check = view->get(entity[1u])](const auto &elem) {
-        ASSERT_EQ(elem, check);
+    view.each([&](const auto &elem) {
+        ASSERT_EQ(elem, view->get(entity[1u]));
     });
 
-    view.each([&]() {
-        storage.erase(entity[1u]);
-    });
+    storage.erase(view.begin(), view.end());
 
     ASSERT_EQ(view.size_hint(), 2u);
     ASSERT_EQ(view->size(), 2u);