forked from CopernicaMarketingSoftware/AMQP-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutbuffer.h
263 lines (235 loc) · 5.16 KB
/
outbuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* OutBuffer.h
*
* This is a utility class for writing various data types to a binary
* string, and converting the values to network byte order
*
* @copyright 2014 Copernica BV
*/
/**
* Set up namespace
*/
namespace AMQP {
/**
* Class definition
*/
class OutBuffer
{
private:
/**
* Pointer to the beginning of the buffer
* @var char*
*/
char *_buffer;
/**
* Pointer to the buffer to be filled
* @var char*
*/
char *_current;
/**
* Current size of the buffer
* @var size_t
*/
size_t _size;
/**
* The total capacity of the out buffer
* @var size_t
*/
size_t _capacity;
public:
/**
* Constructor
* @param capacity
*/
OutBuffer(uint32_t capacity)
{
// initialize members
_size = 0;
_capacity = capacity;
_buffer = _current = new char[capacity];
}
/**
* Copy constructor
* @param that
*/
OutBuffer(const OutBuffer &that)
{
// initialize members
_size = that._size;
_capacity = that._capacity;
_buffer = new char[_capacity];
_current = _buffer + _size;
// copy memory
memcpy(_buffer, that._buffer, _size);
}
/**
* Move constructor
* @param that
*/
OutBuffer(OutBuffer &&that)
{
// copy all members
_size = that._size;
_capacity = that._capacity;
_buffer = that._buffer;
_current = that._current;
// reset the other object
that._size = 0;
that._capacity = 0;
that._buffer = nullptr;
that._current = nullptr;
}
/**
* Destructor
*/
virtual ~OutBuffer()
{
if (_buffer) delete[] _buffer;
}
/**
* Get access to the internal buffer
* @return const char*
*/
const char *data()
{
return _buffer;
}
/**
* Current size of the output buffer
* @return size_t
*/
size_t size()
{
return _size;
}
/**
* Add a binary buffer to the buffer
* @param string char* to the string
* @param size size of string
*/
void add(const char *string, uint32_t size)
{
memcpy(_current, string, size);
_current += size;
_size += size;
}
/**
* Add a binary buffer to the buffer
* @param string char* to the string
* @param size size of string
*/
void add(const std::string &string)
{
add(string.c_str(), string.size());
}
/**
* add a uint8_t to the buffer
* @param value value to add
*/
void add(uint8_t value)
{
memcpy(_current, &value, sizeof(value));
_current += sizeof(value);
_size += sizeof(value);
}
/**
* add a uint16_t to the buffer
* @param value value to add
*/
void add(uint16_t value)
{
uint16_t v = htobe16(value);
memcpy(_current, &v, sizeof(v));
_current += sizeof(v);
_size += sizeof(v);
}
/**
* add a uint32_t to the buffer
* @param value value to add
*/
void add(uint32_t value)
{
uint32_t v = htobe32(value);
memcpy(_current, &v, sizeof(v));
_current += sizeof(v);
_size += sizeof(v);
}
/**
* add a uint64_t to the buffer
* @param value value to add
*/
void add(uint64_t value)
{
uint64_t v = htobe64(value);
memcpy(_current, &v, sizeof(v));
_current += sizeof(v);
_size += sizeof(v);
}
/**
* add a int8_t to the buffer
* @param value value to add
*/
void add(int8_t value)
{
memcpy(_current, &value, sizeof(value));
_current += sizeof(value);
_size += sizeof(value);
}
/**
* add a int16_t to the buffer
* @param value value to add
*/
void add(int16_t value)
{
int16_t v = htobe16(value);
memcpy(_current, &v, sizeof(v));
_current += sizeof(v);
_size += sizeof(v);
}
/**
* add a int32_t to the buffer
* @param value value to add
*/
void add(int32_t value)
{
int32_t v = htobe32(value);
memcpy(_current, &v, sizeof(v));
_current += sizeof(v);
_size += sizeof(v);
}
/**
* add a int64_t to the buffer
* @param value value to add
*/
void add(int64_t value)
{
int64_t v = htobe64(value);
memcpy(_current, &v, sizeof(v));
_current += sizeof(v);
_size += sizeof(v);
}
/**
* add a float to the buffer
* @param value value to add
*/
void add(float value)
{
memcpy(_current, &value, sizeof(value));
_current += sizeof(value);
_size += sizeof(value);
}
/**
* add a double to the buffer
* @param value value to add
*/
void add(double value)
{
memcpy(_current, &value, sizeof(value));
_current += sizeof(value);
_size += sizeof(value);
}
};
/**
* End of namespace
*/
}