diff --git a/TODO b/TODO index c8a79194a..43858eed6 100644 --- a/TODO +++ b/TODO @@ -23,9 +23,4 @@ - non-owning groups can iterate pages and skip empty ones, this should mitigate the lack of the packed array * review 64 bit id: user defined area + dedicated member on the registry to set it * add NIO to the EnTT in Action list (link to my CV/LinkedIN) - * reactive systems - - add exclusion lists to views (part of signature in this case) - - add entt::opt for optional components both to views and groups - - add try_get both to views and groups (for optional components) - - setup support for reactive systems, something like: registry.reactive(entt:get, entt::opt, entt::exclude) diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 88c5abfea..504061ac9 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -66,7 +66,7 @@ class basic_registry { template struct pool_wrapper: sparse_set { template - Component & construct(Entity entt, Args &&... args) { + Component & construct(const Entity entt, Args &&... args) { auto &component = sparse_set::construct(entt, std::forward(args)...); construction.publish(*owner, entt); return component; @@ -85,11 +85,20 @@ class basic_registry { return component; } - void destroy(Entity entt) override { + void destroy(const Entity entt) override { destruction.publish(*owner, entt); sparse_set::destroy(entt); } + template + Component & replace(const Entity entt, Args &&... args) { + destruction.publish(*owner, entt); + auto &component = sparse_set::get(entt); + component = Component{std::forward(args)...}; + construction.publish(*owner, entt); + return component; + } + signal_type construction; signal_type destruction; basic_registry *owner; @@ -855,7 +864,7 @@ public: */ template Component & replace(const entity_type entity, Args &&... args) { - return pool()->get(entity) = Component{std::forward(args)...}; + return pool()->replace(entity, std::forward(args)...); } /** @@ -887,10 +896,9 @@ public: template Component & assign_or_replace(const entity_type entity, Args &&... args) { auto *cpool = assure(); - auto *component = cpool->try_get(entity); - return component - ? (*component = Component{std::forward(args)...}) + return cpool->has(entity) + ? cpool->replace(entity, std::forward(args)...) : cpool->construct(entity, std::forward(args)...); } diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 94ba09ddc..c3007c884 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -909,10 +909,23 @@ TEST(Registry, Signals) { ASSERT_EQ(listener.counter, 1); ASSERT_EQ(listener.last, e0); + registry.destruction().disconnect<&listener::decr>(&listener); + registry.assign_or_replace(e0); + + ASSERT_EQ(listener.counter, 2); + ASSERT_EQ(listener.last, e0); + + registry.construction().disconnect<&listener::incr>(&listener); + registry.destruction().connect<&listener::decr>(&listener); registry.assign_or_replace(e0); ASSERT_EQ(listener.counter, 1); ASSERT_EQ(listener.last, e0); + + registry.replace(e0); + + ASSERT_EQ(listener.counter, 0); + ASSERT_EQ(listener.last, e0); } TEST(Registry, DestroyByComponents) {