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

added each-with-type to multiple component views

Michele Caini 7 лет назад
Родитель
Сommit
f673f2c5bc
2 измененных файлов с 55 добавлено и 0 удалено
  1. 29 0
      src/entt/entity/view.hpp
  2. 26 0
      test/entt/entity/view.cpp

+ 29 - 0
src/entt/entity/view.hpp

@@ -352,6 +352,35 @@ public:
         ((std::get<pool_type<Component> *>(pools) == view ? each<Component>(std::get<pool_type<Component> *>(pools), std::move(func), std::make_index_sequence<sizeof...(Component)-1>{}) : void()), ...);
     }
 
+    /**
+     * @brief Iterates entities and components and applies the given function
+     * object to them.
+     *
+     * The function object is invoked for each entity. It is provided with the
+     * entity itself and a set of references to all its components. The
+     * _constness_ of the components is as requested.<br/>
+     * The signature of the function must be equivalent to one of the following
+     * forms:
+     *
+     * @code{.cpp}
+     * void(const entity_type, Component &...);
+     * void(Component &...);
+     * @endcode
+     *
+     * The pool of the suggested component is used to drive iterations. The
+     * returned entities will therefore respect the order of the pool associated
+     * with that type.<br/>
+     * It is no longer guaranteed that the performance is the best possible, but
+     * there will be greater control over the order of iteration.
+     *
+     * @tparam Func Type of the function object to invoke.
+     * @param func A valid function object.
+     */
+    template<typename Comp, typename Func>
+    void each(Func func) const {
+        each<Comp>(std::get<pool_type<Comp> *>(pools), std::move(func), std::make_index_sequence<sizeof...(Component)-1>{});
+    }
+
 private:
     const std::tuple<pool_type<Component> *...> pools;
 };

+ 26 - 0
test/entt/entity/view.cpp

@@ -321,6 +321,32 @@ TEST(MultipleComponentView, Each) {
     ASSERT_EQ(cnt, std::size_t{0});
 }
 
+TEST(MultipleComponentView, EachWithType) {
+    entt::registry<> registry;
+
+    for(auto i = 0; i < 3; ++i) {
+        const auto entity = registry.create();
+        registry.assign<int>(entity, i);
+        registry.assign<char>(entity);
+    }
+
+    // makes char a better candidate during iterations
+    const auto entity = registry.create();
+    registry.assign<int>(entity, 99);
+
+    registry.view<int, char>().each<int>([value = 2](const auto curr, const auto) mutable {
+        ASSERT_EQ(curr, value--);
+    });
+
+    registry.sort<int>([](const auto lhs, const auto rhs) {
+        return lhs < rhs;
+    });
+
+    registry.view<int, char>().each<int>([value = 0](const auto curr, const auto) mutable {
+        ASSERT_EQ(curr, value++);
+    });
+}
+
 TEST(MultipleComponentView, EachWithHoles) {
     entt::registry<> registry;