diff --git a/TODO b/TODO index 665686acf..a512b9750 100644 --- a/TODO +++ b/TODO @@ -16,5 +16,7 @@ * can we write a bool conv func for entt::entity that silently compares it to null? * reset... reset everywhere... * document undocumented parts (entt::overload and a few others) +* range-assign cannot return iterators (eg group or sort-like listeners, see #386) * any-of rule for views/groups (eg entity has A and any of B/C/D) - get -> all, exclude -> none +* registry::clear should clear entities etc diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index e22fda308..0dfa932ce 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -88,7 +88,7 @@ class basic_registry { template void remove(basic_registry &owner, It first, It last) { - if(std::distance(first, last) == this->size()) { + if(std::distance(first, last) == std::distance(this->begin(), this->end())) { if(!destruction.empty()) { std::for_each(first, last, [this, &owner](const auto entt) { destruction.publish(entt, owner); }); } @@ -484,20 +484,6 @@ public: return entt; } - /** - * @brief Assigns each element in a range an entity. - * - * @sa create - * - * @tparam It Type of forward iterator. - * @param first An iterator to the first element of the range to generate. - * @param last An iterator past the last element of the range to generate. - */ - template - void create(It first, It last) { - std::generate(first, last, [this]() { return create(); }); - } - /** * @brief Creates a new entity and returns it. * @@ -534,6 +520,20 @@ public: return entt; } + /** + * @brief Assigns each element in a range an entity. + * + * @sa create + * + * @tparam It Type of forward iterator. + * @param first An iterator to the first element of the range to generate. + * @param last An iterator past the last element of the range to generate. + */ + template + void create(It first, It last) { + std::generate(first, last, [this]() { return create(); }); + } + /** * @brief Destroys an entity and lets the registry recycle the identifier. * @@ -1250,7 +1250,7 @@ public: } if(!handler) { - group_data gdata = { + group_data candidate = { size, { new handler_type{}, [](void *instance) { delete static_cast(instance); } }, [](const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, @@ -1258,13 +1258,13 @@ public: [](const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info::id()) || ...); }, }; - handler = static_cast(gdata.group.get()); + handler = static_cast(candidate.group.get()); const void *maybe_valid_if = nullptr; const void *discard_if = nullptr; if constexpr(sizeof...(Owned) == 0) { - groups.push_back(std::move(gdata)); + groups.push_back(std::move(candidate)); } else { ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [size](const auto &gdata) { const auto overlapping = (0u + ... + gdata.owned(type_info>::id())); @@ -1282,7 +1282,7 @@ public: maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->group.get()); discard_if = (prev == groups.crend() ? discard_if : prev->group.get()); - groups.insert(next, std::move(gdata)); + groups.insert(next, std::move(candidate)); } ((std::get> &>(cpools).super = std::max(std::get> &>(cpools).super, size)), ...); @@ -1442,11 +1442,11 @@ public: other.entities = entities; if constexpr(sizeof...(Component) == 0) { - for(size_type pos{}; pos < pools.size(); ++pos) { - if(const auto &pdata = pools[pos]; pdata.assure && ((pdata.type_id != type_info::id()) && ...)) { + std::for_each(pools.cbegin(), pools.cend(), [&other](auto &&pdata) { + if(pdata.assure && ((pdata.type_id != type_info::id()) && ...)) { pdata.assure(other, *pdata.pool); } - } + }); } else { static_assert(sizeof...(Exclude) == 0 && std::conjunction_v...>); (other.assure(assure()), ...); @@ -1479,12 +1479,12 @@ public: */ template void stamp(const entity_type dst, const basic_registry &other, const entity_type src, exclude_t = {}) { - for(size_type pos{}; pos < other.pools.size(); ++pos) { - if(const auto &pdata = other.pools[pos]; ((pdata.type_id != type_info::id()) && ...) && pdata.pool->has(src)) { + std::for_each(other.pools.cbegin(), other.pools.cend(), [this, dst, src](auto &&pdata) { + if(((pdata.type_id != type_info::id()) && ...) && pdata.pool->has(src)) { ENTT_ASSERT(pdata.stamp); pdata.stamp(*this, dst, *pdata.pool, src); } - } + }); } /** diff --git a/test/entt/entity/view.cpp b/test/entt/entity/view.cpp index 1fe93b58d..5f51c3858 100644 --- a/test/entt/entity/view.cpp +++ b/test/entt/entity/view.cpp @@ -193,9 +193,9 @@ TEST(SingleComponentView, Find) { TEST(SingleComponentView, Less) { entt::registry registry; auto create = [&](auto... component) { - const auto entity = registry.create(); - (registry.assign(entity, component), ...); - return entity; + const auto entt = registry.create(); + (registry.assign(entt, component), ...); + return entt; }; const auto entity = create(0, entt::tag<"empty"_hs>{});