]> Dogcows Code - chaz/yoink/blob - src/Moof/Packet.hh
198212c352646984e3a3352a6705a73e0de9c600
[chaz/yoink] / src / Moof / Packet.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #ifndef _MOOF_PACKET_HH_
13 #define _MOOF_PACKET_HH_
14
15 #include <cstring>
16 #include <string>
17 #include <vector>
18
19 #include <boost/shared_array.hpp>
20
21
22 namespace Mf {
23
24
25 class Packet
26 {
27 public:
28
29 Packet(size_t size = 1024);
30 Packet(const char* data, size_t size);
31
32 Packet& operator<<(bool value);
33 Packet& operator<<(int8_t value);
34 Packet& operator<<(int16_t value);
35 Packet& operator<<(int32_t value);
36 Packet& operator<<(int64_t value);
37 Packet& operator<<(uint8_t value);
38 Packet& operator<<(uint16_t value);
39 Packet& operator<<(uint32_t value);
40 Packet& operator<<(uint64_t value);
41 //Packet& operator<<(float value);
42 //Packet& operator<<(double value);
43 //Packet& operator<<(long double value);
44
45 size_t write(const void* bytes, size_t size);
46
47 Packet& operator>>(bool& value);
48 Packet& operator>>(int8_t& value);
49 Packet& operator>>(int16_t& value);
50 Packet& operator>>(int32_t& value);
51 Packet& operator>>(int64_t& value);
52 Packet& operator>>(uint8_t& value);
53 Packet& operator>>(uint16_t& value);
54 Packet& operator>>(uint32_t& value);
55 Packet& operator>>(uint64_t& value);
56
57 size_t read(void* bytes, size_t size);
58
59
60 const char* bytes() const
61 {
62 return &mBuffer.get()[mR];
63 }
64
65 size_t size() const
66 {
67 return mW - mR;
68 }
69
70
71 private:
72
73 boost::shared_array<char> mBuffer;
74 size_t mSize;
75
76 size_t mR;
77 size_t mW;
78
79 size_t mBoolR;
80 size_t mBoolW;
81 size_t mBoolNumR;
82 size_t mBoolNumW;
83 };
84
85
86 inline Packet& operator<<(Packet& packet, const char* value)
87 {
88 uint8_t length = strnlen(value, 255);
89 packet << length;
90 packet.write(value, length);
91 return packet;
92 }
93
94 inline Packet& operator<<(Packet& packet, const std::string& value)
95 {
96 packet << (uint8_t)value.length();
97 packet.write(value.c_str(), value.length());
98 return packet;
99 }
100
101 template <class T>
102 inline Packet& operator<<(Packet& packet, const std::vector<T>& value)
103 {
104 packet << (uint8_t)value.size();
105 typename std::vector<T>::const_iterator it;
106 for (it = value.begin(); it != value.end(); ++it)
107 {
108 packet << *it;
109 }
110 return packet;
111 }
112
113
114 inline Packet& operator>>(Packet& packet, std::string& value)
115 {
116 uint8_t length = 0;
117 packet >> length;
118
119 char str[256];
120 size_t charsRead = packet.read(str, length);
121 value.assign(str, charsRead);
122 return packet;
123 }
124
125 template <class T>
126 inline Packet& operator>>(Packet& packet, std::vector<T>& value)
127 {
128 uint8_t size = 0;
129 packet >> size;
130
131 value.clear();
132 for (uint8_t i = 0; i < size; ++i)
133 {
134 T item;
135 packet >> item;
136 value.push_back(item);
137 }
138 return packet;
139 }
140
141
142
143 } // namespace Mf
144
145 #endif // _MOOF_PACKET_HH_
146
This page took 0.038185 seconds and 4 git commands to generate.