From d68c90a42942436d4db9be7c787ba0997222a6ad Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 24 Jan 2019 23:57:21 +0100 Subject: [PATCH] removed registry ::pool/::managed --- src/entt/entity/registry.hpp | 102 ++++++++++++----------------------- 1 file changed, 35 insertions(+), 67 deletions(-) diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 2102cb069..9076a83da 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -102,14 +102,14 @@ class registry { template struct handler_pool, type_list>: sparse_set::size_type, sizeof...(Component)>> { void candidate(registry ®, const Entity entity) { - if((reg.pool().has(entity) && ...) && !(reg.pool().has(entity) || ...)) { + if((reg.assure().has(entity) && ...) && !(reg.assure().has(entity) || ...)) { handler_pool::construct(entity, reg.pools[component_family::type]->get(entity)...); } } template void check(registry ®, const Entity entity) { - const sparse_set &cpool = reg.pool(); + const sparse_set &cpool = reg.assure(); const auto last = *cpool.begin(); if(handler_pool::has(last)) { @@ -147,31 +147,14 @@ class registry { template void connect(handler_pool, type_list> *handler, std::index_sequence) { using handler_type = handler_pool, type_list>; - (pool().construction().template connect<&handler_type::candidate>(handler), ...); - (pool().construction().template connect<&handler_type::discard>(handler), ...); - (pool().destruction().template connect<&handler_type::template check>(handler), ...); - (pool().destruction().template connect<&handler_type::candidate>(handler), ...); + (assure().construction().template connect<&handler_type::candidate>(handler), ...); + (assure().construction().template connect<&handler_type::discard>(handler), ...); + (assure().destruction().template connect<&handler_type::template check>(handler), ...); + (assure().destruction().template connect<&handler_type::candidate>(handler), ...); } template - inline bool managed() const ENTT_NOEXCEPT { - const auto ctype = component_family::type; - return ctype < pools.size() && pools[ctype]; - } - - template - inline const auto & pool() const ENTT_NOEXCEPT { - assert(managed()); - return static_cast> &>(*pools[component_family::type]); - } - - template - inline auto & pool() ENTT_NOEXCEPT { - return const_cast> &>(std::as_const(*this).template pool()); - } - - template - void assure() { + auto & assure() const { const auto ctype = component_family::type; if(!(ctype < pools.size())) { @@ -181,6 +164,8 @@ class registry { if(!pools[ctype]) { pools[ctype] = std::make_unique>>(const_cast(this)); } + + return static_cast> &>(*pools[ctype]); } public: @@ -232,7 +217,7 @@ public: */ template size_type size() const ENTT_NOEXCEPT { - return managed() ? pool().size() : size_type{}; + return assure().size(); } /** @@ -262,8 +247,7 @@ public: */ template void reserve(const size_type cap) { - assure(); - pool().reserve(cap); + assure().reserve(cap); } /** @@ -285,7 +269,7 @@ public: */ template size_type capacity() const ENTT_NOEXCEPT { - return managed() ? pool().capacity() : size_type{}; + return assure().capacity(); } /** @@ -305,7 +289,7 @@ public: */ template bool empty() const ENTT_NOEXCEPT { - return !managed() || pool().empty(); + return assure().empty(); } /** @@ -336,7 +320,7 @@ public: */ template std::add_const_t * raw() const ENTT_NOEXCEPT { - return managed() ? pool().raw() : nullptr; + return assure().raw(); } /*! @copydoc raw */ @@ -361,7 +345,7 @@ public: */ template const entity_type * data() const ENTT_NOEXCEPT { - return managed() ? pool().data() : nullptr; + return assure().data(); } /** @@ -602,8 +586,7 @@ public: template Component & assign(const entity_type entity, Args &&... args) { assert(valid(entity)); - assure(); - return pool().construct(entity, std::forward(args)...); + return assure().construct(entity, std::forward(args)...); } /** @@ -622,8 +605,7 @@ public: template void remove(const entity_type entity) { assert(valid(entity)); - assert(managed()); - pool().destroy(entity); + assure().destroy(entity); } /** @@ -641,7 +623,7 @@ public: template bool has(const entity_type entity) const ENTT_NOEXCEPT { assert(valid(entity)); - return ((managed() && pool().has(entity)) && ...); + return (assure().has(entity) && ...); } /** @@ -661,10 +643,9 @@ public: template decltype(auto) get([[maybe_unused]] const entity_type entity) const ENTT_NOEXCEPT { assert(valid(entity)); - assert((managed() && ...)); if constexpr(sizeof...(Component) == 1) { - return pool().get(entity); + return assure().get(entity); } else { return std::tuple &...>{get(entity)...}; } @@ -706,8 +687,7 @@ public: template Component & get(const entity_type entity, Component &&component) ENTT_NOEXCEPT { assert(valid(entity)); - assure(); - auto &cpool = pool(); + auto &cpool = assure(); auto *comp = cpool.try_get(entity); return comp ? *comp : cpool.construct(entity, std::forward(component)); } @@ -729,7 +709,7 @@ public: assert(valid(entity)); if constexpr(sizeof...(Component) == 1) { - return managed() ? pool().try_get(entity) : nullptr; + return assure().try_get(entity); } else { return std::tuple *...>{try_get(entity)...}; } @@ -767,7 +747,7 @@ public: */ template Component & replace(const entity_type entity, Args &&... args) { - return (pool().get(entity) = std::decay_t{std::forward(args)...}); + return (assure().get(entity) = std::decay_t{std::forward(args)...}); } /** @@ -798,8 +778,7 @@ public: */ template Component & assign_or_replace(const entity_type entity, Args &&... args) { - assure(); - auto &cpool = pool(); + auto &cpool = assure(); return cpool.has(entity) ? cpool.get(entity) = std::decay_t{std::forward(args)...} @@ -831,8 +810,7 @@ public: */ template sink_type construction() ENTT_NOEXCEPT { - assure(); - return pool().construction(); + return assure().construction(); } /** @@ -860,8 +838,7 @@ public: */ template sink_type destruction() ENTT_NOEXCEPT { - assure(); - return pool().destruction(); + return assure().destruction(); } /** @@ -907,8 +884,7 @@ public: */ template void sort(Compare compare, Sort sort = Sort{}, Args &&... args) { - assure(); - pool().sort(std::move(compare), std::move(sort), std::forward(args)...); + assure().sort(std::move(compare), std::move(sort), std::forward(args)...); invalidate.publish(*this, component_family::type); } @@ -944,9 +920,7 @@ public: */ template void sort() { - assure(); - assure(); - pool().respect(pool()); + assure().respect(assure()); invalidate.publish(*this, component_family::type); } @@ -967,8 +941,7 @@ public: template void reset(const entity_type entity) { assert(valid(entity)); - assure(); - auto &cpool = pool(); + auto &cpool = assure(); if(cpool.has(entity)) { cpool.destroy(entity); @@ -985,10 +958,9 @@ public: */ template void reset() { - assure(); - auto &cpool = pool(); + sparse_set &cpool = assure(); - for(const auto entity: static_cast &>(cpool)) { + for(const auto entity: cpool) { cpool.destroy(entity); } } @@ -1131,8 +1103,7 @@ public: */ template entt::view view() { - (assure(), ...); - return { &pool()... }; + return { &assure()... }; } /*! @copydoc view */ @@ -1190,16 +1161,13 @@ public: } if(!handlers[htype]) { - (assure(), ...); - (assure(), ...); - auto handler = std::make_unique(); connect(handler.get(), std::make_index_sequence{}); invalidate.sink().template connect<&handler_type::rebuild>(handler.get()); for(const auto entity: view()) { - if(!(pool().has(entity) || ...)) { + if(!(assure().has(entity) || ...)) { handler->construct(entity, pools[component_family::type]->get(entity)...); } } @@ -1209,7 +1177,7 @@ public: return { static_cast(handlers[htype].get()), - &pool()... + &assure()... }; } @@ -1288,7 +1256,7 @@ public: if(sizeof...(Component)) { static_assert(std::conjunction_v...>); - ((other.pools[component_family::type] = managed() ? pool().clone() : nullptr), ...); + ((other.pools[component_family::type] = assure().clone()), ...); } else { for(auto pos = pools.size(); pos; --pos) { auto &cpool = pools[pos-1]; @@ -1376,7 +1344,7 @@ public: private: std::vector>> handlers; - std::vector>> pools; + mutable std::vector>> pools; std::vector entities; size_type available{}; entity_type next{};