Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get rid of boost in chrono.hpp #1006

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 66 additions & 13 deletions include/msgpack/v1/adaptor/cpp11/chrono.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,60 @@

#include <chrono>

#include <boost/numeric/conversion/cast.hpp>
//#include <boost/numeric/conversion/cast.hpp>

template<typename Dst, typename Src>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline Dst numeric_cast(Src value)
{
typedef std::numeric_limits<Dst> DstLim;
typedef std::numeric_limits<Src> SrcLim;

const bool positive_overflow_possible = DstLim::max() < SrcLim::max();
const bool negative_overflow_possible =
SrcLim::is_signed
or
(DstLim::lowest() > SrcLim::lowest());

// unsigned <-- unsigned
if((not DstLim::is_signed) and (not SrcLim::is_signed)) {
if(positive_overflow_possible and (value > DstLim::max())) {
throw std::overflow_error(__PRETTY_FUNCTION__ +
std::string(": positive overflow"));
}
}
// unsigned <-- signed
else if((not DstLim::is_signed) and SrcLim::is_signed) {
if(positive_overflow_possible and (value > DstLim::max())) {
throw std::overflow_error(__PRETTY_FUNCTION__ +
std::string(": positive overflow"));
}
else if(negative_overflow_possible and (value < 0)) {
throw std::overflow_error(__PRETTY_FUNCTION__ +
std::string(": negative overflow"));
}

}
// signed <-- unsigned
else if(DstLim::is_signed and (not SrcLim::is_signed)) {
if(positive_overflow_possible and (value > DstLim::max())) {
throw std::overflow_error(__PRETTY_FUNCTION__ +
std::string(": positive overflow"));
}
}
// signed <-- signed
else if(DstLim::is_signed and SrcLim::is_signed) {
if(positive_overflow_possible and (value > DstLim::max())) {
throw std::overflow_error(__PRETTY_FUNCTION__ +
std::string(": positive overflow"));
} else if(negative_overflow_possible and (value < DstLim::lowest())) {
throw std::overflow_error(__PRETTY_FUNCTION__ +
std::string(": negative overflow"));
}
}

// limits have been checked, therefore safe to cast
return static_cast<Dst>(value);
}

namespace msgpack {

Expand All @@ -42,7 +95,7 @@ struct as<std::chrono::time_point<Clock, Duration>> {
case 8: {
uint64_t value;
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
uint32_t nanosec = boost::numeric_cast<uint32_t>(value >> 34);
uint32_t nanosec = numeric_cast<uint32_t>(value >> 34);
uint64_t sec = value & 0x00000003ffffffffLL;
tp += std::chrono::duration_cast<Duration>(
std::chrono::nanoseconds(nanosec));
Expand All @@ -66,7 +119,7 @@ struct as<std::chrono::time_point<Clock, Duration>> {
else {
++sec;
tp += std::chrono::seconds(sec);
int64_t ns = boost::numeric_cast<int64_t>(nanosec) - 1000000000L;
int64_t ns = numeric_cast<int64_t>(nanosec) - 1000000000L;
tp += std::chrono::duration_cast<Duration>(
std::chrono::nanoseconds(ns));
}
Expand Down Expand Up @@ -95,7 +148,7 @@ struct convert<std::chrono::time_point<Clock, Duration>> {
case 8: {
uint64_t value;
_msgpack_load64(uint64_t, o.via.ext.data(), &value);
uint32_t nanosec = boost::numeric_cast<uint32_t>(value >> 34);
uint32_t nanosec = numeric_cast<uint32_t>(value >> 34);
uint64_t sec = value & 0x00000003ffffffffLL;
tp += std::chrono::duration_cast<Duration>(
std::chrono::nanoseconds(nanosec));
Expand All @@ -120,7 +173,7 @@ struct convert<std::chrono::time_point<Clock, Duration>> {
else {
++sec;
tp += std::chrono::seconds(sec);
int64_t ns = boost::numeric_cast<int64_t>(nanosec) - 1000000000L;
int64_t ns = numeric_cast<int64_t>(nanosec) - 1000000000L;
tp += std::chrono::duration_cast<Duration>(
std::chrono::nanoseconds(ns));
}
Expand All @@ -139,7 +192,7 @@ template <typename Clock, typename Duration>
struct pack<std::chrono::time_point<Clock, Duration>> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, std::chrono::time_point<Clock, Duration> const& v) const {
int64_t count = boost::numeric_cast<int64_t>(v.time_since_epoch().count());
int64_t count = numeric_cast<int64_t>(v.time_since_epoch().count());
int64_t nano_num =
Duration::period::ratio::num *
(1000000000L / Duration::period::ratio::den);
Expand All @@ -155,11 +208,11 @@ struct pack<std::chrono::time_point<Clock, Duration>> {
/ Duration::period::ratio::den;

if ((sec >> 34) == 0) {
uint64_t data64 = (boost::numeric_cast<uint64_t>(nanosec) << 34) | boost::numeric_cast<uint64_t>(sec);
uint64_t data64 = (numeric_cast<uint64_t>(nanosec) << 34) | numeric_cast<uint64_t>(sec);
if ((data64 & 0xffffffff00000000L) == 0) {
// timestamp 32
o.pack_ext(4, -1);
uint32_t data32 = boost::numeric_cast<uint32_t>(data64);
uint32_t data32 = numeric_cast<uint32_t>(data64);
char buf[4];
_msgpack_store32(buf, data32);
o.pack_ext_body(buf, 4);
Expand All @@ -178,7 +231,7 @@ struct pack<std::chrono::time_point<Clock, Duration>> {
char buf[12];


_msgpack_store32(&buf[0], boost::numeric_cast<uint32_t>(nanosec));
_msgpack_store32(&buf[0], numeric_cast<uint32_t>(nanosec));
_msgpack_store64(&buf[4], sec);
o.pack_ext_body(buf, 12);
}
Expand All @@ -189,7 +242,7 @@ struct pack<std::chrono::time_point<Clock, Duration>> {
template <typename Clock, typename Duration>
struct object_with_zone<std::chrono::time_point<Clock, Duration>> {
void operator()(msgpack::object::with_zone& o, const std::chrono::time_point<Clock, Duration>& v) const {
int64_t count = boost::numeric_cast<int64_t>(v.time_since_epoch().count());
int64_t count = numeric_cast<int64_t>(v.time_since_epoch().count());

int64_t nano_num =
Duration::period::ratio::num *
Expand All @@ -205,14 +258,14 @@ struct object_with_zone<std::chrono::time_point<Clock, Duration>> {
* Duration::period::ratio::num
/ Duration::period::ratio::den;
if ((sec >> 34) == 0) {
uint64_t data64 = (boost::numeric_cast<uint64_t>(nanosec) << 34) | boost::numeric_cast<uint64_t>(sec);
uint64_t data64 = (numeric_cast<uint64_t>(nanosec) << 34) | numeric_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 = boost::numeric_cast<uint32_t>(data64);
uint32_t data32 = numeric_cast<uint32_t>(data64);
_msgpack_store32(&p[1], data32);
o.via.ext.ptr = p;
}
Expand All @@ -232,7 +285,7 @@ struct object_with_zone<std::chrono::time_point<Clock, Duration>> {
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], boost::numeric_cast<uint32_t>(nanosec));
_msgpack_store32(&p[1], numeric_cast<uint32_t>(nanosec));
_msgpack_store64(&p[1 + 4], sec);
o.via.ext.ptr = p;
}
Expand Down