Update NetworkPacket to correctly use move semantics

This commit is contained in:
Ryan Huffman 2015-07-07 12:38:58 -07:00
parent 308335ff69
commit d8b1cc0aa9
2 changed files with 14 additions and 15 deletions

View file

@ -18,38 +18,38 @@
#include "NetworkPacket.h" #include "NetworkPacket.h"
void NetworkPacket::copyContents(const SharedNodePointer& node, const NLPacket& packet) { NetworkPacket::NetworkPacket(const NetworkPacket& other) :
if (packet.size() && packet.size() <= MAX_PACKET_SIZE) { _node(other._node),
_nlPacket(other._nlPacket) {
}
NetworkPacket::NetworkPacket(const SharedNodePointer& node, const NLPacket& packet) {
if (packet.getSizeWitHeader() && packet.getSizeWithHeader() <= MAX_nlPacket_SIZE) {
_node = node; _node = node;
_nlPacket = packet; _nlPacket = packet;
} else { } else {
qCDebug(networking, ">>> NetworkPacket::copyContents() unexpected length = %d", packet.size()); 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 // copy assignment
NetworkPacket& NetworkPacket::operator=(NetworkPacket const& other) { NetworkPacket& NetworkPacket::operator=(NetworkPacket const& other) {
copyContents(other._node, other._packet); _node = other._node;
_nlPacket = other._nlPacket;
return *this; return *this;
} }
#ifdef HAS_MOVE_SEMANTICS #ifdef HAS_MOVE_SEMANTICS
// move, same as copy, but other packet won't be used further // move, same as copy, but other packet won't be used further
NetworkPacket::NetworkPacket(NetworkPacket&& other) { NetworkPacket::NetworkPacket(NetworkPacket&& other) :
copyContents(other._node, other._packet); _node(std::move(other._node)),
_nlPacket(std::move(other._nlPacket)) {
} }
// move assignment // move assignment
NetworkPacket& NetworkPacket::operator=(NetworkPacket&& other) { NetworkPacket& NetworkPacket::operator=(NetworkPacket&& other) {
copyContents(other._node, other._packet); _node = std::move(other._node);
_nlPacket = std::move(other._nlPacket);
return *this; return *this;
} }
#endif #endif

View file

@ -34,7 +34,6 @@ public:
const NLPacket& getPacket() const { return _nlPacket; } const NLPacket& getPacket() const { return _nlPacket; }
private: private:
void copyContents(const SharedNodePointer& node, const NLPacket& nlPacket);
SharedNodePointer _node; SharedNodePointer _node;
NLPacket _nlPacket; NLPacket _nlPacket;