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