MessagePack for C++
timespec.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2018 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#ifndef MSGPACK_V1_TYPE_CPP11_TIMESPEC_HPP
11#define MSGPACK_V1_TYPE_CPP11_TIMESPEC_HPP
12
15#include "msgpack/object.hpp"
16
17#include <ctime>
18
19namespace msgpack {
20
24
25namespace adaptor {
26
27template <>
28struct convert<timespec> {
29 msgpack::object const& operator()(msgpack::object const& o, timespec& v) const {
30 if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
31 if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
32 switch(o.via.ext.size) {
33 case 4: {
34 uint32_t sec;
35 _msgpack_load32(uint32_t, o.via.ext.data(), &sec);
36 v.tv_sec = static_cast<decltype(v.tv_sec)>(sec);
37 v.tv_nsec = 0;
38 } break;
39 case 8: {
40 uint64_t value;
41 _msgpack_load64(uint64_t, o.via.ext.data(), &value);
42 v.tv_sec = static_cast<decltype(v.tv_sec)>(value & 0x00000003ffffffffLL);
43 v.tv_nsec= static_cast<decltype(v.tv_nsec)>(value >> 34);
44 } break;
45 case 12: {
46 uint32_t nanosec;
47 _msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
48 int64_t sec;
49 _msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
50 v.tv_sec = static_cast<decltype(v.tv_sec)>(sec);
51 v.tv_nsec = static_cast<decltype(v.tv_nsec)>(nanosec);
52 } break;
53 default:
54 throw msgpack::type_error();
55 }
56 return o;
57 }
58};
59
60template <>
61struct pack<timespec> {
62 template <typename Stream>
64 if ((static_cast<uint64_t>(v.tv_sec) >> 34) == 0) {
65 uint64_t data64 = (static_cast<uint64_t>(v.tv_nsec) << 34) | static_cast<uint64_t>(v.tv_sec);
66 if ((data64 & 0xffffffff00000000L) == 0) {
67 // timestamp 32
68 o.pack_ext(4, -1);
69 uint32_t data32 = static_cast<uint32_t>(data64);
70 char buf[4];
71 _msgpack_store32(buf, data32);
72 o.pack_ext_body(buf, 4);
73 }
74 else {
75 // timestamp 64
76 o.pack_ext(8, -1);
77 char buf[8];
78 _msgpack_store64(buf, data64);
79 o.pack_ext_body(buf, 8);
80 }
81 }
82 else {
83 // timestamp 96
84 o.pack_ext(12, -1);
85 char buf[12];
86 _msgpack_store32(&buf[0], static_cast<uint32_t>(v.tv_nsec));
87 _msgpack_store64(&buf[4], v.tv_sec);
88 o.pack_ext_body(buf, 12);
89 }
90 return o;
91 }
92};
93
94template <>
95struct object_with_zone<timespec> {
96 void operator()(msgpack::object::with_zone& o, const timespec& v) const {
97 if ((static_cast<uint64_t>(v.tv_sec) >> 34) == 0) {
98 uint64_t data64 = (static_cast<uint64_t>(v.tv_nsec) << 34) | static_cast<uint64_t>(v.tv_sec);
99 if ((data64 & 0xffffffff00000000L) == 0) {
100 // timestamp 32
102 o.via.ext.size = 4;
103 char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
104 p[0] = static_cast<char>(-1);
105 uint32_t data32 = static_cast<uint32_t>(data64);
106 _msgpack_store32(&p[1], data32);
107 o.via.ext.ptr = p;
108 }
109 else {
110 // timestamp 64
112 o.via.ext.size = 8;
113 char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
114 p[0] = static_cast<char>(-1);
115 _msgpack_store64(&p[1], data64);
116 o.via.ext.ptr = p;
117 }
118 }
119 else {
120 // timestamp 96
122 o.via.ext.size = 12;
123 char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
124 p[0] = static_cast<char>(-1);
125 _msgpack_store32(&p[1], static_cast<uint32_t>(v.tv_nsec));
126 _msgpack_store64(&p[1 + 4], v.tv_sec);
127 o.via.ext.ptr = p;
128 }
129 }
130};
131
132} // namespace adaptor
133
135} // MSGPACK_API_VERSION_NAMESPACE(v1)
137
138} // namespace msgpack
139
140#endif // MSGPACK_V1_TYPE_CPP11_TIMESPEC_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
@ EXT
Definition: object_fwd_decl.hpp:42
Definition: adaptor_base.hpp:15
msgpack::object const & operator()(msgpack::object const &o, timespec &v) const
Definition: timespec.hpp:29
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const timespec &v) const
Definition: timespec.hpp:96
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const timespec &v) const
Definition: timespec.hpp:63
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