From d8b1cc0aa99512f62b07bed7312a69e8f29c2f92 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 7 Jul 2015 12:38:58 -0700 Subject: [PATCH] Update NetworkPacket to correctly use move semantics --- libraries/networking/src/NetworkPacket.cpp | 28 +++++++++++----------- libraries/networking/src/NetworkPacket.h | 1 - 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/libraries/networking/src/NetworkPacket.cpp b/libraries/networking/src/NetworkPacket.cpp index b2d1a14d77..fb1c90e392 100644 --- a/libraries/networking/src/NetworkPacket.cpp +++ b/libraries/networking/src/NetworkPacket.cpp @@ -18,38 +18,38 @@ #include "NetworkPacket.h" -void NetworkPacket::copyContents(const SharedNodePointer& node, const NLPacket& packet) { - if (packet.size() && packet.size() <= MAX_PACKET_SIZE) { +NetworkPacket::NetworkPacket(const NetworkPacket& other) : + _node(other._node), + _nlPacket(other._nlPacket) { +} + +NetworkPacket::NetworkPacket(const SharedNodePointer& node, const NLPacket& packet) { + if (packet.getSizeWitHeader() && packet.getSizeWithHeader() <= MAX_nlPacket_SIZE) { _node = node; _nlPacket = packet; } else { qCDebug(networking, ">>> NetworkPacket::copyContents() unexpected length = %d", packet.size()); } -} - -NetworkPacket::NetworkPacket(const NetworkPacket& other) { - copyContents(other._node, other._packet); -} - -NetworkPacket::NetworkPacket(const SharedNodePointer& node, const NLPacket& packet) { - copyContents(node, packet); }; // copy assignment NetworkPacket& NetworkPacket::operator=(NetworkPacket const& other) { - copyContents(other._node, other._packet); + _node = other._node; + _nlPacket = other._nlPacket; return *this; } #ifdef HAS_MOVE_SEMANTICS // move, same as copy, but other packet won't be used further -NetworkPacket::NetworkPacket(NetworkPacket&& other) { - copyContents(other._node, other._packet); +NetworkPacket::NetworkPacket(NetworkPacket&& other) : + _node(std::move(other._node)), + _nlPacket(std::move(other._nlPacket)) { } // move assignment NetworkPacket& NetworkPacket::operator=(NetworkPacket&& other) { - copyContents(other._node, other._packet); + _node = std::move(other._node); + _nlPacket = std::move(other._nlPacket); return *this; } #endif diff --git a/libraries/networking/src/NetworkPacket.h b/libraries/networking/src/NetworkPacket.h index 92c6a8ba59..c42ad286ae 100644 --- a/libraries/networking/src/NetworkPacket.h +++ b/libraries/networking/src/NetworkPacket.h @@ -34,7 +34,6 @@ public: const NLPacket& getPacket() const { return _nlPacket; } private: - void copyContents(const SharedNodePointer& node, const NLPacket& nlPacket); SharedNodePointer _node; NLPacket _nlPacket;