-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_stream.hh
More file actions
2079 lines (2013 loc) · 78.1 KB
/
data_stream.hh
File metadata and controls
2079 lines (2013 loc) · 78.1 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
/*
* Copyright (c) 2016 Zhao DAI <daidodo@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see accompanying file LICENSE.txt
* or <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Data packing library using stream style interfaces.
* @author Zhao DAI
*/
#ifndef DOZERG_DATA_STREAM_H_20070905
#define DOZERG_DATA_STREAM_H_20070905
#include <cassert>
#include <cstring> //memcpy
#include <vector>
#include "impl/data_stream_impl.hh"
NS_SERVER_BEGIN
//TODO:
//ds<<x<<y<<Manip::offset_value(off, in.size())
/**
* @brief Manipulators for stream interface APIs.
*/
namespace Manip{
/**
* @name Mapip::raw
* @brief Pack/unpack a range of elements, @em without leading size field.
* @c Mapip::raw is convenient for pack/unpack fixed number of elements, like an array, a
* string of characters, or a container.
* @par Array
* Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* int c[5]; // an array to receive unpacked results
*
* in >> Mapip::raw(c); // unpack 5 integers
* // This is equivalent to:
* // for(int i = 0;i < 5;++i)
* // in >> c[i];
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* int c[5]; // an array to pack
*
* out << Mapip::raw(c); // pack 5 integers
* // This is equivalent to:
* // for(int i = 0;i < 5;++i)
* // out << c[i];
* @endcode
* If size of array cannot be deduced automatically, a parameter must be provided.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* int * c = new int[sz]; // an array to receive unpacked results
*
* in >> Mapip::raw(c, sz); // unpack 'sz' integers
* // This is equivalent to:
* // for(int i = 0;i < sz;++i)
* // in >> c[i];
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* int * c = new int[sz]; // an array to pack
*
* out << Mapip::raw(c, sz); // pack 'sz' integers
* // This is equivalent to:
* // for(int i = 0;i < sz;++i)
* // out << c[i];
* @endcode
* @par Containers
* Most STL containers are well supported, e.g. @c vector, @c list, @c deque, @c set/multiset,
* @c map/multimap, etc.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* vector<int> c; // a container to receive unpacked results
*
* in >> Mapip::raw(c, sz); // unpack 'sz' integers, and append them to 'c'
* // This is equivalent to:
* // for(int i = 0, v;i < sz;++i){
* // in >> v;
* // c.insert(c.end(), v);
* // }
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* vector<int> c; // an vector to pack
*
* out << Mapip::raw(c); // pack all elements in 'c'
* // This is equivalent to:
* // for(int i = 0;i < c.size();++i)
* // out << c[i];
* @endcode
* @par String
* A C-style string is essentially an array of characters.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* char * c = new char[sz]; // an array of chars to receive unpacked results
*
* in >> Mapip::raw(c, sz); // unpack 'sz' chars
* // This is equivalent to:
* // for(int i = 0;i < sz;++i)
* // in >> c[i];
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* const char * c = "abcde"; // a C-style string to pack
*
* out << Mapip::raw(c, strlen(c)); // pack a C-style string
* // This is equivalent to:
* // for(int i = 0;i < strlen(c);++i)
* // out << c[i];
* @endcode
* @n An @c std::string is a container of characters.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* string c; // an object to receive unpacked data
*
* in >> Mapip::raw(c, len); // unpack 'len' bytes of data, and append them to 'c'
* // This is equivalent to:
* // for(int i = 0;i < len;++i){
* // char v;
* // in >> v;
* // c.push_back(v);
* // }
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* string c = "abcde"; // a string to pack
*
* out << Mapip::raw(c); // pack the string
* // This is equivalent to:
* // for(int i = 0;i < c.size();++i)
* // out << c[i];
* @endcode
* @par Iterator range
* Sometimes you only have a begin and an end iterators to do the packing/unpacking. A `size_t
* *` parameter might be provided to retrieve the number of elements actually packed/unpacked.
* If it doesn't matter, just ignore it.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* list<int> c;
* list<int>::iterator first = c.begin(); // start iterator
* list<int>::iterator last = c.end(); // end iterator
* size_t sz;
*
* in >> Mapip::raw(first, last, &sz); // unpack certain integers to range [first, last), and
* // store the number of elements unpaced in 'sz'
* // This is equivalent to:
* // for(list<int>::iterator it = first;it != last;++it)
* // in >> *it;
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* list<int> c;
* list<int>::const_iterator first = c.begin(); // start iterator
* list<int>::const_iterator last = c.end(); // end iterator
*
* out << Mapip::raw(first, last); // pack certain integers from range [first, last), not
* // interested in the number of elements packed
* // This is equivalent to:
* // for(list<int>::const_iterator it = first;it != last;++it)
* // out << *it;
* @endcode
* @{ */
template<class T, size_t N>
inline NS_IMPL::CManipulatorRawPtr<T> raw(T (&c)[N]){
return NS_IMPL::CManipulatorRawPtr<T>(c, N);
}
template<class T>
inline NS_IMPL::CManipulatorRawPtr<T> raw(T * c, size_t sz){
return NS_IMPL::CManipulatorRawPtr<T>(c, sz);
}
template<class T>
inline NS_IMPL::CManipulatorRawPtr<T> raw(std::vector<T> & c, size_t sz){
const size_t old = c.size();
c.resize(old + sz);
return NS_IMPL::CManipulatorRawPtr<T>(&c[old], sz);
}
template<class T>
inline NS_IMPL::CManipulatorRawPtr<const T> raw(const std::vector<T> & c, size_t * sz = NULL){
if(sz)
*sz = c.size();
return NS_IMPL::CManipulatorRawPtr<const T>(&c[0], c.size());
}
template<typename Char>
inline NS_IMPL::CManipulatorRawPtr<Char> raw(std::basic_string<Char> & c, size_t len){
const size_t old = c.size();
c.append(len, 0);
return NS_IMPL::CManipulatorRawPtr<Char>(&c[old], len);
}
template<typename Char>
inline NS_IMPL::CManipulatorRawPtr<const Char> raw(const std::basic_string<Char> & c, size_t * sz = NULL){
if(sz)
*sz = c.size();
return NS_IMPL::CManipulatorRawPtr<const Char>(c.c_str(), c.size());
}
template<class ForwardIter>
inline NS_IMPL::CManipulatorRawRange<ForwardIter> raw(ForwardIter first, ForwardIter last, size_t * sz = NULL){
return NS_IMPL::CManipulatorRawRange<ForwardIter>(first, last, sz);
}
template<class T>
inline NS_IMPL::CManipulatorRawCont<T> raw(T & c, size_t sz){
return NS_IMPL::CManipulatorRawCont<T>(c, sz, NULL);
}
template<class T>
inline NS_IMPL::CManipulatorRawCont<const T> raw(const T & c, size_t * sz = NULL){
return NS_IMPL::CManipulatorRawCont<const T>(c, 0, sz);
}
/** @} */
/**
* @name Mapip::array
* @brief Pack/unpack a range of elements, @em with leading size field.
* @c Mapip::array is convenient for pack/unpack a number of elements, like an array, a
* string of characters, or a container. It is very similar to @c Manip::raw, except that a
* size field (the number elements) is packed/unpacked first before the elements.
* By default, the type of leading size field is @c uint16_t, unless you specify it explicitly.
* @par Array
* Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* int c[10]; // an array to receive unpacked results
* uint8_t sz; // an integer to retrieve the number of elements actually unpacked
*
* in >> Mapip::array(c, &sz); // firstly unpack an uint8_t to 'sz' as the number of following
* // elements, then unpack 'sz' integers to 'c'
* // This is equivalent to:
* // in >> sz;
* // for(int i = 0;i < sz;++i)
* // in >> c[i];
* @endcode
* Note that if @c sz is larger than the size @c c can hold, the whole operation fails with status
* of @c in set to non-zero.
* @n Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* int c[5]; // an array to pack
*
* out << Mapip::array<uint16_t>(c); // pack a uint16_t equals to 5, then pack 5 integers
* // This is equivalent to:
* // out << uint16_t(5);
* // for(int i = 0;i < 5;++i)
* // out << c[i];
* @endcode
* Note that you @em must specify the type of leading size.
* @n If size of array cannot be deduced automatically, a parameter must be provided.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* int * c = new int[sz]; // an array to receive unpacked results
* uint16_t real_sz; // an integer to retrieve the number of elements actually unpacked
*
* in >> Mapip::array(c, sz, &real_sz);// firstly unpack a uin16_t to 'real_sz' as the number of
* // following elements, then unpack 'real_sz' integers
* // This is equivalent to:
* // in >> real_sz;
* // for(int i = 0;i < real_sz;++i)
* // in >> c[i];
* @endcode
* Note that if @c real_sz is larger than @c sz, the whole operation fails with status of @c in
* is set to non-zero.
* @n Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* int * c = new int[sz]; // an array to pack
*
* out << Mapip::array<uint16_t>(c, sz); // pack a uint16_t equals to 'sz' first, then pack
* // 'sz' integers
* // This is equivalent to:
* // out << uint16_t(sz);
* // for(int i = 0;i < sz;++i)
* // out << c[i];
* @endcode
* Note that you @em must specify the type of leading size.
* @par Containers
* Most STL containers are well supported, e.g. @c vector, @c list, @c deque, @c set/multiset,
* @c map/multimap, etc.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* vector<int> c; // a container to receive unpacked results
*
* in >> Mapip::array(c); // firstly unpack a uint16_t as the number of following elements,
* // then unpack that number of integers, and append them to 'c'
* // This is equivalent to:
* // uint16_t sz;
* // in >> sz;
* // for(int i = 0, v;i < sz;++i){
* // in >> v;
* // c.insert(c.end(), v);
* // }
* @endcode
* If the leading size is not @c uint16_t, you can specify it like this:
* @code{.cpp}
* in >> Mapip::array<uint8_t>(c); // firstly unpack a uint8_t as the number of following
* // elements, then unpack that number of integers, and
* // append them to 'c'
* @endcode
* If there is a limitation of elements, an additional parameter may be provided, and the
* leading size has the same type as the second parameter:
* @code{.cpp}
* uint32_t max_sz = 100;
* in >> Mapip::array(c, max_sz); // firstly unpack a uint32_t as the number of following
* // elements, then unpack that number of integers, and
* // append them to 'c'
* @endcode
* If the leading size unpacked is larger than @c max_sz, the operation fails and status of @c
* in is set to non-zero.
* @n Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* vector<int> c; // an vector to pack
*
* out << Mapip::array(c); // firstly pack a uint16_t equals to 'c.size()', then pack all
* // elements in 'c'
* // This is equivalent to:
* // out << uint16_t(c.size());
* // for(int i = 0;i < c.size();++i)
* // out << c[i];
* @endcode
* Note that you can specify the type of leading size like this:
* @code{.cpp}
* out << Mapip::array<uint8_t>(c); // firstly pack a uint8_t equals to 'c.size()', then pack
* //all elements in 'c'
* @endcode
* @par String
* A C-style string is essentially an array of characters.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* char * c = new char[sz]; // an array of chars to receive unpacked results
* uint16_t real_sz; // an integer to retrieve the number characters actually unpacked
*
* in >> Mapip::array(c, sz, &real_sz); // firstly unpack a uint16_t to 'real_sz' as the
* // number of following characters, then unpack
* // 'real_sz' characters to 'c'
* // This is equivalent to:
* // in >> real_sz;
* // for(int i = 0;i < real_sz;++i)
* // in >> c[i];
* @endcode
* If @c real_sz is larger than @c sz, this operation fails and status of @c in is set to
* non-zero.
* @n Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* const char * c = "abcde"; // a C-style string to pack
*
* out << Mapip::array<uint8_t>(c, strlen(c)); // firstly pack a uint8_t equals to
* // 'strlen(c)', then pack the content of 'c'
* // This is equivalent to:
* // out << uint8_t(strlen(c));
* // for(int i = 0;i < strlen(c);++i)
* // out << c[i];
* @endcode
* Note that the type of leading size @em must be provided.
* @n An @c std::string is a container of characters.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* string c; // an object to receive unpacked data
*
* in >> Mapip::array(c); // firstly unpack a uint16_t as the number of following characters,
* // then unpack that number of bytes of data, and append them to 'c'
* // This is equivalent to:
* // uint16_t sz;
* // in >> sz;
* // for(int i = 0;i < sz;++i){
* // char v;
* // in >> v;
* // c.push_back(v);
* // }
* @endcode
* You can specify the type of leading size like this:
* @code{.cpp}
* in >> Mapip::array<uint8_t>(c); // firstly unpack a uint8_t as the number of following
* // characters, then unpack that number of bytes of data,
* // and append them to 'c'
* @endcode
* If there is a limitation of bytes to unpack, an additional parameter may be provided, and
* the type of leading size is the same as the second parameter:
* @code{.cpp}
* uint8_t max_sz = 100;
* in >> Mapip::array(c, max_sz); // firstly unpack a uint8_t as the number of following
* // characters, then unpack that number of bytes of data,
* // and append them to 'c'
* @endcode
* If the leading size unpacked is larger than @c max_sz, the operation fails and status of @c
* in is set to non-zero.
* @n Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* string c = "abcde"; // a string to pack
*
* out << Mapip::array(c); // firstly pack a uint16_t equals to 'c.size()', then pack the
* // content of 'c'
* // This is equivalent to:
* // out << uint16_t(c.size());
* // for(int i = 0;i < c.size();++i)
* // out << c[i];
* @endcode
* If the type of leading size isn't @c uint16_t, you can specify it like this:
* @code{.cpp}
* out << Mapip::array<uint8_t>(c); // firstly pack a uint8_t equals to 'c.size()', then pack
* // the content of 'c'
* @endcode
* @par Iterator range (pack only)
* Sometimes you only have a begin and an end iterators to do the packing.
* @n Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* list<int> c;
* list<int>::const_iterator first = c.begin(); // start iterator
* list<int>::const_iterator last = c.end(); // end iterator
*
* out << Mapip::array<uint16_t>(first, last);// firstly pack a uint16_t equals to the number of
* // elements in range [first, last), then pack all
* // integers the range
* // This is equivalent to:
* // out << uint16_t(distance(first, last));
* // for(list<int>::const_iterator it = first;it != last;++it)
* // out << *it;
* @endcode
* An additional integer parameter could be provided to retrieve the number of elements packed,
* and it will have the same type of the leading size:
* @code{.cpp}
* uint8_t sz;
* out << Mapip::array(first, last, &sz); // firstly pack a uint8_t equals to the number of
* // elements in range [first, last), then pack all
* // integers in the range
* @endcode
* @{ */
template<typename LenT, class T, size_t N>
inline NS_IMPL::CManipulatorArrayPtr<LenT, T> array(T (&c)[N], LenT * real_sz){
return NS_IMPL::CManipulatorArrayPtr<LenT, T>(c, N, real_sz);
}
template<typename LenT, class T, size_t N>
inline NS_IMPL::CManipulatorArrayPtr<LenT, const T> array(const T (&c)[N]){
return NS_IMPL::CManipulatorArrayPtr<LenT, const T>(c, N, NULL);
}
template<typename LenT, class T>
inline NS_IMPL::CManipulatorArrayPtr<LenT, T> array(T * c, size_t sz, LenT * real_sz){
return NS_IMPL::CManipulatorArrayPtr<LenT, T>(c, sz, real_sz);
}
template<typename LenT, class T>
inline NS_IMPL::CManipulatorArrayPtr<LenT, const T> array(const T * c, size_t sz){
return NS_IMPL::CManipulatorArrayPtr<LenT, const T>(c, sz, NULL);
}
template<typename LenT, class T>
inline NS_IMPL::CManipulatorArrayCont<LenT, T> array(T & c, LenT max_size = 0){
return NS_IMPL::CManipulatorArrayCont<LenT, T>(c, max_size);
}
template<typename LenT, class T>
inline NS_IMPL::CManipulatorArrayCont<LenT, const T> array(const T & c){
return NS_IMPL::CManipulatorArrayCont<LenT, const T>(c, 0);
}
template<class T>
inline NS_IMPL::CManipulatorArrayCont<uint16_t, T> array(T & c, uint16_t max_size = 0){
return NS_IMPL::CManipulatorArrayCont<uint16_t, T>(c, max_size);
}
template<class T>
inline NS_IMPL::CManipulatorArrayCont<uint16_t, const T> array(const T & c){
return NS_IMPL::CManipulatorArrayCont<uint16_t, const T>(c, 0);
}
template<typename LenT, class ForwardIter>
inline NS_IMPL::CManipulatorArrayRange<LenT, ForwardIter> array(ForwardIter first, ForwardIter last, LenT * sz = NULL){
return NS_IMPL::CManipulatorArrayRange<LenT, ForwardIter>(first, last, sz);
}
/** @} */
/**
* @name Byte order
* APIs for changing CInByteStream / COutByteStreamBasic object's byte order, both permanently
* and temporarily.
* @par Change byte order permanently.
* Sample code:
* @code{.cpp}
* CInByteStream in(buf, sz);
* COutByteStream out;
*
* // Set the underlying data buffer as Net Byte Order (Big Endian).
* in >> Mapip::net_order;
* out << Manip::net_order;
*
* // Set the underlying data buffer as Host Byte Order.
* in >> Mapip::host_order;
* out << Manip::host_order;
*
* // Set the underlying data buffer as Little Endian.
* in >> Mapip::little_endian;
* out << Manip::little_endian;
*
* // Set the underlying data buffer as Big Endian (Net Byte Order).
* in >> Mapip::big_endian;
* out << Manip::big_endian;
* @endcode
* @par Change byte order temporarily.
* Set byte order and pack/unpack data, then restore old byte order.
* @n Sample code:
* @code{.cpp}
* CInByteStream in(buf, sz);
* COutByteStream out;
* int val = 10;
*
* in >> Mapip::net_order_value(val); // unpack an integer using Net Byte Order, despite
* // the actual byte order of 'in'
* out << Mapip::net_order_value(val); // pack an integer using Net Byte Order, dspite the
* // actual byte order of 'out'
*
* in >> Mapip::host_order_value(val); // unpack an integer using Host Byte Order, despite
* // the actual byte order of 'in'
* out << Mapip::host_order_value(val); // pack an integer using Host Byte Order, dspite the
* // actual byte order of 'out'
*
* in >> Mapip::little_endian_value(val); // unpack an integer using Little Endian, despite
* // the actual byte order of 'in'
* out << Mapip::little_endian_value(val); // pack an integer using Little Endian, dspite the
* // actual byte order of 'out'
*
* in >> Mapip::big_endian_value(val); // unpack an integer using Big Endian, despite the
* // actual byte order of 'in'
* out << Mapip::big_endian_value(val); // pack an integer using Big Endian, dspite the actual
* // byte order of 'out'
* @endcode
* @{ */
inline void net_order(NS_IMPL::CDataStreamBase & ds){ds.netByteOrder(true);}
inline void host_order(NS_IMPL::CDataStreamBase & ds){ds.netByteOrder(false);}
inline void little_endian(NS_IMPL::CDataStreamBase & ds){ds.littleEndian(true);}
inline void big_endian(NS_IMPL::CDataStreamBase & ds){ds.littleEndian(false);}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<T> net_order_value(T & val){
return NS_IMPL::CManipulatorValueByteOrder<T>(val, net_order);
}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<const T> net_order_value(const T & val){
return NS_IMPL::CManipulatorValueByteOrder<const T>(val, net_order);
}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<T> host_order_value(T & val){
return NS_IMPL::CManipulatorValueByteOrder<T>(val, host_order);
}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<const T> host_order_value(const T & val){
return NS_IMPL::CManipulatorValueByteOrder<const T>(val, host_order);
}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<T> little_endian_value(T & val){
return NS_IMPL::CManipulatorValueByteOrder<T>(val, little_endian);
}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<const T> little_endian_value(const T & val){
return NS_IMPL::CManipulatorValueByteOrder<const T>(val, little_endian);
}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<T> big_endian_value(T & val){
return NS_IMPL::CManipulatorValueByteOrder<T>(val, big_endian);
}
template<class T>
inline NS_IMPL::CManipulatorValueByteOrder<const T> big_endian_value(const T & val){
return NS_IMPL::CManipulatorValueByteOrder<const T>(val, big_endian);
}
/** @} */
/**
* @name Current position
* APIs for changing current position, both permanently and temporarily.
* @par Change current postion permanently
* Sample code:
* @code{.cpp}
* CInByteStream in(buf, sz);
* COutByteStream out;
*
* // Set current position absolutely
* in >> Mapip::seek(10); // Same as in.seek(10)
* out << Mapip::seek(10); // Same as out.seek(10)
*
* // Move current position relatively
* in >> Mapip::skip(10); // Same as in.skip(10)
* out << Mapip::skip(10); // Same as out.skip(10)
* uint32_t off = -10;
* in >> Mapip::skip(&off); // Same as in.skip(off)
* out << Mapip::skip(&off); // Same as out.skip(off)
*
* // Fill while moving
* int fill = 'a';
* in >> Mapip::skip(10, fill); // Same as in.skip(10, fill)
* out << Mapip::skip(10, fill); // Same as out.skip(10, fill)
* uint32_t off = -10;
* in >> Mapip::skip(&off, fill); // Same as in.skip(off, fill)
* out << Mapip::skip(&off, fill); // Same as out.skip(off, fill)
* @endcode
* @par Change current postion temporarily
* Move current position and pack/unpack data, then restore old current position.
* @n Sample code:
* @code{.cpp}
* CInByteStream in(buf, sz);
* COutByteStream out;
* int val = 100;
*
* // Unpack data from an absolute position
* in >> Mapip::offset_value(10, val);
* // This is equivalent to:
* // size_t old = in.cur();
* // in.seek(10);
* // in >> val;
* // in.seek(old);
*
* // Pack data to an absolute position
* out << Mapip::offset_value(10, val);
* // This is equivalent to:
* // size_t old = out.cur();
* // out.seek(10);
* // out << val;
* // out.seek(old);
* @endcode
* @par Insert data (pack only)
* Sometimes you need to @a insert a value to a particular position in already packed data,
* which means all existing data after that position need to move forward, to make room for
* the value. This operation could be complex without @c Manip::insert.
* @n Sample code:
* @code{.cpp}
* COutByteStream out;
* int val = 100;
*
* out << Mapip::insert(10, val); // pack 'val' at absolute position 10. Any existing data
* // after this position will move forward by 'sizeof val'
* // bytes to make room for 'val'
* @endcode
* @{ */
inline NS_IMPL::CManipulatorSeek seek(size_t pos){return NS_IMPL::CManipulatorSeek(pos);}
inline NS_IMPL::CManipulatorSkip skip(ssize_t off){return NS_IMPL::CManipulatorSkip(off);}
inline NS_IMPL::CManipulatorSkipFill skip(ssize_t off, int fill){return NS_IMPL::CManipulatorSkipFill(off, fill);}
template<typename T>
inline NS_IMPL::CManipulatorSkipPtr<T> skip(T * off){return NS_IMPL::CManipulatorSkipPtr<T>(off);}
template<typename T>
inline NS_IMPL::CManipulatorSkipPtrFill<T> skip(T * off, int fill){return NS_IMPL::CManipulatorSkipPtrFill<T>(off);}
template<class T>
inline NS_IMPL::CManipulatorOffsetValue<T> offset_value(size_t pos, T & val){
return NS_IMPL::CManipulatorOffsetValue<T>(pos, val);
}
template<class T>
inline NS_IMPL::CManipulatorOffsetValue<const T> offset_value(size_t pos, const T & val){
return NS_IMPL::CManipulatorOffsetValue<const T>(pos, val);
}
template<class T>
inline NS_IMPL::CManipulatorInsert<T> insert(size_t pos, const T & val){
return NS_IMPL::CManipulatorInsert<T>(pos, val);
}
/** @} */
/**
* @brief Pack/unpack [Protocol Buffers](https://github.com/google/protobuf) messages.
* This manipulator makes protobuf messages easily cooperative with other data formats.
* Packing/unpacking a protobuf message is as simple as for an integer. Boundary checks are
* automatically done, data pointers are efficiently handled.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
*
* AnyMessage msg; // any protobuf message object
*
* in >> Manip::protobuf(msg); // unpack 'msg' from all left data of 'in'
* @endcode
* By default, Manip::protobuf will use all left data of @c in to initialize protobuf message
* object, which means the message should be the last value of @c in.
* @n But if that's not the case, you must provide a size parameter to limit the reading:
* @code{.cpp}
* in >> Manip::protobuf(msg, 50); // unpack 'msg' from 50 bytes of data of 'in'
* @endcode
* @note In both cases Manip::protobuf will try to use all data available to initialize the
* message. If data size is incorrect, no matter smaller or larger than actually needed, this
* operation will fail and status of @c in will be set to non-zero.
*
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* AnyMessage msg; // any protobuf message object
*
* out << Manip::protobuf(msg); // pack 'msg'
* @endcode
* @tparam T Any subclass of @c google::protobuf::MessageLite
* @param[inout] msg A message object
* @param[in] size Size of bytes read when unpacking @c msg
*/
template<class T>
inline NS_IMPL::CManipulatorProtobuf<T> protobuf(T & msg, size_t size = size_t(-1)){
return NS_IMPL::CManipulatorProtobuf<T>(msg, size);
}
/**
* @brief Pack integer using [Base 128 Varints]
* (http://developers.google.com/protocol-buffers/docs/encoding#varints) encoding.
* Varint encoding is compact, byte order independent, and efficient. This manipulator makes it
* super easy to take advantage of it, and supports all primitive integer types, from @c char to
* @c wchar_t to `long long`.
* @n Sample Code:
* @code{.cpp}
* COutByteStream out;
*
* out << Manip::varint(1000); // pack number '1000' using Base 128 Varints encoding
* @endcode
* @tparam T An integer type
* @param val An integer
*/
template<typename T>
inline NS_IMPL::CManipulatorVarint<const T> varint(const T & val){
return NS_IMPL::CManipulatorVarint<const T>(val);
}
/**
* @brief Unpack integer using [Base 128 Varints]
* (http://developers.google.com/protocol-buffers/docs/encoding#varints) encoding.
* Sample Code:
* @code{.cpp}
* CInByteStream in(buf, sz);
* int val;
*
* in >> Manip::varint(val); // unpack an integer to 'val' using Base 128 Varints encoding
* @endcode
* @tparam T An integer type
* @param[out] val An integer to receive the result
*/
template<typename T>
inline NS_IMPL::CManipulatorVarint<T> varint(T & val){
return NS_IMPL::CManipulatorVarint<T>(val);
}
/**
* @brief Set up a stub.
* Any packing/unpacking operation cannot go beyond the stub, or it fails with status set
* to non-zero.
* @n Stubs can stack, the top most one is currently in effect. After popping up it, the next
* top one takes effect immediately, until there is no stubs.
* @n A common use of stubs is to create a robust hierarchical data structure in which errors
* in one part never affect other parts. Besides, additional boundary checks make defects
* harder to hide.
* @n When creating a stub, you need provide offset of the stub, @c sz, relative to *current
* pointer*, i.e. the position of the stub is `cur() + sz`.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
* uint32_t val;
*
* in >> Manip::stub(4); // Set up a stub at (in.cur() + 4)
* in >> val; // Fine, unpack a 4-byte integer
* in >> val; // FAIL! Can NOT go beyond the stub
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* out << Mapip::stub(4); // Set up a stub at (out.cur() + 4)
* out << uint32_t(100); // Fine, pack a 4-byte integer
* out << 'a'; // FAIL! Can NOT go beyong the stub
* @endcode
* @param sz Offset of the stub, relative to *current pointer*
*/
inline NS_IMPL::CManipulatorStubPush stub(size_t sz){
return NS_IMPL::CManipulatorStubPush(sz);
}
/**
* @brief Demolish the top most stub.
* This manipulator demolishes current stub, check and align *current pointer* if needed. The
* next top stub will take effect, until there is no stubs any more.
* @n Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, sz);
* uint32_t val;
*
* in >> Manip::stub(4); // Set up a stub at (in.cur() + 4)
* in >> val; // Fine, unpack a 4-byte integer
*
* in >> Mapip::stub_pop() // Remove the stub
* in >> val; // Fine, unpack another 4-byte integer
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* COutByteStream out;
*
* out << Mapip::stub(4); // Set up a stub at (out.cur() + 4)
* out << uint32_t(100); // Fine, pack a 4-byte integer
*
* out << Manip::stub_pop();// remove the stub
* out << 'a'; // Fine, pack a char
* @endcode
* @param align
* @li @c true: Align *current pointer* to the position of removed stub. This is useful when
* you want to ignore any data in a sub-structure.
* @li @c false: Do not change *current pointer*.
* @param check
* @li @c true: Assert if *current pointer* equals to the position of removed stub. If failed,
* status will be set to non-zero. This is useful when you cannot tolerate any errors in the
* sub-structure.
* @li @c false: Do not check.
*/
inline NS_IMPL::CManipulatorStubPop stub_pop(bool align = false, bool check = false){
return NS_IMPL::CManipulatorStubPop(align, check);
}
/**
* @name Manip::end
* End packing/unpacking operations.
* When all packing/unpacking operations finish, it's a good practice to announce an @c end
* explicitly.
* @n For unpacking operations (CInByteStream), left data size will be checked. If it's not
* zero, status of CInByteStream object will be set to non-zero. Because usually some left data
* means you have missed something, or there is a misunderstanding about the protocol.
* @n For packing operations (COutByteStreamBasic), it's always necessary to @c end.
* Because the underlying data buffer (whether it's internal or external) may have reserved
* some room for performance, so an adjustment is critical to correct its size. Besides, it will
* call corresponding @ref COutByteStreamBasic::finish to export data.
* @note Ending operation will clear all stubs, without any alignments or checks.
* @sa COutByteStreamBasic::finish
* @{ */
/**
* @brief End operations for @ref CInByteStream, @ref COutByteStreamStrRef, @ref
* COutByteStreamVecRef.
* Sample code for CInByteStream:
* @code{.cpp}
* CInByteStream in(buf, 8);
* uint32_t val;
*
* in >> val; // unpack a 4-byte integer
* // in >> Manip::end; // FAIL! There are 4 bytes left
*
* in >> val; // unpack another 4-byte integer
* in >> Manip::end; // Fine, no left data
* @endcode
* Sample code for COutByteStreamBasic:
* @code{.cpp}
* string buf;
* COutByteStreamStrRef out(buf); // 'buf' is the underlying buffer for 'out'
*
* out << uint32_t(100); // pack a 4-byte integer,
* // assert(buf.size() == 4); // ERROR! buf.size() >= 4
*
* out << Manip::end; // adjust buf.size()
* assert(buf.size() == 4); // Correct
* @endcode
*/
inline NS_IMPL::CManipulatorEnd<void, void> end(){
return NS_IMPL::CManipulatorEnd<void, void>();
}
/**
* @brief End operations for @ref COutByteStreamBuf, @ref COutByteStreamStrRef, @ref
* COutByteStreamVecRef.
* This manipulator will also store the byte size of underlying data buffer in @c sz.
* @n Sample code:
* @code{.cpp}
* char * buf = new char[10];
* COutByteStreamBuf out(buf, 10); // 'buf' is the underlying buffer for 'out'
*
* out << uint32_t(100); // pack a 4-byte integer,
*
* size_t sz;
* out << Manip::end(&sz); // set correct size to 'sz'
* assert(sz == 4); // Correct
* @endcode
* @tparam SizeT Type of an integer
* @param[out] sz Pointer to an integer to receive the size of actual data
*/
template<typename SizeT>
inline NS_IMPL::CManipulatorEnd<SizeT *, void> end(SizeT * sz){
return NS_IMPL::CManipulatorEnd<SizeT *, void>(sz);
}
/**
* @brief End operations for @ref COutByteStream / @ref COutByteStreamStr, @ref
* COutByteStreamStrRef, @ref COutByteStreamVec, @ref COutByteStreamVecRef.
* This manipulator will also export data to @c buf. If it's not empty, new data will append to
* it.
* @n Sample code:
* @code{.cpp}
* COutByteStream out;
*
* out << uint32_t(100); // pack a 4-byte integer
*
* string buf;
* out << Manip::end(buf); // export data to 'buf'
* assert(buf.size() == 4); // Correct
* @endcode
* @note Data copy is avoided whenever possible, e.g. @c buf is empty and the underlying buffer
* is internal (@ref COutByteStream / @ref COutByteStreamStr and @ref COutByteStreamVec).
* @tparam BufT Must be the same as the underlying buffer type, e.g. @c std::string for
* @ref COutByteStream / @ref COutByteStreamStr, @ref COutByteStreamStrRef, or @c
* std::vector<char> for @ref COutByteStreamVec, @ref COutByteStreamVecRef
* @param[out] buf A buffer to receive data
*/
template<class BufT>
inline NS_IMPL::CManipulatorEnd<BufT, void> end(BufT & buf){
return NS_IMPL::CManipulatorEnd<BufT, void>(buf);
}
/**
* @brief End operations for @ref COutByteStream / @ref COutByteStreamStr, @ref
* COutByteStreamStrRef, @ref COutByteStreamVec, @ref COutByteStreamVecRef, @ref
* COutByteStreamBuf.
* Sample code;
* @code{.cpp}
* COutByteStream out;
*
* out << uint32_t(100); // pack a 4-byte integer
*
* char * buf = new char[10];
* size_t sz = 10;
*
* out << Manip::end(buf, &sz); // export data to 'buf'
* assert(sz == 4); // Correct
* @endcode
* @tparam CharT Must be the same as the underlying character type, e.g. @c char
* @param[out] buf Pointer to a byte array to receive the data