]> Dogcows Code - chaz/yoink/blob - src/Moof/Packet.hh
208b22e9bc75fa51af739546708e7023206b6bdc
[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
44 size_t write(const void* bytes, size_t size);
45
46 Packet& operator>>(bool& value);
47 Packet& operator>>(int8_t& value);
48 Packet& operator>>(int16_t& value);
49 Packet& operator>>(int32_t& value);
50 Packet& operator>>(int64_t& value);
51 Packet& operator>>(uint8_t& value);
52 Packet& operator>>(uint16_t& value);
53 Packet& operator>>(uint32_t& value);
54 Packet& operator>>(uint64_t& value);
55 Packet& operator>>(float& value);
56 Packet& operator>>(double& value);
57
58 size_t read(void* bytes, size_t size);
59
60
61 const char* bytes() const
62 {
63 return &mBuffer.get()[mR];
64 }
65
66 size_t size() const
67 {
68 return mW - mR;
69 }
70
71
72 private:
73
74 boost::shared_array<char> mBuffer;
75 size_t mSize;
76
77 size_t mR;
78 size_t mW;
79
80 size_t mBoolR;
81 size_t mBoolW;
82 size_t mBoolNumR;
83 size_t mBoolNumW;
84 };
85
86
87 inline Packet& operator<<(Packet& packet, const char* value)
88 {
89 uint16_t length = strlen(value);
90 packet << length;
91 packet.write(value, length);
92 return packet;
93 }
94
95 inline Packet& operator<<(Packet& packet, const std::string& value)
96 {
97 packet << (uint16_t)value.length();
98 packet.write(value.c_str(), value.length());
99 return packet;
100 }
101
102 template <class T>
103 inline Packet& operator<<(Packet& packet, const std::vector<T>& value)
104 {
105 packet << (uint8_t)value.size();
106 typename std::vector<T>::const_iterator it;
107 for (it = value.begin(); it != value.end(); ++it)
108 {
109 packet << *it;
110 }
111 return packet;
112 }
113
114
115 inline Packet& operator>>(Packet& packet, std::string& value)
116 {
117 uint8_t length = 0;
118 packet >> length;
119
120 char str[256];
121 size_t charsRead = packet.read(str, length);
122 value.assign(str, charsRead);
123 return packet;
124 }
125
126 template <class T>
127 inline Packet& operator>>(Packet& packet, std::vector<T>& value)
128 {
129 uint8_t size = 0;
130 packet >> size;
131
132 value.clear();
133 for (uint8_t i = 0; i < size; ++i)
134 {
135 T item;
136 packet >> item;
137 value.push_back(item);
138 }
139 return packet;
140 }
141
142
143
144 } // namespace Mf
145
146 #endif // _MOOF_PACKET_HH_
147
This page took 0.034682 seconds and 4 git commands to generate.