From 9f36bec353fa95e8258af55a5af2e89ae61e95d2 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 6 Sep 2024 15:24:21 +0200 Subject: [PATCH] sparse_set/storage: swap based move assignment operator --- TODO | 2 +- src/entt/entity/mixin.hpp | 6 +----- src/entt/entity/sparse_set.hpp | 10 ++-------- src/entt/entity/storage.hpp | 6 ++---- test/entt/entity/sigh_mixin.cpp | 2 +- test/entt/entity/sparse_set.cpp | 2 +- test/entt/entity/storage.cpp | 2 +- test/entt/entity/storage_entity.cpp | 2 +- test/entt/entity/storage_no_instance.cpp | 2 +- 9 files changed, 11 insertions(+), 23 deletions(-) diff --git a/TODO b/TODO index 0fbae510d..bdce15ad2 100644 --- a/TODO +++ b/TODO @@ -45,4 +45,4 @@ TODO: * view and view iterator specializations for multi, single and filtered elements * organizer support to groups * meta range: move id to meta objects and return plain types (?), then remove id from meta base and meta ctor too -* noexcept move op for sparse set and storage +* review all move assignment operators diff --git a/src/entt/entity/mixin.hpp b/src/entt/entity/mixin.hpp index 62d9825c7..515f0a2cb 100644 --- a/src/entt/entity/mixin.hpp +++ b/src/entt/entity/mixin.hpp @@ -151,11 +151,7 @@ public: * @return This mixin. */ basic_sigh_mixin &operator=(basic_sigh_mixin &&other) noexcept(noexcept(std::declval().operator=(std::move(other)))) { - owner = other.owner; - construction = std::move(other.construction); - destruction = std::move(other.destruction); - update = std::move(other.update); - underlying_type::operator=(std::move(other)); + swap(other); return *this; } diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index 9ef6abeb8..1a0afe9b0 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -468,15 +468,9 @@ public: * @param other The instance to move from. * @return This sparse set. */ - basic_sparse_set &operator=(basic_sparse_set &&other) noexcept(false) { + basic_sparse_set &operator=(basic_sparse_set &&other) noexcept { ENTT_ASSERT(alloc_traits::is_always_equal::value || get_allocator() == other.get_allocator(), "Copying a sparse set is not allowed"); - - release_sparse_pages(); - sparse = std::move(other.sparse); - packed = std::move(other.packed); - info = other.info; - mode = other.mode; - head = std::exchange(other.head, policy_to_head()); + swap(other); return *this; } diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index d313ed7d9..1539b365a 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -476,11 +476,9 @@ public: * @param other The instance to move from. * @return This storage. */ - basic_storage &operator=(basic_storage &&other) noexcept(false) { + basic_storage &operator=(basic_storage &&other) noexcept { ENTT_ASSERT(alloc_traits::is_always_equal::value || get_allocator() == other.get_allocator(), "Copying a storage is not allowed"); - shrink_to_size(0u); - payload = std::move(other.payload); - base_type::operator=(std::move(other)); + swap(other); return *this; } diff --git a/test/entt/entity/sigh_mixin.cpp b/test/entt/entity/sigh_mixin.cpp index 5a4efe7ca..927503d18 100644 --- a/test/entt/entity/sigh_mixin.cpp +++ b/test/entt/entity/sigh_mixin.cpp @@ -343,7 +343,7 @@ TYPED_TEST(SighMixin, Move) { other = std::move(pool); test::is_initialized(pool); - ASSERT_TRUE(pool.empty()); + ASSERT_FALSE(pool.empty()); ASSERT_FALSE(other.empty()); ASSERT_EQ(other.index(entt::entity{3}), 0u); diff --git a/test/entt/entity/sparse_set.cpp b/test/entt/entity/sparse_set.cpp index cb92ed752..a00991d1b 100644 --- a/test/entt/entity/sparse_set.cpp +++ b/test/entt/entity/sparse_set.cpp @@ -128,7 +128,7 @@ TYPED_TEST(SparseSet, Move) { other = std::move(set); test::is_initialized(set); - ASSERT_TRUE(set.empty()); + ASSERT_FALSE(set.empty()); ASSERT_FALSE(other.empty()); ASSERT_EQ(other.policy(), policy); diff --git a/test/entt/entity/storage.cpp b/test/entt/entity/storage.cpp index d57c6689c..7b855c320 100644 --- a/test/entt/entity/storage.cpp +++ b/test/entt/entity/storage.cpp @@ -151,7 +151,7 @@ TYPED_TEST(Storage, Move) { other = std::move(pool); test::is_initialized(pool); - ASSERT_TRUE(pool.empty()); + ASSERT_FALSE(pool.empty()); ASSERT_FALSE(other.empty()); ASSERT_EQ(other.type(), entt::type_id()); diff --git a/test/entt/entity/storage_entity.cpp b/test/entt/entity/storage_entity.cpp index 3ca51ab02..2be2c5884 100644 --- a/test/entt/entity/storage_entity.cpp +++ b/test/entt/entity/storage_entity.cpp @@ -71,7 +71,7 @@ TEST(StorageEntity, Move) { other = std::move(pool); test::is_initialized(pool); - ASSERT_TRUE(pool.empty()); + ASSERT_FALSE(pool.empty()); ASSERT_FALSE(other.empty()); ASSERT_EQ(other.type(), entt::type_id()); diff --git a/test/entt/entity/storage_no_instance.cpp b/test/entt/entity/storage_no_instance.cpp index ca00085c0..99910ff91 100644 --- a/test/entt/entity/storage_no_instance.cpp +++ b/test/entt/entity/storage_no_instance.cpp @@ -116,7 +116,7 @@ TYPED_TEST(StorageNoInstance, Move) { other = std::move(pool); test::is_initialized(pool); - ASSERT_TRUE(pool.empty()); + ASSERT_FALSE(pool.empty()); ASSERT_FALSE(other.empty()); ASSERT_EQ(other.type(), entt::type_id());