diff --git a/src/entt/entity/observer.hpp b/src/entt/entity/observer.hpp index a7ab5ee84..a7ca97635 100644 --- a/src/entt/entity/observer.hpp +++ b/src/entt/entity/observer.hpp @@ -177,8 +177,8 @@ class basic_observer { template struct matcher_handler, type_list, AnyOf>> { template - static void maybe_valid_if(basic_observer &obs, const basic_registry ®, const Entity entt) { - if(reg.template has(entt) && !reg.template any(entt)) { + static void maybe_valid_if(basic_observer &obs, const Entity entt) { + if(obs.target->template has(entt) && !obs.target->template any(entt)) { if(auto *comp = obs.view.try_get(entt); !comp) { obs.view.emplace(entt); } @@ -188,7 +188,7 @@ class basic_observer { } template - static void discard_if(basic_observer &obs, const basic_registry &, const Entity entt) { + static void discard_if(basic_observer &obs, const Entity entt) { if(auto *value = obs.view.try_get(entt); value && !(*value &= (~(1 << Index)))) { obs.view.erase(entt); } @@ -213,12 +213,12 @@ class basic_observer { template struct matcher_handler, type_list, type_list, AllOf...>> { template - static void maybe_valid_if(basic_observer &obs, const basic_registry ®, const Entity entt) { - if([®, entt]() { + static void maybe_valid_if(basic_observer &obs, const Entity entt) { + if([&obs, entt]() { if constexpr(sizeof...(Ignore) == 0) { - return reg.template has(entt) && !reg.template any(entt); + return obs.target->template has(entt) && !obs.target->template any(entt); } else { - return reg.template has(entt) && ((std::is_same_v || !reg.template any(entt)) && ...) && !reg.template any(entt); + return obs.target->template has(entt) && ((std::is_same_v || !obs.target->template any(entt)) && ...) && !obs.target->template any(entt); } }()) { @@ -231,7 +231,7 @@ class basic_observer { } template - static void discard_if(basic_observer &obs, const basic_registry &, const Entity entt) { + static void discard_if(basic_observer &obs, const Entity entt) { if(auto *value = obs.view.try_get(entt); value && !(*value &= (~(1 << Index)))) { obs.view.erase(entt); } diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 6c2dd9e04..f19a973bd 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -60,9 +60,9 @@ class basic_registry { } template - decltype(auto) emplace(basic_registry &owner, const Entity entt, Args &&... args) { + decltype(auto) emplace(const Entity entt, Args &&... args) { storage::emplace(entt, std::forward(args)...); - construction.publish(owner, entt); + construction.publish(entt); if constexpr(!is_eto_eligible_v) { return this->get(entt); @@ -70,57 +70,57 @@ class basic_registry { } template - void insert(basic_registry &owner, It first, It last, Args &&... args) { + void insert(It first, It last, Args &&... args) { storage::insert(first, last, std::forward(args)...); if(!construction.empty()) { - while(first != last) { construction.publish(owner, *(first++)); } + while(first != last) { construction.publish(*(first++)); } } } - void remove(basic_registry &owner, const Entity entt) { - destruction.publish(owner, entt); + void remove(const Entity entt) { + destruction.publish(entt); this->erase(entt); } template - void remove(basic_registry &owner, It first, It last) { + void remove(It first, It last) { if(std::distance(first, last) == std::distance(this->begin(), this->end())) { if(!destruction.empty()) { - while(first != last) { destruction.publish(owner, *(first++)); } + while(first != last) { destruction.publish(*(first++)); } } this->clear(); } else { - while(first != last) { this->remove(owner, *(first++)); } + while(first != last) { this->remove(*(first++)); } } } template - decltype(auto) patch(basic_registry &owner, const Entity entt, [[maybe_unused]] Func &&... func) { + decltype(auto) patch(const Entity entt, [[maybe_unused]] Func &&... func) { if constexpr(is_eto_eligible_v) { - update.publish(owner, entt); + update.publish(entt); } else { (std::forward(func)(this->get(entt)), ...); - update.publish(owner, entt); + update.publish(entt); return this->get(entt); } } - decltype(auto) replace(basic_registry &owner, const Entity entt, Component component) { - return patch(owner, entt, [&component](auto &&curr) { curr = std::move(component); }); + decltype(auto) replace(const Entity entt, Component component) { + return patch(entt, [&component](auto &&curr) { curr = std::move(component); }); } private: - sigh construction{}; - sigh destruction{}; - sigh update{}; + sigh construction{}; + sigh destruction{}; + sigh update{}; }; struct pool_data { id_type type_id{}; std::unique_ptr> pool{}; - void(* remove)(sparse_set &, basic_registry &, const Entity){}; + void(* remove)(sparse_set &, const Entity){}; }; template @@ -130,14 +130,19 @@ class basic_registry { struct group_handler, get_t, Owned...> { static_assert(std::conjunction_v>..., std::is_same>..., std::is_same>...>, "One or more component types are invalid"); std::conditional_t, std::size_t> current{}; + basic_registry *owner; + + group_handler(basic_registry &parent) + : owner{&parent} + {} template - void maybe_valid_if(basic_registry &owner, const Entity entt) { - [[maybe_unused]] const auto cpools = std::forward_as_tuple(owner.assure()...); + void maybe_valid_if(const Entity entt) { + [[maybe_unused]] const auto cpools = std::forward_as_tuple(owner->assure()...); const auto is_valid = ((std::is_same_v || std::get &>(cpools).contains(entt)) && ...) - && ((std::is_same_v || owner.assure().contains(entt)) && ...) - && ((std::is_same_v || !owner.assure().contains(entt)) && ...); + && ((std::is_same_v || owner->assure().contains(entt)) && ...) + && ((std::is_same_v || !owner->assure().contains(entt)) && ...); if constexpr(sizeof...(Owned) == 0) { if(is_valid && !current.contains(entt)) { @@ -151,13 +156,13 @@ class basic_registry { } } - void discard_if([[maybe_unused]] basic_registry &owner, const Entity entt) { + void discard_if(const Entity entt) { if constexpr(sizeof...(Owned) == 0) { if(current.contains(entt)) { current.erase(entt); } } else { - if(const auto cpools = std::forward_as_tuple(owner.assure()...); std::get<0>(cpools).contains(entt) && (std::get<0>(cpools).index(entt) < current)) { + if(const auto cpools = std::forward_as_tuple(owner->assure()...); std::get<0>(cpools).contains(entt) && (std::get<0>(cpools).index(entt) < current)) { const auto pos = --current; (std::get &>(cpools).swap(std::get &>(cpools).data()[pos], entt), ...); } @@ -192,8 +197,8 @@ class basic_registry { if(auto &&pdata = pools[index]; !pdata.pool) { pdata.type_id = type_info::id(); pdata.pool.reset(new pool_handler()); - pdata.remove = [](sparse_set &target, basic_registry &owner, const entity_type entt) { - static_cast &>(target).remove(owner, entt); + pdata.remove = [](sparse_set &target, const entity_type entt) { + static_cast &>(target).remove(entt); }; } @@ -203,8 +208,8 @@ class basic_registry { cpool = pools.emplace_back(pool_data{ type_info::id(), std::unique_ptr>{new pool_handler()}, - [](sparse_set &target, basic_registry &owner, const entity_type entt) { - static_cast &>(target).remove(owner, entt); + [](sparse_set &target, const entity_type entt) { + static_cast &>(target).remove(entt); } }).pool.get(); } else { @@ -644,7 +649,7 @@ public: template decltype(auto) emplace(const entity_type entity, Args &&... args) { ENTT_ASSERT(valid(entity)); - return assure().emplace(*this, entity, std::forward(args)...); + return assure().emplace(entity, std::forward(args)...); } /** @@ -661,7 +666,7 @@ public: template void insert(It first, It last, const Component &value = {}) { ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); })); - assure().insert(*this, first, last, value); + assure().insert(first, last, value); } /** @@ -681,7 +686,7 @@ public: void insert(EIt first, EIt last, CIt from, CIt to) { static_assert(std::is_constructible_v::value_type>, "Invalid value type"); ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); })); - assure().insert(*this, first, last, from, to); + assure().insert(first, last, from, to); } /** @@ -712,8 +717,8 @@ public: auto &cpool = assure(); return cpool.contains(entity) - ? cpool.replace(*this, entity, Component{std::forward(args)...}) - : cpool.emplace(*this, entity, std::forward(args)...); + ? cpool.replace(entity, Component{std::forward(args)...}) + : cpool.emplace(entity, std::forward(args)...); } /** @@ -746,7 +751,7 @@ public: template decltype(auto) patch(const entity_type entity, Func &&... func) { ENTT_ASSERT(valid(entity)); - return assure().patch(*this, entity, std::forward(func)...); + return assure().patch(entity, std::forward(func)...); } /** @@ -771,7 +776,7 @@ public: */ template decltype(auto) replace(const entity_type entity, Args &&... args) { - return assure().replace(*this, entity, Component{std::forward(args)...}); + return assure().replace(entity, Component{std::forward(args)...}); } /** @@ -790,7 +795,7 @@ public: template void remove(const entity_type entity) { ENTT_ASSERT(valid(entity)); - (assure().remove(*this, entity), ...); + (assure().remove(entity), ...); } /** @@ -806,7 +811,7 @@ public: template void remove(It first, It last) { ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); })); - (assure().remove(*this, first, last), ...); + (assure().remove(first, last), ...); } /** @@ -834,7 +839,7 @@ public: ENTT_ASSERT(valid(entity)); return ([this, entity](auto &&cpool) { - return cpool.contains(entity) ? (cpool.remove(*this, entity), true) : false; + return cpool.contains(entity) ? (cpool.remove(entity), true) : false; }(assure()) + ... + size_type{}); } @@ -859,7 +864,7 @@ public: for(auto pos = pools.size(); pos; --pos) { if(auto &pdata = pools[pos-1]; pdata.pool && pdata.pool->contains(entity)) { - pdata.remove(*pdata.pool, *this, entity); + pdata.remove(*pdata.pool, entity); } } } @@ -966,7 +971,7 @@ public: [[nodiscard]] decltype(auto) get_or_emplace(const entity_type entity, Args &&... args) { ENTT_ASSERT(valid(entity)); auto &cpool = assure(); - return cpool.contains(entity) ? cpool.get(entity) : cpool.emplace(*this, entity, std::forward(args)...); + return cpool.contains(entity) ? cpool.get(entity) : cpool.emplace(entity, std::forward(args)...); } /** @@ -1015,7 +1020,7 @@ public: each([this](const auto entity) { this->destroy(entity); }); } else { ([this](auto &&cpool) { - cpool.remove(*this, cpool.sparse_set::begin(), cpool.sparse_set::end()); + cpool.remove(cpool.sparse_set::begin(), cpool.sparse_set::end()); }(assure()), ...); } } @@ -1308,7 +1313,7 @@ public: if(!handler) { group_data candidate = { size, - { new handler_type{}, [](void *instance) { delete static_cast(instance); } }, + { new handler_type{*this}, [](void *instance) { delete static_cast(instance); } }, []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info::id()) || ...); }, @@ -1356,7 +1361,7 @@ public: } else { // we cannot iterate backwards because we want to leave behind valid entities in case of owned types for(auto *first = std::get<0>(cpools).data(), *last = first + std::get<0>(cpools).size(); first != last; ++first) { - handler->template maybe_valid_if...>>>(*this, *first); + handler->template maybe_valid_if...>>>(*first); } } } diff --git a/test/entt/entity/group.cpp b/test/entt/entity/group.cpp index 026cb9504..c7dae6047 100644 --- a/test/entt/entity/group.cpp +++ b/test/entt/entity/group.cpp @@ -570,7 +570,7 @@ TEST(NonOwningGroup, FrontBack) { TEST(NonOwningGroup, SignalRace) { entt::registry registry; - registry.on_construct().connect<&entt::registry::emplace_or_replace>(); + registry.on_construct().connect<&entt::registry::emplace_or_replace>(registry); const auto group = registry.group(entt::get); auto entity = registry.create(); @@ -1227,7 +1227,7 @@ TEST(OwningGroup, FrontBack) { TEST(OwningGroup, SignalRace) { entt::registry registry; - registry.on_construct().connect<&entt::registry::emplace_or_replace>(); + registry.on_construct().connect<&entt::registry::emplace_or_replace>(registry); const auto group = registry.group(entt::get); auto entity = registry.create(); diff --git a/test/entt/entity/helper.cpp b/test/entt/entity/helper.cpp index 497d73d6f..93788dd8d 100644 --- a/test/entt/entity/helper.cpp +++ b/test/entt/entity/helper.cpp @@ -33,7 +33,7 @@ TEST(Helper, Invoke) { entt::registry registry; const auto entity = registry.create(); - registry.on_construct().connect>(); + registry.on_construct().connect>(registry); registry.emplace(entity); ASSERT_EQ(entity, registry.get(entity).entt); diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 7af4da1a1..3ca174fb6 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -27,17 +27,13 @@ struct listener { } template - void incr(const entt::registry ®istry, entt::entity entity) { - ASSERT_TRUE(registry.valid(entity)); - ASSERT_TRUE(registry.has(entity)); + void incr(entt::entity entity) { last = entity; ++counter; } template - void decr(const entt::registry ®istry, entt::entity entity) { - ASSERT_TRUE(registry.valid(entity)); - ASSERT_TRUE(registry.has(entity)); + void decr(entt::entity entity) { last = entity; --counter; } @@ -1378,8 +1374,8 @@ TEST(Registry, Dependencies) { constexpr auto emplace_or_replace = &entt::registry::emplace_or_replace; constexpr auto remove = &entt::registry::remove; - registry.on_construct().connect(); - registry.on_destroy().connect(); + registry.on_construct().connect(registry); + registry.on_destroy().connect(registry); registry.emplace(entity, .3); ASSERT_FALSE(registry.has(entity)); @@ -1394,8 +1390,8 @@ TEST(Registry, Dependencies) { ASSERT_FALSE((registry.any(entity))); - registry.on_construct().disconnect(); - registry.on_destroy().disconnect(); + registry.on_construct().disconnect(registry); + registry.on_destroy().disconnect(registry); registry.emplace(entity); ASSERT_TRUE((registry.any(entity))); @@ -1404,7 +1400,7 @@ TEST(Registry, Dependencies) { TEST(Registry, StableEmplace) { entt::registry registry; - registry.on_construct().connect<&listener::sort>(); + registry.on_construct().connect<&listener::sort>(registry); registry.emplace(registry.create(), 0); ASSERT_EQ(registry.emplace(registry.create(), 1), 1);