-
Notifications
You must be signed in to change notification settings - Fork 1
/
packer.h
276 lines (231 loc) · 8.42 KB
/
packer.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#ifndef MSGPACK_PACKER_H
#define MSGPACK_PACKER_H
#include <limits>
#include <string>
#include <iterator>
#include <algorithm>
#include <cstring>
#include <type_traits>
#include <vector>
#include "platform.h"
#include <codecvt>
namespace msgpack {
class packer {
public:
using buffer_type = std::vector<uint8_t>;
template <typename T> struct is_pair : std::false_type {};
template <typename K, typename V> struct is_pair<std::pair<K, V>> : std::true_type {};
inline packer& operator<<(std::nullptr_t);
template<typename T> typename std::enable_if<std::is_same<bool, T>::value, packer&>::type
operator<<(const T value);
inline packer& operator<<(const int32_t value);
inline packer& operator<<(const int64_t value);
inline packer& operator<<(const uint32_t value);
inline packer& operator<<(const uint64_t value);
inline packer& operator<<(const float value);
inline packer& operator<<(const double value);
inline packer& operator<<(const std::string& str);
inline packer& operator<<(const std::wstring& str);
inline packer& operator<<(const char* str);
inline packer& operator<<(const packer& value);
template <typename T> typename std::enable_if<! std::is_fundamental<T>::value, packer&>::type
operator <<(const T& val) {
return put<T>(std::begin(val), std::end(val));
}
template<typename T, size_t N> packer& operator<<(const T (& array)[N]);
template <typename ... _Args> packer& array(const _Args& ... args) {
put_array_length(sizeof...(args));
int unused[] = { (this->operator<<(args), 0)... };
(void) unused;
return *this;
}
template<typename K, typename V, typename ... _Args> packer& map(const K& k, const V& v, const _Args& ... args) {
put_map_length(sizeof...(args) / 2 + 1);
map_next(k, v, args...);
return *this;
}
std::vector<uint8_t> get_buffer() const {
return _buffer;
}
private:
buffer_type _buffer;
void put_byte(const uint8_t b) {
_buffer.emplace_back(b);
}
template<typename T> void put_numeric(const T t);
inline void put_string_length(size_t length);
inline void put_array_length(size_t length);
inline void put_map_length(size_t length);
template<typename T, typename U = typename std::iterator_traits<typename T::const_iterator>::value_type>
typename std::enable_if<is_pair<U>::value, packer&>::type
put(typename T::const_iterator begin, typename T::const_iterator end) {
put_map_length(static_cast<size_t>(std::distance(begin, end)));
std::for_each(begin, end, [this](const std::pair<typename U::first_type, typename U::second_type>& e) {
*this << e.first;
*this << e.second;
});
return *this;
}
template<typename T, typename U = typename std::iterator_traits<typename T::const_iterator>::value_type>
typename std::enable_if<! is_pair<U>::value, packer&>::type
put(typename T::const_iterator begin, typename T::const_iterator end) {
put_array_length(static_cast<size_t>(std::distance(begin, end)));
std::for_each(begin, end, [this](const U& e) {
*this << e;
});
return *this;
}
void map_next() {}
template<typename K, typename V, typename ... _Args> void map_next(const K& k, const V& v, const _Args& ... args) {
static_assert(std::is_same<typename std::decay<K>::type, char*>::value
|| std::is_same<typename std::decay<K>::type, const char*>::value
|| std::is_same<typename std::decay<K>::type, std::string>::value, "invalid key type");
*this << k;
*this << v;
map_next(args...);
};
};
packer& packer::operator<<(std::nullptr_t) {
put_byte(0xc0);
return *this;
}
template<typename T> typename std::enable_if<std::is_same<bool, T>::value, packer&>::type
packer::operator<<(const T value) {
if (value) {
put_byte(0xc3);
} else {
put_byte(0xc2);
}
return *this;
}
packer& packer::operator<<(const int32_t value) {
if ((value >= 0 && value <= 0x7f) || (value < 0 && value >= -32)) {
put_byte(static_cast<uint8_t>(value));
} else if (value >= std::numeric_limits<int8_t>::min() && value <= std::numeric_limits<int8_t>::max()) {
put_byte(0xd0);
put_byte(static_cast<uint8_t>(value));
} else if (value >= std::numeric_limits<int16_t>::min() && value <= std::numeric_limits<int16_t>::max()) {
put_byte(0xd1);
put_numeric(static_cast<const int16_t>(value));
} else {
put_byte(0xd2);
put_numeric(value);
}
return *this;
}
packer& packer::operator<<(const int64_t value) {
if (value >= std::numeric_limits<int32_t>::min() && value <= std::numeric_limits<int32_t>::max()) {
*this << static_cast<int32_t>(value);
} else {
put_byte(0xd3);
put_numeric(value);
}
return *this;
}
packer& packer::operator<<(const uint32_t value) {
if (value <= 0x7f) {
put_byte(static_cast<uint8_t>(value));
} else if (value <= std::numeric_limits<uint8_t>::max()) {
put_byte(0xcc);
put_byte(static_cast<uint8_t>(value));
} else if (value <= std::numeric_limits<uint16_t>::max()) {
put_byte(0xcd);
put_numeric(static_cast<const int16_t>(value));
} else {
put_byte(0xce);
put_numeric(value);
}
return *this;
}
packer& packer::operator<<(const uint64_t value) {
if (value <= std::numeric_limits<uint32_t>::max()) {
*this << static_cast<uint32_t>(value);
} else {
put_byte(0xcf);
put_numeric(value);
}
return *this;
}
packer& packer::operator<<(const float value) {
put_byte(0xca);
put_numeric(value);
return *this;
}
packer& packer::operator<<(const double value) {
put_byte(0xcb);
put_numeric(value);
return *this;
}
packer& packer::operator<<(const std::string& str) {
put_string_length(str.length());
std::copy(str.data(), str.data() + str.length(), back_inserter(_buffer));
return *this;
}
packer& packer::operator <<(const std::wstring& str) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> cvt;
return *this << cvt.to_bytes(str);
}
packer& packer::operator<<(const char* str) {
const size_t len = strlen(str);
put_string_length(len);
std::copy(str, str + len, back_inserter(_buffer));
return *this;
}
packer& packer::operator<<(const packer& value) {
_buffer.reserve(_buffer.size() + value._buffer.size());
_buffer.insert(_buffer.end(), value._buffer.cbegin(), value._buffer.cend());
return *this;
}
template<typename T> void packer::put_numeric(const T t) {
union {
T data;
uint8_t bytes[sizeof(T)];
} cvt = { t };
cvt.data = platform::hton(cvt.data);
for (uint8_t b : cvt.bytes) { put_byte(b); }
}
template<typename T, size_t N> packer& packer::operator<<(const T (& array)[N]) {
put_array_length(N);
std::for_each(array, array + N, [this] (const T& e) {
*this << e;
});
return *this;
}
void packer::put_string_length(size_t length) {
if (length < 32) {
put_byte(uint8_t { 0xa0u } + static_cast<uint8_t>(length));
} else if (length <= std::numeric_limits<uint8_t>::max()) {
put_byte(0xd9);
put_byte(static_cast<uint8_t>(length));
} else if (length <= std::numeric_limits<uint16_t>::max()) {
put_byte(0xda);
put_numeric(static_cast<uint16_t>(length));
} else if (length <= std::numeric_limits<uint32_t>::max()) {
put_byte(0xdb);
put_numeric(static_cast<uint32_t>(length));
}
}
void packer::put_array_length(size_t length) {
if (length < 16) {
put_byte(uint8_t { 0x90u } + static_cast<uint8_t>(length));
} else if (length <= std::numeric_limits<uint16_t>::max()) {
put_byte(0xdc);
put_numeric(static_cast<uint16_t>(length));
} else if (length <= std::numeric_limits<uint32_t>::max()) {
put_byte(0xdd);
put_numeric(static_cast<uint32_t>(length));
}
}
void packer::put_map_length(size_t length) {
if (length < 16) {
put_byte(uint8_t { 0x80u } + static_cast<uint8_t>(length));
} else if (length <= std::numeric_limits<uint16_t>::max()) {
put_byte(0xde);
put_numeric(static_cast<uint16_t>(length));
} else if (length <= std::numeric_limits<uint32_t>::max()) {
put_byte(0xdf);
put_numeric(static_cast<uint32_t>(length));
}
}
}
#endif //MSGPACK_PACKER_H