diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-05-17 23:01:51 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-05-17 23:01:51 +0200 |
commit | 54b95b16629f0cd4bc30e6899e00019b3ab94012 (patch) | |
tree | d1ce8c21bab489318832e9c6f1b8961e255fa208 | |
parent | e7e70233405a523cfe75536ac5819cbb8cfb5f17 (diff) |
Buffer type has to be moveable, always
-rw-r--r-- | asl/containers/buffer.hpp | 5 | ||||
-rw-r--r-- | asl/containers/buffer_tests.cpp | 34 |
2 files changed, 34 insertions, 5 deletions
diff --git a/asl/containers/buffer.hpp b/asl/containers/buffer.hpp index 0e30745..f554218 100644 --- a/asl/containers/buffer.hpp +++ b/asl/containers/buffer.hpp @@ -16,7 +16,8 @@ namespace asl { -template<is_object T, allocator Allocator = DefaultAllocator> +template<typename T, allocator Allocator = DefaultAllocator> +requires is_object<T> && moveable<T> class buffer { T* m_data{}; @@ -244,7 +245,6 @@ public: } constexpr buffer(buffer&& other) - requires moveable<T> : buffer(std::move(other.m_allocator)) { move_from_other(std::move(other), false); @@ -259,7 +259,6 @@ public: } constexpr buffer& operator=(buffer&& other) - requires moveable<T> { if (&other == this) { return *this; } move_from_other(std::move(other), true); diff --git a/asl/containers/buffer_tests.cpp b/asl/containers/buffer_tests.cpp index 2538e92..eb95ffe 100644 --- a/asl/containers/buffer_tests.cpp +++ b/asl/containers/buffer_tests.cpp @@ -159,13 +159,19 @@ struct MoveableType MoveableType(const MoveableType&) = delete; MoveableType(MoveableType&& other) : moved{other.moved + 1}, value{other.value} {} MoveableType& operator=(const MoveableType&) = delete; - MoveableType& operator=(MoveableType&&) = delete; + MoveableType& operator=(MoveableType&& other) + { + if (this == &other) { return *this; } + moved = other.moved + 1; + value = other.value; + return *this; + } ~MoveableType() = default; }; static_assert(!asl::trivially_copy_constructible<MoveableType>); static_assert(!asl::trivially_move_constructible<MoveableType>); static_assert(!asl::copyable<MoveableType>); -static_assert(asl::move_constructible<MoveableType>); +static_assert(asl::moveable<MoveableType>); // NOLINTNEXTLINE(*-complexity) ASL_TEST(push_move) @@ -213,6 +219,30 @@ ASL_TEST(push_move) ASL_TEST_EXPECT(b[3].moved == 1); ASL_TEST_EXPECT(b[4].value == 4); ASL_TEST_EXPECT(b[4].moved == 0); + + asl::buffer<MoveableType> b2 = std::move(b); + ASL_TEST_EXPECT(b2[0].value == 0); + ASL_TEST_EXPECT(b2[0].moved == 2); + ASL_TEST_EXPECT(b2[1].value == 1); + ASL_TEST_EXPECT(b2[1].moved == 2); + ASL_TEST_EXPECT(b2[2].value == 2); + ASL_TEST_EXPECT(b2[2].moved == 1); + ASL_TEST_EXPECT(b2[3].value == 3); + ASL_TEST_EXPECT(b2[3].moved == 1); + ASL_TEST_EXPECT(b2[4].value == 4); + ASL_TEST_EXPECT(b2[4].moved == 0); + + b = std::move(b2); + ASL_TEST_EXPECT(b[0].value == 0); + ASL_TEST_EXPECT(b[0].moved == 2); + ASL_TEST_EXPECT(b[1].value == 1); + ASL_TEST_EXPECT(b[1].moved == 2); + ASL_TEST_EXPECT(b[2].value == 2); + ASL_TEST_EXPECT(b[2].moved == 1); + ASL_TEST_EXPECT(b[3].value == 3); + ASL_TEST_EXPECT(b[3].moved == 1); + ASL_TEST_EXPECT(b[4].value == 4); + ASL_TEST_EXPECT(b[4].moved == 0); } ASL_TEST(clear) |