fixint.hpp 8.0 KB

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