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