From e9a59bb0149f7a7e3409fb12dcfbd60eadb9082d Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 11 Aug 2022 12:15:49 +0200 Subject: [PATCH] registry: swap --- src/entt/entity/registry.hpp | 17 +++++++++++++++++ test/entt/entity/registry.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 5404b08a7..4ff6b1c2b 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -425,6 +425,23 @@ public: return *this; } + /** + * @brief Exchanges the contents with those of a given registry. + * @param other Registry to exchange the content with. + */ + void swap(basic_registry &other) { + using std::swap; + propagate_on_container_swap(free_list.second(), other.free_list.second()); + swap(free_list.first(), other.free_list.first()); + swap(pools, other.pools); + swap(groups, other.groups); + swap(epool, other.epool); + swap(vars, other.vars); + + rebind(); + other.rebind(); + } + /** * @brief Returns the associated allocator. * @return The associated allocator. diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 488f20351..d0620ac82 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -396,6 +396,36 @@ TEST(Registry, Move) { ASSERT_EQ(test.parent, ®istry); } +TEST(Registry, Swap) { + entt::registry registry; + const auto entity = registry.create(); + owner test{}; + + registry.on_construct().connect<&owner::receive>(test); + registry.on_destroy().connect<&owner::receive>(test); + + ASSERT_EQ(test.parent, nullptr); + + registry.emplace(entity); + + ASSERT_EQ(test.parent, ®istry); + + entt::registry other; + other.swap(registry); + other.erase(entity); + + registry = {}; + registry.emplace(registry.create(entity)); + + ASSERT_EQ(test.parent, &other); + + registry.swap(other); + registry.emplace(entity); + registry.emplace(registry.create(entity)); + + ASSERT_EQ(test.parent, ®istry); +} + TEST(Registry, ReplaceAggregate) { entt::registry registry; const auto entity = registry.create();