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