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