X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FPacket.hh;h=1a5e42fed31bc696cae6913194f535d268e0a99a;hp=096f112ca063289f502dc7b5f60395188a1f8d70;hb=264bdbb09bc86797f1f80d151ac408cb780b9355;hpb=19d555013569026c9e68784ea560cd2e5a21bc5e diff --git a/src/Moof/Packet.hh b/src/Moof/Packet.hh index 096f112..1a5e42f 100644 --- a/src/Moof/Packet.hh +++ b/src/Moof/Packet.hh @@ -32,6 +32,9 @@ public: Packet(size_t size = PAGE_SIZE); Packet(const char* data, size_t size); + Packet(const Packet& copy); + Packet& operator=(const Packet& copy); + ~Packet(); Packet& operator<<(bool value); @@ -63,6 +66,9 @@ public: size_t read(void* bytes, size_t size); + void clear(); + + const char* bytes() const { return mBuffer + mR; @@ -97,17 +103,31 @@ inline Packet& operator<<(Packet& packet, const char* value) return packet; } -inline Packet& operator<<(Packet& packet, const std::string& value) +template +inline Packet& operator<<(Packet& packet, const std::basic_string& value) { packet << (uint16_t)value.length(); - packet.write(value.c_str(), value.length()); + packet.write(value.data(), value.length() * sizeof(T)); + return packet; +} + +template +inline Packet& operator>>(Packet& packet, std::basic_string& value) +{ + uint16_t length = 0; + packet >> length; + + T str[length]; + size_t charsRead = packet.read(str, length * sizeof(T)); + value.assign(str, charsRead); return packet; } + template inline Packet& operator<<(Packet& packet, const std::vector& value) { - packet << (uint8_t)value.size(); + packet << (uint16_t)value.size(); typename std::vector::const_iterator it; for (it = value.begin(); it != value.end(); ++it) { @@ -116,26 +136,14 @@ inline Packet& operator<<(Packet& packet, const std::vector& value) return packet; } - -inline Packet& operator>>(Packet& packet, std::string& value) -{ - uint8_t length = 0; - packet >> length; - - char str[256]; - size_t charsRead = packet.read(str, length); - value.assign(str, charsRead); - return packet; -} - template inline Packet& operator>>(Packet& packet, std::vector& value) { - uint8_t size = 0; + uint16_t size = 0; packet >> size; value.clear(); - for (uint8_t i = 0; i < size; ++i) + for (uint16_t i = 0; i < size; ++i) { T item; packet >> item; @@ -145,7 +153,6 @@ inline Packet& operator>>(Packet& packet, std::vector& value) } - } // namespace Mf #endif // _MOOF_PACKET_HH_