-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmp_reader.h
More file actions
1102 lines (991 loc) · 31.4 KB
/
mp_reader.h
File metadata and controls
1102 lines (991 loc) · 31.4 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <chrono>
#include <cstddef>
#include <stdexcept>
#include <optional>
#include <vector>
#include <map>
#include <cmath>
#include <charconv>
#include "msgpuck/ext_tnt.h"
#include "msgpuck/msgpuck.h"
#include "wtf_buffer.h"
#include "misc.h"
#if defined _WIN32 || defined __CYGWIN__
#ifdef BUILDING_DLL
#ifdef __GNUC__
#define DLL_PUBLIC __attribute__ ((dllexport))
#else
#define DLL_PUBLIC __declspec(dllexport)
#endif
#else
#ifdef __GNUC__
#define DLL_PUBLIC __attribute__ ((dllimport))
#else
#define DLL_PUBLIC __declspec(dllimport)
#endif
#endif
#define DLL_LOCAL
#else
#if __GNUC__ >= 4
#define DLL_PUBLIC __attribute__ ((visibility ("default")))
#define DLL_LOCAL __attribute__ ((visibility ("hidden")))
#else
#define DLL_PUBLIC
#define DLL_LOCAL
#endif
#endif
class wtf_buffer;
class mp_reader_error;
struct mp_plain;
/// Initialize mp_reader environment. It overrides ext types print functions for now.
inline void mp_initialize()
{
mp_snprint_ext = mp_snprint_ext_tnt;
mp_fprint_ext = mp_fprint_ext_tnt;
}
inline std::string mpuck_type_name(mp_type type)
{
switch (type)
{
case MP_NIL:
return "nil";
case MP_UINT:
return "uint";
case MP_INT:
return "int";
case MP_STR:
return "string";
case MP_BIN:
return "bin";
case MP_ARRAY:
return "array";
case MP_MAP:
return "map";
case MP_BOOL:
return "bool";
case MP_FLOAT:
return "float";
case MP_DOUBLE:
return "double";
case MP_EXT:
return "ext";
}
return "MPUCK:" + std::to_string(static_cast<int>(type));
}
template <typename MP>
std::string hex_dump_mp(const MP &mp, const char *pos);
struct mp_plain
{
inline mp_plain(const wtf_buffer &buf) : mp_plain(buf.data(), buf.end) {}
inline mp_plain(const std::vector<char> &buf)
: mp_plain(buf.data(), buf.data() + buf.size()){}
inline constexpr mp_plain(const char *begin = nullptr, const char *end = nullptr)
: begin(begin), end(end)
{}
/// true if not empty
inline constexpr operator bool() const noexcept
{
if (end)
return begin && end > begin;
return false;
}
const char *begin = nullptr;
const char *end = nullptr;
};
/// msgpack parsing error
class DLL_PUBLIC mp_reader_error : public std::runtime_error
{
public:
inline mp_reader_error(const std::string &msg, const mp_plain &reader, const char *pos = nullptr)
: runtime_error(msg + '\n' + hex_dump_mp(reader, pos)) {}
};
#undef DLL_PUBLIC
#undef DLL_LOCAL
struct mp_array : public mp_plain
{
mp_array() = default;
inline mp_array(const wtf_buffer &buf) : mp_array(buf.data(), buf.end) {};
inline mp_array(const char *begin, const char *end = nullptr) : mp_plain(begin, end)
{
if (!begin)
return;
auto head = begin;
auto type = mp_typeof(*head);
if (type == MP_ARRAY)
{
cardinality = mp_decode_array(&head);
}
else if (type == MP_EXT)
{
int8_t ext_type = 0;
mp_decode_extl(&head, &ext_type);
if (ext_type == MP_ERROR) // extract error stack
{
// https://www.tarantool.io/en/doc/2.11/dev_guide/internals/msgpack_extensions/#the-error-type
// acquire key 0 value from the topmost map
uint32_t i = mp_decode_map(&head);
while (i--)
{
uint32_t key = mp_decode_uint(&head);
auto value_pos = head;
if (key == 0x00) // stack
{
cardinality = mp_decode_array(&head);
mp_next(&value_pos);
this->end = value_pos;
this->begin = head;
return;
}
}
throw mp_reader_error("MP_ERROR_STACK not found within ext error", *this, head);
}
else
{
throw mp_reader_error("unable to read map from ext type " + std::to_string(type), *this, head);
}
}
else
{
throw mp_reader_error("array expected, got " + mpuck_type_name(type), *this, head);
}
this->begin = head;
}
/// Interpret `begin` as the first item of the array with the specified cardinality.
inline mp_array(const char *begin, size_t cardinality) : mp_plain(begin), cardinality(cardinality){}
/// Interpret `begin` as the first item of the array with the specified cardinality.
inline mp_array(const char *begin, const char *end, size_t cardinality) : mp_plain(begin, end), cardinality(cardinality){}
/// true if not empty
inline operator bool() const noexcept
{
if (end)
return begin && end > begin;
return begin && cardinality;
}
/// Return mp_plain for a value with the specified index.
mp_plain operator[](size_t i) const
{
auto pos = begin;
const char *prev_pos = pos;
while (i-- >= 0)
{
prev_pos = pos;
mp_next(&pos);
}
return {prev_pos, pos};
}
size_t cardinality = 0;
};
struct mp_map : public mp_plain
{
mp_map() = default;
inline mp_map(const char *begin, const char *end = nullptr) : mp_plain(begin, end)
{
auto head = begin;
auto type = mp_typeof(*head);
if (type == MP_MAP)
{
cardinality = mp_decode_map(&head);
}
else if (type == MP_EXT)
{
int8_t ext_type = 0;
mp_decode_extl(&head, &ext_type);
if (ext_type == MP_INTERVAL)
{
// https://www.tarantool.io/en/doc/2.11/dev_guide/internals/msgpack_extensions/#the-interval-type
cardinality = mp_decode_uint(&head);
}
else if (ext_type == MP_ERROR) // acquire error container (map with key 0x31 (PROTO_ERROR_24) and optional 0x52 (IPROTO_ERROR))
{
cardinality = mp_decode_map(&head);
}
else
{
throw mp_reader_error("unable to read map from ext type " + std::to_string(type), *this, head);
}
}
else
{
throw mp_reader_error("map expected, got " + mpuck_type_name(type), *this, head);
}
this->begin = head;
}
// Interpret `begin` as the first pair of the map with the specified cardinality (items*2).
inline mp_map(const char *begin, size_t cardinality) : mp_plain(begin), cardinality(cardinality){}
inline mp_map(const char *begin, const char *end, size_t cardinality) : mp_plain(begin, end), cardinality(cardinality){}
/// true if not empty
inline operator bool() const noexcept
{
if (end)
return begin && end > begin;
return begin && cardinality;
}
/// Return mp_plain for a value with the specified key.
/// Returns mp_plain reader if the key is not found.
template <typename T>
mp_plain find(const T &key) const;
/// Return mp_plain for a value with the specified key.
/// Throws if the key is not found.
template <typename T>
mp_plain operator[](const T &key) const
{
mp_plain res = find(key);
if (!res)
throw mp_reader_error("key not found", *this);
return res;
}
size_t cardinality = 0;
};
template <size_t maxN>
struct mp_span : public mp_array
{
template<typename T>
friend class mp_reader;
template <size_t innerMaxN>
friend class mp_span;
mp_span() = default;
mp_span(const char *begin, const char *end);
/// extract [first_ind, last_ind] span
template<size_t maxResN = maxN>
const mp_span<maxResN> sub(size_t first_ind, size_t last_ind) const
{
static_assert(maxResN <= maxN);
if (first_ind > last_ind)
throw mp_reader_error("first_ind > last_ind", *this);
if (last_ind >= cardinality)
throw mp_reader_error("read out of bounds", *this);
return mp_span<maxResN>(begin + (first_ind ? rbounds[first_ind - 1] : 0),
begin + rbounds[last_ind],
last_ind - first_ind + 1,
&rbounds[first_ind]);
}
/// Returns mp_plain for a value with the specified index.
/// Returns nil msgpack value if the index is out of bounds
/// (useful for accessing tail nullable fields being truncated).
const mp_plain operator[](size_t i) const
{
if (cardinality <= i)
{
static const char mp_nil[] = "\xc0";
return {mp_nil, mp_nil + 1};
}
return sub<1>(i, i);
}
template <size_t maxArgN>
bool operator== (const mp_span<maxArgN>& s) const
{
if (*this && s && (end - begin) == (s.end - s.begin))
return memcmp(begin, s.begin(), s.end - s.begin) == 0;
return false;
}
protected:
std::array<uint32_t, maxN> rbounds;
mp_span(const char *begin, const char *end, size_t cardinality, const uint32_t *first_rbound)
: mp_array(begin, end, cardinality)
{
std::copy(first_rbound, first_rbound + cardinality, (uint32_t*)rbounds.data());
};
};
template <std::size_t N = 1>
struct mp_none {};
template <typename T>
struct mp_optional
{
mp_optional(T &dst, const T &def) : dst(dst), def(def) {}
T &dst;
const T &def;
};
/// messagepack reader
template<typename MP = mp_plain>
class mp_reader
{
template<typename T>
friend class mp_reader;
static_assert(std::derived_from<MP, mp_plain>, "mp_reader operates on mp_plain and its descendants");
protected:
MP _mp;
const char *_current_pos = nullptr;
uint32_t _current_ind = 0;
public:
mp_reader(MP mp) : _mp(mp), _current_pos(_mp.begin) {}
mp_reader(const wtf_buffer &buf) : mp_reader(buf.data(), buf.end) {};
mp_reader(const std::vector<char> &buf) : mp_reader(buf.data(), buf.data() + buf.size()) {};
template<typename ...Args>
mp_reader(Args... args) : _mp(args ...), _current_pos(_mp.begin)
{
}
MP content() const noexcept
{
return _mp;
}
const char* begin() const noexcept
{
return _mp.begin;
}
const char* end() const noexcept
{
return _mp.end;
}
size_t size() const
{
return _mp.end - _mp.begin;
}
const char* pos() const noexcept
{
return _current_pos;
}
uint32_t ind() const noexcept
{
return _current_ind;
}
/// Cardinality of the array or map.
size_t cardinality() const noexcept
requires (std::is_same<MP, mp_array>::value || std::is_same<MP, mp_map>::value)
{
return _mp.cardinality;
}
/// Try to skip `max` items, return actual items skipped.
bool skip(size_t max)
{
for (size_t i = 0; i < max; ++i)
{
if (!has_next())
return false;
skip();
}
return true;
}
/// Skip current encoded item (in case of array/map skips all its elements).
mp_reader& skip()
{
if (!_current_pos || (_mp.end && _current_pos >= _mp.end))
throw mp_reader_error("read out of bounds", _mp, _mp.end);
if constexpr (requires {_mp.cardinality;})
{
size_t c = _mp.cardinality;
if constexpr (std::is_same<MP, mp_map>::value)
c = c * 2;
if (_current_ind >= c)
throw mp_reader_error("read out of bounds", _mp, _current_pos);
}
const char *prev = _current_pos;
mp_next(&_current_pos);
if (_mp.end && _current_pos > _mp.end)
{
_current_pos = prev;
throw mp_reader_error("invalid messagepack", _mp, prev);
}
++_current_ind;
return *this;
}
/// Skip current encoded item and ensure it has expected type.
mp_reader& skip(mp_type type, bool nullable = false)
{
if (!_current_pos)
throw std::runtime_error("no msgpack data to read");
auto actual_type = mp_typeof(*_current_pos);
if (actual_type != type && (!nullable || actual_type != MP_NIL))
throw mp_reader_error(mpuck_type_name(type) + " expected, got " + mpuck_type_name(actual_type), _mp, _current_pos);
return skip();
}
/// Return current encoded iproto message (header + body) within separate reader
/// and move current position to next item.
mp_reader iproto_message()
{
if (_mp.end - _current_pos < 5)
return {_current_pos, _current_pos}; // empty object
if (static_cast<uint8_t>(*_current_pos) != 0xce)
throw mp_reader_error("invalid iproto packet", _mp);
uint64_t response_size = mp_decode_uint(&_current_pos);
if (static_cast<uint64_t>(_mp.end - _current_pos) < response_size)
throw mp_reader_error("partial iproto packet", _mp);
auto head = _current_pos;
_current_pos += response_size;
return mp_reader{mp_plain{head, _current_pos}};
}
/// Extract and serialize value to string (nil -> 'null') and move current position to next item.
std::string to_string(uint32_t flags = 0)
{
const char *data = _current_pos;
skip();
std::string res(256, '\0');
int cnt = mp_snprint(res.data(), static_cast<int>(res.size()), data, flags);
if (cnt >= static_cast<int>(res.size()))
{
res.resize(static_cast<size_t>(cnt + 1));
cnt = mp_snprint(res.data(), static_cast<int>(res.size()), data, flags);
}
if (cnt < 0)
throw mp_reader_error("mp_snprint error", _mp, data);
res.resize(static_cast<size_t>(cnt));
return res;
}
/// Validate next MsgPack item and check right bound if known
inline void check_next() const
{
if (!_current_pos)
return;
if (!_mp.end)
throw mp_reader_error("right bound is not specified", _mp);
if (_current_pos >= _mp.end)
throw mp_reader_error("read out of bounds", _mp, _current_pos);
auto pos = _current_pos;
if (mp_check(&pos, _mp.end))
throw mp_reader_error("invalid messagepack", _mp, _current_pos);
}
/// Validate MsgPack within current buffer (all items)
void check() const
{
if (!_mp.begin)
return;
if (!_mp.end)
throw mp_reader_error("right bound is not specified", *this);
auto pos = _mp.begin;
while (pos < _mp.end)
{
const char *prev = pos;
if (mp_check(&pos, _mp.end))
throw mp_reader_error("invalid messagepack", *this, prev);
}
}
/// Reset the current reading position back to the beginning.
void rewind() noexcept
{
_current_pos = _mp.begin;
_current_ind = 0;
}
/// Return true if the current value is `nil`.
bool is_null() const
{
if (!has_next())
throw mp_reader_error("read out of bounds", _mp, _current_pos);
return mp_typeof(*_current_pos) == MP_NIL;
}
/// Returns `true` if msgpack has more values to read. Pass `true` as an argument to throw an exception
/// when the possibility is unknown.
bool has_next(bool strict = false) const
{
if (_mp.end)
return _current_pos && _current_pos < _mp.end;
else if (!_mp.begin)
return false;
if constexpr (requires {_mp.cardinality;})
{
if constexpr (std::is_same<MP, mp_map>::value)
return _current_pos && _current_ind < _mp.cardinality * 2;
else
return _current_pos && _current_ind < _mp.cardinality;
}
// allow to read at one's risk
if (!strict)
return true;
throw mp_reader_error("unable to determine if next value exists - no right bound specified",
_mp, _current_pos);
}
/// true if not empty
operator bool() const noexcept
{
return _mp;
/*if (_mp.end)
return _mp.begin && _mp.end > _mp.begin;
if constexpr (requires {_mp.cardinality;})
return _mp.begin && _mp.cardinality;
return false;*/
}
/// Returns reader for a value with the specified index.
/// Current parsing position stays unchanged. Throws if the specified index is out of bounds.
mp_reader<mp_plain> operator[](size_t ind) const
requires (!std::is_same<MP, mp_map>::value)
{
if constexpr (requires {_mp.rbounds;})
return {_mp[ind]};
size_t i = 0;
auto tmp = *this;
if (_current_ind <= ind)
i = _current_ind;
else
tmp.rewind();
for (; i < ind; ++i)
tmp.skip();
auto begin = tmp._current_pos;
tmp.skip();
return {mp_plain{begin, tmp._current_pos}};
}
/// Return `mp_reader<mp_plain>` for a value with the specified key.
/// Current parsing position stays unchanged. Throws if the key is not found.
template <typename T>
mp_reader<mp_plain> operator[](const T &key) const
requires (std::is_same<MP, mp_map>::value)
{
return {_mp[key]};
}
template <typename T>
mp_reader<mp_plain> find(const T &key) const
requires (std::is_same<MP, mp_map>::value)
{
return {_mp.find(key)};
}
mp_reader& operator>> (std::string &val)
{
if (!_current_pos)
throw std::runtime_error("no msgpack data to read");
if (mp_typeof(*_current_pos) == MP_EXT)
{
// regular print
if (val.size() < 128)
val.resize(128, '\0');
size_t len = mp_snprint(val.data(), val.size(), _current_pos, UNQUOTE_UUID);
if (len > val.size())
{
val.resize(len + 1, '\0');
len = mp_snprint(val.data(), len + 1, _current_pos, UNQUOTE_UUID);
}
if (len < 0)
throw mp_reader_error("bad ext value content", _mp, _current_pos);
val.resize(len);
skip();
}
else
{
std::string_view tmp;
*this >> tmp;
if (!tmp.data())
throw mp_reader_error("string expected, got no data", _mp);
val.assign(tmp.data(), tmp.size());
}
return *this;
}
mp_reader& operator>> (std::string_view &val)
{
// for the sake of convenience
if (!has_next())
{
val = {};
return *this;
}
auto type = mp_typeof(*_current_pos);
if (type == MP_STR)
{
const char *prev = _current_pos;
uint32_t len = 0;
skip();
const char *value = mp_decode_str(&prev, &len);
val = {value, len};
}
else if (type == MP_NIL)
{
skip();
val = {}; // data() == nullptr
}
else
{
throw mp_reader_error("string expected, got " + mpuck_type_name(type), _mp, _current_pos);
}
return *this;
}
mp_reader& operator>> (bool &val)
{
if (!_current_pos)
throw std::runtime_error("no msgpack data to read");
const char *data = _current_pos;
auto type = mp_typeof(*data);
if (type != MP_BOOL)
throw mp_reader_error("boolean expected, got " + mpuck_type_name(type), _mp, _current_pos);
val = mp_decode_bool(&data);
skip();
return *this;
}
/// Use `>> mp_none()` to skip a value or `>> mp_none<N>()` to skip N items
template<size_t N = 1>
mp_reader& operator>> (mp_none<N>)
{
for (int i = 0; i < N; ++i)
skip();
return *this;
}
template<size_t maxN>
mp_reader& operator>> (mp_span<maxN> &dst)
{
dst.begin = _current_pos;
for (size_t i = 0; i < maxN && has_next(); ++i)
{
skip();
dst.rbounds[i] = _current_pos - dst.begin;
++dst.cardinality;
}
dst.end = _current_pos;
return *this;
}
template <typename C, typename D>
mp_reader& operator>> (std::chrono::time_point<C, D> &val)
{
if (!_current_pos)
throw std::runtime_error("no msgpack data to read");
using namespace std::chrono;
auto type = mp_typeof(*_current_pos);
switch (type) {
case MP_UINT:
case MP_INT:
val = time_point<C, D>(D(read<int64_t>()));
break;
case MP_FLOAT:
case MP_DOUBLE:
val = time_point<C, D>(D(read<double>()));
break;
case MP_EXT:
{
struct tmp {
int32_t nsec;
int16_t tzoffset;
int16_t tzindex;
} tail = {0, 0, 0};
int8_t ext_type;
const char *data = _current_pos;
uint32_t len = mp_decode_extl(&data, &ext_type);
if (ext_type != MP_DATETIME)
throw mp_reader_error("unable to extract time_point from " + mpuck_type_name(type), _mp, data);
if (len != sizeof(int64_t) && len != sizeof(int64_t) + sizeof(tail))
throw mp_reader_error("unexpected MP_DATETIME value", _mp, data);
int64_t epoch;
memcpy(&epoch, data, sizeof(epoch));
data += sizeof(epoch);
if (len != sizeof(int64_t))
memcpy(&tail, data, sizeof(tail));
if (tail.nsec)
val = time_point<C, D>(duration<int64_t, std::nano>(tail.nsec + epoch*1000000000));
else
val = time_point<C, D>(duration<int64_t>(epoch));
skip();
break;
}
default:
throw mp_reader_error("unable to get time_point from " + mpuck_type_name(type), _mp, _current_pos);
}
return *this;
}
template <typename T>
mp_reader& operator>> (std::optional<T> &val)
{
if (!has_next(true))
{
val = std::nullopt;
}
else if (mp_typeof(*_current_pos) == MP_NIL)
{
skip();
val = std::nullopt;
}
else
{
T non_opt;
*this >> non_opt;
val = std::move(non_opt);
}
return *this;
}
template <typename T>
mp_reader& operator>> (mp_optional<T> &&val)
{
if (!has_next(true))
{
val.dst = val.def;
}
else if (mp_typeof(*_current_pos) == MP_NIL)
{
skip();
val.dst = val.def;
}
else
{
*this >> val.dst;
}
return *this;
}
template <typename T>
T read_or(const T &def)
{
if (!has_next(true))
return def;
if (mp_typeof(*_current_pos) == MP_NIL)
{
skip();
return def;
}
return read<T>();
}
// use external operator overload for 128 bit integers
template <typename T, typename = std::enable_if_t<
(std::is_integral_v<T> && sizeof(T) < 16) ||
std::is_floating_point_v<T>
>>
mp_reader& operator>> (T &val)
{
const char *data = _current_pos;
const char *prev_pos = _current_pos;
skip();
auto ext_via_string = [prev_pos, &val, this]() -> mp_reader& {
char tmp[64];
auto len = mp_snprint(tmp, 64, prev_pos, 0);
auto [ptr, ec] = std::from_chars(tmp, tmp + len, val);
if (ec == std::errc())
return *this;
if (ec == std::errc::invalid_argument)
throw mp_reader_error("not a number", _mp, prev_pos);
else if (ec == std::errc::result_out_of_range)
throw mp_reader_error("out of range", _mp, prev_pos);
throw mp_reader_error("error parsing number", _mp, prev_pos);
};
auto type = mp_typeof(*data);
if constexpr (std::is_floating_point_v<T>)
{
if (type == MP_FLOAT)
{
val = mp_decode_float(&data);
return *this;
}
else if (type == MP_DOUBLE)
{
double res = mp_decode_double(&data);
if constexpr (std::is_same_v<T, float>)
{
if (std::isfinite(res) && (res > std::numeric_limits<T>::max() || res < std::numeric_limits<T>::min()))
throw mp_reader_error("value overflow", _mp);
}
val = static_cast<T>(res);
return *this;
}
else if (type == MP_EXT)
{
return ext_via_string();
}
throw mp_reader_error("float expected, got " + mpuck_type_name(type), _mp, prev_pos);
}
else
{
if (type == MP_UINT)
{
uint64_t res = mp_decode_uint(&data);
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(&data);
if (res <= std::numeric_limits<T>::max() && res >= std::numeric_limits<T>::min())
{
val = static_cast<T>(res);
return *this;
}
}
else if (type == MP_EXT)
{
return ext_via_string();
}
else
{
throw mp_reader_error("integer expected, got " + mpuck_type_name(type), _mp, prev_pos);
}
throw mp_reader_error("value overflow", _mp);
}
}
template <typename T>
mp_reader& operator>> (std::vector<T> &val)
{
auto arr = read<mp_reader<mp_array>>();
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& operator>> (std::map<KeyT, ValueT> &val)
{
auto map = read<mp_reader<mp_map>>();
for (size_t i = 0; i < map.cardinality(); ++i)
{
KeyT k;
ValueT v;
map >> k >> v;
val[k] = std::move(v);
}
return *this;
}
template <typename... Args>
mp_reader& operator>> (std::tuple<Args...> &val)
{
auto arr = read<mp_reader<mp_array>>();
std::apply(
[&arr](auto&... item)
{
((arr >> item), ...);
},
val
);
return *this;
}
template <typename... Args>
mp_reader& operator>> (std::tuple<Args&...> val)
{
auto arr = read<mp_reader<mp_array>>();
std::apply(
[&arr](auto&... item)
{
((arr >> item), ...);
},
val
);
return *this;
}
mp_reader& operator>> (mp_reader<mp_plain> &val) = delete;
mp_reader& operator>> (mp_reader<mp_array> &val)
{
val._mp = mp_array(_current_pos);
val._current_pos = val._mp.begin;
val._current_ind = 0;
skip();
if (!val._mp.end)
val._mp.end = _current_pos;
return *this;
}
mp_reader& operator>> (mp_reader<mp_map> &val)
{
val._mp = mp_map(_current_pos);
val._current_pos = val._mp.begin;
val._current_ind = 0;
skip();
val._mp.end = _current_pos;
return *this;
}
template <typename T>
T read()
{
T res;
*this >> res;
return res;
}
template <size_t maxN>
const mp_span<maxN> read()
{
mp_span<maxN> dst{};
*this >> dst;
return dst;
}
template <typename... Args>
mp_reader& values(Args&... args)
{
((*this) >> ... >> args);
return *this;
}
template <typename T>
bool equals(const T &val) const
{
if (!has_next())
throw mp_reader_error("read out of bounds", _mp);
auto begin = _current_pos;
//auto end = begin;
//mp_next(&end);
//mp_reader tmp(begin, end);
mp_reader tmp(_current_pos);
auto type = mp_typeof(*_current_pos);
if constexpr (std::is_same_v<T, bool>)
{
return type == MP_BOOL && val == tmp.read<T>();
}