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  }
87 };
88 
89 inline void object_char(msgpack::object& o, char v) {
90  return object_char_sign<is_signed<char>::value>::make(o, v);
91 }
92 
93 } // namespace detail
94 } // namespace type
95 
96 namespace adaptor {
97 
98 template <>
99 struct convert<char> {
100  msgpack::object const& operator()(msgpack::object const& o, char& v) const
101  { v = type::detail::convert_integer<char>(o); return o; }
102 };
103 
104 template <>
105 struct convert<signed char> {
106  msgpack::object const& operator()(msgpack::object const& o, signed char& v) const
107  { v = type::detail::convert_integer<signed char>(o); return o; }
108 };
109 
110 template <>
111 struct convert<signed short> {
112  msgpack::object const& operator()(msgpack::object const& o, signed short& v) const
113  { v = type::detail::convert_integer<signed short>(o); return o; }
114 };
115 
116 template <>
117 struct convert<signed int> {
118  msgpack::object const& operator()(msgpack::object const& o, signed int& v) const
119  { v = type::detail::convert_integer<signed int>(o); return o; }
120 };
121 
122 template <>
123 struct convert<signed long> {
124  msgpack::object const& operator()(msgpack::object const& o, signed long& v) const
125  { v = type::detail::convert_integer<signed long>(o); return o; }
126 };
127 
128 template <>
129 struct convert<signed long long> {
130  msgpack::object const& operator()(msgpack::object const& o, signed long long& v) const
131  { v = type::detail::convert_integer<signed long long>(o); return o; }
132 };
133 
134 
135 template <>
136 struct convert<unsigned char> {
137  msgpack::object const& operator()(msgpack::object const& o, unsigned char& v) const
138  { v = type::detail::convert_integer<unsigned char>(o); return o; }
139 };
140 
141 template <>
142 struct convert<unsigned short> {
143  msgpack::object const& operator()(msgpack::object const& o, unsigned short& v) const
144  { v = type::detail::convert_integer<unsigned short>(o); return o; }
145 };
146 
147 template <>
148 struct convert<unsigned int> {
149  msgpack::object const& operator()(msgpack::object const& o, unsigned int& v) const
150  { v = type::detail::convert_integer<unsigned int>(o); return o; }
151 };
152 
153 template <>
154 struct convert<unsigned long> {
155  msgpack::object const& operator()(msgpack::object const& o, unsigned long& v) const
156  { v = type::detail::convert_integer<unsigned long>(o); return o; }
157 };
158 
159 template <>
160 struct convert<unsigned long long> {
161  msgpack::object const& operator()(msgpack::object const& o, unsigned long long& v) const
162  { v = type::detail::convert_integer<unsigned long long>(o); return o; }
163 };
164 
165 
166 template <>
167 struct pack<char> {
168  template <typename Stream>
170  { o.pack_char(v); return o; }
171 };
172 
173 template <>
174 struct pack<signed char> {
175  template <typename Stream>
177  { o.pack_signed_char(v); return o; }
178 };
179 
180 template <>
181 struct pack<signed short> {
182  template <typename Stream>
184  { o.pack_short(v); return o; }
185 };
186 
187 template <>
188 struct pack<signed int> {
189  template <typename Stream>
191  { o.pack_int(v); return o; }
192 };
193 
194 template <>
195 struct pack<signed long> {
196  template <typename Stream>
198  { o.pack_long(v); return o; }
199 };
200 
201 template <>
202 struct pack<signed long long> {
203  template <typename Stream>
205  { o.pack_long_long(v); return o; }
206 };
207 
208 
209 template <>
210 struct pack<unsigned char> {
211  template <typename Stream>
213  { o.pack_unsigned_char(v); return o; }
214 };
215 
216 template <>
217 struct pack<unsigned short> {
218  template <typename Stream>
220  { o.pack_unsigned_short(v); return o; }
221 };
222 
223 template <>
224 struct pack<unsigned int> {
225  template <typename Stream>
227  { o.pack_unsigned_int(v); return o; }
228 };
229 
230 template <>
231 struct pack<unsigned long> {
232  template <typename Stream>
234  { o.pack_unsigned_long(v); return o; }
235 };
236 
237 template <>
238 struct pack<unsigned long long> {
239  template <typename Stream>
241  { o.pack_unsigned_long_long(v); return o; }
242 };
243 
244 
245 template <>
246 struct object<char> {
247  void operator()(msgpack::object& o, char v) const
248  { type::detail::object_char(o, v); }
249 };
250 
251 template <>
252 struct object<signed char> {
253  void operator()(msgpack::object& o, signed char v) const {
254  if (v < 0) {
256  o.via.i64 = v;
257  }
258  else {
260  o.via.u64 = v;
261  }
262  }
263 };
264 
265 template <>
266 struct object<signed short> {
267  void operator()(msgpack::object& o, signed short v) const {
268  if (v < 0) {
270  o.via.i64 = v;
271  }
272  else {
274  o.via.u64 = v;
275  }
276  }
277 };
278 
279 template <>
280 struct object<signed int> {
281  void operator()(msgpack::object& o, signed int v) const {
282  if (v < 0) {
284  o.via.i64 = v;
285  }
286  else {
288  o.via.u64 = v;
289  }
290  }
291 };
292 
293 template <>
294 struct object<signed long> {
295  void operator()(msgpack::object& o, signed long v) const {
296  if (v < 0) {
298  o.via.i64 = v;
299  }
300  else {
302  o.via.u64 = v;
303  }
304  }
305 };
306 
307 template <>
308 struct object<signed long long> {
309  void operator()(msgpack::object& o, signed long long v) const {
310  if (v < 0) {
312  o.via.i64 = v;
313  }
314  else{
316  o.via.u64 = v;
317  }
318  }
319 };
320 
321 template <>
322 struct object<unsigned char> {
323  void operator()(msgpack::object& o, unsigned char v) const
325 };
326 
327 template <>
328 struct object<unsigned short> {
329  void operator()(msgpack::object& o, unsigned short v) const
331 };
332 
333 template <>
334 struct object<unsigned int> {
335  void operator()(msgpack::object& o, unsigned int v) const
337 };
338 
339 template <>
340 struct object<unsigned long> {
341  void operator()(msgpack::object& o, unsigned long v) const
343 };
344 
345 template <>
346 struct object<unsigned long long> {
347  void operator()(msgpack::object& o, unsigned long long v) const
349 };
350 
351 
352 template <>
353 struct object_with_zone<char> {
354  void operator()(msgpack::object::with_zone& o, char v) const
355  { static_cast<msgpack::object&>(o) << v; }
356 };
357 
358 template <>
359 struct object_with_zone<signed char> {
360  void operator()(msgpack::object::with_zone& o, signed char v) const
361  { static_cast<msgpack::object&>(o) << v; }
362 };
363 
364 template <>
365 struct object_with_zone<signed short> {
366  void operator()(msgpack::object::with_zone& o, signed short v) const
367  { static_cast<msgpack::object&>(o) << v; }
368 };
369 
370 template <>
371 struct object_with_zone<signed int> {
372  void operator()(msgpack::object::with_zone& o, signed int v) const
373  { static_cast<msgpack::object&>(o) << v; }
374 };
375 
376 template <>
377 struct object_with_zone<signed long> {
378  void operator()(msgpack::object::with_zone& o, signed long v) const
379  { static_cast<msgpack::object&>(o) << v; }
380 };
381 
382 template <>
383 struct object_with_zone<signed long long> {
384  void operator()(msgpack::object::with_zone& o, const signed long long& v) const
385  { static_cast<msgpack::object&>(o) << v; }
386 };
387 
388 template <>
389 struct object_with_zone<unsigned char> {
390  void operator()(msgpack::object::with_zone& o, unsigned char v) const
391  { static_cast<msgpack::object&>(o) << v; }
392 };
393 
394 template <>
395 struct object_with_zone<unsigned short> {
396  void operator()(msgpack::object::with_zone& o, unsigned short v) const
397  { static_cast<msgpack::object&>(o) << v; }
398 };
399 
400 template <>
401 struct object_with_zone<unsigned int> {
402  void operator()(msgpack::object::with_zone& o, unsigned int v) const
403  { static_cast<msgpack::object&>(o) << v; }
404 };
405 
406 template <>
407 struct object_with_zone<unsigned long> {
408  void operator()(msgpack::object::with_zone& o, unsigned long v) const
409  { static_cast<msgpack::object&>(o) << v; }
410 };
411 
412 template <>
413 struct object_with_zone<unsigned long long> {
414  void operator()(msgpack::object::with_zone& o, const unsigned long long& v) const
415  { static_cast<msgpack::object&>(o) << v; }
416 };
417 
418 } // namespace adaptor
419 
421 } // MSGPACK_API_VERSION_NAMESPACE(v1)
423 
424 } // namespace msgpack
425 
426 #endif // MSGPACK_V1_TYPE_INT_HPP
void operator()(msgpack::object &o, unsigned short v) const
Definition: int.hpp:329
msgpack::object const & operator()(msgpack::object const &o, unsigned char &v) const
Definition: int.hpp:137
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:408
void operator()(msgpack::object::with_zone &o, const signed long long &v) const
Definition: int.hpp:384
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:240
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:219
void operator()(msgpack::object &o, unsigned long long v) const
Definition: int.hpp:347
void operator()(msgpack::object::with_zone &o, unsigned int v) const
Definition: int.hpp:402
void operator()(msgpack::object &o, signed int v) const
Definition: int.hpp:281
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:124
msgpack::object const & operator()(msgpack::object const &o, char &v) const
Definition: int.hpp:100
void operator()(msgpack::object &o, char v) const
Definition: int.hpp:247
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:130
void operator()(msgpack::object &o, signed short v) const
Definition: int.hpp:267
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, unsigned char v) const
Definition: int.hpp:212
msgpack::object const & operator()(msgpack::object const &o, signed char &v) const
Definition: int.hpp:106
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:176
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:354
Definition: adaptor_base.hpp:15
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed long v) const
Definition: int.hpp:197
msgpack::object const & operator()(msgpack::object const &o, unsigned short &v) const
Definition: int.hpp:143
void operator()(msgpack::object::with_zone &o, signed char v) const
Definition: int.hpp:360
void operator()(msgpack::object::with_zone &o, signed short v) const
Definition: int.hpp:366
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:661
Definition: object.hpp:34
void operator()(msgpack::object::with_zone &o, unsigned short v) const
Definition: int.hpp:396
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed long long v) const
Definition: int.hpp:204
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:414
void object_char(msgpack::object &o, char v)
Definition: int.hpp:89
void operator()(msgpack::object &o, signed long v) const
Definition: int.hpp:295
msgpack::object const & operator()(msgpack::object const &o, signed short &v) const
Definition: int.hpp:112
Definition: object_fwd.hpp:236
void operator()(msgpack::object &o, signed long long v) const
Definition: int.hpp:309
Definition: adaptor_base.hpp:32
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed int v) const
Definition: int.hpp:190
void operator()(msgpack::object::with_zone &o, signed int v) const
Definition: int.hpp:372
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:169
void operator()(msgpack::object::with_zone &o, unsigned char v) const
Definition: int.hpp:390
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:161
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:226
T convert_integer(msgpack::object const &o)
Definition: int.hpp:61
void operator()(msgpack::object &o, signed char v) const
Definition: int.hpp:253
void operator()(msgpack::object &o, unsigned char v) const
Definition: int.hpp:323
void operator()(msgpack::object &o, unsigned int v) const
Definition: int.hpp:335
void operator()(msgpack::object::with_zone &o, signed long v) const
Definition: int.hpp:378
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:155
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:118
msgpack::object const & operator()(msgpack::object const &o, unsigned int &v) const
Definition: int.hpp:149
void operator()(msgpack::object &o, unsigned long v) const
Definition: int.hpp:341
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:233
Definition: object_fwd_decl.hpp:31
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, signed short v) const
Definition: int.hpp:183
uint64_t u64
Definition: object_fwd.hpp:78
packer< Stream > & pack_unsigned_char(unsigned char d)
Packing unsigned char.
Definition: pack.hpp:964