Bladeren bron

storage: add proper swap function to entity specialization - close #1273

skypjack 6 maanden geleden
bovenliggende
commit
536a43be90
3 gewijzigde bestanden met toevoegingen van 24 en 3 verwijderingen
  1. 10 0
      src/entt/entity/storage.hpp
  2. 5 3
      test/entt/entity/registry.cpp
  3. 9 0
      test/entt/entity/storage_entity.cpp

+ 10 - 0
src/entt/entity/storage.hpp

@@ -1106,6 +1106,16 @@ public:
         return *this;
     }
 
+    /**
+     * @brief Exchanges the contents with those of a given storage.
+     * @param other Storage to exchange the content with.
+     */
+    void swap(basic_storage &other) noexcept {
+        using std::swap;
+        swap(placeholder, other.placeholder);
+        base_type::swap(other);
+    }
+
     /**
      * @brief Returns the object assigned to an entity, that is `void`.
      *

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

@@ -309,13 +309,15 @@ TEST(Registry, Swap) {
     other.erase<int>(entity);
 
     registry = {};
-    registry.emplace<int>(registry.create(entity));
+    const auto reboot = registry.create();
+    registry.emplace<int>(reboot);
 
+    ASSERT_EQ(entity, reboot);
     ASSERT_EQ(test.parent, &other);
 
     registry.swap(other);
-    registry.emplace<int>(entity);
-    registry.emplace<int>(registry.create(entity));
+    registry.emplace<int>(reboot);
+    registry.emplace<int>(registry.create(reboot));
 
     ASSERT_EQ(test.parent, &registry);
 }

+ 9 - 0
test/entt/entity/storage_entity.cpp

@@ -80,6 +80,8 @@ TEST(StorageEntity, Move) {
 }
 
 TEST(StorageEntity, Swap) {
+    using traits_type = entt::entt_traits<entt::entity>;
+
     entt::storage<entt::entity> pool;
     entt::storage<entt::entity> other;
 
@@ -95,6 +97,9 @@ TEST(StorageEntity, Swap) {
     ASSERT_EQ(pool.size(), 1u);
     ASSERT_EQ(other.size(), 2u);
 
+    pool.start_from(entt::entity{99});
+    other.start_from(entt::entity{999});
+
     pool.swap(other);
 
     ASSERT_EQ(pool.info(), entt::type_id<void>());
@@ -103,6 +108,10 @@ TEST(StorageEntity, Swap) {
     ASSERT_EQ(pool.size(), 2u);
     ASSERT_EQ(other.size(), 1u);
 
+    ASSERT_EQ(pool.generate(), traits_type::construct(2, 1));
+    ASSERT_EQ(pool.generate(), entt::entity{999});
+    ASSERT_EQ(other.generate(), entt::entity{99});
+
     ASSERT_EQ(pool.index(entt::entity{1}), 0u);
     ASSERT_EQ(other.index(entt::entity{4}), 0u);
 }