registry: swap

This commit is contained in:
Michele Caini
2022-08-11 12:15:49 +02:00
parent 76ba063f29
commit e9a59bb014
2 changed files with 47 additions and 0 deletions

View File

@@ -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.

View File

@@ -396,6 +396,36 @@ TEST(Registry, Move) {
ASSERT_EQ(test.parent, &registry);
}
TEST(Registry, Swap) {
entt::registry registry;
const auto entity = registry.create();
owner test{};
registry.on_construct<int>().connect<&owner::receive>(test);
registry.on_destroy<int>().connect<&owner::receive>(test);
ASSERT_EQ(test.parent, nullptr);
registry.emplace<int>(entity);
ASSERT_EQ(test.parent, &registry);
entt::registry other;
other.swap(registry);
other.erase<int>(entity);
registry = {};
registry.emplace<int>(registry.create(entity));
ASSERT_EQ(test.parent, &other);
registry.swap(other);
registry.emplace<int>(entity);
registry.emplace<int>(registry.create(entity));
ASSERT_EQ(test.parent, &registry);
}
TEST(Registry, ReplaceAggregate) {
entt::registry registry;
const auto entity = registry.create();