Skip to content

Commit e16a946

Browse files
committed
buffer: implement move semantics properly
Default implementations of move semantics are not suitable - let's implement proper ones. Along the way, do not accept allocator in constructor by reference since buffer stores its own copy - let's consumer it, and to do so, we should accept rvalue reference there. Part of #110
1 parent 81a097f commit e16a946

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/Buffer/Buffer.hpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,28 @@ class Buffer
265265

266266
/** =============== Buffer definition =============== */
267267
/** Copy of any kind is disabled. Move is allowed. */
268-
Buffer(const allocator& all = allocator());
268+
Buffer(allocator&& all = allocator());
269269
Buffer(const Buffer& buf) = delete;
270270
Buffer& operator = (const Buffer& buf) = delete;
271-
Buffer(Buffer &&buf) noexcept = default;
272-
Buffer &operator=(Buffer &&buf) noexcept = default;
271+
Buffer(Buffer &&other) noexcept
272+
{
273+
/* Call move assignment operator. */
274+
*this = std::forward<Buffer>(other);
275+
}
276+
Buffer &operator=(Buffer &&other)
277+
{
278+
if (this == &other)
279+
return *this;
280+
m_blocks = std::move(other.m_blocks);
281+
assert(other.m_blocks.isEmpty());
282+
m_iterators = std::move(other.m_iterators);
283+
m_begin = other.m_begin;
284+
other.m_begin = nullptr;
285+
m_end = other.m_end;
286+
other.m_end = nullptr;
287+
m_all = std::move(other.m_all);
288+
return *this;
289+
}
273290
~Buffer() noexcept;
274291

275292
/**
@@ -648,7 +665,7 @@ Buffer<N, allocator>::iterator_common<LIGHT>::moveBackward(size_t step)
648665
}
649666

650667
template <size_t N, class allocator>
651-
Buffer<N, allocator>::Buffer(const allocator &all) : m_all(all)
668+
Buffer<N, allocator>::Buffer(allocator &&all) : m_all(std::forward<allocator>(all))
652669
{
653670
static_assert((N & (N - 1)) == 0, "N must be power of 2");
654671
static_assert(allocator::REAL_SIZE % alignof(Block) == 0,

0 commit comments

Comments
 (0)