Michele Caini 3 лет назад
Родитель
Сommit
e9a59bb014
2 измененных файлов с 47 добавлено и 0 удалено
  1. 17 0
      src/entt/entity/registry.hpp
  2. 30 0
      test/entt/entity/registry.cpp

+ 17 - 0
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.

+ 30 - 0
test/entt/entity/registry.cpp

@@ -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();