From 6c1511ed3813f03474dda9c7475f7e3a8ec85466 Mon Sep 17 00:00:00 2001 From: Keyboard Destroyer Date: Sat, 14 Jun 2025 14:07:39 +0300 Subject: [PATCH] Added the rule of 5 to message class --- libraries/messages/AxonMessage.cpp | 76 ++++++++++++++++++++++++------ libraries/messages/AxonMessage.hpp | 15 +++--- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/libraries/messages/AxonMessage.cpp b/libraries/messages/AxonMessage.cpp index 40e16c1..1b47fef 100644 --- a/libraries/messages/AxonMessage.cpp +++ b/libraries/messages/AxonMessage.cpp @@ -31,7 +31,7 @@ Networking::SerializedAxonMessage::SerializedAxonMessage(SerializedAxonMessage & } Networking::SerializedAxonMessage::~SerializedAxonMessage() { - if (bytes && owning) + if (bytes) delete[] bytes; } @@ -120,24 +120,74 @@ Networking::AxonMessage::AxonMessage(const AxonMessage& message) : memcpy(this->message, message.getMessage(), this->size); } -Networking::AxonMessage::AxonMessage(AxonMessage &message, size64_t size, uint8_t partID, uint8_t flags, uint64_t uniqueID, size64_t offset) : +Networking::AxonMessage::AxonMessage(AxonMessage &&other) noexcept : + size(std::exchange(other.size, 0)), + message(std::exchange(other.message, nullptr)), + partID(std::exchange(other.partID, 0)), + flags(std::exchange(other.flags, UNDEFINED)), + uniqueID(std::exchange(other.uniqueID, 0)) +{} + +Networking::AxonMessage::AxonMessage(const AxonMessage &message, size64_t size, uint8_t partID, uint8_t flags, uint64_t uniqueID, size64_t offset) : size(size), - message(message.message), partID(partID), flags(flags), - uniqueID(uniqueID), - offset(offset) + uniqueID(uniqueID) { - message.owning = false; + if (!message.message || size == 0) + return; + + this->message = new char[size]; + memcpy(this->message, message.message + offset, size); } Networking::AxonMessage::~AxonMessage() { - if (this->message && owning) - delete[] static_cast( message ); + if (this->message) + delete[] message; message = nullptr; } +Networking::AxonMessage & Networking::AxonMessage::operator=(const AxonMessage &other) { + if (&other == this) { + return *this; + } + + if (this->message) { + delete[] this->message; + } + + size = other.size; + partID = other.partID; + flags = other.flags; + uniqueID = other.uniqueID; + + if (other.message && other.size > 0) { + message = new char[size]; + memcpy(message, other.message, size); + } + + return *this; +} + +Networking::AxonMessage & Networking::AxonMessage::operator=(AxonMessage &&other) noexcept { + if (&other == this) { + return *this; + } + + if (this->message) { + delete[] message; + } + + size = std::exchange(other.size, 0); + message = std::exchange(other.message, nullptr); + partID = std::exchange(other.partID, 0); + flags = std::exchange(other.flags, UNDEFINED); + uniqueID = std::exchange(other.uniqueID, 0); + + return *this; +} + Networking::SerializedAxonMessage Networking::AxonMessage::getSerialized() const noexcept { return SerializedAxonMessage(*this); } @@ -165,22 +215,18 @@ Networking::AxonMessage::UniqueAxonMessagePtr Networking::AxonMessage::split(con return std::make_unique(*this, left, partID + 1, flags ^ PARTIAL, uniqueID, size); } -void Networking::AxonMessage::append(const Networking::AxonMessage &other) { +void Networking::AxonMessage::append(const AxonMessage &other) { if (!other.getMessage() || other.size == 0 || other.partID != partID + 1) return; char* tempBuffer = new char[other.size + size]; - - // assert((uintptr_t) &size < (uintptr_t) tempBuffer || (uintptr_t) &size > (uintptr_t) tempBuffer + other.size + size); - if (message) { memcpy(tempBuffer, getMessage(), size); - if (owning) - delete[] static_cast(message); + delete[] message; message = nullptr; } - memcpy((static_cast(tempBuffer) + size), other.getMessage(), other.size); + memcpy(tempBuffer + size, other.getMessage(), other.size); message = tempBuffer; size += other.size; partID = other.partID; diff --git a/libraries/messages/AxonMessage.hpp b/libraries/messages/AxonMessage.hpp index 977e036..4faf190 100644 --- a/libraries/messages/AxonMessage.hpp +++ b/libraries/messages/AxonMessage.hpp @@ -64,7 +64,6 @@ namespace Networking void move(SerializedAxonMessage&) noexcept; private: size64_t size = 0; - bool owning = true; // todo: `uint16_t _refcnt;` or shared_ptr around bytes array const char* bytes = nullptr; friend class AxonMessage; @@ -103,15 +102,17 @@ namespace Networking */ AxonMessage(const AxonMessage &message, uint8_t additionalFlags); - /** - * Copy constructor - */ AxonMessage(const AxonMessage &); - AxonMessage(AxonMessage&, size64_t, uint8_t, uint8_t, uint64_t, size64_t); + AxonMessage(AxonMessage &&) noexcept; + + AxonMessage(const AxonMessage&, size64_t, uint8_t, uint8_t, uint64_t, size64_t); ~AxonMessage(); + AxonMessage & operator= (const AxonMessage &); + AxonMessage & operator= (AxonMessage &&) noexcept; + WGETTER(SerializedAxonMessage getSerialized()); - WGETTER(void* getMessage()) { return (message) ? message + offset : message; } + WGETTER(void* getMessage()) { return message; } WGETTER(size64_t getSize()) { return size; } WGETTER(uint16_t ID()) { return uniqueID; } WGETTER(uint8_t getFlags()) { return flags; } @@ -139,10 +140,8 @@ namespace Networking private: size64_t size = 0; char * message = nullptr; - bool owning = true; uint8_t partID = 0; uint8_t flags = UNDEFINED; - size64_t offset = 0; uint16_t uniqueID = generateUniqueID(); }; }