added each-with-type to multiple component views

This commit is contained in:
Michele Caini
2019-03-09 14:37:33 +01:00
parent a7caae5c17
commit f673f2c5bc
2 changed files with 55 additions and 0 deletions

View File

@@ -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;
};

View File

@@ -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;