From e2bd9532b7df80248b51535180e8ad159dfeb3f7 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 7 Jul 2015 12:47:23 -0700 Subject: [PATCH] Implement move ctor/assignment --- libraries/networking/src/Packet.cpp | 35 +++++++++++++++++++++++++++++ libraries/networking/src/Packet.h | 12 +++++----- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/libraries/networking/src/Packet.cpp b/libraries/networking/src/Packet.cpp index 293763f77b..ca49100efe 100644 --- a/libraries/networking/src/Packet.cpp +++ b/libraries/networking/src/Packet.cpp @@ -64,6 +64,41 @@ Packet::Packet(PacketType::Value type, qint64 size) : } } +Packet::Packet(const Packet& other) { + *this = other; +} + +Packet& Packet::operator=(const Packet& other) { + _packetSize = other._packetSize; + _packet = std::unique_ptr(new char(_packetSize)); + memcpy(_packet.get(), other._packet.get(), _packetSize); + + _payloadStart = _packet.get() + (other._payloadStart - other._packet.get()); + _position = other._position; + _capacity = other._capacity; + + _sizeUsed = other._sizeUsed; + + return *this; +} + +Packet::Packet(Packet&& other) { + *this = std::move(other); +} + +Packet& Packet::operator=(Packet&& other) { + _packetSize = other._packetSize; + _packet = std::move(other._packet); + + _payloadStart = other._payloadStart; + _position = other._position; + _capacity = other._capacity; + + _sizeUsed = other._sizeUsed; + + return *this; +} + PacketType::Value Packet::getPacketType() const { return (PacketType::Value)arithmeticCodingValueFromBuffer(_packet.get()); } diff --git a/libraries/networking/src/Packet.h b/libraries/networking/src/Packet.h index 958f56a827..6177cbd60e 100644 --- a/libraries/networking/src/Packet.h +++ b/libraries/networking/src/Packet.h @@ -20,10 +20,10 @@ class Packet : public QIODevice { public: - static std::unique_ptr create(PacketType::Value type, int64_t size = -1); - using SequenceNumber = uint16_t; + static std::unique_ptr create(PacketType::Value type, int64_t size = -1); + static qint64 localHeaderSize(PacketType::Value type); static qint64 maxPayloadSize(PacketType::Value type); @@ -57,10 +57,10 @@ public: protected: Packet(PacketType::Value type, int64_t size); - Packet(Packet&&) = delete; - Packet(const Packet&) = delete; - Packet& operator=(Packet&&) = delete; - Packet& operator=(const Packet&) = delete; + Packet(const Packet& other); + Packet& operator=(const Packet& other); + Packet(Packet&& other); + Packet& operator=(Packet&& other); // QIODevice virtual functions virtual qint64 writeData(const char* data, qint64 maxSize);