mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-25 02:06:05 +02:00
Removed code repeat.
This commit is contained in:
@@ -25,80 +25,191 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
|
|||||||
|
|
||||||
namespace adaptor {
|
namespace adaptor {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
typename T::time_point chrono_as(msgpack::object const& o) {
|
||||||
|
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
||||||
|
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
||||||
|
typename T::time_point tp;
|
||||||
|
switch(o.via.ext.size) {
|
||||||
|
case 4: {
|
||||||
|
uint32_t sec;
|
||||||
|
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
||||||
|
tp += std::chrono::seconds(sec);
|
||||||
|
} break;
|
||||||
|
case 8: {
|
||||||
|
uint64_t value;
|
||||||
|
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
||||||
|
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
||||||
|
uint64_t sec = value & 0x00000003ffffffffLL;
|
||||||
|
tp += std::chrono::duration_cast<typename T::duration>(
|
||||||
|
std::chrono::nanoseconds(nanosec));
|
||||||
|
tp += std::chrono::seconds(sec);
|
||||||
|
} break;
|
||||||
|
case 12: {
|
||||||
|
uint32_t nanosec;
|
||||||
|
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
||||||
|
int64_t sec;
|
||||||
|
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
||||||
|
tp += std::chrono::duration_cast<typename T::duration>(
|
||||||
|
std::chrono::nanoseconds(nanosec));
|
||||||
|
tp += std::chrono::seconds(sec);
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
throw msgpack::type_error();
|
||||||
|
}
|
||||||
|
return tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
msgpack::object const& chrono_convert(msgpack::object const& o, typename T::time_point& v) {
|
||||||
|
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
||||||
|
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
||||||
|
typename T::time_point tp;
|
||||||
|
switch(o.via.ext.size) {
|
||||||
|
case 4: {
|
||||||
|
uint32_t sec;
|
||||||
|
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
||||||
|
tp += std::chrono::seconds(sec);
|
||||||
|
v = tp;
|
||||||
|
} break;
|
||||||
|
case 8: {
|
||||||
|
uint64_t value;
|
||||||
|
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
||||||
|
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
||||||
|
uint64_t sec = value & 0x00000003ffffffffLL;
|
||||||
|
tp += std::chrono::duration_cast<typename T::duration>(
|
||||||
|
std::chrono::nanoseconds(nanosec));
|
||||||
|
tp += std::chrono::seconds(sec);
|
||||||
|
v = tp;
|
||||||
|
} break;
|
||||||
|
case 12: {
|
||||||
|
uint32_t nanosec;
|
||||||
|
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
||||||
|
int64_t sec;
|
||||||
|
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
||||||
|
tp += std::chrono::duration_cast<typename T::duration>(
|
||||||
|
std::chrono::nanoseconds(nanosec));
|
||||||
|
tp += std::chrono::seconds(sec);
|
||||||
|
v = tp;
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
throw msgpack::type_error();
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Stream>
|
||||||
|
msgpack::packer<Stream>& chrono_pack(msgpack::packer<Stream>& o, const typename T::time_point& v) {
|
||||||
|
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
||||||
|
|
||||||
|
int64_t nano_num =
|
||||||
|
T::duration::period::ratio::num *
|
||||||
|
(1000000000 / T::duration::period::ratio::den);
|
||||||
|
|
||||||
|
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
||||||
|
int64_t sec = 0;
|
||||||
|
if (nanosec < 0) {
|
||||||
|
nanosec = 1000000000 + nanosec;
|
||||||
|
--sec;
|
||||||
|
}
|
||||||
|
sec += count
|
||||||
|
* T::duration::period::ratio::num
|
||||||
|
/ T::duration::period::ratio::den;
|
||||||
|
if ((sec >> 34) == 0) {
|
||||||
|
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
||||||
|
if ((data64 & 0xffffffff00000000L) == 0) {
|
||||||
|
// timestamp 32
|
||||||
|
o.pack_ext(4, -1);
|
||||||
|
uint32_t data32 = static_cast<uint32_t>(data64);
|
||||||
|
char buf[4];
|
||||||
|
_msgpack_store32(buf, data32);
|
||||||
|
o.pack_ext_body(buf, 4);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// timestamp 64
|
||||||
|
o.pack_ext(8, -1);
|
||||||
|
char buf[8];
|
||||||
|
_msgpack_store64(buf, data64);
|
||||||
|
o.pack_ext_body(buf, 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// timestamp 96
|
||||||
|
o.pack_ext(12, -1);
|
||||||
|
char buf[12];
|
||||||
|
_msgpack_store32(&buf[0], static_cast<uint32_t>(nanosec));
|
||||||
|
_msgpack_store64(&buf[4], sec);
|
||||||
|
o.pack_ext_body(buf, 12);
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void chrono_object_with_zone(msgpack::object::with_zone& o, const typename T::time_point& v) {
|
||||||
|
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
||||||
|
|
||||||
|
int64_t nano_num =
|
||||||
|
T::duration::period::ratio::num *
|
||||||
|
(1000000000 / T::duration::period::ratio::den);
|
||||||
|
|
||||||
|
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
||||||
|
int64_t sec = 0;
|
||||||
|
if (nanosec < 0) {
|
||||||
|
nanosec = 1000000000 + nanosec;
|
||||||
|
--sec;
|
||||||
|
}
|
||||||
|
sec += count
|
||||||
|
* T::duration::period::ratio::num
|
||||||
|
/ T::duration::period::ratio::den;
|
||||||
|
if ((sec >> 34) == 0) {
|
||||||
|
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
||||||
|
if ((data64 & 0xffffffff00000000L) == 0) {
|
||||||
|
// timestamp 32
|
||||||
|
o.type = msgpack::type::EXT;
|
||||||
|
o.via.ext.size = 4;
|
||||||
|
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
||||||
|
p[0] = static_cast<char>(-1);
|
||||||
|
uint32_t data32 = static_cast<uint32_t>(data64);
|
||||||
|
_msgpack_store32(&p[1], data32);
|
||||||
|
o.via.ext.ptr = p;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// timestamp 64
|
||||||
|
o.type = msgpack::type::EXT;
|
||||||
|
o.via.ext.size = 8;
|
||||||
|
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
||||||
|
p[0] = static_cast<char>(-1);
|
||||||
|
_msgpack_store64(&p[1], data64);
|
||||||
|
o.via.ext.ptr = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// timestamp 96
|
||||||
|
o.type = msgpack::type::EXT;
|
||||||
|
o.via.ext.size = 12;
|
||||||
|
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
||||||
|
p[0] = static_cast<char>(-1);
|
||||||
|
_msgpack_store32(&p[1], static_cast<uint32_t>(nanosec));
|
||||||
|
_msgpack_store64(&p[1 + 4], sec);
|
||||||
|
o.via.ext.ptr = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct as<std::chrono::system_clock::time_point> {
|
struct as<std::chrono::system_clock::time_point> {
|
||||||
typename std::chrono::system_clock::time_point operator()(msgpack::object const& o) const {
|
typename std::chrono::system_clock::time_point operator()(msgpack::object const& o) const {
|
||||||
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
return detail::chrono_as<std::chrono::system_clock>(o);
|
||||||
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
|
||||||
std::chrono::system_clock::time_point tp;
|
|
||||||
switch(o.via.ext.size) {
|
|
||||||
case 4: {
|
|
||||||
uint32_t sec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
case 8: {
|
|
||||||
uint64_t value;
|
|
||||||
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
|
||||||
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
|
||||||
uint64_t sec = value & 0x00000003ffffffffLL;
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::system_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
case 12: {
|
|
||||||
uint32_t nanosec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
|
||||||
int64_t sec;
|
|
||||||
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::system_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
throw msgpack::type_error();
|
|
||||||
}
|
|
||||||
return tp;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct convert<std::chrono::system_clock::time_point> {
|
struct convert<std::chrono::system_clock::time_point> {
|
||||||
msgpack::object const& operator()(msgpack::object const& o, std::chrono::system_clock::time_point& v) const {
|
msgpack::object const& operator()(msgpack::object const& o, std::chrono::system_clock::time_point& v) const {
|
||||||
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
return detail::chrono_convert<std::chrono::system_clock>(o, v);
|
||||||
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
|
||||||
std::chrono::system_clock::time_point tp;
|
|
||||||
switch(o.via.ext.size) {
|
|
||||||
case 4: {
|
|
||||||
uint32_t sec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
case 8: {
|
|
||||||
uint64_t value;
|
|
||||||
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
|
||||||
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
|
||||||
uint64_t sec = value & 0x00000003ffffffffLL;
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::system_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
case 12: {
|
|
||||||
uint32_t nanosec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
|
||||||
int64_t sec;
|
|
||||||
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::system_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
throw msgpack::type_error();
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -106,101 +217,14 @@ template <>
|
|||||||
struct pack<std::chrono::system_clock::time_point> {
|
struct pack<std::chrono::system_clock::time_point> {
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::chrono::system_clock::time_point& v) const {
|
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::chrono::system_clock::time_point& v) const {
|
||||||
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
return detail::chrono_pack<std::chrono::system_clock>(o, v);
|
||||||
|
|
||||||
int64_t nano_num =
|
|
||||||
std::chrono::system_clock::duration::period::ratio::num *
|
|
||||||
(1000000000 / std::chrono::system_clock::duration::period::ratio::den);
|
|
||||||
|
|
||||||
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
|
||||||
int64_t sec = 0;
|
|
||||||
if (nanosec < 0) {
|
|
||||||
nanosec = 1000000000 + nanosec;
|
|
||||||
--sec;
|
|
||||||
}
|
|
||||||
sec += count
|
|
||||||
* std::chrono::system_clock::duration::period::ratio::num
|
|
||||||
/ std::chrono::system_clock::duration::period::ratio::den;
|
|
||||||
if ((sec >> 34) == 0) {
|
|
||||||
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
|
||||||
if ((data64 & 0xffffffff00000000L) == 0) {
|
|
||||||
// timestamp 32
|
|
||||||
o.pack_ext(4, -1);
|
|
||||||
uint32_t data32 = static_cast<uint32_t>(data64);
|
|
||||||
char buf[4];
|
|
||||||
_msgpack_store32(buf, data32);
|
|
||||||
o.pack_ext_body(buf, 4);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 64
|
|
||||||
o.pack_ext(8, -1);
|
|
||||||
char buf[8];
|
|
||||||
_msgpack_store64(buf, data64);
|
|
||||||
o.pack_ext_body(buf, 8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 96
|
|
||||||
o.pack_ext(12, -1);
|
|
||||||
char buf[12];
|
|
||||||
_msgpack_store32(&buf[0], static_cast<uint32_t>(nanosec));
|
|
||||||
_msgpack_store64(&buf[4], sec);
|
|
||||||
o.pack_ext_body(buf, 12);
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct object_with_zone<std::chrono::system_clock::time_point> {
|
struct object_with_zone<std::chrono::system_clock::time_point> {
|
||||||
void operator()(msgpack::object::with_zone& o, const std::chrono::system_clock::time_point& v) const {
|
void operator()(msgpack::object::with_zone& o, const std::chrono::system_clock::time_point& v) const {
|
||||||
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
detail::chrono_object_with_zone<std::chrono::system_clock>(o, v);
|
||||||
|
|
||||||
int64_t nano_num =
|
|
||||||
std::chrono::system_clock::duration::period::ratio::num *
|
|
||||||
(1000000000 / std::chrono::system_clock::duration::period::ratio::den);
|
|
||||||
|
|
||||||
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
|
||||||
int64_t sec = 0;
|
|
||||||
if (nanosec < 0) {
|
|
||||||
nanosec = 1000000000 + nanosec;
|
|
||||||
--sec;
|
|
||||||
}
|
|
||||||
sec += count
|
|
||||||
* std::chrono::system_clock::duration::period::ratio::num
|
|
||||||
/ std::chrono::system_clock::duration::period::ratio::den;
|
|
||||||
if ((sec >> 34) == 0) {
|
|
||||||
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
|
||||||
if ((data64 & 0xffffffff00000000L) == 0) {
|
|
||||||
// timestamp 32
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 4;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
uint32_t data32 = static_cast<uint32_t>(data64);
|
|
||||||
_msgpack_store32(&p[1], data32);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 64
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 8;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
_msgpack_store64(&p[1], data64);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 96
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 12;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
_msgpack_store32(&p[1], static_cast<uint32_t>(nanosec));
|
|
||||||
_msgpack_store64(&p[1 + 4], sec);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -208,77 +232,14 @@ struct object_with_zone<std::chrono::system_clock::time_point> {
|
|||||||
template <>
|
template <>
|
||||||
struct as<std::chrono::steady_clock::time_point> {
|
struct as<std::chrono::steady_clock::time_point> {
|
||||||
typename std::chrono::steady_clock::time_point operator()(msgpack::object const& o) const {
|
typename std::chrono::steady_clock::time_point operator()(msgpack::object const& o) const {
|
||||||
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
return detail::chrono_as<std::chrono::steady_clock>(o);
|
||||||
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
|
||||||
std::chrono::steady_clock::time_point tp;
|
|
||||||
switch(o.via.ext.size) {
|
|
||||||
case 4: {
|
|
||||||
uint32_t sec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
case 8: {
|
|
||||||
uint64_t value;
|
|
||||||
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
|
||||||
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
|
||||||
uint64_t sec = value & 0x00000003ffffffffLL;
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
case 12: {
|
|
||||||
uint32_t nanosec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
|
||||||
int64_t sec;
|
|
||||||
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
throw msgpack::type_error();
|
|
||||||
}
|
|
||||||
return tp;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct convert<std::chrono::steady_clock::time_point> {
|
struct convert<std::chrono::steady_clock::time_point> {
|
||||||
msgpack::object const& operator()(msgpack::object const& o, std::chrono::steady_clock::time_point& v) const {
|
msgpack::object const& operator()(msgpack::object const& o, std::chrono::steady_clock::time_point& v) const {
|
||||||
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
return detail::chrono_convert<std::chrono::steady_clock>(o, v);
|
||||||
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
|
||||||
std::chrono::steady_clock::time_point tp;
|
|
||||||
switch(o.via.ext.size) {
|
|
||||||
case 4: {
|
|
||||||
uint32_t sec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
case 8: {
|
|
||||||
uint64_t value;
|
|
||||||
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
|
||||||
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
|
||||||
uint64_t sec = value & 0x00000003ffffffffLL;
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
case 12: {
|
|
||||||
uint32_t nanosec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
|
||||||
int64_t sec;
|
|
||||||
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
throw msgpack::type_error();
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -286,101 +247,14 @@ template <>
|
|||||||
struct pack<std::chrono::steady_clock::time_point> {
|
struct pack<std::chrono::steady_clock::time_point> {
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::chrono::steady_clock::time_point& v) const {
|
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::chrono::steady_clock::time_point& v) const {
|
||||||
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
return detail::chrono_pack<std::chrono::steady_clock>(o, v);
|
||||||
|
|
||||||
int64_t nano_num =
|
|
||||||
std::chrono::steady_clock::duration::period::ratio::num *
|
|
||||||
(1000000000 / std::chrono::steady_clock::duration::period::ratio::den);
|
|
||||||
|
|
||||||
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
|
||||||
int64_t sec = 0;
|
|
||||||
if (nanosec < 0) {
|
|
||||||
nanosec = 1000000000 + nanosec;
|
|
||||||
--sec;
|
|
||||||
}
|
|
||||||
sec += count
|
|
||||||
* std::chrono::steady_clock::duration::period::ratio::num
|
|
||||||
/ std::chrono::steady_clock::duration::period::ratio::den;
|
|
||||||
if ((sec >> 34) == 0) {
|
|
||||||
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
|
||||||
if ((data64 & 0xffffffff00000000L) == 0) {
|
|
||||||
// timestamp 32
|
|
||||||
o.pack_ext(4, -1);
|
|
||||||
uint32_t data32 = static_cast<uint32_t>(data64);
|
|
||||||
char buf[4];
|
|
||||||
_msgpack_store32(buf, data32);
|
|
||||||
o.pack_ext_body(buf, 4);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 64
|
|
||||||
o.pack_ext(8, -1);
|
|
||||||
char buf[8];
|
|
||||||
_msgpack_store64(buf, data64);
|
|
||||||
o.pack_ext_body(buf, 8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 96
|
|
||||||
o.pack_ext(12, -1);
|
|
||||||
char buf[12];
|
|
||||||
_msgpack_store32(&buf[0], static_cast<uint32_t>(nanosec));
|
|
||||||
_msgpack_store64(&buf[4], sec);
|
|
||||||
o.pack_ext_body(buf, 12);
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct object_with_zone<std::chrono::steady_clock::time_point> {
|
struct object_with_zone<std::chrono::steady_clock::time_point> {
|
||||||
void operator()(msgpack::object::with_zone& o, const std::chrono::steady_clock::time_point& v) const {
|
void operator()(msgpack::object::with_zone& o, const std::chrono::steady_clock::time_point& v) const {
|
||||||
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
detail::chrono_object_with_zone<std::chrono::steady_clock>(o, v);
|
||||||
|
|
||||||
int64_t nano_num =
|
|
||||||
std::chrono::steady_clock::duration::period::ratio::num *
|
|
||||||
(1000000000 / std::chrono::steady_clock::duration::period::ratio::den);
|
|
||||||
|
|
||||||
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
|
||||||
int64_t sec = 0;
|
|
||||||
if (nanosec < 0) {
|
|
||||||
nanosec = 1000000000 + nanosec;
|
|
||||||
--sec;
|
|
||||||
}
|
|
||||||
sec += count
|
|
||||||
* std::chrono::steady_clock::duration::period::ratio::num
|
|
||||||
/ std::chrono::steady_clock::duration::period::ratio::den;
|
|
||||||
if ((sec >> 34) == 0) {
|
|
||||||
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
|
||||||
if ((data64 & 0xffffffff00000000L) == 0) {
|
|
||||||
// timestamp 32
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 4;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
uint32_t data32 = static_cast<uint32_t>(data64);
|
|
||||||
_msgpack_store32(&p[1], data32);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 64
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 8;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
_msgpack_store64(&p[1], data64);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 96
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 12;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
_msgpack_store32(&p[1], static_cast<uint32_t>(nanosec));
|
|
||||||
_msgpack_store64(&p[1 + 4], sec);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -396,37 +270,7 @@ struct as<
|
|||||||
>::type
|
>::type
|
||||||
> {
|
> {
|
||||||
typename std::chrono::high_resolution_clock::time_point operator()(msgpack::object const& o) const {
|
typename std::chrono::high_resolution_clock::time_point operator()(msgpack::object const& o) const {
|
||||||
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
return detail::chrono_as<std::chrono::high_resolution_clock>(o);
|
||||||
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
|
||||||
std::chrono::high_resolution_clock::time_point tp;
|
|
||||||
switch(o.via.ext.size) {
|
|
||||||
case 4: {
|
|
||||||
uint32_t sec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
case 8: {
|
|
||||||
uint64_t value;
|
|
||||||
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
|
||||||
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
|
||||||
uint64_t sec = value & 0x00000003ffffffffLL;
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::high_resolution_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
case 12: {
|
|
||||||
uint32_t nanosec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
|
||||||
int64_t sec;
|
|
||||||
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::high_resolution_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
throw msgpack::type_error();
|
|
||||||
}
|
|
||||||
return tp;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -441,40 +285,7 @@ struct convert<
|
|||||||
>::type
|
>::type
|
||||||
> {
|
> {
|
||||||
msgpack::object const& operator()(msgpack::object const& o, std::chrono::high_resolution_clock::time_point& v) const {
|
msgpack::object const& operator()(msgpack::object const& o, std::chrono::high_resolution_clock::time_point& v) const {
|
||||||
if(o.type != msgpack::type::EXT) { throw msgpack::type_error(); }
|
return detail::chrono_convert<std::chrono::high_resolution_clock>(o, v);
|
||||||
if(o.via.ext.type() != -1) { throw msgpack::type_error(); }
|
|
||||||
std::chrono::high_resolution_clock::time_point tp;
|
|
||||||
switch(o.via.ext.size) {
|
|
||||||
case 4: {
|
|
||||||
uint32_t sec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &sec);
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
case 8: {
|
|
||||||
uint64_t value;
|
|
||||||
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
|
|
||||||
uint32_t nanosec = static_cast<uint32_t>(value >> 34);
|
|
||||||
uint64_t sec = value & 0x00000003ffffffffLL;
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::high_resolution_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
case 12: {
|
|
||||||
uint32_t nanosec;
|
|
||||||
_msgpack_load32(uint32_t, o.via.ext.data(), &nanosec);
|
|
||||||
int64_t sec;
|
|
||||||
_msgpack_load64(int64_t, o.via.ext.data() + 4, &sec);
|
|
||||||
tp += std::chrono::duration_cast<std::chrono::high_resolution_clock::duration>(
|
|
||||||
std::chrono::nanoseconds(nanosec));
|
|
||||||
tp += std::chrono::seconds(sec);
|
|
||||||
v = tp;
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
throw msgpack::type_error();
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -490,48 +301,7 @@ struct pack<
|
|||||||
> {
|
> {
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::chrono::high_resolution_clock::time_point& v) const {
|
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::chrono::high_resolution_clock::time_point& v) const {
|
||||||
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
return detail::chrono_pack<std::chrono::high_resolution_clock>(o, v);
|
||||||
|
|
||||||
int64_t nano_num =
|
|
||||||
std::chrono::high_resolution_clock::duration::period::ratio::num *
|
|
||||||
(1000000000 / std::chrono::high_resolution_clock::duration::period::ratio::den);
|
|
||||||
|
|
||||||
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
|
||||||
int64_t sec = 0;
|
|
||||||
if (nanosec < 0) {
|
|
||||||
nanosec = 1000000000 + nanosec;
|
|
||||||
--sec;
|
|
||||||
}
|
|
||||||
sec += count
|
|
||||||
* std::chrono::high_resolution_clock::duration::period::ratio::num
|
|
||||||
/ std::chrono::high_resolution_clock::duration::period::ratio::den;
|
|
||||||
if ((sec >> 34) == 0) {
|
|
||||||
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
|
||||||
if ((data64 & 0xffffffff00000000L) == 0) {
|
|
||||||
// timestamp 32
|
|
||||||
o.pack_ext(4, -1);
|
|
||||||
uint32_t data32 = static_cast<uint32_t>(data64);
|
|
||||||
char buf[4];
|
|
||||||
_msgpack_store32(buf, data32);
|
|
||||||
o.pack_ext_body(buf, 4);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 64
|
|
||||||
o.pack_ext(8, -1);
|
|
||||||
char buf[8];
|
|
||||||
_msgpack_store64(buf, data64);
|
|
||||||
o.pack_ext_body(buf, 8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 96
|
|
||||||
o.pack_ext(12, -1);
|
|
||||||
char buf[12];
|
|
||||||
_msgpack_store32(&buf[0], static_cast<uint32_t>(nanosec));
|
|
||||||
_msgpack_store64(&buf[4], sec);
|
|
||||||
o.pack_ext_body(buf, 12);
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -546,53 +316,7 @@ struct object_with_zone<
|
|||||||
>::type
|
>::type
|
||||||
> {
|
> {
|
||||||
void operator()(msgpack::object::with_zone& o, const std::chrono::high_resolution_clock::time_point& v) const {
|
void operator()(msgpack::object::with_zone& o, const std::chrono::high_resolution_clock::time_point& v) const {
|
||||||
int64_t count = static_cast<int64_t>(v.time_since_epoch().count());
|
detail::chrono_object_with_zone<std::chrono::high_resolution_clock>(o, v);
|
||||||
|
|
||||||
int64_t nano_num =
|
|
||||||
std::chrono::high_resolution_clock::duration::period::ratio::num *
|
|
||||||
(1000000000 / std::chrono::high_resolution_clock::duration::period::ratio::den);
|
|
||||||
|
|
||||||
int64_t nanosec = count % (1000000000 / nano_num) * nano_num;
|
|
||||||
int64_t sec = 0;
|
|
||||||
if (nanosec < 0) {
|
|
||||||
nanosec = 1000000000 + nanosec;
|
|
||||||
--sec;
|
|
||||||
}
|
|
||||||
sec += count
|
|
||||||
* std::chrono::high_resolution_clock::duration::period::ratio::num
|
|
||||||
/ std::chrono::high_resolution_clock::duration::period::ratio::den;
|
|
||||||
if ((sec >> 34) == 0) {
|
|
||||||
uint64_t data64 = (static_cast<uint64_t>(nanosec) << 34) | static_cast<uint64_t>(sec);
|
|
||||||
if ((data64 & 0xffffffff00000000L) == 0) {
|
|
||||||
// timestamp 32
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 4;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
uint32_t data32 = static_cast<uint32_t>(data64);
|
|
||||||
_msgpack_store32(&p[1], data32);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 64
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 8;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
_msgpack_store64(&p[1], data64);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// timestamp 96
|
|
||||||
o.type = msgpack::type::EXT;
|
|
||||||
o.via.ext.size = 12;
|
|
||||||
char* p = static_cast<char*>(o.zone.allocate_no_align(o.via.ext.size + 1));
|
|
||||||
p[0] = static_cast<char>(-1);
|
|
||||||
_msgpack_store32(&p[1], static_cast<uint32_t>(nanosec));
|
|
||||||
_msgpack_store64(&p[1 + 4], sec);
|
|
||||||
o.via.ext.ptr = p;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user