From e14d78c6dfa470b2a62da25df28488c28bd45136 Mon Sep 17 00:00:00 2001 From: Lizzzka007 <gashchuk2011@mail.ru> Date: Mon, 4 Nov 2024 03:15:46 +0300 Subject: [PATCH] . --- include/buffer.h | 1 + src/buffer.cpp | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/buffer.h b/include/buffer.h index 6f79e98..2f401be 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -15,6 +15,7 @@ public: buffer(const size_t required_size, const MemType mem); // Done buffer(const buffer& other); // Done buffer(buffer&& other) noexcept; // Done + void swap(buffer& other) noexcept; ~buffer(); // Done bool is_available() const; // Done diff --git a/src/buffer.cpp b/src/buffer.cpp index 572da61..8e2a7c3 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -55,7 +55,7 @@ buffer::buffer(const buffer& other) } buffer::buffer(buffer&& other) noexcept : - mem(other.mem), ptr(std::move(other.ptr)), allocated_size(other.allocated_size), + mem(other.mem), ptr(other.ptr), allocated_size(other.allocated_size), scalar_size(other.scalar_size), is_free(other.is_free), id(other.id) { @@ -84,6 +84,23 @@ buffer& buffer::operator=(const buffer& other) return *this; } +void buffer::swap(buffer& other) noexcept +{ + std::swap(ptr, other.ptr); + mem = other.mem; + allocated_size = other.allocated_size; + scalar_size = other.scalar_size; + is_free = other.is_free; + id = other.id; + + other.ptr = nullptr; + other.allocated_size = 0; + other.scalar_size = 0; + other.is_free = false; + other.id = -1; + other.mem = MemType::UNDEF; +} + buffer& buffer::operator=(buffer&& other) noexcept { if (this != &other) @@ -91,19 +108,8 @@ buffer& buffer::operator=(buffer&& other) noexcept assert(other.mem != MemType::UNDEF); deallocate(); - mem = other.mem; - ptr = std::move(other.ptr); - allocated_size = other.allocated_size; - scalar_size = other.scalar_size; - is_free = other.is_free; - id = other.id; - - other.ptr = nullptr; - other.allocated_size = 0; - other.scalar_size = 0; - other.is_free = false; - other.id = -1; - other.mem = MemType::UNDEF; + buffer temp{std::move(other)}; + swap(temp); } return *this; -- GitLab