-
Notifications
You must be signed in to change notification settings - Fork 2
/
msgpack.nvgt
2790 lines (2416 loc) · 79.7 KB
/
msgpack.nvgt
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
/* msgpack.nvgt - fully-featured pure-NVGT msgpack serialization/deserialization library
*
* Copyright (C) 2024 Colton Hill
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
/* type enumeration
* the generic type of each value, also called a format family
* maps to families of formats and corresponding primitive types, depending on range of values
*/
enum mp_type {
MPT_UNDETERMINED = -1,
MPT_INT = 0,
MPT_NIL,
MPT_BOOLEAN,
MPT_FLOAT,
MPT_STRING, // string takes the place of raw from an older version, now distinguished with bin
MPT_BIN,
MPT_ARRAY,
MPT_MAP,
MPT_EXT
}
/* exceptions
* these strings represent common exceptions thrown by this library and can be matched against directly for most cases.
* Specific conditions may result in other exceptions, however, and in that case the only commonality is that they all begin with "msgpack "
*/
const string MP_TYPE_MISMATCH_EXCEPTION = "msgpack type mismatch";
const string MP_INVALID_KEY_TYPE_EXCEPTION = "msgpack invalid key type";
const string MP_RECURSION_LIMIT_EXCEPTION="msgpack maximum recursion depth exceeded";
/* detailed format enumeration
* values in this enum are taken from the starting byte of each format, or the lowest such if a range is possible due to bitfields
* this allows easy comparison with actual bytes against named constants for easier readability
* values named and arranged as presented in the table of formats from the spec
*/
enum mp_format {
MPF_UNDETERMINED = -1,
MPF_POS_FIXINT = 0,
MPF_FIXMAP = 0x80,
MPF_FIXARRAY = 0x90,
MPF_FIXSTR = 0xA0,
MPF_NIL = 0xC0,
MP_INVALID = 0xC1, // never used in msgpack itself, we store here as a potential invalidity marker
MPF_FALSE = 0xC2,
MPF_TRUE = 0xC3,
MPF_BIN8 = 0xC4,
MPF_BIN16 = 0xC5,
MPF_BIN32 = 0xC6,
MPF_EXT8 = 0xC7,
MPF_EXT16 = 0xC8,
MPF_EXT32 = 0xC9,
MPF_FLOAT32 = 0xCA,
MPF_FLOAT64 = 0xCB,
MPF_UINT8 = 0xCC,
MPF_UINT16 = 0xCD,
MPF_UINT32 = 0xCE,
MPF_UINT64 = 0xCF,
MPF_INT8 = 0xD0,
MPF_INT16 = 0xD1,
MPF_INT32 = 0xD2,
MPF_INT64 = 0xD3,
MPF_FIXEXT1 = 0xD4,
MPF_FIXEXT2 = 0xD5,
MPF_FIXEXT4 = 0xD6,
MPF_FIXEXT8 = 0xD7,
MPF_FIXEXT16 = 0xD8,
MPF_STR8 = 0xD9,
MPF_STR16 = 0xDA,
MPF_STR32 = 0xDB,
MPF_ARRAY16 = 0xDC,
MPF_ARRAY32 = 0xDD,
MPF_MAP16 = 0xDE,
MPF_MAP32 = 0xDF,
MPF_NEG_FIXINT = 0xE0,
}
/* count enumeration
* potential argument lengths, telling us how many bytes are in a length/count indicator or else how many bytes make up the value
* If most significant bit is set, indicates a constant width stored in the type information itself
* if most significant bit is clear, indicates the number of bytes to read for a length indicator
* special case: 0 is no further bytes to read (this byte alone specifies the full data), 0x80 is read some number of bits in this byte
* the former is used in literals like booleans and nil,
* whereas the latter is used in formats with fix in the name, where some number of the least significant bits form the count as a small value
*/
enum mp_count {
MPC_UNDETERMINED = -1,
MPC_EMPTY = 0,
MPC_8BIT = 1,
MPC_16BIT = 2,
MPC_32BIT = 4, // nothing is 3 bytes
MPC_SUBFIELD = 0x80,
MPC_1BYTE = 0x81,
MPC_2BYTES = 0x82,
MPC_4BYTES = 0x84,
MPC_8BYTES = 0x88,
MPC_16BYTES = 0x90
}
/* count type enumeration
* indicates whether a count, specified by the above enumeration, is counting bytes or items
* this distinction is important when working with structures like arrays and maps
* where the count is a number of elements to read, which can span any number of bytes
*/
enum mp_count_type {
MPCT_UNDETERMINED = -1,
MPCT_EMPTY = 0, // corresponds to MPC_EMPTY, nothing further to read
MPCT_VALUE, // for numbers
MPCT_BYTES,
MPCT_ITEMS,
MPCT_ITEMS2X // for maps, where you actually read pairs
}
/* info type
* enumeration to be used for indexes into a mapping array
* which gives information about a given start byte
* this info contains the format family and specific format, as well as the count length and type
* this map array shall be a 256x4 array, map every possible start byte to such information
* due to ranges, this needs to be prepared by code, and is done on global initialization
*/
enum mp_info_type {
MPI_FORMAT = 0,
MPI_TYPE,
MPI_COUNT_TYPE,
MPI_COUNT
}
// the mapping itself
const int[][] mp_infomap = mp_prepare_infomap();
const int[][] mp_prepare_infomap() {
int[][] tinfomap(256);
tinfomap[MPF_NIL] = {MPF_NIL, MPT_NIL, MPCT_EMPTY, MPC_EMPTY};
tinfomap[MP_INVALID] = {MP_INVALID, MPT_UNDETERMINED, MPCT_UNDETERMINED, MPC_UNDETERMINED};
tinfomap[MPF_FALSE] = {MPF_FALSE, MPT_BOOLEAN, MPCT_EMPTY, MPC_EMPTY};
tinfomap[MPF_TRUE] = {MPF_TRUE, MPT_BOOLEAN, MPCT_EMPTY, MPC_EMPTY};
tinfomap[MPF_BIN8] = {MPF_BIN8, MPT_BIN, MPCT_BYTES, MPC_8BIT};
tinfomap[MPF_BIN16] = {MPF_BIN16, MPT_BIN, MPCT_BYTES, MPC_16BIT};
tinfomap[MPF_BIN32] = {MPF_BIN32, MPT_BIN, MPCT_BYTES, MPC_32BIT};
tinfomap[MPF_EXT8] = {MPF_EXT8, MPT_EXT, MPCT_BYTES, MPC_8BIT};
tinfomap[MPF_EXT16] = {MPF_EXT16, MPT_EXT, MPCT_BYTES, MPC_16BIT};
tinfomap[MPF_EXT32] = {MPF_EXT32, MPT_EXT, MPCT_BYTES, MPC_32BIT};
tinfomap[MPF_FLOAT32] = {MPF_FLOAT32, MPT_FLOAT, MPCT_VALUE, MPC_4BYTES};
tinfomap[MPF_FLOAT64] = {MPF_FLOAT64, MPT_FLOAT, MPCT_VALUE, MPC_8BYTES};
tinfomap[MPF_UINT8] = {MPF_UINT8, MPT_INT, MPCT_VALUE, MPC_1BYTE};
tinfomap[MPF_UINT16] = {MPF_UINT16, MPT_INT, MPCT_VALUE, MPC_2BYTES};
tinfomap[MPF_UINT32] = {MPF_UINT32, MPT_INT, MPCT_VALUE, MPC_4BYTES};
tinfomap[MPF_UINT64] = {MPF_UINT64, MPT_INT, MPCT_VALUE, MPC_8BYTES};
tinfomap[MPF_INT8] = {MPF_INT8, MPT_INT, MPCT_VALUE, MPC_1BYTE};
tinfomap[MPF_INT16] = {MPF_INT16, MPT_INT, MPCT_VALUE, MPC_2BYTES};
tinfomap[MPF_INT32] = {MPF_INT32, MPT_INT, MPCT_VALUE, MPC_4BYTES};
tinfomap[MPF_INT64] = {MPF_INT64, MPT_INT, MPCT_VALUE, MPC_8BYTES};
tinfomap[MPF_FIXEXT1] = {MPF_FIXEXT1, MPT_EXT, MPCT_BYTES, MPC_1BYTE};
tinfomap[MPF_FIXEXT2] = {MPF_FIXEXT2, MPT_EXT, MPCT_BYTES, MPC_2BYTES};
tinfomap[MPF_FIXEXT4] = {MPF_FIXEXT4, MPT_EXT, MPCT_BYTES, MPC_4BYTES};
tinfomap[MPF_FIXEXT8] = {MPF_FIXEXT8, MPT_EXT, MPCT_BYTES, MPC_8BYTES};
tinfomap[MPF_FIXEXT16] = {MPF_FIXEXT16, MPT_EXT, MPCT_BYTES, MPC_16BYTES};
tinfomap[MPF_STR8] = {MPF_STR8, MPT_STRING, MPCT_BYTES, MPC_8BIT};
tinfomap[MPF_STR16] = {MPF_STR16, MPT_STRING, MPCT_BYTES, MPC_16BIT};
tinfomap[MPF_STR32] = {MPF_STR32, MPT_STRING, MPCT_BYTES, MPC_32BIT};
tinfomap[MPF_ARRAY16] = {MPF_ARRAY16, MPT_ARRAY, MPCT_ITEMS, MPC_16BIT};
tinfomap[MPF_ARRAY32] = {MPF_ARRAY32, MPT_ARRAY, MPCT_ITEMS, MPC_32BIT};
tinfomap[MPF_MAP16] = {MPF_MAP16, MPT_MAP, MPCT_ITEMS2X, MPC_16BIT};
tinfomap[MPF_MAP32] = {MPF_MAP32, MPT_MAP, MPCT_ITEMS2X, MPC_32BIT};
for (uint i = MPF_POS_FIXINT; i <= 0x7f; i++) {
tinfomap[i] = {MPF_POS_FIXINT, MPT_INT, MPCT_VALUE, MPC_SUBFIELD};
}
for (uint i = MPF_FIXMAP; i <= 0x8F; i++) {
tinfomap[i] = {MPF_FIXMAP, MPT_MAP, MPCT_ITEMS2X, MPC_SUBFIELD};
}
for (uint i = MPF_FIXARRAY; i <= 0x9F; i++) {
tinfomap[i] = {MPF_FIXARRAY, MPT_ARRAY, MPCT_ITEMS, MPC_SUBFIELD};
}
for (uint i = MPF_FIXSTR; i <= 0xBF; i++) {
tinfomap[i] = {MPF_FIXSTR, MPT_STRING, MPCT_BYTES, MPC_SUBFIELD};
}
for (uint i = 0xE0; i <= 0xFF; i++) {
tinfomap[i] = {MPF_NEG_FIXINT, MPT_INT, MPCT_VALUE, MPC_SUBFIELD};
}
return tinfomap;
}
// class for all values read from the stream, to be stored in maps and arrays
class mp_value {
const int type {
get const { return this.itype; }
}
const int format {
get const {
// something special here, the arrays and maps stored by this might have been mutated!
// to avoid re-wrapping, just recalculate format based on them
if (this.itype == MPT_ARRAY) {
mp_value@[]@ v = this.get_array();
if (v.length() > 0xFFFFFFFF) throw("msgpack array too big to encode");
int format = 0;
uint len = v.length();
if (len <= 0x0F) format = MPF_FIXARRAY;
else if (len <= 0xFFFF) format = MPF_ARRAY16;
else format = MPF_ARRAY32;
return format;
} else if (this.itype == MPT_MAP) {
mp_map@ v = this.get_map();
int format = 0;
uint len = v.get_size();
if (len <= 0x0F) format = MPF_FIXMAP;
else if (len <= 0xFFFF) format = MPF_MAP16;
else format = MPF_MAP32;
return format;
}
// everything else doesn't matter, strings stored here can't be mutated from outside
else return this.iformat;
}
}
private int itype, iformat;
private any@ content;
mp_value(int type, int format, any@ content) {
this.init(type, format, content);
}
// convenience function used by all the constructors, since you can't call this.
private void init(int type, int format, any@ content) {
assert(type != MPT_UNDETERMINED && format != MPF_UNDETERMINED, "Undetermined type and/or format in value initialization");
assert(mp_infomap[format][MPI_TYPE] == type, "type/format mismatch in value initialization");
this.itype = type;
this.iformat = format;
@this.content = @content;
}
// you can't get nil
// this is just shorthand for type checking
const bool is_nil() const {
return this.itype == MPT_NIL;
}
// type getters
const bool get_bool() const {
if (this.itype == MPT_BOOLEAN)
return this.iformat == MPF_TRUE;
throw(MP_TYPE_MISMATCH_EXCEPTION);
return false;
}
// there is no corresponding get_bin method as the types affixed to these functions represent NVGT types, not msgpack types, and NVGT has only got string
const string get_string() const {
if (this.type == MPT_STRING || this.type == MPT_BIN) {
string ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return "";
}
// allow the float and double get methods to implicitly convert integer values if desired
// the actual retrieve from any will take care of this
// this will be disallowed for the explicit get methods by default, but can be forced by passing true
// and the conversion functions for these types will pass true here
// especially for float, loss of precision concerns apply!
const float get_float(bool allow_int_source = false) const {
if (this.itype == MPT_FLOAT || (allow_int_source && this.itype == MPT_INT)) {
float ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0.0f;
}
const double get_double(bool allow_int_source = false) const {
if (this.itype == MPT_FLOAT || (allow_int_source && this.itype == MPT_INT)) {
double ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0.0;
}
// these integer methods will work with any matching integer type, by the working of the any type, to allow easy type conversion
// especially as msgpack encoders may shrink the size of an integer based on its value
// as with dictionary and other int conversions, potential sign issues are left up to the programmer
const uint64 get_uint64() const {
if (this.itype == MPT_INT) {
uint64 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const int64 get_int64() const {
if (this.itype == MPT_INT) {
int64 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const uint32 get_uint32() const {
if (this.itype == MPT_INT) {
uint32 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const int32 get_int32() const {
if (this.itype == MPT_INT) {
int32 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const uint get_uint() const {
if (this.itype == MPT_INT) {
uint ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const int get_int() const {
if (this.itype == MPT_INT) {
int ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const uint16 get_uint16() const {
if (this.itype == MPT_INT) {
uint16 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const int16 get_int16() const {
if (this.itype == MPT_INT) {
int16 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const uint8 get_uint8() const {
if (this.itype == MPT_INT) {
uint8 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
const int8 get_int8() const {
if (this.itype == MPT_INT) {
int8 ret;
content.retrieve(ret);
return ret;
}
throw(MP_TYPE_MISMATCH_EXCEPTION);
return 0;
}
// this can't be mp_value@[]@, else you get an immutable handle, and you actually want to modify it
mp_value@[]@ get_array(bool needs_throw = true) const {
if (this.itype == MPT_ARRAY) {
mp_value@[]@ ret;
content.retrieve(@ret);
return ret;
}
if (needs_throw) throw(MP_TYPE_MISMATCH_EXCEPTION);
return null;
}
mp_map@ get_map(bool needs_throw = true) const {
if (this.itype == MPT_MAP) {
mp_map@ ret;
content.retrieve(@ret);
return ret;
}
if (needs_throw) throw(MP_TYPE_MISMATCH_EXCEPTION);
return null;
}
mp_ext@ get_ext(bool needs_throw = true) const {
if (this.itype == MPT_EXT) {
mp_ext@ ret;
content.retrieve(@ret);
return ret;
}
if (needs_throw) throw(MP_TYPE_MISMATCH_EXCEPTION);
return null;
}
// conversion operators
const bool opImplConv() const {
return this.get_bool();
}
const bool opConv() {
return this.get_bool();
}
// strings will be handled specially
const float opImplConv() const {
return this.get_float(true);
}
const float opConv() {
return this.get_float(true);
}
const double opImplConv() const {
return this.get_double(true);
}
const double opConv() {
return this.get_double(true);
}
const uint64 opImplConv() const {
return this.get_uint64();
}
const uint64 opConv() {
return this.get_uint64();
}
const int64 opImplConv() const {
return this.get_int64();
}
const int64 opConv() {
return this.get_int64();
}
const uint32 opImplConv() const {
return this.get_uint32();
}
const uint32 opConv() {
return this.get_uint32();
}
const int32 opImplConv() const {
return this.get_int32();
}
const int32 opConv() {
return this.get_int32();
}
const uint16 opImplConv() const {
return this.get_uint16();
}
const uint16 opConv() {
return this.get_uint16();
}
const int16 opImplConv() const {
return this.get_int16();
}
const int16 opConv() {
return this.get_int16();
}
const uint8 opImplConv() const {
return this.get_uint8();
}
const uint8 opConv() {
return this.get_uint8();
}
const int8 opImplConv() const {
return this.get_int8();
}
const int8 opConv() {
return this.get_int8();
}
mp_value@[]@ opImplCast() {
return this.get_array(true);
}
mp_value@[]@ opCast() {
return this.get_array(true);
}
mp_value@[] opImplConv() {
return this.get_array();
}
mp_value@[] opConv() {
return this.get_array();
}
mp_map@ opImplCast() {
return this.get_map(true);
}
mp_map@ opCast() {
return this.get_map(true);
}
mp_map opImplConv() {
return this.get_map();
}
mp_map opConv() {
return this.get_map();
}
mp_ext@ opImplCast() {
return this.get_ext(true);
}
mp_ext@ opCast() {
return this.get_ext(true);
}
mp_ext opImplConv() {
return this.get_ext();
}
mp_ext opConv() {
return this.get_ext();
}
// handling of strings
// get_string must return false if the content is not a string, but we allow conversions here
// so that values may be displayed in strings and the like
const string opConv() const {
switch (this.itype) {
case MPT_STRING:
case MPT_BIN: // fall-through, both are equivalent here
return this.get_string(); // the simple case, it's already a string
break;
case MPT_BOOLEAN:
return this.get_bool(); // true or false
break;
case MPT_NIL:
return "null"; // string formatting of nil consistent with the lang
break;
case MPT_FLOAT:
return this.get_double(); // float to double doesn't lose precision and should print the equivalent value
break;
case MPT_INT: { // we have to be careful with this one because conversions might give incorrect values if the wrong sign
switch (this.iformat) {
// aggressive use of fall-through here to lump into signed and unsigned types
case MPF_POS_FIXINT: // actually stored as uint8, though could just as easily be int8, sign bit is never set
case MPF_UINT8:
case MPF_UINT16:
case MPF_UINT32:
case MPF_UINT64:
return this.get_uint64(); // 0-extended to the left, value is equivalent
case MPF_NEG_FIXINT: // actually stored as int8
case MPF_INT8:
case MPF_INT16:
case MPF_INT32:
case MPF_INT64:
return this.get_int64(); // sign extended to the left, value is equivalent
default:
break; // out to throw
}
}
break;
default:
break; // out to throw, no simple conversion matched
}
throw(MP_TYPE_MISMATCH_EXCEPTION); // we don't support stringifying arrays, maps, or extension types like this
return "";
}
const string opImplConv() const {
return string(this); // avoid duplicating code, this calls opConv
}
// now for conversion constructors, so we can load in types from outside
// first, we need some functions to handle all the possible types we can get and turn them into concrete formats
// all the variants of int, signed and unsigned
// upcast to type of same sign and check if positive or negative, then decide format based on ranges
// once format is decided, squish value to conform to it
private void store_positive_int(uint64 v) {
any a;
int format = 0;
if (v <= 0x7f) {
uint8 va = v;
a.store(va);
format = MPF_POS_FIXINT;
} else if (v <= 0xFF) {
uint8 va = v;
a.store(va);
format = MPF_UINT8;
} else if (v <= 0xFFFF) {
uint16 va = v;
a.store(va);
format = MPF_UINT16;
} else if (v <= 0xFFFFFFFF) {
uint va = v;
a.store(va);
format = MPF_UINT32;
} else {
a.store(v);
format = MPF_UINT64;
}
this.init(MPT_INT, format, a);
}
private void store_negative_int(int64 v) {
any a;
int format = 0;
if (v >= -32) {
int8 va = v;
a.store(va);
format = MPF_NEG_FIXINT;
} else if (v >= -128) {
int8 va = v;
a.store(va);
format = MPF_INT8;
} else if (v >= -32768) {
int16 va = v;
a.store(va);
format = MPF_INT16;
} else if (v >= -2147483648) {
int va = v;
a.store(va);
format = MPF_INT32;
} else {
a.store(v);
format = MPF_INT64;
}
this.init(MPT_INT, format, a);
}
// the rest of the logic can take place in constructors, as it wouldn't otherwise be duplicated
mp_value() {
// empty constructor, perhaps useful way to store nil?
this.init(MPT_NIL, MPF_NIL, null);
}
mp_value(bool v) {
this.init(MPT_BOOLEAN, (v ? MPF_TRUE : MPF_FALSE), any(v));
}
mp_value(float v) {
this.init(MPT_FLOAT, MPF_FLOAT32, any(v));
}
mp_value(double v) {
// if we cast to float, will the value be equivalent?
// if so, we're saving 4 bytes
float vf = v;
if (v == vf) {
this.init(MPT_FLOAT, MPF_FLOAT32, any(vf));
} else {
this.init(MPT_FLOAT, MPF_FLOAT64, any(v));
}
}
// store strings as either text or bin, depending on the value of is_bin
mp_value(const string &in v, bool is_bin = false) {
if (v.length() > 0xFFFFFFFF) throw("msgpack string too long to encode");
int format = 0;
uint len = v.length();
if (!is_bin) {
// str type, we have fixstr available
if (len <= 31) format = MPF_FIXSTR;
else if (len <= 0xFF) format = MPF_STR8;
else if (len <= 0xFFFF) format = MPF_STR16;
else format = MPF_STR32;
} else {
// bin type
if (len <= 0xFF) format = MPF_BIN8;
else if (len <= 0xFFFF) format = MPF_BIN16;
else format = MPF_BIN32;
}
this.init((is_bin ? MPT_BIN : MPT_STRING), format, any(v));
}
mp_value(mp_value@[] & v) {
// ref not handle so can't be null
if (v.length() > 0xFFFFFFFF) throw("msgpack array too big to encode");
int format = 0;
uint len = v.length();
if (len <= 0x0F) format = MPF_FIXARRAY;
else if (len <= 0xFFFF) format = MPF_ARRAY16;
else format = MPF_ARRAY32;
this.init(MPT_ARRAY, format, any(v));
}
mp_value(mp_map& v) {
// ditto with ref
// don't have to check size because map return type is uint, so even if it were too big it can't tell us
int format = 0;
uint len = v.get_size();
if (len <= 0x0F) format = MPF_FIXMAP;
else if (len <= 0xFFFF) format = MPF_MAP16;
else format = MPF_MAP32;
this.init(MPT_MAP, format, any(v));
}
mp_value(mp_ext& v) {
if (v.data.length() >= 0xFFFFFFFF) throw("msgpack string too long to encode");
int format = 0;
uint len = v.data.length();
if (len == 1) format = MPF_FIXEXT1;
else if (len == 2) format = MPF_FIXEXT2;
else if (len == 4) format = MPF_FIXEXT4;
else if (len == 8) format = MPF_FIXEXT8;
else if (len == 16) format = MPF_FIXEXT16;
else if (len <= 0xFF) format = MPF_EXT8;
else if (len <= 0xFFFF) format = MPF_EXT16;
else format = MPF_EXT32;
this.init(MPT_EXT, format, any(v));
}
// now for the constructors for all the integer types that pass off to the ranging functions
mp_value(uint8 v) {
store_positive_int(v);
}
mp_value(uint16 v) {
store_positive_int(v);
}
mp_value(uint v) {
store_positive_int(v);
}
mp_value(uint64 v) {
store_positive_int(v);
}
mp_value(int8 v) {
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
mp_value(int16 v) {
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
mp_value(int v) {
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
mp_value(int64 v) {
if (v >= 0) store_positive_int(v);
else store_negative_int(v);
}
// let's allow equality comparison of two values
bool opEquals(mp_value@ other) const {
if(other is null) return false; // no populated value can be equal to the null handle
if (this is other) return true; // trivial
if (this.itype == other.type && this.format == other.format) {
// thanks to the format property being computed in real-time this holds even under mutability
// now we just deligate equality to the values
switch (this.itype) {
case MPT_NIL:
return true; // nil is always equal to itself
break;
case MPT_BOOLEAN:
return this.get_bool() == other.get_bool();
break;
case MPT_FLOAT:
return this.get_double() == other.get_double();
break;
// note that because of the above, a string and bin will never compare equal, even if the contents are the same!
// this is by design due to msgpack's distinction of the two
case MPT_STRING:
return this.get_string() == other.get_string();
break;
case MPT_BIN:
return this.get_string() == other.get_string();
break;
case MPT_EXT: {
mp_ext@ thisext = this.get_ext();
mp_ext@ otherext = other.get_ext();
return thisext == otherext;
}
break;
case MPT_ARRAY: {
mp_value@[]@ thisarray = this.get_array();
mp_value@[]@ otherarray = other.get_array();
return thisarray == otherarray;
}
break;
case MPT_MAP: {
mp_map@ thismap = this.get_map();
mp_map@ othermap = other.get_map();
return thismap == othermap;
}
break;
case MPT_INT:
// distinguish based on signed and unsigned as elsewhere
switch (this.format) {
case MPF_POS_FIXINT:
case MPF_UINT8:
case MPF_UINT16:
case MPF_UINT32:
case MPF_UINT64:
return this.get_uint64() == other.get_uint64();
break;
case MPF_NEG_FIXINT:
case MPF_INT8:
case MPF_INT16:
case MPF_INT32:
case MPF_INT64:
return this.get_int64() == other.get_int64();
break;
default:
return false;
break;
}
break;
default:
return false;
break;
}
}
return false;
}
}
// msgpack extension type
const int8 MP_EXT_TIMESTAMP = -1; // the specification extension type code for timestamp
class mp_ext {
private string idata;
private int8 itype;
const string data {
get const { return this.idata; }
}
const int8 type {
get const { return this.itype; }
}
mp_ext(int8 type, const string &in data) {
this.itype = type;
this.idata = data;
}
mp_ext(mp_ext& other) {
this.itype = other.type;
this.idata = other.data;
}
mp_timestamp@ opCast() {
if (this.itype == MP_EXT_TIMESTAMP) return mp_timestamp(this);
else return null;
}
// no implicit casts in this direction
bool opEquals(mp_ext@ other) const {
return (this is other) || (other !is null && this.itype == other.type && this.idata == other.data);
}
}
// timestamp, stored as ext type -1
// warning: Due to the fact that NVGT's timestamp precision is microseconds, round-tripping through timestamp objects is not lossless! The nanoseconds will be truncated to 0 and you will be left with microsecond precision.
class mp_timestamp {
private int64 secs; // seconds since unix epoch
private uint nsecs; // nanoseconds on top of this number of seconds, 0..999999999
int64 seconds {
get const { return this.secs; }
}
uint nanoseconds {
get const { return this.nsecs; }
}
// converts the stored time into a number of microseconds since the unix epoch, for timestamp round-tripping
// 1000000 microseconds in a second, 1000 nanoseconds in a microsecond
const int64 as_microseconds() const {
return (this.secs * 1000000) + (this.nsecs / 1000);
}
// store secs and nsecs based on a passed in number of microseconds since the unix epoch, for timestamp round-tripping
private void from_microseconds(int64 usecs) {
int64 sec = usecs / 1000000;
int usec2 = usecs % 1000000;
if (usec2 < 0) {
// negative remainder, negative timestamp
// since negative nanoseconds is not valid, adjust the two values so that they still satisfy producing this time but with a positive remainder
// that is, correct truncation towards 0 rather than negative infinity in the division operation
usec2 += 1000000; // make it positive by adding a second
sec -= 1; // and subtract a second so they agree
}
this.secs = sec;
this.nsecs = usec2 * 1000; // convert to nanoseconds
// this is a signed/unsigned conversion but based on how usec2 was obtained, it will never actually get high enough to make this an issue
}
// constructors
// from an ext instance, parses the three msgpack timestamp formats
mp_timestamp(mp_ext &in v) {
if (v.type != MP_EXT_TIMESTAMP)
throw(MP_TYPE_MISMATCH_EXCEPTION);
string data = v.data;
// deserialization procedure adapted from the spec
datastream ds(data,"",STREAM_BYTE_ORDER_NETWORK);
if (data.length() == 4) {
// timestamp 32, seconds since epoch as an unsigned int
uint sec = ds.read_uint();
this.secs = sec; // unsigned to signed conversion but signed type is bigger so we're safe
this.nsecs = 0;
} else if (data.length() == 8) {
// seconds and nanoseconds in 32-bit unsigned ints, except slight bitfields because nanoseconds is only 30 bits
uint64 d = ds.read_uint64();
this.nsecs = d >> 34;
this.secs = d & 0x00000003ffffffff;
} else if (data.length() == 12) {
// nanoseconds in 32-bit unsigned int, seconds in 64-bit signed int
uint nsec = ds.read_uint();
int64 sec = ds.read_int64();
this.nsecs = nsec;
this.secs = sec;
} else {
ds.close();
throw("msgpack invalid timestamp format");
}
ds.close();
}