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