forked from parihaaraka/cpp2tnt
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmp_reader.h
More file actions
417 lines (370 loc) · 11.7 KB
/
mp_reader.h
File metadata and controls
417 lines (370 loc) · 11.7 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#ifndef MP_READER_H
#define MP_READER_H
#include <cstddef>
#include <stdexcept>
#include <optional>
#include <vector>
#include <map>
#include <string>
#include <memory>
#include <functional>
#include <limits>
#include "msgpuck/msgpuck.h"
class wtf_buffer;
class mp_map_reader;
class mp_array_reader;
class mp_reader;
std::string hex_dump(const char *begin, const char *end, const char *pos = nullptr);
const char* to_string(mp_type type);
/// messagepack parsing error
class mp_reader_error : public std::runtime_error
{
public:
explicit mp_reader_error(const std::string &msg, const mp_reader &reader);
};
std::string mpuck_type_name(mp_type type);
/// messagepack reader
class mp_reader
{
public:
mp_reader(const wtf_buffer &buf);
mp_reader(const char *begin = nullptr, const char *end = nullptr);
const char* begin() const noexcept;
const char* end() const noexcept;
const char* pos() const noexcept;
/// Skip current encoded item (in case of array/map skips all its elements).
void fast_skip();
/// Skip current encoded item (in case of array/map skips all its elements) and check content.
void skip();
/// Skip current encoded item, verify its type and check content.
void skip(mp_type type, bool nullable = false);
/// Return current encoded map within separate reader and move current position to next item.
[[deprecated("consider using read<mp_map_reader> instead")]] mp_map_reader map();
/// Return current encoded array within separate reader and move current position to next item.
[[deprecated("consider using read<mp_array_reader> instead")]] mp_array_reader array();
/// Return current encoded iproto message (header + body) within separate reader
/// and move current position to next item.
mp_reader iproto_message();
/// Extract and serialize value to string (nil -> 'null') and move current position to next item.
std::string to_string();
/// Reset current reading position back to the beginning.
void rewind() noexcept;
/// Return true if current value is nil.
bool is_null() const;
/// true if msgpack has more values to read
bool has_next() const noexcept;
/// true if not empty
operator bool() const noexcept;
mp_reader& operator>> (std::string &val);
mp_reader& operator>> (std::string_view &val);
template <typename T>
mp_reader& operator>> (std::optional<T> &val)
{
if (mp_typeof(*_current_pos) == MP_NIL)
{
mp_decode_nil(&_current_pos);
val = std::nullopt;
return *this;
}
T non_opt;
*this >> non_opt;
val = std::move(non_opt);
return *this;
}
// use external operator overload for 128 bit integers
template <typename T, typename = std::enable_if_t<std::is_integral_v<T> && sizeof(T) < 16>>
mp_reader& operator>> (T &val)
{
if (_current_pos >= _end)
throw mp_reader_error("read out of bounds", *this);
auto type = mp_typeof(*_current_pos);
if constexpr (std::is_same_v<T, bool>)
{
if (type == MP_BOOL)
{
val = mp_decode_bool(&_current_pos);
return *this;
}
throw mp_reader_error("boolean expected, got " + mpuck_type_name(type), *this);
}
else
{
if (type == MP_UINT)
{
uint64_t res = mp_decode_uint(&_current_pos);
if (res <= std::numeric_limits<T>::max())
{
val = static_cast<T>(res);
return *this;
}
}
else if (type == MP_INT)
{
int64_t res = mp_decode_int(&_current_pos);
if (res <= std::numeric_limits<T>::max() && res >= std::numeric_limits<T>::min())
{
val = static_cast<T>(res);
return *this;
}
}
else
{
throw mp_reader_error("integer expected, got " + mpuck_type_name(type), *this);
}
throw mp_reader_error("value overflow", *this);
}
}
/*
template <typename K,typename V>
mp_reader& operator>> (std::map<K,V>& val);
template <typename T>
mp_reader& operator>> (std::vector<T>& val);
*/
template <typename T>
mp_reader& operator>> (std::vector<T> &val);
template <typename KeyT, typename ValueT>
mp_reader& operator>> (std::map<KeyT, ValueT> &val);
template <typename... Args>
mp_reader& operator>> (std::tuple<Args...> &val);
template <typename... Args>
mp_reader& operator>> (std::tuple<Args&...> val);
mp_reader& operator>> (mp_reader &val) = delete;
mp_reader& operator>> (mp_map_reader &val);
mp_reader& operator>> (mp_array_reader &val);
template <typename T>
T read()
{
T res;
*this >> res;
return res;
}
template <typename T>
[[deprecated("consider using read instead")]] T value()
{
return read<T>();
}
template <typename... Args>
mp_reader& values(Args&... args)
{
((*this) >> ... >> args);
return *this;
}
template <typename T>
bool equals(const T &val) const
{
if (_current_pos >= _end)
throw mp_reader_error("read out of bounds", *this);
auto begin = _current_pos;
auto end = begin;
mp_next(&end);
mp_reader tmp(begin, end);
auto type = mp_typeof(*_current_pos);
if constexpr (std::is_same_v<T, bool>)
return type == MP_BOOL && val == tmp.read<T>();
else if constexpr (std::is_enum_v<T> || (std::is_integral_v<T> && sizeof(T) < 16))
{
if (type == MP_UINT && val >= 0)
{
uint64_t res = mp_decode_uint(&begin);
return res == static_cast<uint64_t>(val);
}
else if (type == MP_INT)
{
int64_t res = mp_decode_int(&begin);
if (res < 0 && val < 0)
return res == val;
if (res >= 0 && val >= 0)
return static_cast<uint64_t>(res) == static_cast<uint64_t>(val);
}
}
else if constexpr (std::is_same_v<T, std::string_view>)
{
if (val.data() == nullptr)
return type == MP_NIL;
else if (type == MP_STR)
return val == tmp.read<std::string_view>();
}
else if constexpr (std::is_same_v<T, std::string>)
{
if (type == MP_STR)
return val == tmp.read<std::string_view>();
}
else if constexpr (std::is_same_v<typename std::decay_t<T>, char *>)
{
if (val == nullptr)
return type == MP_NIL;
else if (type == MP_STR)
return val == tmp.read<std::string_view>();
}
else
{
throw mp_reader_error("unsupported key type to find within map", *this);
}
return false;
}
protected:
const char *_begin, *_end, *_current_pos;
};
/// messagepack map reader
class mp_map_reader : public mp_reader
{
public:
mp_map_reader() = default;
/// Return reader for the value with a specified key.
/// Current parsing position stays unchanged. Throws if the key is not found.
template <typename T>
mp_reader operator[](const T &key) const
{
mp_reader res = find(key);
if (!res)
throw mp_reader_error("key not found", *this);
return res;
}
/// Return reader for the value with a specified key.
/// Current parsing position stays unchanged. Returns empty reader if the key is not found.
template <typename T>
mp_reader find(const T &key) const
{
mp_reader tmp(_begin, _end); // to keep it const
auto n = _cardinality;
while (n-- > 0)
{
bool found = tmp.equals(key);
tmp.fast_skip(); // skip a key
auto value_begin = tmp.pos();
tmp.fast_skip();
auto value_end = tmp.pos();
if (found)
return {value_begin, value_end};
}
return {nullptr, nullptr};
}
/// The map's cardinality.
size_t cardinality() const noexcept;
private:
friend class mp_reader;
mp_map_reader(const char *begin, const char *end, size_t cardinality);
size_t _cardinality = 0;
};
/// messagepack array reader
class mp_array_reader : public mp_reader
{
public:
mp_array_reader() = default;
/// Return reader for a value with specified index.
/// Current parsing position stays unchanged. Throws if specified index out of bounds.
mp_reader operator[](size_t ind) const;
/// The array's cardinality.
size_t cardinality() const noexcept;
template <typename T>
mp_array_reader& operator>> (std::optional<T> &val)
{
// try to interpret out of bounds items as trailing tuple fields with NULL values
if (_current_pos >= _end)
val = std::nullopt;
else
mp_reader::operator>>(val);
return *this;
}
// Do not do it this way!
// It breaks out of bounds reading logic (successful for optionals) by switching to mp_reader.
// We need to preserve mp_array_reader type after every reading operation.
//using mp_reader::operator>>;
template <typename T>
mp_array_reader& operator>> (T &val)
{
mp_reader::operator>>(val);
return *this;
}
template <template <typename, typename> class C,
typename T,
typename A = std::allocator<T>>
mp_array_reader& operator>> (C<T, A> &val)
{
mp_reader::operator>>(val);
return *this;
}
template <typename... Args>
mp_array_reader& values(Args&... args)
{
((*this) >> ... >> args);
return *this;
}
private:
friend class mp_reader;
mp_array_reader(const char *begin, const char *end, size_t cardinality);
size_t _cardinality = 0;
};
/*
template <typename T>
mp_reader& mp_reader::operator>> (std::vector<T>& val)
{
auto vector = array();
val.resize(vector.cardinality());
for (size_t i = 0; i < vector.cardinality(); ++i)
vector >> val[i];
return *this;
}
template <typename K,typename V>
mp_reader& mp_reader::operator>> (std::map<K,V>& val)
{
auto map = this->map();
for (size_t i = 0; i < map.cardinality(); ++i)
{
K k;
V v;
map >> k >> v;
val[k] = std::move(v);
}
return *this;
}
*/
template <typename T>
mp_reader& mp_reader::operator>> (std::vector<T> &val)
{
mp_array_reader arr = read<mp_array_reader>();
val.resize(arr.cardinality());
for (size_t i = 0; i < val.size(); ++i)
arr >> val[i];
return *this;
}
template <typename KeyT, typename ValueT>
mp_reader& mp_reader::operator>> (std::map<KeyT, ValueT> &val)
{
mp_map_reader mp_map = read<mp_map_reader>();
for (size_t i = 0; i < mp_map.cardinality(); ++i)
{
KeyT k;
ValueT v;
mp_map >> k >> v;
val[k] = std::move(v);
}
return *this;
}
template <typename... Args>
mp_reader& mp_reader::operator>> (std::tuple<Args...> &val)
{
mp_array_reader arr = read<mp_array_reader>();
std::apply(
[&arr](auto&... item)
{
((arr >> item), ...);
},
val
);
return *this;
}
template <typename... Args>
mp_reader& mp_reader::operator>> (std::tuple<Args&...> val)
{
mp_array_reader arr = read<mp_array_reader>();
std::apply(
[&arr](auto&... item)
{
((arr >> item), ...);
},
val
);
return *this;
}
#endif // MP_READER_H