fixint.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 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_TYPE_FIXINT_HPP
  11. #define MSGPACK_V1_TYPE_FIXINT_HPP
  12. #include "msgpack/v1/adaptor/fixint_decl.hpp"
  13. namespace msgpack {
  14. /// @cond
  15. MSGPACK_API_VERSION_NAMESPACE(v1) {
  16. /// @endcond
  17. namespace type {
  18. template <typename T>
  19. struct fix_int {
  20. fix_int() : value(0) { }
  21. fix_int(T value) : value(value) { }
  22. operator T() const { return value; }
  23. T get() const { return value; }
  24. private:
  25. T value;
  26. };
  27. } // namespace type
  28. namespace adaptor {
  29. template <>
  30. struct convert<type::fix_int8> {
  31. msgpack::object const& operator()(msgpack::object const& o, type::fix_int8& v) const
  32. { v = type::detail::convert_integer<int8_t>(o); return o; }
  33. };
  34. template <>
  35. struct convert<type::fix_int16> {
  36. msgpack::object const& operator()(msgpack::object const& o, type::fix_int16& v) const
  37. { v = type::detail::convert_integer<int16_t>(o); return o; }
  38. };
  39. template <>
  40. struct convert<type::fix_int32> {
  41. msgpack::object const& operator()(msgpack::object const& o, type::fix_int32& v) const
  42. { v = type::detail::convert_integer<int32_t>(o); return o; }
  43. };
  44. template <>
  45. struct convert<type::fix_int64> {
  46. msgpack::object const& operator()(msgpack::object const& o, type::fix_int64& v) const
  47. { v = type::detail::convert_integer<int64_t>(o); return o; }
  48. };
  49. template <>
  50. struct convert<type::fix_uint8> {
  51. msgpack::object const& operator()(msgpack::object const& o, type::fix_uint8& v) const
  52. { v = type::detail::convert_integer<uint8_t>(o); return o; }
  53. };
  54. template <>
  55. struct convert<type::fix_uint16> {
  56. msgpack::object const& operator()(msgpack::object const& o, type::fix_uint16& v) const
  57. { v = type::detail::convert_integer<uint16_t>(o); return o; }
  58. };
  59. template <>
  60. struct convert<type::fix_uint32> {
  61. msgpack::object const& operator()(msgpack::object const& o, type::fix_uint32& v) const
  62. { v = type::detail::convert_integer<uint32_t>(o); return o; }
  63. };
  64. template <>
  65. struct convert<type::fix_uint64> {
  66. msgpack::object const& operator()(msgpack::object const& o, type::fix_uint64& v) const
  67. { v = type::detail::convert_integer<uint64_t>(o); return o; }
  68. };
  69. template <>
  70. struct pack<type::fix_int8> {
  71. template <typename Stream>
  72. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_int8& v) const
  73. { o.pack_fix_int8(v); return o; }
  74. };
  75. template <>
  76. struct pack<type::fix_int16> {
  77. template <typename Stream>
  78. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_int16& v) const
  79. { o.pack_fix_int16(v); return o; }
  80. };
  81. template <>
  82. struct pack<type::fix_int32> {
  83. template <typename Stream>
  84. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_int32& v) const
  85. { o.pack_fix_int32(v); return o; }
  86. };
  87. template <>
  88. struct pack<type::fix_int64> {
  89. template <typename Stream>
  90. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_int64& v) const
  91. { o.pack_fix_int64(v); return o; }
  92. };
  93. template <>
  94. struct pack<type::fix_uint8> {
  95. template <typename Stream>
  96. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_uint8& v) const
  97. { o.pack_fix_uint8(v); return o; }
  98. };
  99. template <>
  100. struct pack<type::fix_uint16> {
  101. template <typename Stream>
  102. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_uint16& v) const
  103. { o.pack_fix_uint16(v); return o; }
  104. };
  105. template <>
  106. struct pack<type::fix_uint32> {
  107. template <typename Stream>
  108. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_uint32& v) const
  109. { o.pack_fix_uint32(v); return o; }
  110. };
  111. template <>
  112. struct pack<type::fix_uint64> {
  113. template <typename Stream>
  114. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const type::fix_uint64& v) const
  115. { o.pack_fix_uint64(v); return o; }
  116. };
  117. template <>
  118. struct object<type::fix_int8> {
  119. void operator()(msgpack::object& o, type::fix_int8 v) const {
  120. if (v.get() < 0) {
  121. o.type = msgpack::type::NEGATIVE_INTEGER;
  122. o.via.i64 = v.get();
  123. }
  124. else {
  125. o.type = msgpack::type::POSITIVE_INTEGER;
  126. o.via.u64 = v.get();
  127. }
  128. }
  129. };
  130. template <>
  131. struct object<type::fix_int16> {
  132. void operator()(msgpack::object& o, type::fix_int16 v) const {
  133. if(v.get() < 0) {
  134. o.type = msgpack::type::NEGATIVE_INTEGER;
  135. o.via.i64 = v.get();
  136. }
  137. else {
  138. o.type = msgpack::type::POSITIVE_INTEGER;
  139. o.via.u64 = v.get();
  140. }
  141. }
  142. };
  143. template <>
  144. struct object<type::fix_int32> {
  145. void operator()(msgpack::object& o, type::fix_int32 v) const {
  146. if (v.get() < 0) {
  147. o.type = msgpack::type::NEGATIVE_INTEGER;
  148. o.via.i64 = v.get();
  149. }
  150. else {
  151. o.type = msgpack::type::POSITIVE_INTEGER;
  152. o.via.u64 = v.get();
  153. }
  154. }
  155. };
  156. template <>
  157. struct object<type::fix_int64> {
  158. void operator()(msgpack::object& o, type::fix_int64 v) const {
  159. if (v.get() < 0) {
  160. o.type = msgpack::type::NEGATIVE_INTEGER;
  161. o.via.i64 = v.get();
  162. }
  163. else {
  164. o.type = msgpack::type::POSITIVE_INTEGER;
  165. o.via.u64 = v.get();
  166. }
  167. }
  168. };
  169. template <>
  170. struct object<type::fix_uint8> {
  171. void operator()(msgpack::object& o, type::fix_uint8 v) const {
  172. o.type = msgpack::type::POSITIVE_INTEGER;
  173. o.via.u64 = v.get();
  174. }
  175. };
  176. template <>
  177. struct object<type::fix_uint16> {
  178. void operator()(msgpack::object& o, type::fix_uint16 v) const {
  179. o.type = msgpack::type::POSITIVE_INTEGER;
  180. o.via.u64 = v.get();
  181. }
  182. };
  183. template <>
  184. struct object<type::fix_uint32> {
  185. void operator()(msgpack::object& o, type::fix_uint32 v) const {
  186. o.type = msgpack::type::POSITIVE_INTEGER;
  187. o.via.u64 = v.get();
  188. }
  189. };
  190. template <>
  191. struct object<type::fix_uint64> {
  192. void operator()(msgpack::object& o, type::fix_uint64 v) const {
  193. o.type = msgpack::type::POSITIVE_INTEGER;
  194. o.via.u64 = v.get();
  195. }
  196. };
  197. template <>
  198. struct object_with_zone<type::fix_int8> {
  199. void operator()(msgpack::object::with_zone& o, type::fix_int8 v) const {
  200. static_cast<msgpack::object&>(o) << v;
  201. }
  202. };
  203. template <>
  204. struct object_with_zone<type::fix_int16> {
  205. void operator()(msgpack::object::with_zone& o, type::fix_int16 v) const {
  206. static_cast<msgpack::object&>(o) << v;
  207. }
  208. };
  209. template <>
  210. struct object_with_zone<type::fix_int32> {
  211. void operator()(msgpack::object::with_zone& o, type::fix_int32 v) const {
  212. static_cast<msgpack::object&>(o) << v;
  213. }
  214. };
  215. template <>
  216. struct object_with_zone<type::fix_int64> {
  217. void operator()(msgpack::object::with_zone& o, type::fix_int64 v) const {
  218. static_cast<msgpack::object&>(o) << v;
  219. }
  220. };
  221. template <>
  222. struct object_with_zone<type::fix_uint8> {
  223. void operator()(msgpack::object::with_zone& o, type::fix_uint8 v) const {
  224. static_cast<msgpack::object&>(o) << v;
  225. }
  226. };
  227. template <>
  228. struct object_with_zone<type::fix_uint16> {
  229. void operator()(msgpack::object::with_zone& o, type::fix_uint16 v) const {
  230. static_cast<msgpack::object&>(o) << v;
  231. }
  232. };
  233. template <>
  234. struct object_with_zone<type::fix_uint32> {
  235. void operator()(msgpack::object::with_zone& o, type::fix_uint32 v) const {
  236. static_cast<msgpack::object&>(o) << v;
  237. }
  238. };
  239. template <>
  240. struct object_with_zone<type::fix_uint64> {
  241. void operator()(msgpack::object::with_zone& o, type::fix_uint64 v) const {
  242. static_cast<msgpack::object&>(o) << v;
  243. }
  244. };
  245. } // namespace adaptor
  246. /// @cond
  247. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  248. /// @endcond
  249. } // namespace msgpack
  250. #endif // MSGPACK_V1_TYPE_FIXINT_HPP