MessagePack for C++
Loading...
Searching...
No Matches
chrono.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2017 KONDO Takatoshi
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
11#ifndef MSGPACK_V1_TYPE_CPP11_CHRONO_HPP
12#define MSGPACK_V1_TYPE_CPP11_CHRONO_HPP
13
16#include "msgpack/object.hpp"
18
19#include <limits>
20#include <chrono>
21
22namespace msgpack {
23
27
28namespace adaptor {
29
30namespace detail {
31template <
32 typename Target,
33 typename Source,
34 bool target_is_signed = std::is_signed<Target>::value,
35 bool source_is_signed = std::is_signed<Source>::value,
36 typename = typename std::enable_if<
37 std::is_integral<Target>::value &&
38 std::is_integral<Source>::value
39 >::type
40>
42 // The default case includes the cases that Source being unsigned, and since Source
43 // is unsigned, no underflow can happen
44 would_underflow(Source) : value{false} {}
45 bool value;
46};
47
48template <typename Target, typename Source>
49struct would_underflow<Target, Source, false, true> {
50 // When Source is signed and Target is unsigned, we only need to compare with 0 to
51 // detect underflow, this works correctly and also avoids warnings from the compiler
52 would_underflow(Source source) : value{source < 0} {}
53 bool value;
54};
55template <typename Target, typename Source>
56struct would_underflow<Target, Source, true, true> {
57 // When Source and Target are signed, the promotion rules apply sensibly so we do
58 // not need to do anything
59 would_underflow(Source source)
60 : value{source < std::numeric_limits<Target>::min()} {}
61 bool value;
62};
63
64template <
65 typename Target,
66 typename Source,
67 bool target_is_signed = std::is_signed<Target>::value,
68 bool source_is_signed = std::is_signed<Source>::value,
69 typename = typename std::enable_if<
70 std::is_integral<Target>::value &&
71 std::is_integral<Source>::value
72 >::type
73>
75 // The default case is Source and Target having the same signedness, the promotion
76 // rule also apply sensibly here so nothing special needs to be done
77 would_overflow(Source source)
78 : value{source > std::numeric_limits<Target>::max()} {}
79 bool value;
80};
81template <typename Target, typename Source>
82struct would_overflow <Target, Source, false, true> {
83 // When Target is unsigned and Source is signed, we cannot rely on the promotion
84 // rule.
85 would_overflow(Source source)
86 : value{
87 sizeof(Target) >= sizeof(Source)
88 // Given Source is signed, Target being unsigned and having at least the
89 // same size makes impossible to overflow
90 ? false
91 // Source being larger than Target makes it safe to cast the maximum value
92 // of Target to Source
93 : source > static_cast<Source>(std::numeric_limits<Target>::max())
94 } {}
95 bool value;
96};
97template <typename Target, typename Source>
98struct would_overflow <Target, Source, true, false> {
99 // When Target is signed and Source is unsigned, we cannot rely on the promotion
100 // rule.
101 would_overflow(Source source)
102 : value{
103 sizeof(Target) > sizeof(Source)
104 // Target being larger than Source makes it impossible to overflow
105 ? false
106 // Source being unsigned and having at least the size of Target makes it
107 // safe to cast the maximum value of Target to Source
108 : source > static_cast<Source>(std::numeric_limits<Target>::max())
109 } {}
110 bool value;
111};
112
113template <
114 typename Target,
115 typename Source,
116 typename = typename std::enable_if<
117 std::is_integral<Target>::value &&
118 std::is_integral<Source>::value
119 >::type
120>
121Target integral_cast(Source source) {
122 if (would_underflow<Target, Source>(source).value) {
123 throw std::underflow_error{
124 "casting from Source to Target causes an underflow error"
125 };
126 }
127 if(would_overflow<Target, Source>(source).value) {
128 throw std::overflow_error{
129 "casting from Source to Target causes an overflow error"
130 };
131 }
132
133 return static_cast<Target>(source);
134}
135} // namespace detail
136
137template <typename Clock, typename Duration>
138struct as<std::chrono::time_point<Clock, Duration>> {
139 typename std::chrono::time_point<Clock, Duration> operator()(msgpack::object const& o) const {
140 if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
141 if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
142 std::chrono::time_point<Clock, Duration> tp;
143 switch(o.via.ext.size) {
144 case 4: {
145 uint32_t sec;
146 _msgpack_load32(uint32_t, o.via.ext.data(), &sec);
147 tp += std::chrono::seconds(sec);
148 } break;
149 case 8: {
150 uint64_t value;
151 _msgpack_load64(uint64_t, o.via.ext.data(), &value);
152 uint32_t nanosec = detail::integral_cast<uint32_t>(value >> 34);
153 uint64_t sec = value & 0x00000003ffffffffLL;
154 tp += std::chrono::duration_cast<Duration>(
155 std::chrono::nanoseconds(nanosec));
156 tp += std::chrono::seconds(sec);
157 } break;
158 case 12: {
159 uint32_t nanosec;
160 _msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
161 int64_t sec;
162 _msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
163
164 if (sec > 0) {
165 tp += std::chrono::seconds(sec);
166 tp += std::chrono::duration_cast<Duration>(
167 std::chrono::nanoseconds(nanosec));
168 }
169 else {
170 if (nanosec == 0) {
171 tp += std::chrono::seconds(sec);
172 }
173 else {
174 ++sec;
175 tp += std::chrono::seconds(sec);
176 int64_t ns = detail::integral_cast<int64_t>(nanosec) - 1000000000L;
177 tp += std::chrono::duration_cast<Duration>(
178 std::chrono::nanoseconds(ns));
179 }
180 }
181 } break;
182 default:
183 throw msgpack::type_error();
184 }
185 return tp;
186 }
187};
188
189template <typename Clock, typename Duration>
190struct convert<std::chrono::time_point<Clock, Duration>> {
191 msgpack::object const& operator()(msgpack::object const& o, std::chrono::time_point<Clock, Duration>& v) const {
192 if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
193 if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
194 std::chrono::time_point<Clock, Duration> tp;
195 switch(o.via.ext.size) {
196 case 4: {
197 uint32_t sec;
198 _msgpack_load32(uint32_t, o.via.ext.data(), &sec);
199 tp += std::chrono::seconds(sec);
200 v = tp;
201 } break;
202 case 8: {
203 uint64_t value;
204 _msgpack_load64(uint64_t, o.via.ext.data(), &value);
205 uint32_t nanosec = detail::integral_cast<uint32_t>(value >> 34);
206 uint64_t sec = value & 0x00000003ffffffffLL;
207 tp += std::chrono::duration_cast<Duration>(
208 std::chrono::nanoseconds(nanosec));
209 tp += std::chrono::seconds(sec);
210 v = tp;
211 } break;
212 case 12: {
213 uint32_t nanosec;
214 _msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
215 int64_t sec;
216 _msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
217
218 if (sec > 0) {
219 tp += std::chrono::seconds(sec);
220 tp += std::chrono::duration_cast<Duration>(
221 std::chrono::nanoseconds(nanosec));
222 }
223 else {
224 if (nanosec == 0) {
225 tp += std::chrono::seconds(sec);
226 }
227 else {
228 ++sec;
229 tp += std::chrono::seconds(sec);
230 int64_t ns = detail::integral_cast<int64_t>(nanosec) - 1000000000L;
231 tp += std::chrono::duration_cast<Duration>(
232 std::chrono::nanoseconds(ns));
233 }
234 }
235
236 v = tp;
237 } break;
238 default:
239 throw msgpack::type_error();
240 }
241 return o;
242 }
243};
244
245template <typename Clock, typename Duration>
246struct pack<std::chrono::time_point<Clock, Duration>> {
247 template <typename Stream>
248 msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, std::chrono::time_point<Clock, Duration> const& v) const {
249 int64_t count = detail::integral_cast<int64_t>(v.time_since_epoch().count());
250 int64_t nano_num =
251 Duration::period::ratio::num *
252 (1000000000L / Duration::period::ratio::den);
253
254 int64_t nanosec = count % (1000000000L / nano_num) * nano_num;
255 int64_t sec = 0;
256 if (nanosec < 0) {
257 nanosec = 1000000000L + nanosec;
258 --sec;
259 }
260 sec += count
261 * Duration::period::ratio::num
262 / Duration::period::ratio::den;
263
264 if ((sec >> 34) == 0) {
265 uint64_t data64 = (detail::integral_cast<uint64_t>(nanosec) << 34) | detail::integral_cast<uint64_t>(sec);
266 if ((data64 & 0xffffffff00000000L) == 0) {
267 // timestamp 32
268 o.pack_ext(4, -1);
269 uint32_t data32 = detail::integral_cast<uint32_t>(data64);
270 char buf[4];
271 _msgpack_store32(buf, data32);
272 o.pack_ext_body(buf, 4);
273 }
274 else {
275 // timestamp 64
276 o.pack_ext(8, -1);
277 char buf[8];
278 _msgpack_store64(buf, data64);
279 o.pack_ext_body(buf, 8);
280 }
281 }
282 else {
283 // timestamp 96
284 o.pack_ext(12, -1);
285 char buf[12];
286
287
288 _msgpack_store32(&buf[0], detail::integral_cast<uint32_t>(nanosec));
289 _msgpack_store64(&buf[4], sec);
290 o.pack_ext_body(buf, 12);
291 }
292 return o;
293 }
294};
295
296template <typename Clock, typename Duration>
297struct object_with_zone<std::chrono::time_point<Clock, Duration>> {
298 void operator()(msgpack::object::with_zone& o, const std::chrono::time_point<Clock, Duration>& v) const {
299 int64_t count = detail::integral_cast<int64_t>(v.time_since_epoch().count());
300
301 int64_t nano_num =
302 Duration::period::ratio::num *
303 (1000000000L / Duration::period::ratio::den);
304
305 int64_t nanosec = count % (1000000000L / nano_num) * nano_num;
306 int64_t sec = 0;
307 if (nanosec < 0) {
308 nanosec = 1000000000L + nanosec;
309 --sec;
310 }
311 sec += count
312 * Duration::period::ratio::num
313 / Duration::period::ratio::den;
314 if ((sec >> 34) == 0) {
315 uint64_t data64 = (detail::integral_cast<uint64_t>(nanosec) << 34) | detail::integral_cast<uint64_t>(sec);
316 if ((data64 & 0xffffffff00000000L) == 0) {
317 // timestamp 32
318 o.type = msgpack::type::EXT;
319 o.via.ext.size = 4;
320 char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
321 p[0] = static_cast<char>(-1);
322 uint32_t data32 = detail::integral_cast<uint32_t>(data64);
323 _msgpack_store32(&p[1], data32);
324 o.via.ext.ptr = p;
325 }
326 else {
327 // timestamp 64
328 o.type = msgpack::type::EXT;
329 o.via.ext.size = 8;
330 char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
331 p[0] = static_cast<char>(-1);
332 _msgpack_store64(&p[1], data64);
333 o.via.ext.ptr = p;
334 }
335 }
336 else {
337 // timestamp 96
338 o.type = msgpack::type::EXT;
339 o.via.ext.size = 12;
340 char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
341 p[0] = static_cast<char>(-1);
342 _msgpack_store32(&p[1], detail::integral_cast<uint32_t>(nanosec));
343 _msgpack_store64(&p[1 + 4], sec);
344 o.via.ext.ptr = p;
345 }
346 }
347};
348
349} // namespace adaptor
350
352} // MSGPACK_API_VERSION_NAMESPACE(v1)
354
355} // namespace msgpack
356
357#endif // MSGPACK_V1_TYPE_CPP11_CHRONO_HPP
The class template that supports continuous packing.
Definition pack.hpp:33
packer< Stream > & pack_ext(size_t l, int8_t type)
Packing ext header, type, and length.
Definition pack.hpp:1316
packer< Stream > & pack_ext_body(const char *b, uint32_t l)
Packing ext body.
Definition pack.hpp:1375
Definition object_fwd.hpp:231
void * allocate_no_align(size_t size)
Definition cpp03_zone.hpp:270
Target integral_cast(Source source)
Definition chrono.hpp:121
Definition adaptor_base.hpp:15
std::chrono::time_point< Clock, Duration > operator()(msgpack::object const &o) const
Definition chrono.hpp:139
Definition object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::chrono::time_point< Clock, Duration > &v) const
Definition chrono.hpp:191
Definition adaptor_base.hpp:27
would_overflow(Source source)
Definition chrono.hpp:77
bool value
Definition chrono.hpp:79
would_underflow(Source)
Definition chrono.hpp:44
bool value
Definition chrono.hpp:45
void operator()(msgpack::object::with_zone &o, const std::chrono::time_point< Clock, Duration > &v) const
Definition chrono.hpp:298
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, std::chrono::time_point< Clock, Duration > const &v) const
Definition chrono.hpp:248
Definition adaptor_base.hpp:32
Definition object.hpp:35
msgpack::zone & zone
Definition object.hpp:37
int8_t type() const
Definition object_fwd.hpp:43
const char * ptr
Definition object_fwd.hpp:46
const char * data() const
Definition object_fwd.hpp:44
uint32_t size
Definition object_fwd.hpp:45
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
#define _msgpack_load64(cast, from, to)
Definition sysdep.hpp:180
#define _msgpack_store32(to, num)
Definition sysdep.hpp:187
#define _msgpack_store64(to, num)
Definition sysdep.hpp:189
#define _msgpack_load32(cast, from, to)
Definition sysdep.hpp:176
msgpack::object_ext ext
Definition object_fwd.hpp:89
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:66