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