pack.hpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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. #include "msgpack/v1/pack_decl.hpp"
  13. #include <stdexcept>
  14. #include <limits>
  15. #include <cstring>
  16. #include <climits>
  17. #include <ostream>
  18. namespace msgpack {
  19. /// @cond
  20. MSGPACK_API_VERSION_NAMESPACE(v1) {
  21. /// @endcond
  22. /// The class template that supports continuous packing.
  23. /**
  24. * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)`
  25. *
  26. */
  27. template <typename Stream>
  28. class packer {
  29. public:
  30. /// Constructor
  31. /**
  32. * This constructor is left for compatibility.
  33. * Use `packer(Stream& s)` instead of the constructor.
  34. *
  35. * @param s A pointer to packing destination stream object.
  36. */
  37. packer(Stream* s);
  38. /// Constructor
  39. /**
  40. * @param s Packing destination stream object.
  41. */
  42. packer(Stream& s);
  43. public:
  44. /// Packing function template
  45. /**
  46. * @tparam T The type of packing object.
  47. *
  48. * @param v a packing object.
  49. *
  50. * @return The reference of `*this`.
  51. */
  52. template <typename T>
  53. packer<Stream>& pack(const T& v);
  54. /// Packing uint8
  55. /**
  56. * The byte size of the packed data depends on `d`.
  57. * The packed type is positive fixnum or uint8.
  58. * The minimum byte size expression is used.
  59. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  60. *
  61. * @param d a packing object.
  62. *
  63. * @return The reference of `*this`.
  64. */
  65. packer<Stream>& pack_uint8(uint8_t d);
  66. /// Packing uint16
  67. /**
  68. * The byte size of the packed data depends on `d`.
  69. * The packed type is positive fixnum, uint8 or uint16.
  70. * The minimum byte size expression is used.
  71. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  72. *
  73. * @param d a packing object.
  74. *
  75. * @return The reference of `*this`.
  76. */
  77. packer<Stream>& pack_uint16(uint16_t d);
  78. /// Packing uint32
  79. /**
  80. * The byte size of the packed data depends on `d`.
  81. * The packed type is positive fixnum, uint8, uint16 or uint32.
  82. * The minimum byte size expression is used.
  83. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  84. *
  85. * @param d a packing object.
  86. *
  87. * @return The reference of `*this`.
  88. */
  89. packer<Stream>& pack_uint32(uint32_t d);
  90. /// Packing uint16
  91. /**
  92. * The byte size of the packed data depends on `d`.
  93. * The packed type is positive fixnum, uint8, uint16, uint32 or uint64.
  94. * The minimum byte size expression is used.
  95. * positive fixnum, uint8, uint16, or uint32 is used.
  96. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  97. *
  98. * @param d a packing object.
  99. *
  100. * @return The reference of `*this`.
  101. */
  102. packer<Stream>& pack_uint64(uint64_t d);
  103. /// Packing int8
  104. /**
  105. * The byte size of the packed data depends on `d`.
  106. * If `d` is zero or positive, the packed type is positive fixnum, or uint8,
  107. * else the packed type is negative fixnum, or int8
  108. * The minimum byte size expression is used.
  109. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  110. *
  111. * @param d a packing object.
  112. *
  113. * @return The reference of `*this`.
  114. */
  115. packer<Stream>& pack_int8(int8_t d);
  116. /// Packing int16
  117. /**
  118. * The byte size of the packed data depends on `d`.
  119. * If `d` is zero or positive, the packed type is positive fixnum, uint8, or uint16,
  120. * else the packed type is negative fixnum, int8, or int16.
  121. * The minimum byte size expression is used.
  122. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  123. *
  124. * @param d a packing object.
  125. *
  126. * @return The reference of `*this`.
  127. */
  128. packer<Stream>& pack_int16(int16_t d);
  129. /// Packing int32
  130. /**
  131. * The byte size of the packed data depends on `d`.
  132. * If `d` is zero or positive, the packed type is positive fixnum, uint8, uint16, or uint32,
  133. * else the packed type is negative fixnum, int8, int16, or int32.
  134. * The minimum byte size expression is used.
  135. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  136. *
  137. * @param d a packing object.
  138. *
  139. * @return The reference of `*this`.
  140. */
  141. packer<Stream>& pack_int32(int32_t d);
  142. /// Packing int32
  143. /**
  144. * The byte size of the packed data depends on `d`.
  145. * If `d` is zero or positive, the packed type is positive fixnum, uint8, uint16, uint32, or uint64,
  146. * else the packed type is negative fixnum, int8, int16, int32, or int64.
  147. * The minimum byte size expression is used.
  148. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  149. *
  150. * @param d a packing object.
  151. *
  152. * @return The reference of `*this`.
  153. */
  154. packer<Stream>& pack_int64(int64_t d);
  155. /// Packing uint8 (fixed packed type).
  156. /**
  157. * The packed type is always uint8.
  158. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  159. *
  160. * @param d a packing object.
  161. *
  162. * @return The reference of `*this`.
  163. */
  164. packer<Stream>& pack_fix_uint8(uint8_t d);
  165. /// Packing uint8 (fixed packed type).
  166. /**
  167. * The packed type is always uint16.
  168. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  169. *
  170. * @param d a packing object.
  171. *
  172. * @return The reference of `*this`.
  173. */
  174. packer<Stream>& pack_fix_uint16(uint16_t d);
  175. /// Packing uint8 (fixed packed type).
  176. /**
  177. * The packed type is always uint32.
  178. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  179. *
  180. * @param d a packing object.
  181. *
  182. * @return The reference of `*this`.
  183. */
  184. packer<Stream>& pack_fix_uint32(uint32_t d);
  185. /// Packing uint8 (fixed packed type).
  186. /**
  187. * The packed type is always uint64.
  188. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  189. *
  190. * @param d a packing object.
  191. *
  192. * @return The reference of `*this`.
  193. */
  194. packer<Stream>& pack_fix_uint64(uint64_t d);
  195. /// Packing uint8 (fixed packed type).
  196. /**
  197. * The packed type is always int8.
  198. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  199. *
  200. * @param d a packing object.
  201. *
  202. * @return The reference of `*this`.
  203. */
  204. packer<Stream>& pack_fix_int8(int8_t d);
  205. /// Packing uint8 (fixed packed type).
  206. /**
  207. * The packed type is always int16.
  208. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  209. *
  210. * @param d a packing object.
  211. *
  212. * @return The reference of `*this`.
  213. */
  214. packer<Stream>& pack_fix_int16(int16_t d);
  215. /// Packing uint8 (fixed packed type).
  216. /**
  217. * The packed type is always int32.
  218. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  219. *
  220. * @param d a packing object.
  221. *
  222. * @return The reference of `*this`.
  223. */
  224. packer<Stream>& pack_fix_int32(int32_t d);
  225. /// Packing uint8 (fixed packed type).
  226. /**
  227. * The packed type is always int64.
  228. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  229. *
  230. * @param d a packing object.
  231. *
  232. * @return The reference of `*this`.
  233. */
  234. packer<Stream>& pack_fix_int64(int64_t d);
  235. /// Packing char
  236. /**
  237. * The byte size of the packed data depends on `d`.
  238. * If `d` is zero or positive, the packed type is positive fixnum, or uint*,
  239. * else the packed type is negative fixnum, or int*
  240. * The minimum byte size expression is used.
  241. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  242. *
  243. * @param d a packing object.
  244. *
  245. * @return The reference of `*this`.
  246. */
  247. packer<Stream>& pack_char(char d);
  248. /// Packing wchar_t
  249. /**
  250. * The byte size of the packed data depends on `d`.
  251. * If `d` is zero or positive, the packed type is positive fixnum, or uint*,
  252. * else the packed type is negative fixnum, or int*
  253. * The minimum byte size expression is used.
  254. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  255. *
  256. * @param d a packing object.
  257. *
  258. * @return The reference of `*this`.
  259. */
  260. packer<Stream>& pack_wchar(wchar_t d);
  261. /// Packing signed char
  262. /**
  263. * The byte size of the packed data depends on `d`.
  264. * If `d` is zero or positive, the packed type is positive fixnum, or uint*,
  265. * else the packed type is negative fixnum, or int*
  266. * The minimum byte size expression is used.
  267. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  268. *
  269. * @param d a packing object.
  270. *
  271. * @return The reference of `*this`.
  272. */
  273. packer<Stream>& pack_signed_char(signed char d);
  274. /// Packing short
  275. /**
  276. * The byte size of the packed data depends on `d`.
  277. * If `d` is zero or positive, the packed type is positive fixnum, or uint*,
  278. * else the packed type is negative fixnum, or int*
  279. * The minimum byte size expression is used.
  280. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  281. *
  282. * @param d a packing object.
  283. *
  284. * @return The reference of `*this`.
  285. */
  286. packer<Stream>& pack_short(short d);
  287. /// Packing int
  288. /**
  289. * The byte size of the packed data depends on `d`.
  290. * If `d` is zero or positive, the packed type is positive fixnum, or uint*,
  291. * else the packed type is negative fixnum, or int*
  292. * The minimum byte size expression is used.
  293. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  294. *
  295. * @param d a packing object.
  296. *
  297. * @return The reference of `*this`.
  298. */
  299. packer<Stream>& pack_int(int d);
  300. /// Packing long
  301. /**
  302. * The byte size of the packed data depends on `d`.
  303. * If `d` is zero or positive, the packed type is positive fixnum, or uint*,
  304. * else the packed type is negative fixnum, or int*
  305. * The minimum byte size expression is used.
  306. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  307. *
  308. * @param d a packing object.
  309. *
  310. * @return The reference of `*this`.
  311. */
  312. packer<Stream>& pack_long(long d);
  313. /// Packing long long
  314. /**
  315. * The byte size of the packed data depends on `d`.
  316. * If `d` is zero or positive, the packed type is positive fixnum, or uint*,
  317. * else the packed type is negative fixnum, or int*
  318. * The minimum byte size expression is used.
  319. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  320. *
  321. * @param d a packing object.
  322. *
  323. * @return The reference of `*this`.
  324. */
  325. packer<Stream>& pack_long_long(long long d);
  326. /// Packing unsigned char
  327. /**
  328. * The byte size of the packed data depends on `d`.
  329. * The packed type is positive fixnum, or uint*.
  330. * The minimum byte size expression is used.
  331. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  332. *
  333. * @param d a packing object.
  334. *
  335. * @return The reference of `*this`.
  336. */
  337. packer<Stream>& pack_unsigned_char(unsigned char d);
  338. /// Packing unsigned short
  339. /**
  340. * The byte size of the packed data depends on `d`.
  341. * The packed type is positive fixnum, or uint*.
  342. * The minimum byte size expression is used.
  343. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  344. *
  345. * @param d a packing object.
  346. *
  347. * @return The reference of `*this`.
  348. */
  349. packer<Stream>& pack_unsigned_short(unsigned short d);
  350. /// Packing unsigned int
  351. /**
  352. * The byte size of the packed data depends on `d`.
  353. * The packed type is positive fixnum, or uint*.
  354. * The minimum byte size expression is used.
  355. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  356. *
  357. * @param d a packing object.
  358. *
  359. * @return The reference of `*this`.
  360. */
  361. packer<Stream>& pack_unsigned_int(unsigned int d);
  362. /// Packing unsigned long
  363. /**
  364. * The byte size of the packed data depends on `d`.
  365. * The packed type is positive fixnum, or uint*.
  366. * The minimum byte size expression is used.
  367. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  368. *
  369. * @param d a packing object.
  370. *
  371. * @return The reference of `*this`.
  372. */
  373. packer<Stream>& pack_unsigned_long(unsigned long d);
  374. /// Packing unsigned long long
  375. /**
  376. * The byte size of the packed data depends on `d`.
  377. * The packed type is positive fixnum, or uint*.
  378. * The minimum byte size expression is used.
  379. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-int
  380. *
  381. * @param d a packing object.
  382. *
  383. * @return The reference of `*this`.
  384. */
  385. packer<Stream>& pack_unsigned_long_long(unsigned long long d);
  386. /// Packing float
  387. /**
  388. * The packed type is float32.
  389. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float
  390. *
  391. * @param d a packing object.
  392. *
  393. * @return The reference of `*this`.
  394. */
  395. packer<Stream>& pack_float(float d);
  396. /// Packing double
  397. /**
  398. * The packed type is float64.
  399. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float
  400. *
  401. * @param d a packing object.
  402. *
  403. * @return The reference of `*this`.
  404. */
  405. packer<Stream>& pack_double(double d);
  406. /// Packing nil
  407. /**
  408. * The packed type is nil.
  409. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-nil
  410. *
  411. * @return The reference of `*this`.
  412. */
  413. packer<Stream>& pack_nil();
  414. /// Packing true
  415. /**
  416. * The packed type is bool, value is true.
  417. * See https://github.com/msgpack/msgpack/blob/master/spec.md#bool-format-family
  418. *
  419. * @return The reference of `*this`.
  420. */
  421. packer<Stream>& pack_true();
  422. /// Packing false
  423. /**
  424. * The packed type is bool, value is false.
  425. * See https://github.com/msgpack/msgpack/blob/master/spec.md#bool-format-family
  426. *
  427. * @return The reference of `*this`.
  428. */
  429. packer<Stream>& pack_false();
  430. /// Packing array header and size
  431. /**
  432. * The packed type is array header and array size.
  433. * You need to pack `n` msgpack objects following this header and size.
  434. * See https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family
  435. *
  436. * @param n The number of array elements (array size).
  437. *
  438. * @return The reference of `*this`.
  439. */
  440. packer<Stream>& pack_array(uint32_t n);
  441. /// Packing map header and size
  442. /**
  443. * The packed type is map header and map size.
  444. * You need to pack `n` pairs of msgpack objects following this header and size.
  445. * See https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family
  446. *
  447. * @param n The number of array elements (array size).
  448. *
  449. * @return The reference of `*this`.
  450. */
  451. packer<Stream>& pack_map(uint32_t n);
  452. /// Packing str header and length
  453. /**
  454. * The packed type is str header and length.
  455. * The minimum byte size length expression is used.
  456. * You need to call `pack_str_body(const char* b, uint32_t l)` after this function calling with the same `l` value.
  457. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str
  458. *
  459. * @param l The length of string.
  460. *
  461. * @return The reference of `*this`.
  462. */
  463. packer<Stream>& pack_str(uint32_t l);
  464. /// Packing str body
  465. /**
  466. * You need to call this function just after `pack_str(uint32_t l)` calling.
  467. * The value `l` should be the same as `pack_str(uint32_t l)` argument `l`.
  468. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str
  469. *
  470. * @param l The length of string.
  471. *
  472. * @return The reference of `*this`.
  473. */
  474. packer<Stream>& pack_str_body(const char* b, uint32_t l);
  475. /// Packing raw (v4) header and length
  476. /**
  477. * The packed type is raw header and length.
  478. * The minimum byte size length expression is used.
  479. * The format raw (v4) is old MessagePack version4 format.
  480. * You need to call `pack_v4raw_body(const char* b, uint32_t l)` after this function calling with the same `l` value.
  481. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str
  482. *
  483. * @param l The length of string.
  484. *
  485. * @return The reference of `*this`.
  486. */
  487. packer<Stream>& pack_v4raw(uint32_t l);
  488. /// Packing raw (v4) body
  489. /**
  490. * The format raw (v4) is old MessagePack version4 format.
  491. * You need to call this function just after `pack_v4raw(uint32_t l)` calling.
  492. * The value `l` should be the same as `pack_v4raw(uint32_t l)` argument `l`.
  493. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str
  494. *
  495. * @param l The length of string.
  496. *
  497. * @return The reference of `*this`.
  498. */
  499. packer<Stream>& pack_v4raw_body(const char* b, uint32_t l);
  500. /// Packing bin header and length
  501. /**
  502. * The packed type is bin header and length.
  503. * The minimum byte size length expression is used.
  504. * You need to call `pack_bin_body(const char* b, uint32_t l)` after this function calling with the same `l` value.
  505. * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family
  506. *
  507. * @param l The length of string.
  508. *
  509. * @return The reference of `*this`.
  510. */
  511. packer<Stream>& pack_bin(uint32_t l);
  512. /// Packing bin body
  513. /**
  514. * You need to call this function just after `pack_bin(uint32_t l)` calling.
  515. * The value `l` should be the same as `pack_bin(uint32_t l)` argument `l`.
  516. * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family
  517. *
  518. * @param l The length of string.
  519. *
  520. * @return The reference of `*this`.
  521. */
  522. packer<Stream>& pack_bin_body(const char* b, uint32_t l);
  523. /// Packing ext header, type, and length
  524. /**
  525. * The packed type is ext.
  526. * The minimum byte size length expression is used.
  527. * The length 1, 2, 4, 8, and 16 can be encoded in the header.
  528. * You need to call `pack_ext_body(const char* b, uint32_t l)` after this function calling with the same `l` value.
  529. * See https://github.com/msgpack/msgpack/blob/master/spec.md#formats-ext
  530. *
  531. * @param l The length of string.
  532. *
  533. * @return The reference of `*this`.
  534. */
  535. packer<Stream>& pack_ext(size_t l, int8_t type);
  536. /// Packing ext body
  537. /**
  538. * You need to call this function just after `pack_ext(size_t l, int8_t type)` calling.
  539. * The value `l` should be the same as `pack_ext(size_t l, int8_t type)` argument `l`.
  540. * See https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family
  541. *
  542. * @param l The length of string.
  543. *
  544. * @return The reference of `*this`.
  545. */
  546. packer<Stream>& pack_ext_body(const char* b, uint32_t l);
  547. private:
  548. template <typename T>
  549. void pack_imp_uint8(T d);
  550. template <typename T>
  551. void pack_imp_uint16(T d);
  552. template <typename T>
  553. void pack_imp_uint32(T d);
  554. template <typename T>
  555. void pack_imp_uint64(T d);
  556. template <typename T>
  557. void pack_imp_int8(T d);
  558. template <typename T>
  559. void pack_imp_int16(T d);
  560. template <typename T>
  561. void pack_imp_int32(T d);
  562. template <typename T>
  563. void pack_imp_int64(T d);
  564. void append_buffer(const char* buf, size_t len)
  565. {
  566. append_buffer(&Stream::write, buf, len);
  567. }
  568. template <typename Ret, typename Cls, typename SizeType>
  569. void append_buffer(Ret (Cls::*)(const char*, SizeType), const char* buf, size_t len)
  570. {
  571. m_stream.write(buf, static_cast<SizeType>(len));
  572. }
  573. private:
  574. Stream& m_stream;
  575. #if defined(MSGPACK_USE_CPP03)
  576. private:
  577. packer(const packer&);
  578. packer& operator=(const packer&);
  579. packer();
  580. #else // defined(MSGPACK_USE_CPP03)
  581. public:
  582. packer(const packer&) = delete;
  583. packer& operator=(const packer&) = delete;
  584. packer() = delete;
  585. #endif // defined(MSGPACK_USE_CPP03)
  586. };
  587. /// Pack the value as MessagePack format into the stream
  588. /**
  589. * This function template is left for compatibility.
  590. * Use `void pack(Stream& s, const T& v)` instead of the function template.
  591. *
  592. * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)`
  593. * @tparam T Any type that is adapted to MessagePack
  594. * @param s The pointer to packing destination stream
  595. * @param v Packing value
  596. */
  597. template <typename Stream, typename T>
  598. inline void pack(Stream* s, const T& v)
  599. {
  600. packer<Stream>(*s).pack(v);
  601. }
  602. /// Pack the value as MessagePack format into the stream
  603. /**
  604. * @tparam Stream Any type that have a member function `Stream write(const char*, size_t s)`
  605. * @tparam T Any type that is adapted to MessagePack
  606. * @param s Packing destination stream
  607. * @param v Packing value
  608. */
  609. template <typename Stream, typename T>
  610. inline void pack(Stream& s, const T& v)
  611. {
  612. packer<Stream>(s).pack(v);
  613. }
  614. #if MSGPACK_ENDIAN_LITTLE_BYTE
  615. template <typename T>
  616. inline char take8_8(T d) {
  617. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
  618. }
  619. template <typename T>
  620. inline char take8_16(T d) {
  621. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
  622. }
  623. template <typename T>
  624. inline char take8_32(T d) {
  625. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
  626. }
  627. template <typename T>
  628. inline char take8_64(T d) {
  629. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
  630. }
  631. #elif MSGPACK_ENDIAN_BIG_BYTE
  632. template <typename T>
  633. inline char take8_8(T d) {
  634. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[0]);
  635. }
  636. template <typename T>
  637. inline char take8_16(T d) {
  638. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[1]);
  639. }
  640. template <typename T>
  641. inline char take8_32(T d) {
  642. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[3]);
  643. }
  644. template <typename T>
  645. inline char take8_64(T d) {
  646. return static_cast<char>(reinterpret_cast<uint8_t*>(&d)[7]);
  647. }
  648. #else
  649. #error msgpack-c supports only big endian and little endian
  650. #endif
  651. template <typename Stream>
  652. inline packer<Stream>::packer(Stream* s) : m_stream(*s) { }
  653. template <typename Stream>
  654. inline packer<Stream>::packer(Stream& s) : m_stream(s) { }
  655. template <typename Stream>
  656. inline packer<Stream>& packer<Stream>::pack_uint8(uint8_t d)
  657. { pack_imp_uint8(d); return *this; }
  658. template <typename Stream>
  659. inline packer<Stream>& packer<Stream>::pack_uint16(uint16_t d)
  660. { pack_imp_uint16(d); return *this; }
  661. template <typename Stream>
  662. inline packer<Stream>& packer<Stream>::pack_uint32(uint32_t d)
  663. { pack_imp_uint32(d); return *this; }
  664. template <typename Stream>
  665. inline packer<Stream>& packer<Stream>::pack_uint64(uint64_t d)
  666. { pack_imp_uint64(d); return *this; }
  667. template <typename Stream>
  668. inline packer<Stream>& packer<Stream>::pack_int8(int8_t d)
  669. { pack_imp_int8(d); return *this; }
  670. template <typename Stream>
  671. inline packer<Stream>& packer<Stream>::pack_int16(int16_t d)
  672. { pack_imp_int16(d); return *this; }
  673. template <typename Stream>
  674. inline packer<Stream>& packer<Stream>::pack_int32(int32_t d)
  675. { pack_imp_int32(d); return *this; }
  676. template <typename Stream>
  677. inline packer<Stream>& packer<Stream>::pack_int64(int64_t d)
  678. { pack_imp_int64(d); return *this;}
  679. template <typename Stream>
  680. inline packer<Stream>& packer<Stream>::pack_fix_uint8(uint8_t d)
  681. {
  682. char buf[2] = {static_cast<char>(0xccu), take8_8(d)};
  683. append_buffer(buf, 2);
  684. return *this;
  685. }
  686. template <typename Stream>
  687. inline packer<Stream>& packer<Stream>::pack_fix_uint16(uint16_t d)
  688. {
  689. char buf[3];
  690. buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], d);
  691. append_buffer(buf, 3);
  692. return *this;
  693. }
  694. template <typename Stream>
  695. inline packer<Stream>& packer<Stream>::pack_fix_uint32(uint32_t d)
  696. {
  697. char buf[5];
  698. buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], d);
  699. append_buffer(buf, 5);
  700. return *this;
  701. }
  702. template <typename Stream>
  703. inline packer<Stream>& packer<Stream>::pack_fix_uint64(uint64_t d)
  704. {
  705. char buf[9];
  706. buf[0] = static_cast<char>(0xcfu); _msgpack_store64(&buf[1], d);
  707. append_buffer(buf, 9);
  708. return *this;
  709. }
  710. template <typename Stream>
  711. inline packer<Stream>& packer<Stream>::pack_fix_int8(int8_t d)
  712. {
  713. char buf[2] = {static_cast<char>(0xd0u), take8_8(d)};
  714. append_buffer(buf, 2);
  715. return *this;
  716. }
  717. template <typename Stream>
  718. inline packer<Stream>& packer<Stream>::pack_fix_int16(int16_t d)
  719. {
  720. char buf[3];
  721. buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], (uint16_t)d);
  722. append_buffer(buf, 3);
  723. return *this;
  724. }
  725. template <typename Stream>
  726. inline packer<Stream>& packer<Stream>::pack_fix_int32(int32_t d)
  727. {
  728. char buf[5];
  729. buf[0] = static_cast<char>(0xd2u); _msgpack_store32(&buf[1], (uint32_t)d);
  730. append_buffer(buf, 5);
  731. return *this;
  732. }
  733. template <typename Stream>
  734. inline packer<Stream>& packer<Stream>::pack_fix_int64(int64_t d)
  735. {
  736. char buf[9];
  737. buf[0] = static_cast<char>(0xd3u); _msgpack_store64(&buf[1], d);
  738. append_buffer(buf, 9);
  739. return *this;
  740. }
  741. template <typename Stream>
  742. inline packer<Stream>& packer<Stream>::pack_char(char d)
  743. {
  744. #if defined(CHAR_MIN)
  745. #if CHAR_MIN < 0
  746. pack_imp_int8(d);
  747. #else
  748. pack_imp_uint8(d);
  749. #endif
  750. #else
  751. #error CHAR_MIN is not defined
  752. #endif
  753. return *this;
  754. }
  755. template <typename Stream>
  756. inline packer<Stream>& packer<Stream>::pack_wchar(wchar_t d)
  757. {
  758. if (d < 0) {
  759. pack_imp_int64(static_cast<int64_t>(d));
  760. }
  761. else {
  762. pack_imp_uint64(static_cast<uint64_t>(d));
  763. }
  764. return *this;
  765. }
  766. template <typename Stream>
  767. inline packer<Stream>& packer<Stream>::pack_signed_char(signed char d)
  768. {
  769. pack_imp_int8(d);
  770. return *this;
  771. }
  772. template <typename Stream>
  773. inline packer<Stream>& packer<Stream>::pack_short(short d)
  774. {
  775. #if defined(SIZEOF_SHORT)
  776. #if SIZEOF_SHORT == 2
  777. pack_imp_int16(d);
  778. #elif SIZEOF_SHORT == 4
  779. pack_imp_int32(d);
  780. #else
  781. pack_imp_int64(d);
  782. #endif
  783. #elif defined(SHRT_MAX)
  784. #if SHRT_MAX == 0x7fff
  785. pack_imp_int16(d);
  786. #elif SHRT_MAX == 0x7fffffff
  787. pack_imp_int32(d);
  788. #else
  789. pack_imp_int64(d);
  790. #endif
  791. #else
  792. if(sizeof(short) == 2) {
  793. pack_imp_int16(d);
  794. } else if(sizeof(short) == 4) {
  795. pack_imp_int32(d);
  796. } else {
  797. pack_imp_int64(d);
  798. }
  799. #endif
  800. return *this;
  801. }
  802. template <typename Stream>
  803. inline packer<Stream>& packer<Stream>::pack_int(int d)
  804. {
  805. #if defined(SIZEOF_INT)
  806. #if SIZEOF_INT == 2
  807. pack_imp_int16(d);
  808. #elif SIZEOF_INT == 4
  809. pack_imp_int32(d);
  810. #else
  811. pack_imp_int64(d);
  812. #endif
  813. #elif defined(INT_MAX)
  814. #if INT_MAX == 0x7fff
  815. pack_imp_int16(d);
  816. #elif INT_MAX == 0x7fffffff
  817. pack_imp_int32(d);
  818. #else
  819. pack_imp_int64(d);
  820. #endif
  821. #else
  822. if(sizeof(int) == 2) {
  823. pack_imp_int16(d);
  824. } else if(sizeof(int) == 4) {
  825. pack_imp_int32(d);
  826. } else {
  827. pack_imp_int64(d);
  828. }
  829. #endif
  830. return *this;
  831. }
  832. template <typename Stream>
  833. inline packer<Stream>& packer<Stream>::pack_long(long d)
  834. {
  835. #if defined(SIZEOF_LONG)
  836. #if SIZEOF_LONG == 2
  837. pack_imp_int16(d);
  838. #elif SIZEOF_LONG == 4
  839. pack_imp_int32(d);
  840. #else
  841. pack_imp_int64(d);
  842. #endif
  843. #elif defined(LONG_MAX)
  844. #if LONG_MAX == 0x7fffL
  845. pack_imp_int16(d);
  846. #elif LONG_MAX == 0x7fffffffL
  847. pack_imp_int32(d);
  848. #else
  849. pack_imp_int64(d);
  850. #endif
  851. #else
  852. if(sizeof(long) == 2) {
  853. pack_imp_int16(d);
  854. } else if(sizeof(long) == 4) {
  855. pack_imp_int32(d);
  856. } else {
  857. pack_imp_int64(d);
  858. }
  859. #endif
  860. return *this;
  861. }
  862. template <typename Stream>
  863. inline packer<Stream>& packer<Stream>::pack_long_long(long long d)
  864. {
  865. #if defined(SIZEOF_LONG_LONG)
  866. #if SIZEOF_LONG_LONG == 2
  867. pack_imp_int16(d);
  868. #elif SIZEOF_LONG_LONG == 4
  869. pack_imp_int32(d);
  870. #else
  871. pack_imp_int64(d);
  872. #endif
  873. #elif defined(LLONG_MAX)
  874. #if LLONG_MAX == 0x7fffL
  875. pack_imp_int16(d);
  876. #elif LLONG_MAX == 0x7fffffffL
  877. pack_imp_int32(d);
  878. #else
  879. pack_imp_int64(d);
  880. #endif
  881. #else
  882. if(sizeof(long long) == 2) {
  883. pack_imp_int16(d);
  884. } else if(sizeof(long long) == 4) {
  885. pack_imp_int32(d);
  886. } else {
  887. pack_imp_int64(d);
  888. }
  889. #endif
  890. return *this;
  891. }
  892. template <typename Stream>
  893. inline packer<Stream>& packer<Stream>::pack_unsigned_char(unsigned char d)
  894. {
  895. pack_imp_uint8(d);
  896. return *this;
  897. }
  898. template <typename Stream>
  899. inline packer<Stream>& packer<Stream>::pack_unsigned_short(unsigned short d)
  900. {
  901. #if defined(SIZEOF_SHORT)
  902. #if SIZEOF_SHORT == 2
  903. pack_imp_uint16(d);
  904. #elif SIZEOF_SHORT == 4
  905. pack_imp_uint32(d);
  906. #else
  907. pack_imp_uint64(d);
  908. #endif
  909. #elif defined(USHRT_MAX)
  910. #if USHRT_MAX == 0xffffU
  911. pack_imp_uint16(d);
  912. #elif USHRT_MAX == 0xffffffffU
  913. pack_imp_uint32(d);
  914. #else
  915. pack_imp_uint64(d);
  916. #endif
  917. #else
  918. if(sizeof(unsigned short) == 2) {
  919. pack_imp_uint16(d);
  920. } else if(sizeof(unsigned short) == 4) {
  921. pack_imp_uint32(d);
  922. } else {
  923. pack_imp_uint64(d);
  924. }
  925. #endif
  926. return *this;
  927. }
  928. template <typename Stream>
  929. inline packer<Stream>& packer<Stream>::pack_unsigned_int(unsigned int d)
  930. {
  931. #if defined(SIZEOF_INT)
  932. #if SIZEOF_INT == 2
  933. pack_imp_uint16(d);
  934. #elif SIZEOF_INT == 4
  935. pack_imp_uint32(d);
  936. #else
  937. pack_imp_uint64(d);
  938. #endif
  939. #elif defined(UINT_MAX)
  940. #if UINT_MAX == 0xffffU
  941. pack_imp_uint16(d);
  942. #elif UINT_MAX == 0xffffffffU
  943. pack_imp_uint32(d);
  944. #else
  945. pack_imp_uint64(d);
  946. #endif
  947. #else
  948. if(sizeof(unsigned int) == 2) {
  949. pack_imp_uint16(d);
  950. } else if(sizeof(unsigned int) == 4) {
  951. pack_imp_uint32(d);
  952. } else {
  953. pack_imp_uint64(d);
  954. }
  955. #endif
  956. return *this;
  957. }
  958. template <typename Stream>
  959. inline packer<Stream>& packer<Stream>::pack_unsigned_long(unsigned long d)
  960. {
  961. #if defined(SIZEOF_LONG)
  962. #if SIZEOF_LONG == 2
  963. pack_imp_uint16(d);
  964. #elif SIZEOF_LONG == 4
  965. pack_imp_uint32(d);
  966. #else
  967. pack_imp_uint64(d);
  968. #endif
  969. #elif defined(ULONG_MAX)
  970. #if ULONG_MAX == 0xffffUL
  971. pack_imp_uint16(d);
  972. #elif ULONG_MAX == 0xffffffffUL
  973. pack_imp_uint32(d);
  974. #else
  975. pack_imp_uint64(d);
  976. #endif
  977. #else
  978. if(sizeof(unsigned long) == 2) {
  979. pack_imp_uint16(d);
  980. } else if(sizeof(unsigned long) == 4) {
  981. pack_imp_uint32(d);
  982. } else {
  983. pack_imp_uint64(d);
  984. }
  985. #endif
  986. return *this;
  987. }
  988. template <typename Stream>
  989. inline packer<Stream>& packer<Stream>::pack_unsigned_long_long(unsigned long long d)
  990. {
  991. #if defined(SIZEOF_LONG_LONG)
  992. #if SIZEOF_LONG_LONG == 2
  993. pack_imp_uint16(d);
  994. #elif SIZEOF_LONG_LONG == 4
  995. pack_imp_uint32(d);
  996. #else
  997. pack_imp_uint64(d);
  998. #endif
  999. #elif defined(ULLONG_MAX)
  1000. #if ULLONG_MAX == 0xffffUL
  1001. pack_imp_uint16(d);
  1002. #elif ULLONG_MAX == 0xffffffffUL
  1003. pack_imp_uint32(d);
  1004. #else
  1005. pack_imp_uint64(d);
  1006. #endif
  1007. #else
  1008. if(sizeof(unsigned long long) == 2) {
  1009. pack_imp_uint16(d);
  1010. } else if(sizeof(unsigned long long) == 4) {
  1011. pack_imp_uint32(d);
  1012. } else {
  1013. pack_imp_uint64(d);
  1014. }
  1015. #endif
  1016. return *this;
  1017. }
  1018. template <typename Stream>
  1019. inline packer<Stream>& packer<Stream>::pack_float(float d)
  1020. {
  1021. union { float f; uint32_t i; } mem;
  1022. mem.f = d;
  1023. char buf[5];
  1024. buf[0] = static_cast<char>(0xcau); _msgpack_store32(&buf[1], mem.i);
  1025. append_buffer(buf, 5);
  1026. return *this;
  1027. }
  1028. template <typename Stream>
  1029. inline packer<Stream>& packer<Stream>::pack_double(double d)
  1030. {
  1031. union { double f; uint64_t i; } mem;
  1032. mem.f = d;
  1033. char buf[9];
  1034. buf[0] = static_cast<char>(0xcbu);
  1035. #if defined(TARGET_OS_IPHONE)
  1036. // ok
  1037. #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi
  1038. // https://github.com/msgpack/msgpack-perl/pull/1
  1039. mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
  1040. #endif
  1041. _msgpack_store64(&buf[1], mem.i);
  1042. append_buffer(buf, 9);
  1043. return *this;
  1044. }
  1045. template <typename Stream>
  1046. inline packer<Stream>& packer<Stream>::pack_nil()
  1047. {
  1048. const char d = static_cast<char>(0xc0u);
  1049. append_buffer(&d, 1);
  1050. return *this;
  1051. }
  1052. template <typename Stream>
  1053. inline packer<Stream>& packer<Stream>::pack_true()
  1054. {
  1055. const char d = static_cast<char>(0xc3u);
  1056. append_buffer(&d, 1);
  1057. return *this;
  1058. }
  1059. template <typename Stream>
  1060. inline packer<Stream>& packer<Stream>::pack_false()
  1061. {
  1062. const char d = static_cast<char>(0xc2u);
  1063. append_buffer(&d, 1);
  1064. return *this;
  1065. }
  1066. template <typename Stream>
  1067. inline packer<Stream>& packer<Stream>::pack_array(uint32_t n)
  1068. {
  1069. if(n < 16) {
  1070. char d = static_cast<char>(0x90u | n);
  1071. append_buffer(&d, 1);
  1072. } else if(n < 65536) {
  1073. char buf[3];
  1074. buf[0] = static_cast<char>(0xdcu); _msgpack_store16(&buf[1], static_cast<uint16_t>(n));
  1075. append_buffer(buf, 3);
  1076. } else {
  1077. char buf[5];
  1078. buf[0] = static_cast<char>(0xddu); _msgpack_store32(&buf[1], static_cast<uint32_t>(n));
  1079. append_buffer(buf, 5);
  1080. }
  1081. return *this;
  1082. }
  1083. template <typename Stream>
  1084. inline packer<Stream>& packer<Stream>::pack_map(uint32_t n)
  1085. {
  1086. if(n < 16) {
  1087. unsigned char d = static_cast<unsigned char>(0x80u | n);
  1088. char buf = take8_8(d);
  1089. append_buffer(&buf, 1);
  1090. } else if(n < 65536) {
  1091. char buf[3];
  1092. buf[0] = static_cast<char>(0xdeu); _msgpack_store16(&buf[1], static_cast<uint16_t>(n));
  1093. append_buffer(buf, 3);
  1094. } else {
  1095. char buf[5];
  1096. buf[0] = static_cast<char>(0xdfu); _msgpack_store32(&buf[1], static_cast<uint32_t>(n));
  1097. append_buffer(buf, 5);
  1098. }
  1099. return *this;
  1100. }
  1101. template <typename Stream>
  1102. inline packer<Stream>& packer<Stream>::pack_str(uint32_t l)
  1103. {
  1104. if(l < 32) {
  1105. unsigned char d = static_cast<uint8_t>(0xa0u | l);
  1106. char buf = take8_8(d);
  1107. append_buffer(&buf, 1);
  1108. } else if(l < 256) {
  1109. char buf[2];
  1110. buf[0] = static_cast<char>(0xd9u); buf[1] = static_cast<char>(l);
  1111. append_buffer(buf, 2);
  1112. } else if(l < 65536) {
  1113. char buf[3];
  1114. buf[0] = static_cast<char>(0xdau); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
  1115. append_buffer(buf, 3);
  1116. } else {
  1117. char buf[5];
  1118. buf[0] = static_cast<char>(0xdbu); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
  1119. append_buffer(buf, 5);
  1120. }
  1121. return *this;
  1122. }
  1123. template <typename Stream>
  1124. inline packer<Stream>& packer<Stream>::pack_str_body(const char* b, uint32_t l)
  1125. {
  1126. append_buffer(b, l);
  1127. return *this;
  1128. }
  1129. // Raw (V4)
  1130. template <typename Stream>
  1131. inline packer<Stream>& packer<Stream>::pack_v4raw(uint32_t l)
  1132. {
  1133. if(l < 32) {
  1134. unsigned char d = static_cast<uint8_t>(0xa0u | l);
  1135. char buf = take8_8(d);
  1136. append_buffer(&buf, 1);
  1137. } else if(l < 65536) {
  1138. char buf[3];
  1139. buf[0] = static_cast<char>(0xdau); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
  1140. append_buffer(buf, 3);
  1141. } else {
  1142. char buf[5];
  1143. buf[0] = static_cast<char>(0xdbu); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
  1144. append_buffer(buf, 5);
  1145. }
  1146. return *this;
  1147. }
  1148. template <typename Stream>
  1149. inline packer<Stream>& packer<Stream>::pack_v4raw_body(const char* b, uint32_t l)
  1150. {
  1151. append_buffer(b, l);
  1152. return *this;
  1153. }
  1154. template <typename Stream>
  1155. inline packer<Stream>& packer<Stream>::pack_bin(uint32_t l)
  1156. {
  1157. if(l < 256) {
  1158. char buf[2];
  1159. buf[0] = static_cast<char>(0xc4u); buf[1] = static_cast<char>(l);
  1160. append_buffer(buf, 2);
  1161. } else if(l < 65536) {
  1162. char buf[3];
  1163. buf[0] = static_cast<char>(0xc5u); _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
  1164. append_buffer(buf, 3);
  1165. } else {
  1166. char buf[5];
  1167. buf[0] = static_cast<char>(0xc6u); _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
  1168. append_buffer(buf, 5);
  1169. }
  1170. return *this;
  1171. }
  1172. template <typename Stream>
  1173. inline packer<Stream>& packer<Stream>::pack_bin_body(const char* b, uint32_t l)
  1174. {
  1175. append_buffer(b, l);
  1176. return *this;
  1177. }
  1178. template <typename Stream>
  1179. inline packer<Stream>& packer<Stream>::pack_ext(size_t l, int8_t type)
  1180. {
  1181. switch(l) {
  1182. case 1: {
  1183. char buf[2];
  1184. buf[0] = static_cast<char>(0xd4u);
  1185. buf[1] = static_cast<char>(type);
  1186. append_buffer(buf, 2);
  1187. } break;
  1188. case 2: {
  1189. char buf[2];
  1190. buf[0] = static_cast<char>(0xd5u);
  1191. buf[1] = static_cast<char>(type);
  1192. append_buffer(buf, 2);
  1193. } break;
  1194. case 4: {
  1195. char buf[2];
  1196. buf[0] = static_cast<char>(0xd6u);
  1197. buf[1] = static_cast<char>(type);
  1198. append_buffer(buf, 2);
  1199. } break;
  1200. case 8: {
  1201. char buf[2];
  1202. buf[0] = static_cast<char>(0xd7u);
  1203. buf[1] = static_cast<char>(type);
  1204. append_buffer(buf, 2);
  1205. } break;
  1206. case 16: {
  1207. char buf[2];
  1208. buf[0] = static_cast<char>(0xd8u);
  1209. buf[1] = static_cast<char>(type);
  1210. append_buffer(buf, 2);
  1211. } break;
  1212. default:
  1213. if(l < 256) {
  1214. char buf[3];
  1215. buf[0] = static_cast<char>(0xc7u);
  1216. buf[1] = static_cast<char>(l);
  1217. buf[2] = static_cast<char>(type);
  1218. append_buffer(buf, 3);
  1219. } else if(l < 65536) {
  1220. char buf[4];
  1221. buf[0] = static_cast<char>(0xc8u);
  1222. _msgpack_store16(&buf[1], static_cast<uint16_t>(l));
  1223. buf[3] = static_cast<char>(type);
  1224. append_buffer(buf, 4);
  1225. } else {
  1226. char buf[6];
  1227. buf[0] = static_cast<char>(0xc9u);
  1228. _msgpack_store32(&buf[1], static_cast<uint32_t>(l));
  1229. buf[5] = static_cast<char>(type);
  1230. append_buffer(buf, 6);
  1231. }
  1232. break;
  1233. }
  1234. return *this;
  1235. }
  1236. template <typename Stream>
  1237. inline packer<Stream>& packer<Stream>::pack_ext_body(const char* b, uint32_t l)
  1238. {
  1239. append_buffer(b, l);
  1240. return *this;
  1241. }
  1242. template <typename Stream>
  1243. template <typename T>
  1244. inline void packer<Stream>::pack_imp_uint8(T d)
  1245. {
  1246. if(d < (1<<7)) {
  1247. /* fixnum */
  1248. char buf = take8_8(d);
  1249. append_buffer(&buf, 1);
  1250. } else {
  1251. /* unsigned 8 */
  1252. char buf[2] = {static_cast<char>(0xccu), take8_8(d)};
  1253. append_buffer(buf, 2);
  1254. }
  1255. }
  1256. template <typename Stream>
  1257. template <typename T>
  1258. inline void packer<Stream>::pack_imp_uint16(T d)
  1259. {
  1260. if(d < (1<<7)) {
  1261. /* fixnum */
  1262. char buf = take8_16(d);
  1263. append_buffer(&buf, 1);
  1264. } else if(d < (1<<8)) {
  1265. /* unsigned 8 */
  1266. char buf[2] = {static_cast<char>(0xccu), take8_16(d)};
  1267. append_buffer(buf, 2);
  1268. } else {
  1269. /* unsigned 16 */
  1270. char buf[3];
  1271. buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
  1272. append_buffer(buf, 3);
  1273. }
  1274. }
  1275. template <typename Stream>
  1276. template <typename T>
  1277. inline void packer<Stream>::pack_imp_uint32(T d)
  1278. {
  1279. if(d < (1<<8)) {
  1280. if(d < (1<<7)) {
  1281. /* fixnum */
  1282. char buf = take8_32(d);
  1283. append_buffer(&buf, 1);
  1284. } else {
  1285. /* unsigned 8 */
  1286. char buf[2] = {static_cast<char>(0xccu), take8_32(d)};
  1287. append_buffer(buf, 2);
  1288. }
  1289. } else {
  1290. if(d < (1<<16)) {
  1291. /* unsigned 16 */
  1292. char buf[3];
  1293. buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
  1294. append_buffer(buf, 3);
  1295. } else {
  1296. /* unsigned 32 */
  1297. char buf[5];
  1298. buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
  1299. append_buffer(buf, 5);
  1300. }
  1301. }
  1302. }
  1303. template <typename Stream>
  1304. template <typename T>
  1305. inline void packer<Stream>::pack_imp_uint64(T d)
  1306. {
  1307. if(d < (1ULL<<8)) {
  1308. if(d < (1ULL<<7)) {
  1309. /* fixnum */
  1310. char buf = take8_64(d);
  1311. append_buffer(&buf, 1);
  1312. } else {
  1313. /* unsigned 8 */
  1314. char buf[2] = {static_cast<char>(0xccu), take8_64(d)};
  1315. append_buffer(buf, 2);
  1316. }
  1317. } else {
  1318. if(d < (1ULL<<16)) {
  1319. /* unsigned 16 */
  1320. char buf[3];
  1321. buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
  1322. append_buffer(buf, 3);
  1323. } else if(d < (1ULL<<32)) {
  1324. /* unsigned 32 */
  1325. char buf[5];
  1326. buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
  1327. append_buffer(buf, 5);
  1328. } else {
  1329. /* unsigned 64 */
  1330. char buf[9];
  1331. buf[0] = static_cast<char>(0xcfu); _msgpack_store64(&buf[1], d);
  1332. append_buffer(buf, 9);
  1333. }
  1334. }
  1335. }
  1336. template <typename Stream>
  1337. template <typename T>
  1338. inline void packer<Stream>::pack_imp_int8(T d)
  1339. {
  1340. if(d < -(1<<5)) {
  1341. /* signed 8 */
  1342. char buf[2] = {static_cast<char>(0xd0u), take8_8(d)};
  1343. append_buffer(buf, 2);
  1344. } else {
  1345. /* fixnum */
  1346. char buf = take8_8(d);
  1347. append_buffer(&buf, 1);
  1348. }
  1349. }
  1350. template <typename Stream>
  1351. template <typename T>
  1352. inline void packer<Stream>::pack_imp_int16(T d)
  1353. {
  1354. if(d < -(1<<5)) {
  1355. if(d < -(1<<7)) {
  1356. /* signed 16 */
  1357. char buf[3];
  1358. buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], static_cast<int16_t>(d));
  1359. append_buffer(buf, 3);
  1360. } else {
  1361. /* signed 8 */
  1362. char buf[2] = {static_cast<char>(0xd0u), take8_16(d)};
  1363. append_buffer(buf, 2);
  1364. }
  1365. } else if(d < (1<<7)) {
  1366. /* fixnum */
  1367. char buf = take8_16(d);
  1368. append_buffer(&buf, 1);
  1369. } else {
  1370. if(d < (1<<8)) {
  1371. /* unsigned 8 */
  1372. char buf[2] = {static_cast<char>(0xccu), take8_16(d)};
  1373. append_buffer(buf, 2);
  1374. } else {
  1375. /* unsigned 16 */
  1376. char buf[3];
  1377. buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
  1378. append_buffer(buf, 3);
  1379. }
  1380. }
  1381. }
  1382. template <typename Stream>
  1383. template <typename T>
  1384. inline void packer<Stream>::pack_imp_int32(T d)
  1385. {
  1386. if(d < -(1<<5)) {
  1387. if(d < -(1<<15)) {
  1388. /* signed 32 */
  1389. char buf[5];
  1390. buf[0] = static_cast<char>(0xd2u); _msgpack_store32(&buf[1], static_cast<int32_t>(d));
  1391. append_buffer(buf, 5);
  1392. } else if(d < -(1<<7)) {
  1393. /* signed 16 */
  1394. char buf[3];
  1395. buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], static_cast<int16_t>(d));
  1396. append_buffer(buf, 3);
  1397. } else {
  1398. /* signed 8 */
  1399. char buf[2] = { static_cast<char>(0xd0u), take8_32(d)};
  1400. append_buffer(buf, 2);
  1401. }
  1402. } else if(d < (1<<7)) {
  1403. /* fixnum */
  1404. char buf = take8_32(d);
  1405. append_buffer(&buf, 1);
  1406. } else {
  1407. if(d < (1<<8)) {
  1408. /* unsigned 8 */
  1409. char buf[2] = { static_cast<char>(0xccu), take8_32(d)};
  1410. append_buffer(buf, 2);
  1411. } else if(d < (1<<16)) {
  1412. /* unsigned 16 */
  1413. char buf[3];
  1414. buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
  1415. append_buffer(buf, 3);
  1416. } else {
  1417. /* unsigned 32 */
  1418. char buf[5];
  1419. buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
  1420. append_buffer(buf, 5);
  1421. }
  1422. }
  1423. }
  1424. template <typename Stream>
  1425. template <typename T>
  1426. inline void packer<Stream>::pack_imp_int64(T d)
  1427. {
  1428. if(d < -(1LL<<5)) {
  1429. if(d < -(1LL<<15)) {
  1430. if(d < -(1LL<<31)) {
  1431. /* signed 64 */
  1432. char buf[9];
  1433. buf[0] = static_cast<char>(0xd3u); _msgpack_store64(&buf[1], d);
  1434. append_buffer(buf, 9);
  1435. } else {
  1436. /* signed 32 */
  1437. char buf[5];
  1438. buf[0] = static_cast<char>(0xd2u); _msgpack_store32(&buf[1], static_cast<int32_t>(d));
  1439. append_buffer(buf, 5);
  1440. }
  1441. } else {
  1442. if(d < -(1<<7)) {
  1443. /* signed 16 */
  1444. char buf[3];
  1445. buf[0] = static_cast<char>(0xd1u); _msgpack_store16(&buf[1], static_cast<int16_t>(d));
  1446. append_buffer(buf, 3);
  1447. } else {
  1448. /* signed 8 */
  1449. char buf[2] = {static_cast<char>(0xd0u), take8_64(d)};
  1450. append_buffer(buf, 2);
  1451. }
  1452. }
  1453. } else if(d < (1<<7)) {
  1454. /* fixnum */
  1455. char buf = take8_64(d);
  1456. append_buffer(&buf, 1);
  1457. } else {
  1458. if(d < (1LL<<16)) {
  1459. if(d < (1<<8)) {
  1460. /* unsigned 8 */
  1461. char buf[2] = {static_cast<char>(0xccu), take8_64(d)};
  1462. append_buffer(buf, 2);
  1463. } else {
  1464. /* unsigned 16 */
  1465. char buf[3];
  1466. buf[0] = static_cast<char>(0xcdu); _msgpack_store16(&buf[1], static_cast<uint16_t>(d));
  1467. append_buffer(buf, 3);
  1468. }
  1469. } else {
  1470. if(d < (1LL<<32)) {
  1471. /* unsigned 32 */
  1472. char buf[5];
  1473. buf[0] = static_cast<char>(0xceu); _msgpack_store32(&buf[1], static_cast<uint32_t>(d));
  1474. append_buffer(buf, 5);
  1475. } else {
  1476. /* unsigned 64 */
  1477. char buf[9];
  1478. buf[0] = static_cast<char>(0xcfu); _msgpack_store64(&buf[1], d);
  1479. append_buffer(buf, 9);
  1480. }
  1481. }
  1482. }
  1483. }
  1484. /// @cond
  1485. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  1486. /// @endcond
  1487. } // namespace msgpack
  1488. #endif // MSGPACK_V1_PACK_HPP