MessagePack for C++
int.hpp
Go to the documentation of this file.
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 
14 #include "msgpack/object.hpp"
15 
16 #include <limits>
17 
18 namespace msgpack {
19 
23 
24 namespace type {
25 namespace detail {
26 
27 template <typename T>
28 struct convert_integer_sign<T, true> {
29  static T convert(msgpack::object const& o) {
31  if(o.via.u64 > static_cast<uint64_t>(std::numeric_limits<T>::max()))
32  { throw msgpack::type_error(); }
33  return static_cast<T>(o.via.u64);
34  } else if(o.type == msgpack::type::NEGATIVE_INTEGER) {
35  if(o.via.i64 < static_cast<int64_t>(std::numeric_limits<T>::min()))
36  { throw msgpack::type_error(); }
37  return static_cast<T>(o.via.i64);
38  }
39  throw msgpack::type_error();
40  }
41 };
42 
43 template <typename T>
44 struct convert_integer_sign<T, false> {
45  static T convert(msgpack::object const& o) {
47  if(o.via.u64 > static_cast<uint64_t>(std::numeric_limits<T>::max()))
48  { throw msgpack::type_error(); }
49  return static_cast<T>(o.via.u64);
50  }
51  throw msgpack::type_error();
52  }
53 };
54 
55 template <typename T>
56 struct is_signed {
57  static const bool value = std::numeric_limits<T>::is_signed;
58 };
59 
60 template <typename T>
61 inline T convert_integer(msgpack::object const& o)
62 {
64 }
65 
66 template <>
67 struct object_char_sign<true> {
68  template <typename T>
70  make(msgpack::object& o, T v) {
71  if (v < 0) {
73  o.via.i64 = v;
74  }
75  else {
77  o.via.u64 = v;
78  }
79  }
80 };
81 
82 template <>
83 struct object_char_sign<false> {
84  static void make(msgpack::object& o, char v) {
86  o.via.u64 = v;
87  }
88 };
89 
90 inline void object_char(msgpack::object& o, char v) {
91  return object_char_sign<is_signed<char>::value>::make(o, v);
92 }
93 
94 } // namespace detail
95 } // namespace type
96 
97 namespace adaptor {
98 
99 template <>
100 struct convert<char> {
101  msgpack::object const& operator()(msgpack::object const& o, char& v) const
102  { v = type::detail::convert_integer<char>(o); return o; }
103 };
104 
105 template <>
106 struct convert<signed char> {
107  msgpack::object const& operator()(msgpack::object const& o, signed char& v) const
108  { v = type::detail::convert_integer<signed char>(o); return o; }
109 };
110 
111 template <>
112 struct convert<signed short> {
113  msgpack::object const& operator()(msgpack::object const& o, signed short& v) const
114  { v = type::detail::convert_integer<signed short>(o); return o; }
115 };
116 
117 template <>
118 struct convert<signed int> {
119  msgpack::object const& operator()(msgpack::object const& o, signed int& v) const
120  { v = type::detail::convert_integer<signed int>(o); return o; }
121 };
122 
123 template <>
124 struct convert<signed long> {
125  msgpack::object const& operator()(msgpack::object const& o, signed long& v) const
126  { v = type::detail::convert_integer<signed long>(o); return o; }
127 };
128 
129 template <>
130 struct convert<signed long long> {
131  msgpack::object const& operator()(msgpack::object const& o, signed long long& v) const
132  { v = type::detail::convert_integer<signed long long>(o); return o; }
133 };
134 
135 
136 template <>
137 struct convert<unsigned char> {
138  msgpack::object const& operator()(msgpack::object const& o, unsigned char& v) const
139  { v = type::detail::convert_integer<unsigned char>(o); return o; }
140 };
141 
142 template <>
143 struct convert<unsigned short> {
144  msgpack::object const& operator()(msgpack::object const& o, unsigned short& v) const
145  { v = type::detail::convert_integer<unsigned short>(o); return o; }
146 };
147 
148 template <>
149 struct convert<unsigned int> {
150  msgpack::object const& operator()(msgpack::object const& o, unsigned int& v) const
151  { v = type::detail::convert_integer<unsigned int>(o); return o; }
152 };
153 
154 template <>
155 struct convert<unsigned long> {
156  msgpack::object const& operator()(msgpack::object const& o, unsigned long& v) const
157  { v = type::detail::convert_integer<unsigned long>(o); return o; }
158 };
159 
160 template <>
161 struct convert<unsigned long long> {
162  msgpack::object const& operator()(msgpack::object const& o, unsigned long long& v) const
163  { v = type::detail::convert_integer<unsigned long long>(o); return o; }
164 };
165 
166 
167 template <>
168 struct pack<char> {
169  template <typename Stream>
171  { o.pack_char(v); return o; }
172 };
173 
174 template <>
175 struct pack<signed char> {
176  template <typename Stream>
178  { o.pack_signed_char(v); return o; }
179 };
180 
181 template <>
182 struct pack<signed short> {
183  template <typename Stream>
185  { o.pack_short(v); return o; }
186 };
187 
188 template <>
189 struct pack<signed int> {
190  template <typename Stream>
192  { o.pack_int(v); return o; }
193 };
194 
195 template <>
196 struct pack<signed long> {
197  template <typename Stream>
199  { o.pack_long(v); return o; }
200 };
201 
202 template <>
203 struct pack<signed long long> {
204  template <typename Stream>
206  { o.pack_long_long(v); return o; }
207 };
208 
209 
210 template <>
211 struct pack<unsigned char> {
212  template <typename Stream>
214  { o.pack_unsigned_char(v); return o; }
215 };
216 
217 template <>
218 struct pack<unsigned short> {
219  template <typename Stream>
221  { o.pack_unsigned_short(v); return o; }
222 };
223 
224 template <>
225 struct pack<unsigned int> {
226  template <typename Stream>
228  { o.pack_unsigned_int(v); return o; }
229 };
230 
231 template <>
232 struct pack<unsigned long> {
233  template <typename Stream>
235  { o.pack_unsigned_long(v); return o; }
236 };
237 
238 template <>
239 struct pack<unsigned long long> {
240  template <typename Stream>
242  { o.pack_unsigned_long_long(v); return o; }
243 };
244 
245 
246 template <>
247 struct object<char> {
248  void operator()(msgpack::object& o, char v) const
249  { type::detail::object_char(o, v); }
250 };
251 
252 template <>
253 struct object<signed char> {
254  void operator()(msgpack::object& o, signed char v) const {
255  if (v < 0) {
257  o.via.i64 = v;
258  }
259  else {
261  o.via.u64 = v;
262  }
263  }
264 };
265 
266 template <>
267 struct object<signed short> {
268  void operator()(msgpack::object& o, signed short v) const {
269  if (v < 0) {
271  o.via.i64 = v;
272  }
273  else {
275  o.via.u64 = v;
276  }
277  }
278 };
279 
280 template <>
281 struct object<signed int> {
282  void operator()(msgpack::object& o, signed int v) const {
283  if (v < 0) {
285  o.via.i64 = v;
286  }
287  else {
289  o.via.u64 = v;
290  }
291  }
292 };
293 
294 template <>
295 struct object<signed long> {
296  void operator()(msgpack::object& o, signed long v) const {
297  if (v < 0) {
299  o.via.i64 = v;
300  }
301  else {
303  o.via.u64 = v;
304  }
305  }
306 };
307 
308 template <>
309 struct object<signed long long> {
310  void operator()(msgpack::object& o, signed long long v) const {
311  if (v < 0) {
313  o.via.i64 = v;
314  }
315  else{
317  o.via.u64 = v;
318  }
319  }
320 };
321 
322 template <>
323 struct object<unsigned char> {
324  void operator()(msgpack::object& o, unsigned char v) const {
326  o.via.u64 = v;
327  }
328 };
329 
330 template <>
331 struct object<unsigned short> {
332  void operator()(msgpack::object& o, unsigned short v) const {
334  o.via.u64 = v;
335  }
336 };
337 
338 template <>
339 struct object<unsigned int> {
340  void operator()(msgpack::object& o, unsigned int v) const {
342  o.via.u64 = v;
343  }
344 };
345 
346 template <>
347 struct object<unsigned long> {
348  void operator()(msgpack::object& o, unsigned long v) const {
350  o.via.u64 = v;
351  }
352 };
353 
354 template <>
355 struct object<unsigned long long> {
356  void operator()(msgpack::object& o, unsigned long long v) const {
358  o.via.u64 = v;
359  }
360 };
361 
362 
363 template <>
364 struct object_with_zone<char> {
365  void operator()(msgpack::object::with_zone& o, char v) const {
366  static_cast<msgpack::object&>(o) << v;
367  }
368 };
369 
370 template <>
371 struct object_with_zone<signed char> {
372  void operator()(msgpack::object::with_zone& o, signed char v) const {
373  static_cast<msgpack::object&>(o) << v;
374  }
375 };
376 
377 template <>
378 struct object_with_zone<signed short> {
379  void operator()(msgpack::object::with_zone& o, signed short v) const {
380  static_cast<msgpack::object&>(o) << v;
381  }
382 };
383 
384 template <>
385 struct object_with_zone<signed int> {
386  void operator()(msgpack::object::with_zone& o, signed int v) const {
387  static_cast<msgpack::object&>(o) << v;
388  }
389 };
390 
391 template <>
392 struct object_with_zone<signed long> {
393  void operator()(msgpack::object::with_zone& o, signed long v) const {
394  static_cast<msgpack::object&>(o) << v;
395  }
396 };
397 
398 template <>
399 struct object_with_zone<signed long long> {
400  void operator()(msgpack::object::with_zone& o, const signed long long& v) const {
401  static_cast<msgpack::object&>(o) << v;
402  }
403 };
404 
405 template <>
406 struct object_with_zone<unsigned char> {
407  void operator()(msgpack::object::with_zone& o, unsigned char v) const {
408  static_cast<msgpack::object&>(o) << v;
409  }
410 };
411 
412 template <>
413 struct object_with_zone<unsigned short> {
414  void operator()(msgpack::object::with_zone& o, unsigned short v) const {
415  static_cast<msgpack::object&>(o) << v;
416  }
417 };
418 
419 template <>
420 struct object_with_zone<unsigned int> {
421  void operator()(msgpack::object::with_zone& o, unsigned int v) const {
422  static_cast<msgpack::object&>(o) << v;
423  }
424 };
425 
426 template <>
427 struct object_with_zone<unsigned long> {
428  void operator()(msgpack::object::with_zone& o, unsigned long v) const {
429  static_cast<msgpack::object&>(o) << v;
430  }
431 };
432 
433 template <>
434 struct object_with_zone<unsigned long long> {
435  void operator()(msgpack::object::with_zone& o, const unsigned long long& v) const {
436  static_cast<msgpack::object&>(o) << v;
437  }
438 };
439 
440 } // namespace adaptor
441 
443 } // MSGPACK_API_VERSION_NAMESPACE(v1)
445 
446 } // namespace msgpack
447 
448 #endif // MSGPACK_V1_TYPE_INT_HPP
void operator()(msgpack::object &o, unsigned short v) const
Definition: int.hpp:332
msgpack::object const & operator()(msgpack::object const &o, unsigned char &v) const
Definition: int.hpp:138
static msgpack::enable_if< msgpack::is_same< T, char >::value >::type make(msgpack::object &o, T v)
Definition: int.hpp:70
void operator()(msgpack::object::with_zone &o, unsigned long v) const
Definition: int.hpp:428
void operator()(msgpack::object::with_zone &o, const signed long long &v) const
Definition: int.hpp:400
packer< Stream > & pack_char(char d)
Packing char.
Definition: pack.hpp:809
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, unsigned long long v) const
Definition: int.hpp:241
packer< Stream > & pack_long_long(long long d)
Packing long long.
Definition: pack.hpp:930
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, unsigned short v) const
Definition: int.hpp:220
void operator()(msgpack::object &o, unsigned long long v) const
Definition: int.hpp:356
void operator()(msgpack::object::with_zone &o, unsigned int v) const
Definition: int.hpp:421
void operator()(msgpack::object &o, signed int v) const
Definition: int.hpp:282
packer< Stream > & pack_unsigned_int(unsigned int d)
Packing unsigned int.
Definition: pack.hpp:1004
msgpack::object const & operator()(msgpack::object const &o, signed long &v) const
Definition: int.hpp:125
msgpack::object const & operator()(msgpack::object const &o, char &v) const
Definition: int.hpp:101
void operator()(msgpack::object &o, char v) const
Definition: int.hpp:248
packer< Stream > & pack_short(short d)
Packing short.
Definition: pack.hpp:831
msgpack::object const & operator()(msgpack::object const &o, signed long long &v) const
Definition: int.hpp:131
void operator()(msgpack::object &o, signed short v) const
Definition: int.hpp:268
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, unsigned char v) const
Definition: int.hpp:213
msgpack::object const & operator()(msgpack::object const &o, signed char &v) const
Definition: int.hpp:107
union_type via
Definition: object_fwd.hpp:93
static void make(msgpack::object &o, char v)
Definition: int.hpp:84
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed char v) const
Definition: int.hpp:177
packer< Stream > & pack_long(long d)
Packing long.
Definition: pack.hpp:897
void operator()(msgpack::object::with_zone &o, char v) const
Definition: int.hpp:365
Definition: adaptor_base.hpp:15
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed long v) const
Definition: int.hpp:198
msgpack::object const & operator()(msgpack::object const &o, unsigned short &v) const
Definition: int.hpp:144
void operator()(msgpack::object::with_zone &o, signed char v) const
Definition: int.hpp:372
void operator()(msgpack::object::with_zone &o, signed short v) const
Definition: int.hpp:379
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1194
Definition: object.hpp:35
void operator()(msgpack::object::with_zone &o, unsigned short v) const
Definition: int.hpp:414
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed long long v) const
Definition: int.hpp:205
int64_t i64
Definition: object_fwd.hpp:79
Definition: object_fwd_decl.hpp:32
Definition: adaptor_base.hpp:43
Definition: int.hpp:56
void operator()(msgpack::object::with_zone &o, const unsigned long long &v) const
Definition: int.hpp:435
void object_char(msgpack::object &o, char v)
Definition: int.hpp:90
void operator()(msgpack::object &o, signed long v) const
Definition: int.hpp:296
msgpack::object const & operator()(msgpack::object const &o, signed short &v) const
Definition: int.hpp:113
Definition: object_fwd.hpp:236
void operator()(msgpack::object &o, signed long long v) const
Definition: int.hpp:310
Definition: adaptor_base.hpp:32
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed int v) const
Definition: int.hpp:191
void operator()(msgpack::object::with_zone &o, signed int v) const
Definition: int.hpp:386
static T convert(msgpack::object const &o)
Definition: int.hpp:29
packer< Stream > & pack_unsigned_short(unsigned short d)
Packing unsigned short.
Definition: pack.hpp:971
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, char v) const
Definition: int.hpp:170
void operator()(msgpack::object::with_zone &o, unsigned char v) const
Definition: int.hpp:407
Definition: cpp_config_decl.hpp:56
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::object const & operator()(msgpack::object const &o, unsigned long long &v) const
Definition: int.hpp:162
msgpack::type::object_type type
Definition: object_fwd.hpp:92
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, unsigned int v) const
Definition: int.hpp:227
T convert_integer(msgpack::object const &o)
Definition: int.hpp:61
void operator()(msgpack::object &o, signed char v) const
Definition: int.hpp:254
void operator()(msgpack::object &o, unsigned char v) const
Definition: int.hpp:324
void operator()(msgpack::object &o, unsigned int v) const
Definition: int.hpp:340
void operator()(msgpack::object::with_zone &o, signed long v) const
Definition: int.hpp:393
static T convert(msgpack::object const &o)
Definition: int.hpp:45
Definition: adaptor_base.hpp:38
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::object const & operator()(msgpack::object const &o, unsigned long &v) const
Definition: int.hpp:156
packer< Stream > & pack_signed_char(signed char d)
Packing signed char.
Definition: pack.hpp:824
msgpack::object const & operator()(msgpack::object const &o, signed int &v) const
Definition: int.hpp:119
msgpack::object const & operator()(msgpack::object const &o, unsigned int &v) const
Definition: int.hpp:150
void operator()(msgpack::object &o, unsigned long v) const
Definition: int.hpp:348
Definition: adaptor_base.hpp:27
packer< Stream > & pack_unsigned_long(unsigned long d)
Packing unsigned long.
Definition: pack.hpp:1037
packer< Stream > & pack_unsigned_long_long(unsigned long long d)
Packing unsigned long long.
Definition: pack.hpp:1070
packer< Stream > & pack_int(int d)
Packing int.
Definition: pack.hpp:864
Definition: int_decl.hpp:36
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, unsigned long v) const
Definition: int.hpp:234
Definition: object_fwd_decl.hpp:31
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed short v) const
Definition: int.hpp:184
uint64_t u64
Definition: object_fwd.hpp:78
packer< Stream > & pack_unsigned_char(unsigned char d)
Packing unsigned char.
Definition: pack.hpp:964