int.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_char_sign<true> {
  58. template <typename T>
  59. static typename msgpack::enable_if<msgpack::is_same<T, char>::value>::type
  60. make(msgpack::object& o, T v) {
  61. if (v < 0) {
  62. o.type = msgpack::type::NEGATIVE_INTEGER;
  63. o.via.i64 = v;
  64. }
  65. else {
  66. o.type = msgpack::type::POSITIVE_INTEGER;
  67. o.via.u64 = v;
  68. }
  69. }
  70. };
  71. template <>
  72. struct object_char_sign<false> {
  73. static void make(msgpack::object& o, char v) {
  74. o.type = msgpack::type::POSITIVE_INTEGER;
  75. o.via.u64 = v;
  76. }
  77. };
  78. inline void object_char(msgpack::object& o, char v) {
  79. return object_char_sign<is_signed<char>::value>::make(o, v);
  80. }
  81. } // namespace detail
  82. } // namespace type
  83. namespace adaptor {
  84. template <>
  85. struct convert<char> {
  86. msgpack::object const& operator()(msgpack::object const& o, char& v) const
  87. { v = type::detail::convert_integer<char>(o); return o; }
  88. };
  89. template <>
  90. struct convert<signed char> {
  91. msgpack::object const& operator()(msgpack::object const& o, signed char& v) const
  92. { v = type::detail::convert_integer<signed char>(o); return o; }
  93. };
  94. template <>
  95. struct convert<signed short> {
  96. msgpack::object const& operator()(msgpack::object const& o, signed short& v) const
  97. { v = type::detail::convert_integer<signed short>(o); return o; }
  98. };
  99. template <>
  100. struct convert<signed int> {
  101. msgpack::object const& operator()(msgpack::object const& o, signed int& v) const
  102. { v = type::detail::convert_integer<signed int>(o); return o; }
  103. };
  104. template <>
  105. struct convert<signed long> {
  106. msgpack::object const& operator()(msgpack::object const& o, signed long& v) const
  107. { v = type::detail::convert_integer<signed long>(o); return o; }
  108. };
  109. template <>
  110. struct convert<signed long long> {
  111. msgpack::object const& operator()(msgpack::object const& o, signed long long& v) const
  112. { v = type::detail::convert_integer<signed long long>(o); return o; }
  113. };
  114. template <>
  115. struct convert<unsigned char> {
  116. msgpack::object const& operator()(msgpack::object const& o, unsigned char& v) const
  117. { v = type::detail::convert_integer<unsigned char>(o); return o; }
  118. };
  119. template <>
  120. struct convert<unsigned short> {
  121. msgpack::object const& operator()(msgpack::object const& o, unsigned short& v) const
  122. { v = type::detail::convert_integer<unsigned short>(o); return o; }
  123. };
  124. template <>
  125. struct convert<unsigned int> {
  126. msgpack::object const& operator()(msgpack::object const& o, unsigned int& v) const
  127. { v = type::detail::convert_integer<unsigned int>(o); return o; }
  128. };
  129. template <>
  130. struct convert<unsigned long> {
  131. msgpack::object const& operator()(msgpack::object const& o, unsigned long& v) const
  132. { v = type::detail::convert_integer<unsigned long>(o); return o; }
  133. };
  134. template <>
  135. struct convert<unsigned long long> {
  136. msgpack::object const& operator()(msgpack::object const& o, unsigned long long& v) const
  137. { v = type::detail::convert_integer<unsigned long long>(o); return o; }
  138. };
  139. template <>
  140. struct pack<char> {
  141. template <typename Stream>
  142. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, char v) const
  143. { o.pack_char(v); return o; }
  144. };
  145. template <>
  146. struct pack<signed char> {
  147. template <typename Stream>
  148. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed char v) const
  149. { o.pack_signed_char(v); return o; }
  150. };
  151. template <>
  152. struct pack<signed short> {
  153. template <typename Stream>
  154. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed short v) const
  155. { o.pack_short(v); return o; }
  156. };
  157. template <>
  158. struct pack<signed int> {
  159. template <typename Stream>
  160. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed int v) const
  161. { o.pack_int(v); return o; }
  162. };
  163. template <>
  164. struct pack<signed long> {
  165. template <typename Stream>
  166. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed long v) const
  167. { o.pack_long(v); return o; }
  168. };
  169. template <>
  170. struct pack<signed long long> {
  171. template <typename Stream>
  172. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, signed long long v) const
  173. { o.pack_long_long(v); return o; }
  174. };
  175. template <>
  176. struct pack<unsigned char> {
  177. template <typename Stream>
  178. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned char v) const
  179. { o.pack_unsigned_char(v); return o; }
  180. };
  181. template <>
  182. struct pack<unsigned short> {
  183. template <typename Stream>
  184. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned short v) const
  185. { o.pack_unsigned_short(v); return o; }
  186. };
  187. template <>
  188. struct pack<unsigned int> {
  189. template <typename Stream>
  190. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned int v) const
  191. { o.pack_unsigned_int(v); return o; }
  192. };
  193. template <>
  194. struct pack<unsigned long> {
  195. template <typename Stream>
  196. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned long v) const
  197. { o.pack_unsigned_long(v); return o; }
  198. };
  199. template <>
  200. struct pack<unsigned long long> {
  201. template <typename Stream>
  202. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, unsigned long long v) const
  203. { o.pack_unsigned_long_long(v); return o; }
  204. };
  205. template <>
  206. struct object<char> {
  207. void operator()(msgpack::object& o, char v) const
  208. { type::detail::object_char(o, v); }
  209. };
  210. template <>
  211. struct object<signed char> {
  212. void operator()(msgpack::object& o, signed char v) const {
  213. if (v < 0) {
  214. o.type = msgpack::type::NEGATIVE_INTEGER;
  215. o.via.i64 = v;
  216. }
  217. else {
  218. o.type = msgpack::type::POSITIVE_INTEGER;
  219. o.via.u64 = v;
  220. }
  221. }
  222. };
  223. template <>
  224. struct object<signed short> {
  225. void operator()(msgpack::object& o, signed short v) const {
  226. if (v < 0) {
  227. o.type = msgpack::type::NEGATIVE_INTEGER;
  228. o.via.i64 = v;
  229. }
  230. else {
  231. o.type = msgpack::type::POSITIVE_INTEGER;
  232. o.via.u64 = v;
  233. }
  234. }
  235. };
  236. template <>
  237. struct object<signed int> {
  238. void operator()(msgpack::object& o, signed int v) const {
  239. if (v < 0) {
  240. o.type = msgpack::type::NEGATIVE_INTEGER;
  241. o.via.i64 = v;
  242. }
  243. else {
  244. o.type = msgpack::type::POSITIVE_INTEGER;
  245. o.via.u64 = v;
  246. }
  247. }
  248. };
  249. template <>
  250. struct object<signed long> {
  251. void operator()(msgpack::object& o, signed long v) const {
  252. if (v < 0) {
  253. o.type = msgpack::type::NEGATIVE_INTEGER;
  254. o.via.i64 = v;
  255. }
  256. else {
  257. o.type = msgpack::type::POSITIVE_INTEGER;
  258. o.via.u64 = v;
  259. }
  260. }
  261. };
  262. template <>
  263. struct object<signed long long> {
  264. void operator()(msgpack::object& o, signed long long v) const {
  265. if (v < 0) {
  266. o.type = msgpack::type::NEGATIVE_INTEGER;
  267. o.via.i64 = v;
  268. }
  269. else{
  270. o.type = msgpack::type::POSITIVE_INTEGER;
  271. o.via.u64 = v;
  272. }
  273. }
  274. };
  275. template <>
  276. struct object<unsigned char> {
  277. void operator()(msgpack::object& o, unsigned char v) const {
  278. o.type = msgpack::type::POSITIVE_INTEGER;
  279. o.via.u64 = v;
  280. }
  281. };
  282. template <>
  283. struct object<unsigned short> {
  284. void operator()(msgpack::object& o, unsigned short v) const {
  285. o.type = msgpack::type::POSITIVE_INTEGER;
  286. o.via.u64 = v;
  287. }
  288. };
  289. template <>
  290. struct object<unsigned int> {
  291. void operator()(msgpack::object& o, unsigned int v) const {
  292. o.type = msgpack::type::POSITIVE_INTEGER;
  293. o.via.u64 = v;
  294. }
  295. };
  296. template <>
  297. struct object<unsigned long> {
  298. void operator()(msgpack::object& o, unsigned long v) const {
  299. o.type = msgpack::type::POSITIVE_INTEGER;
  300. o.via.u64 = v;
  301. }
  302. };
  303. template <>
  304. struct object<unsigned long long> {
  305. void operator()(msgpack::object& o, unsigned long long v) const {
  306. o.type = msgpack::type::POSITIVE_INTEGER;
  307. o.via.u64 = v;
  308. }
  309. };
  310. template <>
  311. struct object_with_zone<char> {
  312. void operator()(msgpack::object::with_zone& o, char v) const {
  313. static_cast<msgpack::object&>(o) << v;
  314. }
  315. };
  316. template <>
  317. struct object_with_zone<signed char> {
  318. void operator()(msgpack::object::with_zone& o, signed char v) const {
  319. static_cast<msgpack::object&>(o) << v;
  320. }
  321. };
  322. template <>
  323. struct object_with_zone<signed short> {
  324. void operator()(msgpack::object::with_zone& o, signed short v) const {
  325. static_cast<msgpack::object&>(o) << v;
  326. }
  327. };
  328. template <>
  329. struct object_with_zone<signed int> {
  330. void operator()(msgpack::object::with_zone& o, signed int v) const {
  331. static_cast<msgpack::object&>(o) << v;
  332. }
  333. };
  334. template <>
  335. struct object_with_zone<signed long> {
  336. void operator()(msgpack::object::with_zone& o, signed long v) const {
  337. static_cast<msgpack::object&>(o) << v;
  338. }
  339. };
  340. template <>
  341. struct object_with_zone<signed long long> {
  342. void operator()(msgpack::object::with_zone& o, const signed long long& v) const {
  343. static_cast<msgpack::object&>(o) << v;
  344. }
  345. };
  346. template <>
  347. struct object_with_zone<unsigned char> {
  348. void operator()(msgpack::object::with_zone& o, unsigned char v) const {
  349. static_cast<msgpack::object&>(o) << v;
  350. }
  351. };
  352. template <>
  353. struct object_with_zone<unsigned short> {
  354. void operator()(msgpack::object::with_zone& o, unsigned short v) const {
  355. static_cast<msgpack::object&>(o) << v;
  356. }
  357. };
  358. template <>
  359. struct object_with_zone<unsigned int> {
  360. void operator()(msgpack::object::with_zone& o, unsigned int v) const {
  361. static_cast<msgpack::object&>(o) << v;
  362. }
  363. };
  364. template <>
  365. struct object_with_zone<unsigned long> {
  366. void operator()(msgpack::object::with_zone& o, unsigned long v) const {
  367. static_cast<msgpack::object&>(o) << v;
  368. }
  369. };
  370. template <>
  371. struct object_with_zone<unsigned long long> {
  372. void operator()(msgpack::object::with_zone& o, const unsigned long long& v) const {
  373. static_cast<msgpack::object&>(o) << v;
  374. }
  375. };
  376. } // namespace adaptor
  377. /// @cond
  378. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  379. /// @endcond
  380. } // namespace msgpack
  381. #endif // MSGPACK_V1_TYPE_INT_HPP