-
Notifications
You must be signed in to change notification settings - Fork 7
/
amsg.hpp
1568 lines (1432 loc) · 39.4 KB
/
amsg.hpp
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
///
/// Copyright (c) 2012 - 2015 Ning Ding (lordoffox@gmail.com)
///
/// Distributed under the Boost Software License, Version 1.0. (See accompanying
/// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
///
/// See https://github.com/lordoffox/amsg for latest version.
///
#ifndef AMSG_AMSG_HPP
#define AMSG_AMSG_HPP
#include <stdint.h>
#include <string>
#include <deque>
#include <list>
#include <vector>
#include <map>
#include <limits>
#include <type_traits>
#include <array>
#include <forward_list>
#include <unordered_map>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/seq.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#ifndef AMSG_INLINE
# ifdef _MSC_VER
# pragma inline_recursion(on)
# define AMSG_INLINE __forceinline
# elif defined(_GUNC_)
# define AMSG_INLINE inline __attribute__((always_inline))
# else
# define AMSG_INLINE inline
# endif
#endif
namespace amsg
{
#if _MSC_VER > 1300
#include <intrin.h>
#define byte_swap_16(x) _byteswap_ushort(x)
#define byte_swap_32(x) _byteswap_ulong(x)
#define byte_swap_64(x) _byteswap_uint64(x)
#elif __GNUC__ >= 3
#include <byteswap.h>
#define byte_swap_16(x) bswap_16(x)
#define byte_swap_32(x) bswap_32(x)
#define byte_swap_64(x) bswap_64(x)
#else
template<typename _ty>
AMSG_INLINE _ty byte_swap_16(_ty value)
{
value = (value >> 8) | (value << 8);
return value;
}
template<typename _ty>
AMSG_INLINE _ty byte_swap_32(_ty value)
{
value = ((value << 8) & 0xFF00FF00) | ((value >> 8) & 0x00FF00FF);
value = (value >> 16) | (value << 16);
return value;
}
template<typename _ty>
AMSG_INLINE _ty byte_swap_64(_ty value)
{
value = ((value << 8) & 0xFF00FF00FF00FF00ULL) | ((value >> 8) & 0x00FF00FF00FF00FFULL);
value = ((value << 16) & 0xFFFF0000FFFF0000ULL) | ((value >> 16) & 0x0000FFFF0000FFFFULL);
return (value >> 32) | (value << 32);
}
#endif
//define endian check macro
#ifdef _WIN32
#else
#include <arpa/inet.h> /* __BYTE_ORDER */
#endif
# if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define __LITTLE_ENDIAN__
# elif __BYTE_ORDER == __BIG_ENDIAN
# define __BIG_ENDIAN__
# elif _WIN32
# define __LITTLE_ENDIAN__
# endif
# endif
#if defined(__LITTLE_ENDIAN__)
#define host_to_little_endian16(value) (value)
#define host_to_little_endian32(value) (value)
#define host_to_little_endian64(value) (value)
#define little_endian_to_host16(value) (value)
#define little_endian_to_host32(value) (value)
#define little_endian_to_host64(value) (value)
#else
#define host_to_little_endian16(value) byte_swap_16(value)
#define host_to_little_endian32(value) byte_swap_32(value)
#define host_to_little_endian64(value) byte_swap_64(value)
#define little_endian_to_host16(value) byte_swap_16(value)
#define little_endian_to_host32(value) byte_swap_32(value)
#define little_endian_to_host64(value) byte_swap_64(value)
#endif
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<sizeof(value_type) == 1, value_type>::type
le_to_host(const value_type& value)
{
return value;
}
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<sizeof(value_type) == 2, value_type>::type
le_to_host(const value_type& value)
{
return (value_type)little_endian_to_host16(value);
}
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<sizeof(value_type) == 4, value_type>::type
le_to_host(const value_type& value)
{
return (value_type)little_endian_to_host32((uint32_t)value);
}
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<sizeof(value_type) == 8, value_type>::type
le_to_host(const value_type& value)
{
return (value_type)little_endian_to_host64((uint64_t)value);
}
AMSG_INLINE float le_to_host(const float& value)
{
union { uint32_t uv; float fv; }u;
u.fv = value;
u.uv = little_endian_to_host32(u.uv);
return u.fv;
}
AMSG_INLINE double le_to_host(const double& value)
{
union { uint32_t uv; double fv; }u;
u.fv = value;
u.uv = little_endian_to_host64(u.uv);
return u.fv;
}
template<typename value_type,
typename ::std::enable_if<sizeof(value_type) == 1, int >::type = 0
>
AMSG_INLINE value_type host_to_le(const value_type& value)
{
return value;
}
template<typename value_type,
typename ::std::enable_if<sizeof(value_type) == 2, int >::type = 0
>
AMSG_INLINE value_type host_to_le(const value_type& value)
{
return (value_type)host_to_little_endian16(value);
}
template<typename value_type,
typename ::std::enable_if<sizeof(value_type) == 4, int >::type = 0
>
AMSG_INLINE value_type host_to_le(const value_type& value)
{
return (value_type)host_to_little_endian32((uint32_t)value);
}
template<typename value_type,
typename ::std::enable_if<sizeof(value_type) == 8, int >::type = 0
>
AMSG_INLINE value_type host_to_le(const value_type& value)
{
return (value_type)host_to_little_endian64((uint64_t)value);
}
AMSG_INLINE float host_to_le(const float& value)
{
union { uint32_t uv; float fv; }u;
u.fv = value;
u.uv = host_to_little_endian32(u.uv);
return u.fv;
}
AMSG_INLINE double host_to_le(const double& value)
{
union { uint32_t uv; double fv; }u;
u.fv = value;
u.uv = host_to_little_endian64(u.uv);
return u.fv;
}
static size_t to_str(const uint32_t& Value, char * resultbuffer, size_t len)
{
uint32_t temp = Value;
resultbuffer[len - 1] = 0;
size_t pos = len - 2;
if (temp == 0)
{
resultbuffer[pos--] = '0';
}
while (temp)
{
resultbuffer[pos--] = (char)((temp % 10) + '0');
temp = temp / 10;
}
++pos;
memmove(resultbuffer, resultbuffer + pos, (len - pos));
return len - pos;
}
enum error_code_t
{
success = 0,
negative_assign_to_unsigned_integer_number,
value_too_large_to_integer_number,
sequence_length_overflow,
stream_buffer_overflow,
number_of_element_not_macth
};
struct basic_store
{
basic_store()
: m_error_code(success)
{
}
AMSG_INLINE error_code_t error_code() const
{
return m_error_code;
}
AMSG_INLINE void set_error_code(error_code_t value)
{
m_error_code = value;
}
AMSG_INLINE const char * message()
{
switch (m_error_code)
{
case success:
break;
case negative_assign_to_unsigned_integer_number:
return "can't assign negative number to unsigned integer number.";
case value_too_large_to_integer_number:
return "value too large to integer number";
case sequence_length_overflow:
return "sequence length overflow";
case stream_buffer_overflow:
return "stream buffer overflow";
case number_of_element_not_macth:
return "number of element not macth";
default:
break;
}
return "";
}
AMSG_INLINE bool error() const // true if error
{
return m_error_code != success;
}
AMSG_INLINE void clear()
{
m_error_code = success;
}
error_code_t m_error_code;
};
template< typename stream_ty, typename error_string_ty = ::std::string >
struct store : public basic_store
{
public:
store(stream_ty& stream)
: basic_store()
, m_stream(stream)
{
}
AMSG_INLINE const error_string_ty& info() const { return this->m_error_info; }
AMSG_INLINE void append_debug_info(const char * info)
{
this->m_error_info.append(info);
}
AMSG_INLINE bool bad()const { return this->m_stream.bad(); }
AMSG_INLINE ::std::size_t read(char * buffer, ::std::size_t len)
{
return this->m_stream.read(buffer, len);
}
AMSG_INLINE ::std::size_t write(const char * buffer, ::std::size_t len)
{
return this->m_stream.write(buffer, len);
}
AMSG_INLINE void skip_read(::std::size_t len)
{
this->m_stream.seekg(len);
}
AMSG_INLINE ::std::size_t read_length()
{
return this->m_stream.tellg();
}
AMSG_INLINE ::std::size_t write_length()
{
return this->m_stream.tellp();
}
AMSG_INLINE void clear()
{
basic_store::clear();
this->m_stream.clear();
this->m_error_info.clear();
}
private:
stream_ty& m_stream;
error_string_ty m_error_info;
};
template<typename stream_ty>
AMSG_INLINE store<stream_ty> make_store(stream_ty& stream)
{
return store<stream_ty>(stream);
}
enum
{
const_interger_byte_msak = 0x3f,
const_negative_bit_value = 0x40,
const_tag_as_value = 0x7f,
const_tag_as_type,
const_store_postive_integer_byte_mask = 0x80 - 2,
const_store_negative_integer_byte_mask = 0x80 + const_negative_bit_value - 2
};
template <typename value_type>
struct sfix_op
{
value_type& val;
sfix_op(value_type& value)
:val(value)
{}
sfix_op(const sfix_op& rv)
:val(rv.val)
{}
sfix_op& operator = (const sfix_op& rv)
{
val = rv.val;
}
};
struct sfix_def{};
namespace
{
static sfix_def sfix = sfix_def();
}
template<typename ty>
AMSG_INLINE sfix_op<ty> operator & (ty& value, const sfix_def&)
{
sfix_op<ty> op(value);
return op;
}
template <typename value_type>
struct smax_valid
{
uint32_t size;
value_type & val;
smax_valid(uint32_t max, value_type& value)
:size(max), val(value)
{}
smax_valid(smax_valid& rv)
:size(rv.size), val(rv.val)
{}
smax_valid& operator = (const smax_valid& rv)
{
size = rv.size;
val = rv.val;
}
};
struct smax
{
uint32_t size;
smax(uint32_t max = 0)
:size(max)
{}
};
template<typename ty>
AMSG_INLINE smax_valid<ty> operator & (ty& value, const smax& sm)
{
smax_valid<ty> valid(sm.size, value);
return valid;
}
template<typename value_type>
AMSG_INLINE bool can_skip(const value_type&)
{
return false;
}
AMSG_INLINE uint32_t size_of(bool)
{
return 1;
}
template<typename store_ty>
AMSG_INLINE void skip_read(store_ty& store_data, bool*)
{
store_data.skip_read(1);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
return;
}
template<typename store_ty>
AMSG_INLINE void read(store_ty& store_data, bool& value)
{
store_data.read((char*)&value, 1);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
return;
}
template<typename store_ty>
AMSG_INLINE void write(store_ty& store_data, const bool& value)
{
uint8_t val = value ? 1 : 0;
store_data.write((char*)&value, 1);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
return;
}
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_signed<value_type>::value && ::std::is_integral<value_type>::value, uint32_t>::type
size_of(const value_type& value)
{
if (0 <= value && value < const_tag_as_type)
{
return 1;
}
else
{
value_type temp = value;
if (value < 0)
{
temp = -value;
}
if (temp < 0x100)
{
return 2;
}
else if (temp < 0x10000)
{
return 3;
}
else if (temp < 0x1000000)
{
return 4;
}
else if (temp < 0x100000000)
{
return 5;
}
else if (temp < 0x10000000000LL)
{
return 6;
}
else if (temp < 0x1000000000000LL)
{
return 7;
}
else if (temp < 0x100000000000000LL)
{
return 8;
}
}
return 9;
}
template<typename store_ty, typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_integral<value_type>::value, void>::type
skip_read(store_ty& store_data, value_type *)
{
uint8_t tag;
store_data.read((char*)&tag, 1);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
if (tag > const_tag_as_value)
{
int read_bytes = (tag & const_interger_byte_msak) + 1;
store_data.skip_read(read_bytes);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
}
}
template<typename store_ty, typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_signed<value_type>::value && ::std::is_integral<value_type>::value, void>::type
read(store_ty& store_data, value_type& value)
{
const int bytes = sizeof(value_type);
value_type read_value[2] = { 0 };
uint8_t * ptr = (uint8_t *)read_value;
store_data.read((char*)ptr, 1);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
value = *ptr;
if (value > const_tag_as_value)
{
int sign = 1;
if ((long)value & const_negative_bit_value)
{
sign = -1;
}
int read_bytes = (value & const_interger_byte_msak) + 1;
if (bytes < read_bytes)
{
store_data.set_error_code(value_too_large_to_integer_number);
return;
}
ptr = (uint8_t *)&read_value[1];
store_data.read((char*)ptr, read_bytes);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
if (sign < 0)
{
value = -(value_type)le_to_host(read_value[1]);
}
else
{
value = le_to_host(read_value[1]);
}
}
}
template<typename store_ty, typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_signed<value_type>::value && ::std::is_integral<value_type>::value, void>::type
write(store_ty& store_data, const value_type& value)
{
value_type write_value[2] = { 0 };
int write_bytes = 0;
uint8_t * ptr = (uint8_t *)write_value;
uint8_t tag = static_cast<uint8_t>(value);
write_bytes = 1;
if (0 <= value && value < const_tag_as_type)
{
write_value[0] = tag;
}
else
{
uint8_t negative_bit = 0;
value_type temp = value;
if (value < 0)
{
negative_bit = const_negative_bit_value;
temp = -value;
}
write_value[1] = host_to_le(temp);
ptr = (uint8_t *)(&write_value[1]) - 1;
if (temp < 0x100)
{
write_bytes = 2;
}
else if (temp < 0x10000)
{
write_bytes = 3;
}
else if (temp < 0x1000000)
{
write_bytes = 4;
}
else if (temp < 0x100000000)
{
write_bytes = 5;
}
else if (temp < 0x10000000000LL)
{
write_bytes = 6;
}
else if (temp < 0x1000000000000LL)
{
write_bytes = 7;
}
else if (temp < 0x100000000000000LL)
{
write_bytes = 8;
}
else
{
write_bytes = 9;
}
*ptr = const_store_postive_integer_byte_mask + negative_bit + write_bytes;
}
store_data.write((const char *)ptr, write_bytes);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
}
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_unsigned<value_type>::value && ::std::is_integral<value_type>::value, uint32_t>::type
size_of(const value_type& value)
{
if (value < const_tag_as_type)
{
return 1;
}
else
{
if (value < 0x100)
{
return 2;
}
else if (value < 0x10000)
{
return 3;
}
else if (value < 0x1000000)
{
return 4;
}
else if (value < 0x100000000)
{
return 5;
}
else if (value < 0x10000000000LL)
{
return 6;
}
else if (value < 0x1000000000000LL)
{
return 7;
}
else if (value < 0x100000000000000LL)
{
return 8;
}
}
return 9;
}
template<typename store_ty, typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_unsigned<value_type>::value && ::std::is_integral<value_type>::value, void>::type
read(store_ty& store_data, value_type& value)
{
const int bytes = sizeof(value_type);
value_type read_value[2] = { 0 };
uint8_t * ptr = (uint8_t *)read_value;
store_data.read((char*)ptr, 1);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
value = *ptr;
if (value > const_tag_as_value)
{
if ((long)value & const_negative_bit_value)
{
store_data.set_error_code(negative_assign_to_unsigned_integer_number);
return;
}
int read_bytes = int(value & const_interger_byte_msak) + 1;
if (bytes < read_bytes)
{
store_data.set_error_code(value_too_large_to_integer_number);
return;
}
ptr = (uint8_t *)&read_value[1];
store_data.read((char*)ptr, read_bytes);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
value = le_to_host(read_value[1]);
}
}
template<typename store_ty, typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_unsigned<value_type>::value && ::std::is_integral<value_type>::value, void>::type
write(store_ty& store_data, const value_type& value)
{
value_type write_value[2] = { 0 };
int write_bytes = 0;
uint8_t * ptr = (uint8_t *)write_value;
uint8_t tag = static_cast<uint8_t>(value);
write_bytes = 1;
if (value < const_tag_as_type)
{
write_value[0] = tag;
}
else
{
write_value[1] = host_to_le(value);
ptr = (uint8_t *)(&write_value[1]) - 1;
if (value < 0x100)
{
write_bytes = 2;
}
else if (value < 0x10000)
{
write_bytes = 3;
}
else if (value < 0x1000000)
{
write_bytes = 4;
}
else if (value < 0x100000000)
{
write_bytes = 5;
}
else if (value < 0x10000000000LL)
{
write_bytes = 6;
}
else if (value < 0x1000000000000LL)
{
write_bytes = 7;
}
else if (value < 0x100000000000000LL)
{
write_bytes = 8;
}
else
{
write_bytes = 9;
}
*ptr = (uint8_t)(const_store_postive_integer_byte_mask + write_bytes);
}
store_data.write((const char *)ptr, write_bytes);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
}
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_enum<value_type>::value, uint32_t>::type
size_of(const value_type& value)
{
return size_of((int64_t)value);
}
template<typename store_ty, typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_enum<value_type>::value, void>::type
read(store_ty& store_data, value_type& value)
{
int64_t data;
read(store_data, data);
value = static_cast<value_type>(data);
}
template<typename store_ty, typename value_type>
AMSG_INLINE
typename ::std::enable_if<::std::is_enum<value_type>::value, void>::type
write(store_ty& store_data, const value_type& value)
{
int64_t data = static_cast<int64_t>(value);
write(store_data, data);
}
AMSG_INLINE uint32_t size_of(const float&)
{
return sizeof(float);
}
template<typename store_ty>
AMSG_INLINE void skip_read(store_ty& store_data, float *)
{
store_data.skip_read(sizeof(float));
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
}
}
template<typename store_ty>
AMSG_INLINE void read(store_ty& store_data, float& value)
{
store_data.read((char*)&value, sizeof(float));
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
}
value = le_to_host(value);
}
template<typename store_ty>
AMSG_INLINE void write(store_ty& store_data, const float& value)
{
float data = host_to_le(value);
store_data.write((char*)&data, sizeof(double));
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
}
}
AMSG_INLINE uint32_t size_of(const double&)
{
return sizeof(double);
}
template<typename store_ty>
AMSG_INLINE void skip_read(store_ty& store_data, double *)
{
store_data.skip_read(sizeof(double));
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
}
}
template<typename store_ty>
AMSG_INLINE void read(store_ty& store_data, double& value)
{
store_data.read((char*)&value, sizeof(double));
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
}
value = le_to_host(value);
}
template<typename store_ty>
AMSG_INLINE void write(store_ty& store_data, const double& value)
{
double data = host_to_le(value);
store_data.write((char*)&data, sizeof(double));
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
}
}
template<typename alloc_ty>
AMSG_INLINE uint32_t size_of(const ::std::basic_string<char, ::std::char_traits<char>, alloc_ty>& value, uint32_t max = 0)
{
(max);
uint32_t len = (uint32_t)value.length();
return size_of(len) + len;
}
template<typename alloc_ty>
AMSG_INLINE bool can_skip(const ::std::basic_string<char, ::std::char_traits<char>, alloc_ty>& value)
{
return value.empty();
}
template<typename store_ty, typename alloc_ty>
AMSG_INLINE void skip_read(store_ty& store_data, ::std::basic_string<char, ::std::char_traits<char>, alloc_ty>*, uint32_t max = 0)
{
(max);
uint32_t len;
read(store_data, len);
if (store_data.error())
{
return;
}
store_data.skip_read(len);
}
template<typename store_ty, typename alloc_ty>
AMSG_INLINE void read(store_ty& store_data, ::std::basic_string<char, ::std::char_traits<char>, alloc_ty>& value, uint32_t max = 0)
{
uint32_t len;
read(store_data, len);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
if (max > 0 && max < len)
{
store_data.set_error_code(sequence_length_overflow);
return;
}
value.resize(len);
store_data.read((char*)value.data(), len);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
}
template<typename store_ty, typename alloc_ty>
void write(store_ty& store_data, const ::std::basic_string<char, ::std::char_traits<char>, alloc_ty>& value, uint32_t max = 0)
{
uint32_t len = (uint32_t)value.length();
if (max > 0 && max < len)
{
store_data.set_error_code(sequence_length_overflow);
return;
}
write(store_data, len);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
store_data.write(value.data(), len);
if (store_data.bad())
{
store_data.set_error_code(stream_buffer_overflow);
return;
}
}
template<typename type>
struct is_sequence_container : public ::std::false_type{};
template<typename type, typename alloc_type>
struct is_sequence_container< ::std::deque<type, alloc_type> > : public ::std::true_type{};
template<typename type, typename alloc_type>
struct is_sequence_container< ::std::list<type, alloc_type> > : public ::std::true_type{};
template<typename type, typename alloc_type>
struct is_sequence_container< ::std::vector<type, alloc_type> > : public ::std::true_type{};
template<typename type, typename alloc_type>
struct is_sequence_container< ::std::forward_list<type, alloc_type> > : public ::std::true_type{};
template<typename value_type>
AMSG_INLINE
typename ::std::enable_if<is_sequence_container<value_type>::value, uint32_t>::type