diff --git a/src/entt/entity/view.hpp b/src/entt/entity/view.hpp index daf525ad3..c0c9685c7 100644 --- a/src/entt/entity/view.hpp +++ b/src/entt/entity/view.hpp @@ -352,6 +352,35 @@ public: ((std::get *>(pools) == view ? each(std::get *>(pools), std::move(func), std::make_index_sequence{}) : 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.
+ * 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.
+ * 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 + void each(Func func) const { + each(std::get *>(pools), std::move(func), std::make_index_sequence{}); + } + private: const std::tuple *...> pools; }; diff --git a/test/entt/entity/view.cpp b/test/entt/entity/view.cpp index 6464d4e7b..0f6cdcf74 100644 --- a/test/entt/entity/view.cpp +++ b/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(entity, i); + registry.assign(entity); + } + + // makes char a better candidate during iterations + const auto entity = registry.create(); + registry.assign(entity, 99); + + registry.view().each([value = 2](const auto curr, const auto) mutable { + ASSERT_EQ(curr, value--); + }); + + registry.sort([](const auto lhs, const auto rhs) { + return lhs < rhs; + }); + + registry.view().each([value = 0](const auto curr, const auto) mutable { + ASSERT_EQ(curr, value++); + }); +} + TEST(MultipleComponentView, EachWithHoles) { entt::registry<> registry;