MessagePack for C++
pack.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ serializing routine
3 //
4 // Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 #ifndef MSGPACK_V1_PACK_HPP
11 #define MSGPACK_V1_PACK_HPP
12 
13 #include "msgpack/v1/pack_decl.hpp"
14 
15 #include <stdexcept>
16 #include <limits>
17 #include <cstring>
18 #include <climits>
19 
20 namespace msgpack {
21 
25 
27 
31 template <typename Stream>
32 class packer {
33 public:
35 
41  packer(Stream* s);
43 
46  packer(Stream& s);
47 
48 public:
50 
57  template <typename T>
58  packer<Stream>& pack(const T& v);
59 
61 
71  packer<Stream>& pack_uint8(uint8_t d);
72 
74 
84  packer<Stream>& pack_uint16(uint16_t d);
85 
87 
97  packer<Stream>& pack_uint32(uint32_t d);
98 
100 
111  packer<Stream>& pack_uint64(uint64_t d);
112 
114 
125  packer<Stream>& pack_int8(int8_t d);
126 
128 
139  packer<Stream>& pack_int16(int16_t d);
140 
142 
153  packer<Stream>& pack_int32(int32_t d);
154 
156 
167  packer<Stream>& pack_int64(int64_t d);
168 
169 
170 
172 
180  packer<Stream>& pack_fix_uint8(uint8_t d);
181 
183 
191  packer<Stream>& pack_fix_uint16(uint16_t d);
192 
194 
202  packer<Stream>& pack_fix_uint32(uint32_t d);
203 
205 
213  packer<Stream>& pack_fix_uint64(uint64_t d);
214 
216 
224  packer<Stream>& pack_fix_int8(int8_t d);
225 
227 
235  packer<Stream>& pack_fix_int16(int16_t d);
236 
238 
246  packer<Stream>& pack_fix_int32(int32_t d);
247 
249 
257  packer<Stream>& pack_fix_int64(int64_t d);
258 
259 
261 
272  packer<Stream>& pack_char(char d);
273 
275 
286  packer<Stream>& pack_signed_char(signed char d);
287 
289 
300  packer<Stream>& pack_short(short d);
301 
303 
314  packer<Stream>& pack_int(int d);
315 
317 
328  packer<Stream>& pack_long(long d);
329 
331 
342  packer<Stream>& pack_long_long(long long d);
343 
344 
346 
356  packer<Stream>& pack_unsigned_char(unsigned char d);
357 
359 
369  packer<Stream>& pack_unsigned_short(unsigned short d);
370 
372 
382  packer<Stream>& pack_unsigned_int(unsigned int d);
383 
385 
395  packer<Stream>& pack_unsigned_long(unsigned long d);
396 
398 
408  packer<Stream>& pack_unsigned_long_long(unsigned long long d);
409 
411 
419  packer<Stream>& pack_float(float d);
420 
422 
430  packer<Stream>& pack_double(double d);
431 
432 
434 
440  packer<Stream>& pack_nil();
441 
443 
449  packer<Stream>& pack_true();
450 
452 
458  packer<Stream>& pack_false();
459 
461 
470  packer<Stream>& pack_array(uint32_t n);
471 
473 
482  packer<Stream>& pack_map(uint32_t n);
483 
484 
486 
496  packer<Stream>& pack_str(uint32_t l);
497 
499 
508  packer<Stream>& pack_str_body(const char* b, uint32_t l);
509 
511 
522  packer<Stream>& pack_v4raw(uint32_t l);
523 
525 
535  packer<Stream>& pack_v4raw_body(const char* b, uint32_t l);
536 
538 
548  packer<Stream>& pack_bin(uint32_t l);
549 
551 
560  packer<Stream>& pack_bin_body(const char* b, uint32_t l);
561 
563 
574  packer<Stream>& pack_ext(size_t l, int8_t type);
575 
577 
586  packer<Stream>& pack_ext_body(const char* b, uint32_t l);
587 
588 private:
589  template <typename T>
590  void pack_imp_uint8(T d);
591  template <typename T>
592  void pack_imp_uint16(T d);
593  template <typename T>
594  void pack_imp_uint32(T d);
595  template <typename T>
596  void pack_imp_uint64(T d);
597  template <typename T>
598  void pack_imp_int8(T d);
599  template <typename T>
600  void pack_imp_int16(T d);
601  template <typename T>
602  void pack_imp_int32(T d);
603  template <typename T>
604  void pack_imp_int64(T d);
605 
606  void append_buffer(const char* buf, size_t len)
607  { m_stream.write(buf, len); }
608 
609 private:
610  Stream& m_stream;
611 
612 #if defined(MSGPACK_USE_CPP03)
613 private:
614  packer(const packer&);
615  packer& operator=(const packer&);
616  packer();
617 #else // defined(MSGPACK_USE_CPP03)
618 public:
619  packer(const packer&) = delete;
620  packer& operator=(const packer&) = delete;
621  packer() = delete;
622 #endif // defined(MSGPACK_USE_CPP03)
623 };
624 
625 
627 
636 template <typename Stream, typename T>
637 inline void pack(Stream* s, const T& v)
638 {
639  packer<Stream>(*s).pack(v);
640 }
641 
643 
649 template <typename Stream, typename T>
650 inline void pack(Stream& s, const T& v)
651 {
652  packer<Stream>(s).pack(v);
653 }
654 
655 
656 #if MSGPACK_ENDIAN_LITTLE_BYTE
657 template <typename T>
658 inline char take8_8(T d) {
659  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
660 }
661 template <typename T>
662 inline char take8_16(T d) {
663  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
664 }
665 template <typename T>
666 inline char take8_32(T d) {
667  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
668 }
669 template <typename T>
670 inline char take8_64(T d) {
671  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
672 }
673 
674 #elif MSGPACK_ENDIAN_BIG_BYTE
675 
676 template <typename T>
677 inline char take8_8(T d) {
678  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
679 }
680 template <typename T>
681 inline char take8_16(T d) {
682  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[1]);
683 }
684 template <typename T>
685 inline char take8_32(T d) {
686  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[3]);
687 }
688 template <typename T>
689 inline char take8_64(T d) {
690  return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[7]);
691 }
692 
693 #else
694 #error msgpack-c supports only big endian and little endian
695 #endif
696 
697 template <typename Stream>
698 inline packer<Stream>::packer(Stream* s) : m_stream(*s) { }
699 
700 template <typename Stream>
701 inline packer<Stream>::packer(Stream& s) : m_stream(s) { }
702 
703 
704 template <typename Stream>
706 { pack_imp_uint8(d); return *this; }
707 
708 template <typename Stream>
710 { pack_imp_uint16(d); return *this; }
711 
712 template <typename Stream>
714 { pack_imp_uint32(d); return *this; }
715 
716 template <typename Stream>
718 { pack_imp_uint64(d); return *this; }
719 
720 template <typename Stream>
722 { pack_imp_int8(d); return *this; }
723 
724 template <typename Stream>
726 { pack_imp_int16(d); return *this; }
727 
728 template <typename Stream>
730 { pack_imp_int32(d); return *this; }
731 
732 template <typename Stream>
734 { pack_imp_int64(d); return *this;}
735 
736 
737 template <typename Stream>
739 {
740  char buf[2] = {static_cast<char>(0xccu), take8_8(d)};
741  append_buffer(buf, 2);
742  return *this;
743 }
744 
745 template <typename Stream>
747 {
748  char buf[3];
749  buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], d);
750  append_buffer(buf, 3);
751  return *this;
752 }
753 
754 template <typename Stream>
756 {
757  char buf[5];
758  buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], d);
759  append_buffer(buf, 5);
760  return *this;
761 }
762 
763 template <typename Stream>
765 {
766  char buf[9];
767  buf[0] = static_cast<char>(0xcfu); _msgpack_store64(&buf[1], d);
768  append_buffer(buf, 9);
769  return *this;
770 }
771 
772 template <typename Stream>
774 {
775  char buf[2] = {static_cast<char>(0xd0u), take8_8(d)};
776  append_buffer(buf, 2);
777  return *this;
778 }
779 
780 template <typename Stream>
782 {
783  char buf[3];
784  buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], d);
785  append_buffer(buf, 3);
786  return *this;
787 }
788 
789 template <typename Stream>
791 {
792  char buf[5];
793  buf[0] = static_cast<char>(0xd2u); _msgpack_store32(&buf[1], d);
794  append_buffer(buf, 5);
795  return *this;
796 }
797 
798 template <typename Stream>
800 {
801  char buf[9];
802  buf[0] = static_cast<char>(0xd3u); _msgpack_store64(&buf[1], d);
803  append_buffer(buf, 9);
804  return *this;
805 }
806 
807 
808 template <typename Stream>
810 {
811 #if defined(CHAR_MIN)
812 #if CHAR_MIN < 0
813  pack_imp_int8(d);
814 #else
815  pack_imp_uint8(d);
816 #endif
817 #else
818 #error CHAR_MIN is not defined
819 #endif
820  return *this;
821 }
822 
823 template <typename Stream>
825 {
826  pack_imp_int8(d);
827  return *this;
828 }
829 
830 template <typename Stream>
832 {
833 #if defined(SIZEOF_SHORT)
834 #if SIZEOF_SHORT == 2
835  pack_imp_int16(d);
836 #elif SIZEOF_SHORT == 4
837  pack_imp_int32(d);
838 #else
839  pack_imp_int64(d);
840 #endif
841 
842 #elif defined(SHRT_MAX)
843 #if SHRT_MAX == 0x7fff
844  pack_imp_int16(d);
845 #elif SHRT_MAX == 0x7fffffff
846  pack_imp_int32(d);
847 #else
848  pack_imp_int64(d);
849 #endif
850 
851 #else
852  if(sizeof(short) == 2) {
853  pack_imp_int16(d);
854  } else if(sizeof(short) == 4) {
855  pack_imp_int32(d);
856  } else {
857  pack_imp_int64(d);
858  }
859 #endif
860  return *this;
861 }
862 
863 template <typename Stream>
865 {
866 #if defined(SIZEOF_INT)
867 #if SIZEOF_INT == 2
868  pack_imp_int16(d);
869 #elif SIZEOF_INT == 4
870  pack_imp_int32(d);
871 #else
872  pack_imp_int64(d);
873 #endif
874 
875 #elif defined(INT_MAX)
876 #if INT_MAX == 0x7fff
877  pack_imp_int16(d);
878 #elif INT_MAX == 0x7fffffff
879  pack_imp_int32(d);
880 #else
881  pack_imp_int64(d);
882 #endif
883 
884 #else
885  if(sizeof(int) == 2) {
886  pack_imp_int16(d);
887  } else if(sizeof(int) == 4) {
888  pack_imp_int32(d);
889  } else {
890  pack_imp_int64(d);
891  }
892 #endif
893  return *this;
894 }
895 
896 template <typename Stream>
898 {
899 #if defined(SIZEOF_LONG)
900 #if SIZEOF_LONG == 2
901  pack_imp_int16(d);
902 #elif SIZEOF_LONG == 4
903  pack_imp_int32(d);
904 #else
905  pack_imp_int64(d);
906 #endif
907 
908 #elif defined(LONG_MAX)
909 #if LONG_MAX == 0x7fffL
910  pack_imp_int16(d);
911 #elif LONG_MAX == 0x7fffffffL
912  pack_imp_int32(d);
913 #else
914  pack_imp_int64(d);
915 #endif
916 
917 #else
918  if(sizeof(long) == 2) {
919  pack_imp_int16(d);
920  } else if(sizeof(long) == 4) {
921  pack_imp_int32(d);
922  } else {
923  pack_imp_int64(d);
924  }
925 #endif
926  return *this;
927 }
928 
929 template <typename Stream>
931 {
932 #if defined(SIZEOF_LONG_LONG)
933 #if SIZEOF_LONG_LONG == 2
934  pack_imp_int16(d);
935 #elif SIZEOF_LONG_LONG == 4
936  pack_imp_int32(d);
937 #else
938  pack_imp_int64(d);
939 #endif
940 
941 #elif defined(LLONG_MAX)
942 #if LLONG_MAX == 0x7fffL
943  pack_imp_int16(d);
944 #elif LLONG_MAX == 0x7fffffffL
945  pack_imp_int32(d);
946 #else
947  pack_imp_int64(d);
948 #endif
949 
950 #else
951  if(sizeof(long long) == 2) {
952  pack_imp_int16(d);
953  } else if(sizeof(long long) == 4) {
954  pack_imp_int32(d);
955  } else {
956  pack_imp_int64(d);
957  }
958 #endif
959  return *this;
960 }
961 
962 
963 template <typename Stream>
965 {
966  pack_imp_uint8(d);
967  return *this;
968 }
969 
970 template <typename Stream>
972 {
973 #if defined(SIZEOF_SHORT)
974 #if SIZEOF_SHORT == 2
975  pack_imp_uint16(d);
976 #elif SIZEOF_SHORT == 4
977  pack_imp_uint32(d);
978 #else
979  pack_imp_uint64(d);
980 #endif
981 
982 #elif defined(USHRT_MAX)
983 #if USHRT_MAX == 0xffffU
984  pack_imp_uint16(d);
985 #elif USHRT_MAX == 0xffffffffU
986  pack_imp_uint32(d);
987 #else
988  pack_imp_uint64(d);
989 #endif
990 
991 #else
992  if(sizeof(unsigned short) == 2) {
993  pack_imp_uint16(d);
994  } else if(sizeof(unsigned short) == 4) {
995  pack_imp_uint32(d);
996  } else {
997  pack_imp_uint64(d);
998  }
999 #endif
1000  return *this;
1001 }
1002 
1003 template <typename Stream>
1005 {
1006 #if defined(SIZEOF_INT)
1007 #if SIZEOF_INT == 2
1008  pack_imp_uint16(d);
1009 #elif SIZEOF_INT == 4
1010  pack_imp_uint32(d);
1011 #else
1012  pack_imp_uint64(d);
1013 #endif
1014 
1015 #elif defined(UINT_MAX)
1016 #if UINT_MAX == 0xffffU
1017  pack_imp_uint16(d);
1018 #elif UINT_MAX == 0xffffffffU
1019  pack_imp_uint32(d);
1020 #else
1021  pack_imp_uint64(d);
1022 #endif
1023 
1024 #else
1025  if(sizeof(unsigned int) == 2) {
1026  pack_imp_uint16(d);
1027  } else if(sizeof(unsigned int) == 4) {
1028  pack_imp_uint32(d);
1029  } else {
1030  pack_imp_uint64(d);
1031  }
1032 #endif
1033  return *this;
1034 }
1035 
1036 template <typename Stream>
1038 {
1039 #if defined(SIZEOF_LONG)
1040 #if SIZEOF_LONG == 2
1041  pack_imp_uint16(d);
1042 #elif SIZEOF_LONG == 4
1043  pack_imp_uint32(d);
1044 #else
1045  pack_imp_uint64(d);
1046 #endif
1047 
1048 #elif defined(ULONG_MAX)
1049 #if ULONG_MAX == 0xffffUL
1050  pack_imp_uint16(d);
1051 #elif ULONG_MAX == 0xffffffffUL
1052  pack_imp_uint32(d);
1053 #else
1054  pack_imp_uint64(d);
1055 #endif
1056 
1057 #else
1058  if(sizeof(unsigned long) == 2) {
1059  pack_imp_uint16(d);
1060  } else if(sizeof(unsigned long) == 4) {
1061  pack_imp_uint32(d);
1062  } else {
1063  pack_imp_uint64(d);
1064  }
1065 #endif
1066  return *this;
1067 }
1068 
1069 template <typename Stream>
1071 {
1072 #if defined(SIZEOF_LONG_LONG)
1073 #if SIZEOF_LONG_LONG == 2
1074  pack_imp_uint16(d);
1075 #elif SIZEOF_LONG_LONG == 4
1076  pack_imp_uint32(d);
1077 #else
1078  pack_imp_uint64(d);
1079 #endif
1080 
1081 #elif defined(ULLONG_MAX)
1082 #if ULLONG_MAX == 0xffffUL
1083  pack_imp_uint16(d);
1084 #elif ULLONG_MAX == 0xffffffffUL
1085  pack_imp_uint32(d);
1086 #else
1087  pack_imp_uint64(d);
1088 #endif
1089 
1090 #else
1091  if(sizeof(unsigned long long) == 2) {
1092  pack_imp_uint16(d);
1093  } else if(sizeof(unsigned long long) == 4) {
1094  pack_imp_uint32(d);
1095  } else {
1096  pack_imp_uint64(d);
1097  }
1098 #endif
1099  return *this;
1100 }
1101 
1102 
1103 template <typename Stream>
1105 {
1106  union { float f; uint32_t i; } mem;
1107  mem.f = d;
1108  char buf[5];
1109  buf[0] = static_cast<char>(0xcau); _msgpack_store32(&buf[1], mem.i);
1110  append_buffer(buf, 5);
1111  return *this;
1112 }
1113 
1114 template <typename Stream>
1116 {
1117  union { double f; uint64_t i; } mem;
1118  mem.f = d;
1119  char buf[9];
1120  buf[0] = static_cast<char>(0xcbu);
1121 
1122 #if defined(TARGET_OS_IPHONE)
1123  // ok
1124 #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi
1125  // https://github.com/msgpack/msgpack-perl/pull/1
1126  mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
1127 #endif
1128  _msgpack_store64(&buf[1], mem.i);
1129  append_buffer(buf, 9);
1130  return *this;
1131 }
1132 
1133 
1134 template <typename Stream>
1136 {
1137  const char d = static_cast<char>(0xc0u);
1138  append_buffer(&d, 1);
1139  return *this;
1140 }
1141 
1142 template <typename Stream>
1144 {
1145  const char d = static_cast<char>(0xc3u);
1146  append_buffer(&d, 1);
1147  return *this;
1148 }
1149 
1150 template <typename Stream>
1152 {
1153  const char d = static_cast<char>(0xc2u);
1154  append_buffer(&d, 1);
1155  return *this;
1156 }
1157 
1158 
1159 template <typename Stream>
1161 {
1162  if(n < 16) {
1163  char d = static_cast<char>(0x90u | n);
1164  append_buffer(&d, 1);
1165  } else if(n < 65536) {
1166  char buf[3];
1167  buf[0] = static_cast<char>(0xdcu); _msgpack_store16(&buf[1], static_cast<uint16_t>(n));
1168  append_buffer(buf, 3);
1169  } else {
1170  char buf[5];
1171  buf[0] = static_cast<char>(0xddu); _msgpack_store32(&buf[1], static_cast<uint32_t>(n));
1172  append_buffer(buf, 5);
1173  }
1174  return *this;
1175 }
1176 
1177 template <typename Stream>
1179 {
1180  if(n < 16) {
1181  unsigned char d = static_cast<unsigned char>(0x80u | n);
1182  char buf = take8_8(d);
1183  append_buffer(&buf, 1);
1184  } else if(n < 65536) {
1185  char buf[3];
1186  buf[0] = static_cast<char>(0xdeu); _msgpack_store16(&buf[1], static_cast<uint16_t>(n));
1187  append_buffer(buf, 3);
1188  } else {
1189  char buf[5];
1190  buf[0] = static_cast<char>(0xdfu); _msgpack_store32(&buf[1], static_cast<uint32_t>(n));
1191  append_buffer(buf, 5);
1192  }
1193  return *this;
1194 }
1195 
1196 template <typename Stream>
1198 {
1199  if(l < 32) {
1200  unsigned char d = static_cast<uint8_t>(0xa0u | l);
1201  char buf = take8_8(d);
1202  append_buffer(&buf, 1);
1203  } else if(l < 256) {
1204  char buf[2];
1205  buf[0] = static_cast<char>(0xd9u); buf[1] = static_cast<uint8_t>(l);
1206  append_buffer(buf, 2);
1207  } else if(l < 65536) {
1208  char buf[3];
1209  buf[0] = static_cast<char>(0xdau); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
1210  append_buffer(buf, 3);
1211  } else {
1212  char buf[5];
1213  buf[0] = static_cast<char>(0xdbu); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
1214  append_buffer(buf, 5);
1215  }
1216  return *this;
1217 }
1218 
1219 template <typename Stream>
1220 inline packer<Stream>& packer<Stream>::pack_str_body(const char* b, uint32_t l)
1221 {
1222  append_buffer(b, l);
1223  return *this;
1224 }
1225 
1226 // Raw (V4)
1227 
1228 template <typename Stream>
1230 {
1231  if(l < 32) {
1232  unsigned char d = static_cast<uint8_t>(0xa0u | l);
1233  char buf = take8_8(d);
1234  append_buffer(&buf, 1);
1235  } else if(l < 65536) {
1236  char buf[3];
1237  buf[0] = static_cast<char>(0xdau); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
1238  append_buffer(buf, 3);
1239  } else {
1240  char buf[5];
1241  buf[0] = static_cast<char>(0xdbu); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
1242  append_buffer(buf, 5);
1243  }
1244  return *this;
1245 }
1246 
1247 template <typename Stream>
1248 inline packer<Stream>& packer<Stream>::pack_v4raw_body(const char* b, uint32_t l)
1249 {
1250  append_buffer(b, l);
1251  return *this;
1252 }
1253 
1254 template <typename Stream>
1256 {
1257  if(l < 256) {
1258  char buf[2];
1259  buf[0] = static_cast<char>(0xc4u); buf[1] = static_cast<uint8_t>(l);
1260  append_buffer(buf, 2);
1261  } else if(l < 65536) {
1262  char buf[3];
1263  buf[0] = static_cast<char>(0xc5u); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
1264  append_buffer(buf, 3);
1265  } else {
1266  char buf[5];
1267  buf[0] = static_cast<char>(0xc6u); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
1268  append_buffer(buf, 5);
1269  }
1270  return *this;
1271 }
1272 
1273 template <typename Stream>
1274 inline packer<Stream>& packer<Stream>::pack_bin_body(const char* b, uint32_t l)
1275 {
1276  append_buffer(b, l);
1277  return *this;
1278 }
1279 
1280 template <typename Stream>
1281 inline packer<Stream>& packer<Stream>::pack_ext(size_t l, int8_t type)
1282 {
1283  switch(l) {
1284  case 1: {
1285  char buf[2];
1286  buf[0] = static_cast<char>(0xd4u);
1287  buf[1] = static_cast<char>(type);
1288  append_buffer(buf, 2);
1289  } break;
1290  case 2: {
1291  char buf[2];
1292  buf[0] = static_cast<char>(0xd5u);
1293  buf[1] = static_cast<char>(type);
1294  append_buffer(buf, 2);
1295  } break;
1296  case 4: {
1297  char buf[2];
1298  buf[0] = static_cast<char>(0xd6u);
1299  buf[1] = static_cast<char>(type);
1300  append_buffer(buf, 2);
1301  } break;
1302  case 8: {
1303  char buf[2];
1304  buf[0] = static_cast<char>(0xd7u);
1305  buf[1] = static_cast<char>(type);
1306  append_buffer(buf, 2);
1307  } break;
1308  case 16: {
1309  char buf[2];
1310  buf[0] = static_cast<char>(0xd8u);
1311  buf[1] = static_cast<char>(type);
1312  append_buffer(buf, 2);
1313  } break;
1314  default:
1315  if(l < 256) {
1316  char buf[3];
1317  buf[0] = static_cast<char>(0xc7u);
1318  buf[1] = static_cast<char>(l);
1319  buf[2] = static_cast<char>(type);
1320  append_buffer(buf, 3);
1321  } else if(l < 65536) {
1322  char buf[4];
1323  buf[0] = static_cast<char>(0xc8u);
1324  _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
1325  buf[3] = static_cast<char>(type);
1326  append_buffer(buf, 4);
1327  } else {
1328  char buf[6];
1329  buf[0] = static_cast<char>(0xc9u);
1330  _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
1331  buf[5] = static_cast<char>(type);
1332  append_buffer(buf, 6);
1333  }
1334  break;
1335  }
1336  return *this;
1337 }
1338 
1339 template <typename Stream>
1340 inline packer<Stream>& packer<Stream>::pack_ext_body(const char* b, uint32_t l)
1341 {
1342  append_buffer(b, l);
1343  return *this;
1344 }
1345 
1346 template <typename Stream>
1347 template <typename T>
1348 inline void packer<Stream>::pack_imp_uint8(T d)
1349 {
1350  if(d < (1<<7)) {
1351  /* fixnum */
1352  char buf = take8_8(d);
1353  append_buffer(&buf, 1);
1354  } else {
1355  /* unsigned 8 */
1356  char buf[2] = {static_cast<char>(0xccu), take8_8(d)};
1357  append_buffer(buf, 2);
1358  }
1359 }
1360 
1361 template <typename Stream>
1362 template <typename T>
1363 inline void packer<Stream>::pack_imp_uint16(T d)
1364 {
1365  if(d < (1<<7)) {
1366  /* fixnum */
1367  char buf = take8_16(d);
1368  append_buffer(&buf, 1);
1369  } else if(d < (1<<8)) {
1370  /* unsigned 8 */
1371  char buf[2] = {static_cast<char>(0xccu), take8_16(d)};
1372  append_buffer(buf, 2);
1373  } else {
1374  /* unsigned 16 */
1375  char buf[3];
1376  buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
1377  append_buffer(buf, 3);
1378  }
1379 }
1380 
1381 template <typename Stream>
1382 template <typename T>
1383 inline void packer<Stream>::pack_imp_uint32(T d)
1384 {
1385  if(d < (1<<8)) {
1386  if(d < (1<<7)) {
1387  /* fixnum */
1388  char buf = take8_32(d);
1389  append_buffer(&buf, 1);
1390  } else {
1391  /* unsigned 8 */
1392  char buf[2] = {static_cast<char>(0xccu), take8_32(d)};
1393  append_buffer(buf, 2);
1394  }
1395  } else {
1396  if(d < (1<<16)) {
1397  /* unsigned 16 */
1398  char buf[3];
1399  buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
1400  append_buffer(buf, 3);
1401  } else {
1402  /* unsigned 32 */
1403  char buf[5];
1404  buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
1405  append_buffer(buf, 5);
1406  }
1407  }
1408 }
1409 
1410 template <typename Stream>
1411 template <typename T>
1412 inline void packer<Stream>::pack_imp_uint64(T d)
1413 {
1414  if(d < (1ULL<<8)) {
1415  if(d < (1ULL<<7)) {
1416  /* fixnum */
1417  char buf = take8_64(d);
1418  append_buffer(&buf, 1);
1419  } else {
1420  /* unsigned 8 */
1421  char buf[2] = {static_cast<char>(0xccu), take8_64(d)};
1422  append_buffer(buf, 2);
1423  }
1424  } else {
1425  if(d < (1ULL<<16)) {
1426  /* unsigned 16 */
1427  char buf[3];
1428  buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
1429  append_buffer(buf, 3);
1430  } else if(d < (1ULL<<32)) {
1431  /* unsigned 32 */
1432  char buf[5];
1433  buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
1434  append_buffer(buf, 5);
1435  } else {
1436  /* unsigned 64 */
1437  char buf[9];
1438  buf[0] = static_cast<char>(0xcfu); _msgpack_store64(&buf[1], d);
1439  append_buffer(buf, 9);
1440  }
1441  }
1442 }
1443 
1444 template <typename Stream>
1445 template <typename T>
1446 inline void packer<Stream>::pack_imp_int8(T d)
1447 {
1448  if(d < -(1<<5)) {
1449  /* signed 8 */
1450  char buf[2] = {static_cast<char>(0xd0u), take8_8(d)};
1451  append_buffer(buf, 2);
1452  } else {
1453  /* fixnum */
1454  char buf = take8_8(d);
1455  append_buffer(&buf, 1);
1456  }
1457 }
1458 
1459 template <typename Stream>
1460 template <typename T>
1461 inline void packer<Stream>::pack_imp_int16(T d)
1462 {
1463  if(d < -(1<<5)) {
1464  if(d < -(1<<7)) {
1465  /* signed 16 */
1466  char buf[3];
1467  buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], static_cast<int16_t>(d));
1468  append_buffer(buf, 3);
1469  } else {
1470  /* signed 8 */
1471  char buf[2] = {static_cast<char>(0xd0u), take8_16(d)};
1472  append_buffer(buf, 2);
1473  }
1474  } else if(d < (1<<7)) {
1475  /* fixnum */
1476  char buf = take8_16(d);
1477  append_buffer(&buf, 1);
1478  } else {
1479  if(d < (1<<8)) {
1480  /* unsigned 8 */
1481  char buf[2] = {static_cast<char>(0xccu), take8_16(d)};
1482  append_buffer(buf, 2);
1483  } else {
1484  /* unsigned 16 */
1485  char buf[3];
1486  buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
1487  append_buffer(buf, 3);
1488  }
1489  }
1490 }
1491 
1492 template <typename Stream>
1493 template <typename T>
1494 inline void packer<Stream>::pack_imp_int32(T d)
1495 {
1496  if(d < -(1<<5)) {
1497  if(d < -(1<<15)) {
1498  /* signed 32 */
1499  char buf[5];
1500  buf[0] = static_cast<char>(0xd2u); _msgpack_store32(&buf[1], static_cast<int32_t>(d));
1501  append_buffer(buf, 5);
1502  } else if(d < -(1<<7)) {
1503  /* signed 16 */
1504  char buf[3];
1505  buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], static_cast<int16_t>(d));
1506  append_buffer(buf, 3);
1507  } else {
1508  /* signed 8 */
1509  char buf[2] = { static_cast<char>(0xd0u), take8_32(d)};
1510  append_buffer(buf, 2);
1511  }
1512  } else if(d < (1<<7)) {
1513  /* fixnum */
1514  char buf = take8_32(d);
1515  append_buffer(&buf, 1);
1516  } else {
1517  if(d < (1<<8)) {
1518  /* unsigned 8 */
1519  char buf[2] = { static_cast<char>(0xccu), take8_32(d)};
1520  append_buffer(buf, 2);
1521  } else if(d < (1<<16)) {
1522  /* unsigned 16 */
1523  char buf[3];
1524  buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
1525  append_buffer(buf, 3);
1526  } else {
1527  /* unsigned 32 */
1528  char buf[5];
1529  buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
1530  append_buffer(buf, 5);
1531  }
1532  }
1533 }
1534 
1535 template <typename Stream>
1536 template <typename T>
1537 inline void packer<Stream>::pack_imp_int64(T d)
1538 {
1539  if(d < -(1LL<<5)) {
1540  if(d < -(1LL<<15)) {
1541  if(d < -(1LL<<31)) {
1542  /* signed 64 */
1543  char buf[9];
1544  buf[0] = static_cast<char>(0xd3u); _msgpack_store64(&buf[1], d);
1545  append_buffer(buf, 9);
1546  } else {
1547  /* signed 32 */
1548  char buf[5];
1549  buf[0] = static_cast<char>(0xd2u); _msgpack_store32(&buf[1], static_cast<int32_t>(d));
1550  append_buffer(buf, 5);
1551  }
1552  } else {
1553  if(d < -(1<<7)) {
1554  /* signed 16 */
1555  char buf[3];
1556  buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], static_cast<int16_t>(d));
1557  append_buffer(buf, 3);
1558  } else {
1559  /* signed 8 */
1560  char buf[2] = {static_cast<char>(0xd0u), take8_64(d)};
1561  append_buffer(buf, 2);
1562  }
1563  }
1564  } else if(d < (1<<7)) {
1565  /* fixnum */
1566  char buf = take8_64(d);
1567  append_buffer(&buf, 1);
1568  } else {
1569  if(d < (1LL<<16)) {
1570  if(d < (1<<8)) {
1571  /* unsigned 8 */
1572  char buf[2] = {static_cast<char>(0xccu), take8_64(d)};
1573  append_buffer(buf, 2);
1574  } else {
1575  /* unsigned 16 */
1576  char buf[3];
1577  buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
1578  append_buffer(buf, 3);
1579  }
1580  } else {
1581  if(d < (1LL<<32)) {
1582  /* unsigned 32 */
1583  char buf[5];
1584  buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
1585  append_buffer(buf, 5);
1586  } else {
1587  /* unsigned 64 */
1588  char buf[9];
1589  buf[0] = static_cast<char>(0xcfu); _msgpack_store64(&buf[1], d);
1590  append_buffer(buf, 9);
1591  }
1592  }
1593  }
1594 }
1595 
1597 } // MSGPACK_API_VERSION_NAMESPACE(v1)
1599 
1600 } // namespace msgpack
1601 
1602 #endif // MSGPACK_V1_PACK_HPP
packer< Stream > & pack_bin(uint32_t l)
Packing bin header and length.
Definition: pack.hpp:1255
packer< Stream > & pack_fix_uint16(uint16_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:746
packer< Stream > & pack_fix_uint32(uint32_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:755
packer< Stream > & pack_char(char d)
Packing char.
Definition: pack.hpp:809
packer< Stream > & pack_v4raw(uint32_t l)
Packing raw (v4) header and length.
Definition: pack.hpp:1229
packer< Stream > & pack_long_long(long long d)
Packing long long.
Definition: pack.hpp:930
packer< Stream > & pack_ext(size_t l, int8_t type)
Packing ext header, type, and length.
Definition: pack.hpp:1281
packer< Stream > & pack_uint16(uint16_t d)
Packing uint16.
Definition: pack.hpp:709
packer & operator=(const packer &)=delete
packer< Stream > & pack_unsigned_int(unsigned int d)
Packing unsigned int.
Definition: pack.hpp:1004
packer< Stream > & pack_short(short d)
Packing short.
Definition: pack.hpp:831
packer< Stream > & pack_long(long d)
Packing long.
Definition: pack.hpp:897
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1160
packer< Stream > & pack_uint64(uint64_t d)
Packing uint16.
Definition: pack.hpp:717
Definition: adaptor_base.hpp:15
packer< Stream > & pack_int64(int64_t d)
Packing int32.
Definition: pack.hpp:733
packer< Stream > & pack_uint8(uint8_t d)
Packing uint8.
Definition: pack.hpp:705
packer< Stream > & pack_bin_body(const char *b, uint32_t l)
Packing bin body.
Definition: pack.hpp:1274
packer< Stream > & pack_false()
Packing false.
Definition: pack.hpp:1151
packer< Stream > & pack(const T &v)
Packing function template.
packer< Stream > & pack_str_body(const char *b, uint32_t l)
Packing str body.
Definition: pack.hpp:1220
packer< Stream > & pack_int16(int16_t d)
Packing int16.
Definition: pack.hpp:725
packer< Stream > & pack_true()
Packing true.
Definition: pack.hpp:1143
packer< Stream > & pack_uint32(uint32_t d)
Packing uint32.
Definition: pack.hpp:713
packer< Stream > & pack_fix_int32(int32_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:790
packer< Stream > & pack_unsigned_short(unsigned short d)
Packing unsigned short.
Definition: pack.hpp:971
packer< Stream > & pack_map(uint32_t n)
Packing map header and size.
Definition: pack.hpp:1178
packer< Stream > & pack_int32(int32_t d)
Packing int32.
Definition: pack.hpp:729
packer< Stream > & pack_float(float d)
Packing float.
Definition: pack.hpp:1104
packer< Stream > & pack_v4raw_body(const char *b, uint32_t l)
Packing raw (v4) body.
Definition: pack.hpp:1248
packer< Stream > & pack_double(double d)
Packing double.
Definition: pack.hpp:1115
packer< Stream > & pack_ext_body(const char *b, uint32_t l)
Packing ext body.
Definition: pack.hpp:1340
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
packer< Stream > & pack_fix_int16(int16_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:781
packer< Stream > & pack_int8(int8_t d)
Packing int8.
Definition: pack.hpp:721
packer< Stream > & pack_fix_int8(int8_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:773
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
packer< Stream > & pack_fix_uint8(uint8_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:738
packer< Stream > & pack_signed_char(signed char d)
Packing signed char.
Definition: pack.hpp:824
packer< Stream > & pack_fix_uint64(uint64_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:764
packer< Stream > & pack_nil()
Packing nil.
Definition: pack.hpp:1135
packer< Stream > & pack_str(uint32_t l)
Packing str header and length.
Definition: pack.hpp:1197
packer< Stream > & pack_unsigned_long(unsigned long d)
Packing unsigned long.
Definition: pack.hpp:1037
packer< Stream > & pack_unsigned_long_long(unsigned long long d)
Packing unsigned long long.
Definition: pack.hpp:1070
packer< Stream > & pack_int(int d)
Packing int.
Definition: pack.hpp:864
packer< Stream > & pack_fix_int64(int64_t d)
Packing uint8 (fixed packed type).
Definition: pack.hpp:799
packer< Stream > & pack_unsigned_char(unsigned char d)
Packing unsigned char.
Definition: pack.hpp:964