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