Network/NetPacket: Add move constructor/operator

Former-commit-id: 56cb793ab75e5bcd97b89c5be3592922c23a7a4d
This commit is contained in:
Lynix 2016-02-24 14:17:47 +01:00
parent a851056c0a
commit d24de1f719
2 changed files with 30 additions and 2 deletions

View File

@ -25,7 +25,7 @@ namespace Nz
inline NetPacket(UInt16 netCode, std::size_t minCapacity = 0);
inline NetPacket(UInt16 netCode, const void* ptr, std::size_t size);
NetPacket(const NetPacket&) = delete;
NetPacket(NetPacket&&) = default;
NetPacket(NetPacket&& packet);
inline ~NetPacket();
inline const UInt8* GetConstData() const;
@ -45,7 +45,7 @@ namespace Nz
inline void SetNetCode(UInt16 netCode);
NetPacket& operator=(const NetPacket&) = delete;
NetPacket& operator=(NetPacket&&) = default;
NetPacket& operator=(NetPacket&& packet);
static bool DecodeHeader(const void* data, UInt16* packetSize, UInt16* netCode);
static bool EncodeHeader(void* data, UInt16 packetSize, UInt16 netCode);

View File

@ -24,6 +24,17 @@ namespace Nz
Reset(netCode, ptr, size);
}
inline NetPacket::NetPacket(NetPacket&& packet) :
ByteStream(std::move(packet)),
m_buffer(std::move(packet.m_buffer)),
m_memoryStream(std::move(packet.m_memoryStream)),
m_netCode(packet.m_netCode)
{
///< Redirect memory stream to the moved buffer
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode());
SetStream(&m_memoryStream);
}
inline NetPacket::~NetPacket()
{
FlushBits(); //< Needs to be done here as the stream will be freed before ByteStream calls it
@ -88,6 +99,23 @@ namespace Nz
{
m_netCode = netCode;
}
inline NetPacket& Nz::NetPacket::operator=(NetPacket&& packet)
{
FreeStream();
ByteStream::operator=(std::move(packet));
m_buffer = std::move(packet.m_buffer);
m_memoryStream = std::move(packet.m_memoryStream);
m_netCode = packet.m_netCode;
///< Redirect memory stream to the moved buffer
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode());
SetStream(&m_memoryStream);
return *this;
}
}
#include <Nazara/Core/DebugOff.hpp>