int.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2008-2016 FURUHASHI Sadayuki
  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_TYPE_INT_HPP
  11. #define MSGPACK_V1_TYPE_INT_HPP
  12. #include "msgpack/v1/adaptor/int_decl.hpp"
  13. #include "msgpack/object.hpp"
  14. #include <limits>
  15. namespace msgpack {
  16. /// @cond
  17. MSGPACK_API_VERSION_NAMESPACE(v1){
  18. /// @endcond
  19. namespace type {
  20. namespace detail {
  21. template <typename T>
  22. struct convert_integer_sign<T, true> {
  23. static T convert(msgpack::object const& o) {
  24. if(o.type == msgpack::type::POSITIVE_INTEGER) {
  25. if(o.via.u64 > static_cast<uint64_t>(std::numeric_limits<T>::max()))
  26. { throw msgpack::type_error(); }
  27. return static_cast<T>(o.via.u64);
  28. } else if(o.type == msgpack::type::NEGATIVE_INTEGER) {
  29. if(o.via.i64 < static_cast<int64_t>(std::numeric_limits<T>::min()))
  30. { throw msgpack::type_error(); }
  31. return static_cast<T>(o.via.i64);
  32. }
  33. throw msgpack::type_error();
  34. }
  35. };
  36. template <typename T>
  37. struct convert_integer_sign<T, false> {
  38. static T convert(msgpack::object const& o) {
  39. if(o.type == msgpack::type::POSITIVE_INTEGER) {
  40. if(o.via.u64 > static_cast<uint64_t>(std::numeric_limits<T>::max()))
  41. { throw msgpack::type_error(); }
  42. return static_cast<T>(o.via.u64);
  43. }
  44. throw msgpack::type_error();
  45. }
  46. };
  47. template <typename T>
  48. struct is_signed {
  49. static const bool value = std::numeric_limits<T>::is_signed;
  50. };
  51. template <typename T>
  52. inline T convert_integer(msgpack::object const& o)
  53. {
  54. return detail::convert_integer_sign<T, is_signed<T>::value>::convert(o);
  55. }
  56. template <>
  57. struct object_sign<true> {
  58. template <typename T>
  59. static void make(msgpack::object& o, T v) {
  60. if (v < 0) {
  61. o.type = msgpack::type::NEGATIVE_INTEGER;
  62. o.via.i64 = v;
  63. }
  64. else {
  65. o.type = msgpack::type::POSITIVE_INTEGER;
  66. o.via.u64 = static_cast<uint64_t>(v);
  67. }
  68. }
  69. };
  70. template <>
  71. struct object_sign<false> {
  72. template <typename T>
  73. static void make(msgpack::object& o, T v) {
  74. o.type = msgpack::type::POSITIVE_INTEGER;
  75. o.via.u64 = v;
  76. }
  77. };
  78. template <typename T>
  79. inline void object_char(msgpack::object& o, T v) {
  80. return object_sign<is_signed<T>::value>::make(o, v);
  81. }
  82. } // namespace detail
  83. } // namespace type
  84. namespace adaptor {
  85. template <>
  86. struct convert<char> {
  87. msgpack::object const& operator()(msgpack::object const& o, char& v) const
  88. { v = type::detail::convert_integer<char>(o); return o; }
  89. };
  90. template <>
  91. struct convert<wchar_t> {
  92. msgpack::object const& operator()(msgpack::object const& o, wchar_t& v) const
  93. { v = type::detail::convert_integer<wchar_t>(o); return o; }
  94. };
  95. template <>
  96. struct convert<signed char> {
  97. msgpack::object const& operator()(msgpack::object const& o, signed char& v) const
  98. { v = type::detail::convert_integer<signed char>(o); return o; }
  99. };
  100. template <>
  101. struct convert<signed short> {
  102. msgpack::object const& operator()(msgpack::object const& o, signed short& v) const
  103. { v = type::detail::convert_integer<signed short>(o); return o; }
  104. };
  105. template <>
  106. struct convert<signed int> {
  107. msgpack::object const& operator()(msgpack::object const& o, signed int& v) const
  108. { v = type::detail::convert_integer<signed int>(o); return o; }
  109. };
  110. template <>
  111. struct convert<signed long> {
  112. msgpack::object const& operator()(msgpack::object const& o, signed long& v) const
  113. { v = type::detail::convert_integer<signed long>(o); return o; }
  114. };
  115. template <>
  116. struct convert<signed long long> {
  117. msgpack::object const& operator()(msgpack::object const& o, signed long long& v) const
  118. { v = type::detail::convert_integer<signed long long>(o); return o; }
  119. };
  120. template <>
  121. struct convert<unsigned char> {
  122. msgpack::object const& operator()(msgpack::object const& o, unsigned char& v) const
  123. { v = type::detail::convert_integer<unsigned char>(o); return o; }
  124. };
  125. template <>
  126. struct convert<unsigned short> {
  127. msgpack::object const& operator()(msgpack::object const& o, unsigned short& v) const
  128. { v = type::detail::convert_integer<unsigned short>(o); return o; }
  129. };
  130. template <>
  131. struct convert<unsigned int> {
  132. msgpack::object const& operator()(msgpack::object const& o, unsigned int& v) const
  133. { v = type::detail::convert_integer<unsigned int>(o); return o; }
  134. };
  135. template <>
  136. struct convert<unsigned long> {
  137. msgpack::object const& operator()(msgpack::object const& o, unsigned long& v) const
  138. { v = type::detail::convert_integer<unsigned long>(o); return o; }
  139. };
  140. template <>
  141. struct convert<unsigned long long> {
  142. msgpack::object const& operator()(msgpack::object const& o, unsigned long long& v) const
  143. { v = type::detail::convert_integer<unsigned long long>(o); return o; }
  144. };
  145. template <>
  146. struct pack<char> {
  147. template <typename Stream>
  148. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, char v) const
  149. { o.pack_char(v); return o; }
  150. };
  151. template <>
  152. struct pack<wchar_t> {
  153. template <typename Stream>
  154. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, wchar_t v) const
  155. { o.pack_wchar(v); return o; }
  156. };
  157. template <>
  158. struct pack<signed char> {
  159. template <typename Stream>
  160. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed char v) const
  161. { o.pack_signed_char(v); return o; }
  162. };
  163. template <>
  164. struct pack<signed short> {
  165. template <typename Stream>
  166. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed short v) const
  167. { o.pack_short(v); return o; }
  168. };
  169. template <>
  170. struct pack<signed int> {
  171. template <typename Stream>
  172. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed int v) const
  173. { o.pack_int(v); return o; }
  174. };
  175. template <>
  176. struct pack<signed long> {
  177. template <typename Stream>
  178. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed long v) const
  179. { o.pack_long(v); return o; }
  180. };
  181. template <>
  182. struct pack<signed long long> {
  183. template <typename Stream>
  184. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed long long v) const
  185. { o.pack_long_long(v); return o; }
  186. };
  187. template <>
  188. struct pack<unsigned char> {
  189. template <typename Stream>
  190. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned char v) const
  191. { o.pack_unsigned_char(v); return o; }
  192. };
  193. template <>
  194. struct pack<unsigned short> {
  195. template <typename Stream>
  196. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned short v) const
  197. { o.pack_unsigned_short(v); return o; }
  198. };
  199. template <>
  200. struct pack<unsigned int> {
  201. template <typename Stream>
  202. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned int v) const
  203. { o.pack_unsigned_int(v); return o; }
  204. };
  205. template <>
  206. struct pack<unsigned long> {
  207. template <typename Stream>
  208. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned long v) const
  209. { o.pack_unsigned_long(v); return o; }
  210. };
  211. template <>
  212. struct pack<unsigned long long> {
  213. template <typename Stream>
  214. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned long long v) const
  215. { o.pack_unsigned_long_long(v); return o; }
  216. };
  217. template <>
  218. struct object<char> {
  219. void operator()(msgpack::object& o, char v) const
  220. { type::detail::object_char(o, v); }
  221. };
  222. template <>
  223. struct object<wchar_t> {
  224. void operator()(msgpack::object& o, wchar_t v) const
  225. { type::detail::object_char(o, v); }
  226. };
  227. template <>
  228. struct object<signed char> {
  229. void operator()(msgpack::object& o, signed char v) const {
  230. if (v < 0) {
  231. o.type = msgpack::type::NEGATIVE_INTEGER;
  232. o.via.i64 = v;
  233. }
  234. else {
  235. o.type = msgpack::type::POSITIVE_INTEGER;
  236. o.via.u64 = static_cast<uint64_t>(v);
  237. }
  238. }
  239. };
  240. template <>
  241. struct object<signed short> {
  242. void operator()(msgpack::object& o, signed short v) const {
  243. if (v < 0) {
  244. o.type = msgpack::type::NEGATIVE_INTEGER;
  245. o.via.i64 = v;
  246. }
  247. else {
  248. o.type = msgpack::type::POSITIVE_INTEGER;
  249. o.via.u64 = static_cast<uint64_t>(v);
  250. }
  251. }
  252. };
  253. template <>
  254. struct object<signed int> {
  255. void operator()(msgpack::object& o, signed int v) const {
  256. if (v < 0) {
  257. o.type = msgpack::type::NEGATIVE_INTEGER;
  258. o.via.i64 = v;
  259. }
  260. else {
  261. o.type = msgpack::type::POSITIVE_INTEGER;
  262. o.via.u64 = static_cast<uint64_t>(v);
  263. }
  264. }
  265. };
  266. template <>
  267. struct object<signed long> {
  268. void operator()(msgpack::object& o, signed long v) const {
  269. if (v < 0) {
  270. o.type = msgpack::type::NEGATIVE_INTEGER;
  271. o.via.i64 = v;
  272. }
  273. else {
  274. o.type = msgpack::type::POSITIVE_INTEGER;
  275. o.via.u64 = static_cast<uint64_t>(v);
  276. }
  277. }
  278. };
  279. template <>
  280. struct object<signed long long> {
  281. void operator()(msgpack::object& o, signed long long v) const {
  282. if (v < 0) {
  283. o.type = msgpack::type::NEGATIVE_INTEGER;
  284. o.via.i64 = v;
  285. }
  286. else{
  287. o.type = msgpack::type::POSITIVE_INTEGER;
  288. o.via.u64 = static_cast<uint64_t>(v);
  289. }
  290. }
  291. };
  292. template <>
  293. struct object<unsigned char> {
  294. void operator()(msgpack::object& o, unsigned char v) const {
  295. o.type = msgpack::type::POSITIVE_INTEGER;
  296. o.via.u64 = v;
  297. }
  298. };
  299. template <>
  300. struct object<unsigned short> {
  301. void operator()(msgpack::object& o, unsigned short v) const {
  302. o.type = msgpack::type::POSITIVE_INTEGER;
  303. o.via.u64 = v;
  304. }
  305. };
  306. template <>
  307. struct object<unsigned int> {
  308. void operator()(msgpack::object& o, unsigned int v) const {
  309. o.type = msgpack::type::POSITIVE_INTEGER;
  310. o.via.u64 = v;
  311. }
  312. };
  313. template <>
  314. struct object<unsigned long> {
  315. void operator()(msgpack::object& o, unsigned long v) const {
  316. o.type = msgpack::type::POSITIVE_INTEGER;
  317. o.via.u64 = v;
  318. }
  319. };
  320. template <>
  321. struct object<unsigned long long> {
  322. void operator()(msgpack::object& o, unsigned long long v) const {
  323. o.type = msgpack::type::POSITIVE_INTEGER;
  324. o.via.u64 = v;
  325. }
  326. };
  327. template <>
  328. struct object_with_zone<char> {
  329. void operator()(msgpack::object::with_zone& o, char v) const {
  330. static_cast<msgpack::object&>(o) << v;
  331. }
  332. };
  333. template <>
  334. struct object_with_zone<wchar_t> {
  335. void operator()(msgpack::object::with_zone& o, wchar_t v) const {
  336. static_cast<msgpack::object&>(o) << v;
  337. }
  338. };
  339. template <>
  340. struct object_with_zone<signed char> {
  341. void operator()(msgpack::object::with_zone& o, signed char v) const {
  342. static_cast<msgpack::object&>(o) << v;
  343. }
  344. };
  345. template <>
  346. struct object_with_zone<signed short> {
  347. void operator()(msgpack::object::with_zone& o, signed short v) const {
  348. static_cast<msgpack::object&>(o) << v;
  349. }
  350. };
  351. template <>
  352. struct object_with_zone<signed int> {
  353. void operator()(msgpack::object::with_zone& o, signed int v) const {
  354. static_cast<msgpack::object&>(o) << v;
  355. }
  356. };
  357. template <>
  358. struct object_with_zone<signed long> {
  359. void operator()(msgpack::object::with_zone& o, signed long v) const {
  360. static_cast<msgpack::object&>(o) << v;
  361. }
  362. };
  363. template <>
  364. struct object_with_zone<signed long long> {
  365. void operator()(msgpack::object::with_zone& o, const signed long long& v) const {
  366. static_cast<msgpack::object&>(o) << v;
  367. }
  368. };
  369. template <>
  370. struct object_with_zone<unsigned char> {
  371. void operator()(msgpack::object::with_zone& o, unsigned char v) const {
  372. static_cast<msgpack::object&>(o) << v;
  373. }
  374. };
  375. template <>
  376. struct object_with_zone<unsigned short> {
  377. void operator()(msgpack::object::with_zone& o, unsigned short v) const {
  378. static_cast<msgpack::object&>(o) << v;
  379. }
  380. };
  381. template <>
  382. struct object_with_zone<unsigned int> {
  383. void operator()(msgpack::object::with_zone& o, unsigned int v) const {
  384. static_cast<msgpack::object&>(o) << v;
  385. }
  386. };
  387. template <>
  388. struct object_with_zone<unsigned long> {
  389. void operator()(msgpack::object::with_zone& o, unsigned long v) const {
  390. static_cast<msgpack::object&>(o) << v;
  391. }
  392. };
  393. template <>
  394. struct object_with_zone<unsigned long long> {
  395. void operator()(msgpack::object::with_zone& o, const unsigned long long& v) const {
  396. static_cast<msgpack::object&>(o) << v;
  397. }
  398. };
  399. } // namespace adaptor
  400. /// @cond
  401. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  402. /// @endcond
  403. } // namespace msgpack
  404. #endif // MSGPACK_V1_TYPE_INT_HPP