Browse Source

fixed the clone functionality (close #273)

Michele Caini 6 years ago
parent
commit
23aa097d0a
2 changed files with 11 additions and 4 deletions
  1. 8 4
      src/entt/entity/registry.hpp
  2. 3 0
      test/entt/entity/registry.cpp

+ 8 - 4
src/entt/entity/registry.hpp

@@ -200,7 +200,7 @@ class basic_registry {
     struct pool_data {
         std::unique_ptr<sparse_set<Entity>> pool;
         std::unique_ptr<sparse_set<Entity>> (* clone)(const sparse_set<Entity> &);
-        delegate<void(basic_registry &, const Entity)> remove;
+        void (* remove)(sparse_set<Entity> &, basic_registry &, const Entity);
         ENTT_ID_TYPE runtime_type;
     };
 
@@ -280,7 +280,10 @@ class basic_registry {
         if(!pdata->pool) {
             pdata->runtime_type = ctype;
             pdata->pool = std::make_unique<pool_type<Component>>();
-            pdata->remove.template connect<&pool_type<Component>::remove>(static_cast<pool_type<Component> *>(pdata->pool.get()));
+
+            pdata->remove = [](sparse_set<Entity> &other, basic_registry &registry, const Entity entt) {
+                static_cast<pool_type<Component> &>(other).remove(registry, entt);
+            };
 
             pdata->clone = [](const sparse_set<Entity> &other) -> std::unique_ptr<sparse_set<Entity>> {
                 if constexpr(std::is_copy_constructible_v<std::decay_t<Component>>) {
@@ -644,7 +647,7 @@ public:
 
         for(auto pos = pools.size(); pos; --pos) {
             if(auto &pdata = pools[pos-1]; pdata.pool && pdata.pool->has(entity)) {
-                pdata.remove(*this, entity);
+                pdata.remove(*pdata.pool, *this, entity);
             }
         };
 
@@ -1468,7 +1471,8 @@ public:
             if(auto &pdata = pools[pos-1]; pdata.pool && (!sizeof...(Component) || ... || (pdata.runtime_type == type<Component>()))) {
                 auto &curr = other.pools[pos-1];
                 curr.clone = pdata.clone;
-                curr.pool = curr.clone(*pdata.pool);
+                curr.remove = pdata.remove;
+                curr.pool = pdata.clone(*pdata.pool);
                 curr.runtime_type = pdata.runtime_type;
                 ENTT_ASSERT(sizeof...(Component) == 0 || curr.pool);
             }

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

@@ -1303,6 +1303,9 @@ TEST(Registry, Clone) {
 
     ASSERT_TRUE(other.orphan(e0));
     ASSERT_EQ(other.get<char>(e2), '2');
+
+    // the remove erased function must be available after cloning
+    other.reset();
 }
 
 TEST(Registry, CloneMoveOnlyComponent) {