| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076 |
- #ifndef ENTT_ENTITY_GROUP_HPP
- #define ENTT_ENTITY_GROUP_HPP
- #include <tuple>
- #include <type_traits>
- #include <utility>
- #include "../config/config.h"
- #include "../core/fwd.hpp"
- #include "../core/iterator.hpp"
- #include "../core/type_info.hpp"
- #include "../core/type_traits.hpp"
- #include "entity.hpp"
- #include "fwd.hpp"
- #include "sparse_set.hpp"
- #include "storage.hpp"
- namespace entt {
- /**
- * @cond TURN_OFF_DOXYGEN
- * Internal details not to be documented.
- */
- namespace internal {
- template<typename, typename, typename>
- class extended_group_iterator;
- template<typename It, typename... Owned, typename... Get>
- class extended_group_iterator<It, owned_t<Owned...>, get_t<Get...>> {
- template<typename Type>
- auto index_to_element([[maybe_unused]] Type &cpool) const {
- if constexpr(Type::traits_type::page_size == 0u) {
- return std::make_tuple();
- } else {
- return std::forward_as_tuple(cpool.rbegin()[it.index()]);
- }
- }
- public:
- using iterator_type = It;
- using difference_type = std::ptrdiff_t;
- using value_type = decltype(std::tuple_cat(std::make_tuple(*std::declval<It>()), std::declval<Owned>().get_as_tuple({})..., std::declval<Get>().get_as_tuple({})...));
- using pointer = input_iterator_pointer<value_type>;
- using reference = value_type;
- using iterator_category = std::input_iterator_tag;
- using iterator_concept = std::forward_iterator_tag;
- constexpr extended_group_iterator()
- : it{},
- pools{} {}
- extended_group_iterator(iterator_type from, const std::tuple<Owned *..., Get *...> &cpools)
- : it{from},
- pools{cpools} {}
- extended_group_iterator &operator++() noexcept {
- return ++it, *this;
- }
- extended_group_iterator operator++(int) noexcept {
- extended_group_iterator orig = *this;
- return ++(*this), orig;
- }
- [[nodiscard]] reference operator*() const noexcept {
- return std::tuple_cat(std::make_tuple(*it), index_to_element(*std::get<Owned *>(pools))..., std::get<Get *>(pools)->get_as_tuple(*it)...);
- }
- [[nodiscard]] pointer operator->() const noexcept {
- return operator*();
- }
- [[nodiscard]] constexpr iterator_type base() const noexcept {
- return it;
- }
- template<typename... Lhs, typename... Rhs>
- friend constexpr bool operator==(const extended_group_iterator<Lhs...> &, const extended_group_iterator<Rhs...> &) noexcept;
- private:
- It it;
- std::tuple<Owned *..., Get *...> pools;
- };
- template<typename... Lhs, typename... Rhs>
- [[nodiscard]] constexpr bool operator==(const extended_group_iterator<Lhs...> &lhs, const extended_group_iterator<Rhs...> &rhs) noexcept {
- return lhs.it == rhs.it;
- }
- template<typename... Lhs, typename... Rhs>
- [[nodiscard]] constexpr bool operator!=(const extended_group_iterator<Lhs...> &lhs, const extended_group_iterator<Rhs...> &rhs) noexcept {
- return !(lhs == rhs);
- }
- struct group_descriptor {
- using size_type = std::size_t;
- virtual ~group_descriptor() = default;
- virtual size_type owned(const id_type *, const size_type) const noexcept {
- return 0u;
- }
- };
- template<typename, typename, typename>
- class group_handler;
- template<typename... Owned, typename... Get, typename... Exclude>
- class group_handler<owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>> final: public group_descriptor {
- // nasty workaround for an issue with the toolset v141 that doesn't accept a fold expression here
- static_assert(!std::disjunction_v<std::bool_constant<Owned::traits_type::in_place_delete>...>, "Groups do not support in-place delete");
- static_assert(!std::disjunction_v<std::is_const<Owned>..., std::is_const<Get>..., std::is_const<Exclude>...>, "Const storage type not allowed");
- using base_type = std::common_type_t<typename Owned::base_type..., typename Get::base_type..., typename Exclude::base_type...>;
- using entity_type = typename base_type::entity_type;
- void swap_elements(const std::size_t pos, const entity_type entt) {
- std::apply([pos, entt](auto *...cpool) { (cpool->swap_elements(cpool->data()[pos], entt), ...); }, pools);
- }
- void push_on_construct(const entity_type entt) {
- if(std::apply([entt, len = len](auto *cpool, auto *...other) { return cpool->contains(entt) && !(cpool->index(entt) < len) && (other->contains(entt) && ...); }, pools)
- && std::apply([entt](auto *...cpool) { return (!cpool->contains(entt) && ...); }, filter)) {
- swap_elements(len++, entt);
- }
- }
- void push_on_destroy(const entity_type entt) {
- if(std::apply([entt, len = len](auto *cpool, auto *...other) { return cpool->contains(entt) && !(cpool->index(entt) < len) && (other->contains(entt) && ...); }, pools)
- && std::apply([entt](auto *...cpool) { return (0u + ... + cpool->contains(entt)) == 1u; }, filter)) {
- swap_elements(len++, entt);
- }
- }
- void remove_if(const entity_type entt) {
- if(std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < len)) {
- swap_elements(--len, entt);
- }
- }
- public:
- using size_type = typename base_type::size_type;
- group_handler(Owned &...opool, Get &...gpool, Exclude &...epool)
- : pools{&opool..., &gpool...},
- filter{&epool...},
- len{} {
- std::apply([this](auto *...cpool) { ((cpool->on_construct().template connect<&group_handler::push_on_construct>(*this), cpool->on_destroy().template connect<&group_handler::remove_if>(*this)), ...); }, pools);
- std::apply([this](auto *...cpool) { ((cpool->on_construct().template connect<&group_handler::remove_if>(*this), cpool->on_destroy().template connect<&group_handler::push_on_destroy>(*this)), ...); }, filter);
- // we cannot iterate backwards because we want to leave behind valid entities in case of owned types
- for(auto *first = std::get<0>(pools)->data(), *last = first + std::get<0>(pools)->size(); first != last; ++first) {
- push_on_construct(*first);
- }
- }
- size_type owned(const id_type *elem, const size_type length) const noexcept final {
- size_type cnt = 0u;
- for(auto pos = 0u; pos < length; ++pos) {
- cnt += ((elem[pos] == entt::type_hash<typename Owned::value_type>::value()) || ...);
- }
- return cnt;
- }
- [[nodiscard]] size_type length() const noexcept {
- return len;
- }
- template<typename Type>
- Type pools_as() const noexcept {
- return pools;
- }
- template<typename Type>
- Type filter_as() const noexcept {
- return filter;
- }
- private:
- std::tuple<Owned *..., Get *...> pools;
- std::tuple<Exclude *...> filter;
- std::size_t len;
- };
- template<typename... Get, typename... Exclude>
- class group_handler<owned_t<>, get_t<Get...>, exclude_t<Exclude...>> final: public group_descriptor {
- // nasty workaround for an issue with the toolset v141 that doesn't accept a fold expression here
- static_assert(!std::disjunction_v<std::is_const<Get>..., std::is_const<Exclude>...>, "Const storage type not allowed");
- using base_type = std::common_type_t<typename Get::base_type..., typename Exclude::base_type...>;
- using entity_type = typename base_type::entity_type;
- void push_on_construct(const entity_type entt) {
- if(!elem.contains(entt)
- && std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
- && std::apply([entt](auto *...cpool) { return (!cpool->contains(entt) && ...); }, filter)) {
- elem.push(entt);
- }
- }
- void push_on_destroy(const entity_type entt) {
- if(!elem.contains(entt)
- && std::apply([entt](auto *...cpool) { return (cpool->contains(entt) && ...); }, pools)
- && std::apply([entt](auto *...cpool) { return (0u + ... + cpool->contains(entt)) == 1u; }, filter)) {
- elem.push(entt);
- }
- }
- void remove_if(const entity_type entt) {
- elem.remove(entt);
- }
- public:
- using common_type = base_type;
- template<typename Alloc>
- group_handler(const Alloc &alloc, Get &...gpool, Exclude &...epool)
- : pools{&gpool...},
- filter{&epool...},
- elem{alloc} {
- std::apply([this](auto *...cpool) { ((cpool->on_construct().template connect<&group_handler::push_on_construct>(*this), cpool->on_destroy().template connect<&group_handler::remove_if>(*this)), ...); }, pools);
- std::apply([this](auto *...cpool) { ((cpool->on_construct().template connect<&group_handler::remove_if>(*this), cpool->on_destroy().template connect<&group_handler::push_on_destroy>(*this)), ...); }, filter);
- for(const auto entity: static_cast<base_type &>(*std::get<0>(pools))) {
- push_on_construct(entity);
- }
- }
- common_type &handle() noexcept {
- return elem;
- }
- const common_type &handle() const noexcept {
- return elem;
- }
- template<typename Type>
- Type pools_as() const noexcept {
- return pools;
- }
- template<typename Type>
- Type filter_as() const noexcept {
- return filter;
- }
- private:
- std::tuple<Get *...> pools;
- std::tuple<Exclude *...> filter;
- base_type elem;
- };
- } // namespace internal
- /**
- * Internal details not to be documented.
- * @endcond
- */
- /**
- * @brief Group.
- *
- * Primary template isn't defined on purpose. All the specializations give a
- * compile-time error, but for a few reasonable cases.
- */
- template<typename, typename, typename>
- class basic_group;
- /**
- * @brief Non-owning group.
- *
- * A non-owning group returns all entities and only the entities that are at
- * least in the given storage. Moreover, it's guaranteed that the entity list is
- * tightly packed in memory for fast iterations.
- *
- * @b Important
- *
- * Iterators aren't invalidated if:
- *
- * * New elements are added to the storage.
- * * The entity currently pointed is modified (for example, components are added
- * or removed from it).
- * * The entity currently pointed is destroyed.
- *
- * In all other cases, modifying the pools iterated by the group in any way
- * invalidates all the iterators.
- *
- * @tparam Get Types of storage _observed_ by the group.
- * @tparam Exclude Types of storage used to filter the group.
- */
- template<typename... Get, typename... Exclude>
- class basic_group<owned_t<>, get_t<Get...>, exclude_t<Exclude...>> {
- using base_type = std::common_type_t<typename Get::base_type..., typename Exclude::base_type...>;
- using underlying_type = typename base_type::entity_type;
- template<typename Type>
- static constexpr std::size_t index_of = type_list_index_v<std::remove_const_t<Type>, type_list<typename Get::value_type..., typename Exclude::value_type...>>;
- auto pools() const noexcept {
- using return_type = std::tuple<Get *...>;
- return descriptor ? descriptor->template pools_as<return_type>() : return_type{};
- }
- auto filter() const noexcept {
- using return_type = std::tuple<Exclude *...>;
- return descriptor ? descriptor->template filter_as<return_type>() : return_type{};
- }
- public:
- /*! @brief Underlying entity identifier. */
- using entity_type = underlying_type;
- /*! @brief Unsigned integer type. */
- using size_type = std::size_t;
- /*! @brief Common type among all storage types. */
- using common_type = base_type;
- /*! @brief Random access iterator type. */
- using iterator = typename common_type::iterator;
- /*! @brief Reversed iterator type. */
- using reverse_iterator = typename common_type::reverse_iterator;
- /*! @brief Iterable group type. */
- using iterable = iterable_adaptor<internal::extended_group_iterator<iterator, owned_t<>, get_t<Get...>>>;
- /*! @brief Group handler type. */
- using handler = internal::group_handler<owned_t<>, get_t<std::remove_const_t<Get>...>, exclude_t<std::remove_const_t<Exclude>...>>;
- /*! @brief Default constructor to use to create empty, invalid groups. */
- basic_group() noexcept
- : descriptor{} {}
- /**
- * @brief Constructs a group from a set of storage classes.
- * @param ref A reference to a group handler.
- */
- basic_group(handler &ref) noexcept
- : descriptor{&ref} {}
- /**
- * @brief Returns the leading storage of a group.
- * @return The leading storage of the group.
- */
- [[nodiscard]] const common_type &handle() const noexcept {
- return descriptor->handle();
- }
- /**
- * @brief Returns the storage for a given component type, if any.
- * @tparam Type Type of component of which to return the storage.
- * @return The storage for the given component type.
- */
- template<typename Type>
- [[nodiscard]] auto *storage() const noexcept {
- return storage<index_of<Type>>();
- }
- /**
- * @brief Returns the storage for a given index, if any.
- * @tparam Index Index of the storage to return.
- * @return The storage for the given index.
- */
- template<std::size_t Index>
- [[nodiscard]] auto *storage() const noexcept {
- constexpr auto offset = sizeof...(Get);
- if constexpr(Index < offset) {
- return std::get<Index>(pools());
- } else {
- return std::get<Index - offset>(filter());
- }
- }
- /**
- * @brief Returns the number of entities that are part of the group.
- * @return Number of entities that are part of the group.
- */
- [[nodiscard]] size_type size() const noexcept {
- return *this ? handle().size() : size_type{};
- }
- /**
- * @brief Returns the number of elements that a group has currently
- * allocated space for.
- * @return Capacity of the group.
- */
- [[nodiscard]] size_type capacity() const noexcept {
- return *this ? handle().capacity() : size_type{};
- }
- /*! @brief Requests the removal of unused capacity. */
- void shrink_to_fit() {
- if(*this) {
- descriptor->handle().shrink_to_fit();
- }
- }
- /**
- * @brief Checks whether a group is empty.
- * @return True if the group is empty, false otherwise.
- */
- [[nodiscard]] bool empty() const noexcept {
- return !*this || handle().empty();
- }
- /**
- * @brief Returns an iterator to the first entity of the group.
- *
- * If the group is empty, the returned iterator will be equal to `end()`.
- *
- * @return An iterator to the first entity of the group.
- */
- [[nodiscard]] iterator begin() const noexcept {
- return *this ? handle().begin() : iterator{};
- }
- /**
- * @brief Returns an iterator that is past the last entity of the group.
- * @return An iterator to the entity following the last entity of the
- * group.
- */
- [[nodiscard]] iterator end() const noexcept {
- return *this ? handle().end() : iterator{};
- }
- /**
- * @brief Returns an iterator to the first entity of the reversed group.
- *
- * If the group is empty, the returned iterator will be equal to `rend()`.
- *
- * @return An iterator to the first entity of the reversed group.
- */
- [[nodiscard]] reverse_iterator rbegin() const noexcept {
- return *this ? handle().rbegin() : reverse_iterator{};
- }
- /**
- * @brief Returns an iterator that is past the last entity of the reversed
- * group.
- * @return An iterator to the entity following the last entity of the
- * reversed group.
- */
- [[nodiscard]] reverse_iterator rend() const noexcept {
- return *this ? handle().rend() : reverse_iterator{};
- }
- /**
- * @brief Returns the first entity of the group, if any.
- * @return The first entity of the group if one exists, the null entity
- * otherwise.
- */
- [[nodiscard]] entity_type front() const noexcept {
- const auto it = begin();
- return it != end() ? *it : null;
- }
- /**
- * @brief Returns the last entity of the group, if any.
- * @return The last entity of the group if one exists, the null entity
- * otherwise.
- */
- [[nodiscard]] entity_type back() const noexcept {
- const auto it = rbegin();
- return it != rend() ? *it : null;
- }
- /**
- * @brief Finds an entity.
- * @param entt A valid identifier.
- * @return An iterator to the given entity if it's found, past the end
- * iterator otherwise.
- */
- [[nodiscard]] iterator find(const entity_type entt) const noexcept {
- return *this ? handle().find(entt) : iterator{};
- }
- /**
- * @brief Returns the identifier that occupies the given position.
- * @param pos Position of the element to return.
- * @return The identifier that occupies the given position.
- */
- [[nodiscard]] entity_type operator[](const size_type pos) const {
- return begin()[pos];
- }
- /**
- * @brief Checks if a group is properly initialized.
- * @return True if the group is properly initialized, false otherwise.
- */
- [[nodiscard]] explicit operator bool() const noexcept {
- return descriptor != nullptr;
- }
- /**
- * @brief Checks if a group contains an entity.
- * @param entt A valid identifier.
- * @return True if the group contains the given entity, false otherwise.
- */
- [[nodiscard]] bool contains(const entity_type entt) const noexcept {
- return *this && handle().contains(entt);
- }
- /**
- * @brief Returns the components assigned to the given entity.
- * @tparam Type Type of the component to get.
- * @tparam Other Other types of components to get.
- * @param entt A valid identifier.
- * @return The components assigned to the entity.
- */
- template<typename Type, typename... Other>
- [[nodiscard]] decltype(auto) get(const entity_type entt) const {
- return get<index_of<Type>, index_of<Other>...>(entt);
- }
- /**
- * @brief Returns the components assigned to the given entity.
- * @tparam Index Indexes of the components to get.
- * @param entt A valid identifier.
- * @return The components assigned to the entity.
- */
- template<std::size_t... Index>
- [[nodiscard]] decltype(auto) get(const entity_type entt) const {
- const auto cpools = pools();
- if constexpr(sizeof...(Index) == 0) {
- return std::apply([entt](auto *...curr) { return std::tuple_cat(curr->get_as_tuple(entt)...); }, cpools);
- } else if constexpr(sizeof...(Index) == 1) {
- return (std::get<Index>(cpools)->get(entt), ...);
- } else {
- return std::tuple_cat(std::get<Index>(cpools)->get_as_tuple(entt)...);
- }
- }
- /**
- * @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 non-empty 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, Type &...);
- * void(Type &...);
- * @endcode
- *
- * @note
- * Empty types aren't explicitly instantiated and therefore they are never
- * returned during iterations.
- *
- * @tparam Func Type of the function object to invoke.
- * @param func A valid function object.
- */
- template<typename Func>
- void each(Func func) const {
- for(const auto entt: *this) {
- if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_group>().get({})))>) {
- std::apply(func, std::tuple_cat(std::make_tuple(entt), get(entt)));
- } else {
- std::apply(func, get(entt));
- }
- }
- }
- /**
- * @brief Returns an iterable object to use to _visit_ a group.
- *
- * The iterable object returns tuples that contain the current entity and a
- * set of references to its non-empty components. The _constness_ of the
- * components is as requested.
- *
- * @note
- * Empty types aren't explicitly instantiated and therefore they are never
- * returned during iterations.
- *
- * @return An iterable object to use to _visit_ the group.
- */
- [[nodiscard]] iterable each() const noexcept {
- const auto cpools = pools();
- return iterable{{begin(), cpools}, {end(), cpools}};
- }
- /**
- * @brief Sort a group according to the given comparison function.
- *
- * The comparison function object must return `true` if the first element
- * is _less_ than the second one, `false` otherwise. The signature of the
- * comparison function should be equivalent to one of the following:
- *
- * @code{.cpp}
- * bool(std::tuple<Type &...>, std::tuple<Type &...>);
- * bool(const Type &..., const Type &...);
- * bool(const Entity, const Entity);
- * @endcode
- *
- * Where `Type` are such that they are iterated by the group.<br/>
- * Moreover, the comparison function object shall induce a
- * _strict weak ordering_ on the values.
- *
- * The sort function object must offer a member function template
- * `operator()` that accepts three arguments:
- *
- * * An iterator to the first element of the range to sort.
- * * An iterator past the last element of the range to sort.
- * * A comparison function to use to compare the elements.
- *
- * @tparam Type Optional type of component to compare.
- * @tparam Other Other optional types of components to compare.
- * @tparam Compare Type of comparison function object.
- * @tparam Sort Type of sort function object.
- * @tparam Args Types of arguments to forward to the sort function object.
- * @param compare A valid comparison function object.
- * @param algo A valid sort function object.
- * @param args Arguments to forward to the sort function object, if any.
- */
- template<typename Type, typename... Other, typename Compare, typename Sort = std_sort, typename... Args>
- void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
- sort<index_of<Type>, index_of<Other>...>(std::move(compare), std::move(algo), std::forward<Args>(args)...);
- }
- /**
- * @brief Sort a group according to the given comparison function.
- *
- * @sa sort
- *
- * @tparam Index Optional indexes of components to compare.
- * @tparam Compare Type of comparison function object.
- * @tparam Sort Type of sort function object.
- * @tparam Args Types of arguments to forward to the sort function object.
- * @param compare A valid comparison function object.
- * @param algo A valid sort function object.
- * @param args Arguments to forward to the sort function object, if any.
- */
- template<std::size_t... Index, typename Compare, typename Sort = std_sort, typename... Args>
- void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
- if(*this) {
- if constexpr(sizeof...(Index) == 0) {
- static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
- descriptor->handle().sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
- } else {
- auto comp = [&compare, cpools = pools()](const entity_type lhs, const entity_type rhs) {
- if constexpr(sizeof...(Index) == 1) {
- return compare((std::get<Index>(cpools)->get(lhs), ...), (std::get<Index>(cpools)->get(rhs), ...));
- } else {
- return compare(std::forward_as_tuple(std::get<Index>(cpools)->get(lhs)...), std::forward_as_tuple(std::get<Index>(cpools)->get(rhs)...));
- }
- };
- descriptor->handle().sort(std::move(comp), std::move(algo), std::forward<Args>(args)...);
- }
- }
- }
- /**
- * @brief Sort entities according to their order in a range.
- *
- * The shared pool of entities and thus its order is affected by the changes
- * to each and every pool that it tracks.
- *
- * @tparam It Type of input iterator.
- * @param first An iterator to the first element of the range of entities.
- * @param last An iterator past the last element of the range of entities.
- */
- template<typename It>
- void sort_as(It first, It last) const {
- if(*this) {
- descriptor->handle().sort_as(first, last);
- }
- }
- /**
- * @brief Sort entities according to their order in a range.
- * @param other The storage to use to impose the order.
- */
- [[deprecated("use iterator based sort_as instead")]] void sort_as(const common_type &other) const {
- sort_as(other.begin(), other.end());
- }
- private:
- handler *descriptor;
- };
- /**
- * @brief Owning group.
- *
- * Owning groups returns all entities and only the entities that are at
- * least in the given storage. Moreover:
- *
- * * It's guaranteed that the entity list is tightly packed in memory for fast
- * iterations.
- * * It's guaranteed that all components in the owned storage are tightly packed
- * in memory for even faster iterations and to allow direct access.
- * * They stay true to the order of the owned storage and all instances have the
- * same order in memory.
- *
- * The more types of storage are owned, the faster it is to iterate a group.
- *
- * @b Important
- *
- * Iterators aren't invalidated if:
- *
- * * New elements are added to the storage.
- * * The entity currently pointed is modified (for example, components are added
- * or removed from it).
- * * The entity currently pointed is destroyed.
- *
- * In all other cases, modifying the pools iterated by the group in any way
- * invalidates all the iterators.
- *
- * @tparam Owned Types of storage _owned_ by the group.
- * @tparam Get Types of storage _observed_ by the group.
- * @tparam Exclude Types of storage used to filter the group.
- */
- template<typename... Owned, typename... Get, typename... Exclude>
- class basic_group<owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>> {
- using base_type = std::common_type_t<typename Owned::base_type..., typename Get::base_type..., typename Exclude::base_type...>;
- using underlying_type = typename base_type::entity_type;
- template<typename Type>
- static constexpr std::size_t index_of = type_list_index_v<std::remove_const_t<Type>, type_list<typename Owned::value_type..., typename Get::value_type..., typename Exclude::value_type...>>;
- auto pools() const noexcept {
- using return_type = std::tuple<Owned *..., Get *...>;
- return descriptor ? descriptor->template pools_as<return_type>() : return_type{};
- }
- auto filter() const noexcept {
- using return_type = std::tuple<Exclude *...>;
- return descriptor ? descriptor->template filter_as<return_type>() : return_type{};
- }
- public:
- /*! @brief Underlying entity identifier. */
- using entity_type = underlying_type;
- /*! @brief Unsigned integer type. */
- using size_type = std::size_t;
- /*! @brief Common type among all storage types. */
- using common_type = base_type;
- /*! @brief Random access iterator type. */
- using iterator = typename common_type::iterator;
- /*! @brief Reversed iterator type. */
- using reverse_iterator = typename common_type::reverse_iterator;
- /*! @brief Iterable group type. */
- using iterable = iterable_adaptor<internal::extended_group_iterator<iterator, owned_t<Owned...>, get_t<Get...>>>;
- /*! @brief Group handler type. */
- using handler = internal::group_handler<owned_t<std::remove_const_t<Owned>...>, get_t<std::remove_const_t<Get>...>, exclude_t<std::remove_const_t<Exclude>...>>;
- /*! @brief Default constructor to use to create empty, invalid groups. */
- basic_group() noexcept
- : descriptor{} {}
- /**
- * @brief Constructs a group from a set of storage classes.
- * @param ref A reference to a group handler.
- */
- basic_group(handler &ref) noexcept
- : descriptor{&ref} {}
- /**
- * @brief Returns the leading storage of a group.
- * @return The leading storage of the group.
- */
- [[nodiscard]] const common_type &handle() const noexcept {
- return *storage<0>();
- }
- /**
- * @brief Returns the storage for a given component type, if any.
- * @tparam Type Type of component of which to return the storage.
- * @return The storage for the given component type.
- */
- template<typename Type>
- [[nodiscard]] auto *storage() const noexcept {
- return storage<index_of<Type>>();
- }
- /**
- * @brief Returns the storage for a given index, if any.
- * @tparam Index Index of the storage to return.
- * @return The storage for the given index.
- */
- template<std::size_t Index>
- [[nodiscard]] auto *storage() const noexcept {
- constexpr auto offset = sizeof...(Owned) + sizeof...(Get);
- if constexpr(Index < offset) {
- return std::get<Index>(pools());
- } else {
- return std::get<Index - offset>(filter());
- }
- }
- /**
- * @brief Returns the number of entities that that are part of the group.
- * @return Number of entities that that are part of the group.
- */
- [[nodiscard]] size_type size() const noexcept {
- return *this ? descriptor->length() : size_type{};
- }
- /**
- * @brief Checks whether a group is empty.
- * @return True if the group is empty, false otherwise.
- */
- [[nodiscard]] bool empty() const noexcept {
- return !*this || !descriptor->length();
- }
- /**
- * @brief Returns an iterator to the first entity of the group.
- *
- * If the group is empty, the returned iterator will be equal to `end()`.
- *
- * @return An iterator to the first entity of the group.
- */
- [[nodiscard]] iterator begin() const noexcept {
- return *this ? (handle().end() - descriptor->length()) : iterator{};
- }
- /**
- * @brief Returns an iterator that is past the last entity of the group.
- * @return An iterator to the entity following the last entity of the
- * group.
- */
- [[nodiscard]] iterator end() const noexcept {
- return *this ? handle().end() : iterator{};
- }
- /**
- * @brief Returns an iterator to the first entity of the reversed group.
- *
- * If the group is empty, the returned iterator will be equal to `rend()`.
- *
- * @return An iterator to the first entity of the reversed group.
- */
- [[nodiscard]] reverse_iterator rbegin() const noexcept {
- return *this ? handle().rbegin() : reverse_iterator{};
- }
- /**
- * @brief Returns an iterator that is past the last entity of the reversed
- * group.
- * @return An iterator to the entity following the last entity of the
- * reversed group.
- */
- [[nodiscard]] reverse_iterator rend() const noexcept {
- return *this ? (handle().rbegin() + descriptor->length()) : reverse_iterator{};
- }
- /**
- * @brief Returns the first entity of the group, if any.
- * @return The first entity of the group if one exists, the null entity
- * otherwise.
- */
- [[nodiscard]] entity_type front() const noexcept {
- const auto it = begin();
- return it != end() ? *it : null;
- }
- /**
- * @brief Returns the last entity of the group, if any.
- * @return The last entity of the group if one exists, the null entity
- * otherwise.
- */
- [[nodiscard]] entity_type back() const noexcept {
- const auto it = rbegin();
- return it != rend() ? *it : null;
- }
- /**
- * @brief Finds an entity.
- * @param entt A valid identifier.
- * @return An iterator to the given entity if it's found, past the end
- * iterator otherwise.
- */
- [[nodiscard]] iterator find(const entity_type entt) const noexcept {
- const auto it = *this ? handle().find(entt) : iterator{};
- return it >= begin() ? it : iterator{};
- }
- /**
- * @brief Returns the identifier that occupies the given position.
- * @param pos Position of the element to return.
- * @return The identifier that occupies the given position.
- */
- [[nodiscard]] entity_type operator[](const size_type pos) const {
- return begin()[pos];
- }
- /**
- * @brief Checks if a group is properly initialized.
- * @return True if the group is properly initialized, false otherwise.
- */
- [[nodiscard]] explicit operator bool() const noexcept {
- return descriptor != nullptr;
- }
- /**
- * @brief Checks if a group contains an entity.
- * @param entt A valid identifier.
- * @return True if the group contains the given entity, false otherwise.
- */
- [[nodiscard]] bool contains(const entity_type entt) const noexcept {
- return *this && handle().contains(entt) && (handle().index(entt) < (descriptor->length()));
- }
- /**
- * @brief Returns the components assigned to the given entity.
- * @tparam Type Type of the component to get.
- * @tparam Other Other types of components to get.
- * @param entt A valid identifier.
- * @return The components assigned to the entity.
- */
- template<typename Type, typename... Other>
- [[nodiscard]] decltype(auto) get(const entity_type entt) const {
- return get<index_of<Type>, index_of<Other>...>(entt);
- }
- /**
- * @brief Returns the components assigned to the given entity.
- * @tparam Index Indexes of the components to get.
- * @param entt A valid identifier.
- * @return The components assigned to the entity.
- */
- template<std::size_t... Index>
- [[nodiscard]] decltype(auto) get(const entity_type entt) const {
- const auto cpools = pools();
- if constexpr(sizeof...(Index) == 0) {
- return std::apply([entt](auto *...curr) { return std::tuple_cat(curr->get_as_tuple(entt)...); }, cpools);
- } else if constexpr(sizeof...(Index) == 1) {
- return (std::get<Index>(cpools)->get(entt), ...);
- } else {
- return std::tuple_cat(std::get<Index>(cpools)->get_as_tuple(entt)...);
- }
- }
- /**
- * @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 non-empty 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, Type &...);
- * void(Type &...);
- * @endcode
- *
- * @note
- * Empty types aren't explicitly instantiated and therefore they are never
- * returned during iterations.
- *
- * @tparam Func Type of the function object to invoke.
- * @param func A valid function object.
- */
- template<typename Func>
- void each(Func func) const {
- for(auto args: each()) {
- if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_group>().get({})))>) {
- std::apply(func, args);
- } else {
- std::apply([&func](auto, auto &&...less) { func(std::forward<decltype(less)>(less)...); }, args);
- }
- }
- }
- /**
- * @brief Returns an iterable object to use to _visit_ a group.
- *
- * The iterable object returns tuples that contain the current entity and a
- * set of references to its non-empty components. The _constness_ of the
- * components is as requested.
- *
- * @note
- * Empty types aren't explicitly instantiated and therefore they are never
- * returned during iterations.
- *
- * @return An iterable object to use to _visit_ the group.
- */
- [[nodiscard]] iterable each() const noexcept {
- const auto cpools = pools();
- return {{begin(), cpools}, {end(), cpools}};
- }
- /**
- * @brief Sort a group according to the given comparison function.
- *
- * The comparison function object must return `true` if the first element
- * is _less_ than the second one, `false` otherwise. The signature of the
- * comparison function should be equivalent to one of the following:
- *
- * @code{.cpp}
- * bool(std::tuple<Type &...>, std::tuple<Type &...>);
- * bool(const Type &, const Type &);
- * bool(const Entity, const Entity);
- * @endcode
- *
- * Where `Type` are either owned types or not but still such that they are
- * iterated by the group.<br/>
- * Moreover, the comparison function object shall induce a
- * _strict weak ordering_ on the values.
- *
- * The sort function object must offer a member function template
- * `operator()` that accepts three arguments:
- *
- * * An iterator to the first element of the range to sort.
- * * An iterator past the last element of the range to sort.
- * * A comparison function to use to compare the elements.
- *
- * @tparam Type Optional type of component to compare.
- * @tparam Other Other optional types of components to compare.
- * @tparam Compare Type of comparison function object.
- * @tparam Sort Type of sort function object.
- * @tparam Args Types of arguments to forward to the sort function object.
- * @param compare A valid comparison function object.
- * @param algo A valid sort function object.
- * @param args Arguments to forward to the sort function object, if any.
- */
- template<typename Type, typename... Other, typename Compare, typename Sort = std_sort, typename... Args>
- void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const {
- sort<index_of<Type>, index_of<Other>...>(std::move(compare), std::move(algo), std::forward<Args>(args)...);
- }
- /**
- * @brief Sort a group according to the given comparison function.
- *
- * @sa sort
- *
- * @tparam Index Optional indexes of components to compare.
- * @tparam Compare Type of comparison function object.
- * @tparam Sort Type of sort function object.
- * @tparam Args Types of arguments to forward to the sort function object.
- * @param compare A valid comparison function object.
- * @param algo A valid sort function object.
- * @param args Arguments to forward to the sort function object, if any.
- */
- template<std::size_t... Index, typename Compare, typename Sort = std_sort, typename... Args>
- void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const {
- const auto cpools = pools();
- if constexpr(sizeof...(Index) == 0) {
- static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
- storage<0>()->sort_n(descriptor->length(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
- } else {
- auto comp = [&compare, &cpools](const entity_type lhs, const entity_type rhs) {
- if constexpr(sizeof...(Index) == 1) {
- return compare((std::get<Index>(cpools)->get(lhs), ...), (std::get<Index>(cpools)->get(rhs), ...));
- } else {
- return compare(std::forward_as_tuple(std::get<Index>(cpools)->get(lhs)...), std::forward_as_tuple(std::get<Index>(cpools)->get(rhs)...));
- }
- };
- storage<0>()->sort_n(descriptor->length(), std::move(comp), std::move(algo), std::forward<Args>(args)...);
- }
- auto cb = [this](auto *head, auto *...other) {
- for(auto next = descriptor->length(); next; --next) {
- const auto pos = next - 1;
- [[maybe_unused]] const auto entt = head->data()[pos];
- (other->swap_elements(other->data()[pos], entt), ...);
- }
- };
- std::apply(cb, cpools);
- }
- private:
- handler *descriptor;
- };
- } // namespace entt
- #endif
|