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"
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

View file

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