#ifndef BOOST_EX_CHRONO_DURATION_HPP
#define BOOST_EX_CHRONO_DURATION_HPP
#include "static_assert.hpp"
#include <boost/config.hpp>
#include <climits>
#include <limits>
#include <boost/mpl/logical.hpp>
#include <boost/ratio/ratio.hpp>
#include <boost/ratio/detail/is_ratio.hpp>
#include <boost/type_traits/common_type.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/type_traits/is_floating_point.hpp>
#include <boost/type_traits/is_unsigned.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/cstdint.hpp>
#if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__)
#else
#include <boost/utility/enable_if.hpp>
#endif
#include <boost/detail/workaround.hpp>
#include <boost/integer_traits.hpp>
#if !defined(BOOST_NO_CXX11_STATIC_ASSERT) || !defined(BOOST_EX_CHRONO_USES_MPL_ASSERT)
#define BOOST_EX_CHRONO_A_DURATION_REPRESENTATION_CAN_NOT_BE_A_DURATION "A duration representation can not be a duration"
#define BOOST_EX_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_DURATION_MUST_BE_A_STD_RATIO "Second template parameter of duration must be a boost::ratio"
#define BOOST_EX_CHRONO_DURATION_PERIOD_MUST_BE_POSITIVE "duration period must be positive"
#define BOOST_EX_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_TIME_POINT_MUST_BE_A_BOOST_EX_CHRONO_DURATION "Second template parameter of time_point must be a boost_ex::chrono::duration"
#endif
namespace boost_ex {
using boost::ratio;
namespace chrono {
template <class Rep, class Period = ratio<1> >
class duration;
namespace detail
{
template <class T>
struct is_duration
: boost::false_type {};
template <class Rep, class Period>
struct is_duration<duration<Rep, Period> >
: boost::true_type {};
template <class Duration, class Rep, bool = is_duration<Rep>::value>
struct duration_divide_result
{
};
template <class Duration, class Rep2,
bool = (
(boost::is_convertible<typename Duration::rep,
typename boost::common_type<typename Duration::rep, Rep2>::type>::value)
&& (boost::is_convertible<Rep2,
typename boost::common_type<typename Duration::rep, Rep2>::type>::value)
)
>
struct duration_divide_imp
{
};
template <class Rep1, class Period, class Rep2>
struct duration_divide_imp<duration<Rep1, Period>, Rep2, true>
{
typedef duration<typename boost::common_type<Rep1, Rep2>::type, Period> type;
};
template <class Rep1, class Period, class Rep2>
struct duration_divide_result<duration<Rep1, Period>, Rep2, false>
: duration_divide_imp<duration<Rep1, Period>, Rep2>
{
};
template <class Rep, class Duration, bool = is_duration<Rep>::value>
struct duration_divide_result2
{
};
template <class Rep, class Duration,
bool = (
(boost::is_convertible<typename Duration::rep,
typename boost::common_type<typename Duration::rep, Rep>::type>::value)
&& (boost::is_convertible<Rep,
typename boost::common_type<typename Duration::rep, Rep>::type>::value)
)
>
struct duration_divide_imp2
{
};
template <class Rep1, class Rep2, class Period >
struct duration_divide_imp2<Rep1, duration<Rep2, Period>, true>
{
typedef double type;
};
template <class Rep1, class Rep2, class Period >
struct duration_divide_result2<Rep1, duration<Rep2, Period>, false>
: duration_divide_imp2<Rep1, duration<Rep2, Period> >
{
};
template <class Duration, class Rep, bool = is_duration<Rep>::value>
struct duration_modulo_result
{
};
template <class Duration, class Rep2,
bool = (
boost::is_convertible<Rep2,
typename boost::common_type<typename Duration::rep, Rep2>::type>::value
)
>
struct duration_modulo_imp
{
};
template <class Rep1, class Period, class Rep2>
struct duration_modulo_imp<duration<Rep1, Period>, Rep2, true>
{
typedef duration<typename boost::common_type<Rep1, Rep2>::type, Period> type;
};
template <class Rep1, class Period, class Rep2>
struct duration_modulo_result<duration<Rep1, Period>, Rep2, false>
: duration_modulo_imp<duration<Rep1, Period>, Rep2>
{
};
} } }
namespace boost {
template <class Rep1, class Period1, class Rep2, class Period2>
struct common_type<boost_ex::chrono::duration<Rep1, Period1>,
boost_ex::chrono::duration<Rep2, Period2> >;
}
namespace boost_ex {
namespace chrono {
template <class Rep> struct treat_as_floating_point;
template <class Rep> struct duration_values;
typedef duration<boost::int_least64_t, boost::nano> nanoseconds; typedef duration<boost::int_least64_t, boost::micro> microseconds; typedef duration<boost::int_least64_t, boost::milli> milliseconds; typedef duration<boost::int_least64_t> seconds; typedef duration<boost::int_least32_t, ratio< 60> > minutes; typedef duration<boost::int_least32_t, ratio<3600> > hours;
namespace detail
{
template <class FromDuration, class ToDuration,
class Period = typename boost::ratio_divide<typename FromDuration::period,
typename ToDuration::period>::type,
bool = Period::num == 1,
bool = Period::den == 1>
struct duration_cast;
template <class FromDuration, class ToDuration, class Period>
struct duration_cast<FromDuration, ToDuration, Period, true, true>
{
ToDuration operator()(const FromDuration& fd) const
{
return ToDuration(static_cast<typename ToDuration::rep>(fd.count()));
}
};
template <class FromDuration, class ToDuration, class Period>
struct duration_cast<FromDuration, ToDuration, Period, true, false>
{
ToDuration operator()(const FromDuration& fd) const
{
typedef typename boost::common_type<
typename ToDuration::rep,
typename FromDuration::rep,
boost::intmax_t>::type C;
return ToDuration(static_cast<typename ToDuration::rep>(
static_cast<C>(fd.count()) / static_cast<C>(Period::den)));
}
};
template <class FromDuration, class ToDuration, class Period>
struct duration_cast<FromDuration, ToDuration, Period, false, true>
{
ToDuration operator()(const FromDuration& fd) const
{
typedef typename boost::common_type<
typename ToDuration::rep,
typename FromDuration::rep,
boost::intmax_t>::type C;
return ToDuration(static_cast<typename ToDuration::rep>(
static_cast<C>(fd.count()) * static_cast<C>(Period::num)));
}
};
template <class FromDuration, class ToDuration, class Period>
struct duration_cast<FromDuration, ToDuration, Period, false, false>
{
ToDuration operator()(const FromDuration& fd) const
{
typedef typename boost::common_type<
typename ToDuration::rep,
typename FromDuration::rep,
boost::intmax_t>::type C;
return ToDuration(static_cast<typename ToDuration::rep>(
static_cast<C>(fd.count()) * static_cast<C>(Period::num)
/ static_cast<C>(Period::den)));
}
};
}
template <class Rep>
struct treat_as_floating_point : boost::is_floating_point<Rep> {};
namespace detail {
template <class T, bool = boost::is_arithmetic<T>::value>
struct chrono_numeric_limits {
static T lowest() throw() {return (std::numeric_limits<T>::min) ();}
};
template <class T>
struct chrono_numeric_limits<T,true> {
static T lowest() throw() {return (std::numeric_limits<T>::min) ();}
};
template <>
struct chrono_numeric_limits<float,true> {
static float lowest() throw() {return -(std::numeric_limits<float>::max) ();}
};
template <>
struct chrono_numeric_limits<double,true> {
static double lowest() throw() {return -(std::numeric_limits<double>::max) ();}
};
template <>
struct chrono_numeric_limits<long double,true> {
static long double lowest() throw() {return -(std::numeric_limits<long double>::max)();}
};
template <class T>
struct numeric_limits : chrono_numeric_limits<typename boost::remove_cv<T>::type> {};
}
template <class Rep>
struct duration_values
{
static Rep zero() {return Rep(0);}
static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (std::numeric_limits<Rep>::max)();}
static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {return detail::numeric_limits<Rep>::lowest();}
};
} }
namespace boost {
template <class Rep1, class Period1, class Rep2, class Period2>
struct common_type<boost_ex::chrono::duration<Rep1, Period1>,
boost_ex::chrono::duration<Rep2, Period2> >
{
typedef boost_ex::chrono::duration<typename common_type<Rep1, Rep2>::type,
typename boost::ratio_gcd<Period1, Period2>::type> type;
};
}
namespace boost_ex {
namespace chrono {
template <class Rep, class Period>
class duration
{
BOOST_EX_CHRONO_STATIC_ASSERT(!boost_ex::chrono::detail::is_duration<Rep>::value, BOOST_EX_CHRONO_A_DURATION_REPRESENTATION_CAN_NOT_BE_A_DURATION, ());
BOOST_EX_CHRONO_STATIC_ASSERT(boost::ratio_detail::is_ratio<Period>::value, BOOST_EX_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_DURATION_MUST_BE_A_STD_RATIO, ());
BOOST_EX_CHRONO_STATIC_ASSERT(Period::num>0, BOOST_EX_CHRONO_DURATION_PERIOD_MUST_BE_POSITIVE, ());
public:
typedef Rep rep;
typedef Period period;
private:
rep rep_;
public:
duration() { } template <class Rep2>
explicit duration(const Rep2& r
#if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__)
#else
, typename boost::enable_if <
boost::mpl::and_ <
boost::is_convertible<Rep2, rep>,
boost::mpl::or_ <
treat_as_floating_point<rep>,
boost::mpl::and_ <
boost::mpl::not_ < treat_as_floating_point<rep> >,
boost::mpl::not_ < treat_as_floating_point<Rep2> >
>
>
>
>::type* = 0
#endif
)
: rep_(r) { }
~duration() {} duration(const duration& rhs) : rep_(rhs.rep_) {} duration& operator=(const duration& rhs) {
if (&rhs != this) rep_= rhs.rep_;
return *this;
}
template <class Rep2, class Period2>
duration(const duration<Rep2, Period2>& d
#if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__)
#else
, typename boost::enable_if <
boost::mpl::or_ <
treat_as_floating_point<rep>,
boost::mpl::and_ <
boost::mpl::bool_ < boost::ratio_divide<Period2, period>::type::den == 1>,
boost::mpl::not_ < treat_as_floating_point<Rep2> >
>
>
>::type* = 0
#endif
)
: rep_(chrono::detail::duration_cast<duration<Rep2, Period2>, duration>()(d).count()) {}
rep count() const {return rep_;}
duration operator+() const {return *this;}
duration operator-() const {return duration(-rep_);}
duration& operator++() {++rep_; return *this;}
duration operator++(int) {return duration(rep_++);}
duration& operator--() {--rep_; return *this;}
duration operator--(int) {return duration(rep_--);}
duration& operator+=(const duration& d) {rep_ += d.count(); return *this;}
duration& operator-=(const duration& d) {rep_ -= d.count(); return *this;}
duration& operator*=(const rep& rhs) {rep_ *= rhs; return *this;}
duration& operator/=(const rep& rhs) {rep_ /= rhs; return *this;}
duration& operator%=(const rep& rhs) {rep_ %= rhs; return *this;}
duration& operator%=(const duration& rhs) {rep_ %= rhs.count(); return *this;};
static duration zero() {return duration(duration_values<rep>::zero());}
static duration min BOOST_PREVENT_MACRO_SUBSTITUTION () {return duration((duration_values<rep>::min)());}
static duration max BOOST_PREVENT_MACRO_SUBSTITUTION () {return duration((duration_values<rep>::max)());}
};
template <class Rep1, class Period1, class Rep2, class Period2>
inline
typename boost::common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
typename boost::common_type<duration<Rep1, Period1>,
duration<Rep2, Period2> >::type result = lhs;
result += rhs;
return result;
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
typename boost::common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
typename boost::common_type<duration<Rep1, Period1>,
duration<Rep2, Period2> >::type result = lhs;
result -= rhs;
return result;
}
template <class Rep1, class Period, class Rep2>
inline
#if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__)
duration<typename boost::common_type<Rep1, Rep2>::type, Period>
#else
typename boost::enable_if <
boost::mpl::and_ <
boost::is_convertible<Rep1, typename boost::common_type<Rep1, Rep2>::type>,
boost::is_convertible<Rep2, typename boost::common_type<Rep1, Rep2>::type>
>,
duration<typename boost::common_type<Rep1, Rep2>::type, Period>
>::type
#endif
operator*(const duration<Rep1, Period>& d, const Rep2& s)
{
typedef typename boost::common_type<Rep1, Rep2>::type CR;
duration<CR, Period> r = d;
r *= static_cast<CR>(s);
return r;
}
template <class Rep1, class Period, class Rep2>
inline
#if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__)
duration<typename boost::common_type<Rep1, Rep2>::type, Period>
#else
typename boost::enable_if <
boost::mpl::and_ <
boost::is_convertible<Rep1, typename boost::common_type<Rep1, Rep2>::type>,
boost::is_convertible<Rep2, typename boost::common_type<Rep1, Rep2>::type>
>,
duration<typename boost::common_type<Rep1, Rep2>::type, Period>
>::type
#endif
operator*(const Rep1& s, const duration<Rep2, Period>& d)
{
return d * s;
}
template <class Rep1, class Period, class Rep2>
inline
typename boost::disable_if <boost_ex::chrono::detail::is_duration<Rep2>,
typename boost_ex::chrono::detail::duration_divide_result<duration<Rep1, Period>, Rep2>::type
>::type
operator/(const duration<Rep1, Period>& d, const Rep2& s)
{
typedef typename boost::common_type<Rep1, Rep2>::type CR;
duration<CR, Period> r = d;
r /= static_cast<CR>(s);
return r;
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
typename boost::common_type<Rep1, Rep2>::type
operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
typedef typename boost::common_type<duration<Rep1, Period1>,
duration<Rep2, Period2> >::type CD;
return CD(lhs).count() / CD(rhs).count();
}
template <class Rep1, class Rep2, class Period>
inline
typename boost::disable_if <boost_ex::chrono::detail::is_duration<Rep1>,
typename boost_ex::chrono::detail::duration_divide_result2<Rep1, duration<Rep2, Period> >::type
>::type
operator/(const Rep1& s, const duration<Rep2, Period>& d)
{
typedef typename boost::common_type<Rep1, Rep2>::type CR;
duration<CR, Period> r = d;
return static_cast<CR>(s)/r.count();
}
template <class Rep1, class Period, class Rep2>
typename boost::disable_if <boost_ex::chrono::detail::is_duration<Rep2>,
typename boost_ex::chrono::detail::duration_modulo_result<duration<Rep1, Period>, Rep2>::type
>::type
operator%(const duration<Rep1, Period>& d, const Rep2& s) {
typedef typename boost::common_type<Rep1, Rep2>::type CR;
duration<CR, Period> r = d;
r %= static_cast<CR>(s);
return r;
}
template <class Rep1, class Period1, class Rep2, class Period2>
typename boost::common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
operator%(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs) {
typedef typename boost::common_type<duration<Rep1, Period1>,
duration<Rep2, Period2> >::type CD;
CD r(lhs);
r%=CD(rhs);
return r;
}
namespace detail
{
template <class LhsDuration, class RhsDuration>
struct duration_eq
{
bool operator()(const LhsDuration& lhs, const RhsDuration& rhs)
{
typedef typename boost::common_type<LhsDuration, RhsDuration>::type CD;
return CD(lhs).count() == CD(rhs).count();
}
};
template <class LhsDuration>
struct duration_eq<LhsDuration, LhsDuration>
{
bool operator()(const LhsDuration& lhs, const LhsDuration& rhs)
{return lhs.count() == rhs.count();}
};
template <class LhsDuration, class RhsDuration>
struct duration_lt
{
bool operator()(const LhsDuration& lhs, const RhsDuration& rhs)
{
typedef typename boost::common_type<LhsDuration, RhsDuration>::type CD;
return CD(lhs).count() < CD(rhs).count();
}
};
template <class LhsDuration>
struct duration_lt<LhsDuration, LhsDuration>
{
bool operator()(const LhsDuration& lhs, const LhsDuration& rhs)
{return lhs.count() < rhs.count();}
};
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
bool
operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
return boost_ex::chrono::detail::duration_eq<duration<Rep1, Period1>, duration<Rep2, Period2> >()(lhs, rhs);
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
bool
operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
return !(lhs == rhs);
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
bool
operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
return boost_ex::chrono::detail::duration_lt<duration<Rep1, Period1>, duration<Rep2, Period2> >()(lhs, rhs);
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
bool
operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
return rhs < lhs;
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
bool
operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
return !(rhs < lhs);
}
template <class Rep1, class Period1, class Rep2, class Period2>
inline
bool
operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
{
return !(lhs < rhs);
}
template <class ToDuration, class Rep, class Period>
inline
#if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__)
ToDuration
#else
typename boost::enable_if <boost_ex::chrono::detail::is_duration<ToDuration>, ToDuration>::type
#endif
duration_cast(const duration<Rep, Period>& fd)
{
return boost_ex::chrono::detail::duration_cast<duration<Rep, Period>, ToDuration>()(fd);
}
}
}
#endif