// duration.hpp --------------------------------------------------------------// // Copyright 2008 Howard Hinnant // Copyright 2008 Beman Dawes // Copyright 2009-2010 Vicente J. Botet Escriba // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt /* This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. Many thanks to Howard for making his code available under the Boost license. The original code was modified to conform to Boost conventions and to section 20.9 Time utilities [time] of the C++ committee's working paper N2798. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. time2_demo contained this comment: Much thanks to Andrei Alexandrescu, Walter Brown, Peter Dimov, Jeff Garland, Terry Golubiewski, Daniel Krugler, Anthony Williams. */ #ifndef BOOST_EX_CHRONO_DURATION_HPP #define BOOST_EX_CHRONO_DURATION_HPP #include "config.hpp" #include "static_assert.hpp" //~ #include #include #include #include #include #include #include #include #include #include #include #if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__) #else #include #endif #include #include #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 //----------------------------------------------------------------------------// // // // 20.9 Time utilities [time] // // synopsis // // // //----------------------------------------------------------------------------// namespace boost_ex { using boost::ratio; namespace chrono { template > class duration; namespace detail { template struct is_duration : boost::false_type {}; template struct is_duration > : boost::true_type {}; //template // struct is_duration // : is_duration::type> {}; template ::value> struct duration_divide_result { }; template ::type>::value) && (boost::is_convertible::type>::value) ) > struct duration_divide_imp { }; template struct duration_divide_imp, Rep2, true> { typedef duration::type, Period> type; }; template struct duration_divide_result, Rep2, false> : duration_divide_imp, Rep2> { }; /// template ::value> struct duration_divide_result2 { }; template ::type>::value) && (boost::is_convertible::type>::value) ) > struct duration_divide_imp2 { }; template struct duration_divide_imp2, true> { //typedef typename boost::common_type::type type; typedef double type; }; template struct duration_divide_result2, false> : duration_divide_imp2 > { }; /// template ::value> struct duration_modulo_result { }; template ::type>::value //&& boost::is_convertible::type>::value ) > struct duration_modulo_imp { }; template struct duration_modulo_imp, Rep2, true> { typedef duration::type, Period> type; }; template struct duration_modulo_result, Rep2, false> : duration_modulo_imp, Rep2> { }; } // namespace detail } // namespace chrono } namespace boost { // common_type trait specializations template struct common_type, boost_ex::chrono::duration >; } namespace boost_ex { namespace chrono { // customization traits template struct treat_as_floating_point; template struct duration_values; // duration arithmetic // template // typename common_type, duration >::type // operator+(const duration& lhs, const duration& rhs); // template // typename common_type, duration >::type // operator-(const duration& lhs, const duration& rhs); // template // typename boost::enable_if_c // < // boost::is_convertible::type>::value // && boost::is_convertible::type>::value, // duration::type, Period> // >::type // operator*(const duration& d, const Rep2& s); // template // typename boost::enable_if_c // < // boost::is_convertible::type>::value // && boost::is_convertible::type>::value, // duration::type, Period> // >::type // operator*(const Rep1& s, const duration& d); // template // typename boost::disable_if , // typename detail::duration_divide_result, Rep2>::type // >::type // operator/(const duration& d, const Rep2& s); // template // typename common_type::type // operator/(const duration& lhs, const duration& rhs); // duration comparisons // template // bool operator==(const duration& lhs, const duration& rhs); // template // bool operator!=(const duration& lhs, const duration& rhs); // template // bool operator< (const duration& lhs, const duration& rhs); // template // bool operator<=(const duration& lhs, const duration& rhs); // template // bool operator> (const duration& lhs, const duration& rhs); // template // bool operator>=(const duration& lhs, const duration& rhs); // duration_cast //template // ToDuration duration_cast(const duration& d); // convenience typedefs typedef duration nanoseconds; // at least 64 bits needed typedef duration microseconds; // at least 55 bits needed typedef duration milliseconds; // at least 45 bits needed typedef duration seconds; // at least 35 bits needed typedef duration > minutes; // at least 29 bits needed typedef duration > hours; // at least 23 bits needed //----------------------------------------------------------------------------// // duration helpers // //----------------------------------------------------------------------------// namespace detail { // duration_cast // duration_cast is the heart of this whole prototype. It can convert any // duration to any other. It is also (implicitly) used in converting // time_points. The conversion is always exact if possible. And it is // always as efficient as hand written code. If different representations // are involved, care is taken to never require implicit conversions. // Instead static_cast is used explicitly for every required conversion. // If there are a mixture of integral and floating point representations, // the use of common_type ensures that the most logical "intermediate" // representation is used. template ::type, bool = Period::num == 1, bool = Period::den == 1> struct duration_cast; // When the two periods are the same, all that is left to do is static_cast from // the source representation to the target representation (which may be a no-op). // This conversion is always exact as long as the static_cast from the source // representation to the destination representation is exact. template struct duration_cast { ToDuration operator()(const FromDuration& fd) const { return ToDuration(static_cast(fd.count())); } }; // When the numerator of FromPeriod / ToPeriod is 1, then all we need to do is // divide by the denominator of FromPeriod / ToPeriod. The common_type of // the two representations is used for the intermediate computation before // static_cast'ing to the destination. // This conversion is generally not exact because of the division (but could be // if you get lucky on the run time value of fd.count()). template struct duration_cast { 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( static_cast(fd.count()) / static_cast(Period::den))); } }; // When the denomenator of FromPeriod / ToPeriod is 1, then all we need to do is // multiply by the numerator of FromPeriod / ToPeriod. The common_type of // the two representations is used for the intermediate computation before // static_cast'ing to the destination. // This conversion is always exact as long as the static_cast's involved are exact. template struct duration_cast { 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( static_cast(fd.count()) * static_cast(Period::num))); } }; // When neither the numerator or denominator of FromPeriod / ToPeriod is 1, then we need to // multiply by the numerator and divide by the denominator of FromPeriod / ToPeriod. The // common_type of the two representations is used for the intermediate computation before // static_cast'ing to the destination. // This conversion is generally not exact because of the division (but could be // if you get lucky on the run time value of fd.count()). template struct duration_cast { 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( static_cast(fd.count()) * static_cast(Period::num) / static_cast(Period::den))); } }; } // namespace detail //----------------------------------------------------------------------------// // // // 20.9.2 Time-related traits [time.traits] // // // //----------------------------------------------------------------------------// //----------------------------------------------------------------------------// // 20.9.2.1 treat_as_floating_point [time.traits.is_fp] // // Probably should have been treat_as_floating_point. Editor notifed. // //----------------------------------------------------------------------------// // Support bidirectional (non-exact) conversions for floating point rep types // (or user defined rep types which specialize treat_as_floating_point). template struct treat_as_floating_point : boost::is_floating_point {}; //----------------------------------------------------------------------------// // 20.9.2.2 duration_values [time.traits.duration_values] // //----------------------------------------------------------------------------// namespace detail { template ::value> struct chrono_numeric_limits { static T lowest() throw() {return (std::numeric_limits::min) ();} }; template struct chrono_numeric_limits { static T lowest() throw() {return (std::numeric_limits::min) ();} }; template <> struct chrono_numeric_limits { static float lowest() throw() {return -(std::numeric_limits::max) ();} }; template <> struct chrono_numeric_limits { static double lowest() throw() {return -(std::numeric_limits::max) ();} }; template <> struct chrono_numeric_limits { static long double lowest() throw() {return -(std::numeric_limits::max)();} }; template struct numeric_limits : chrono_numeric_limits::type> {}; } template struct duration_values { static Rep zero() {return Rep(0);} static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {return (std::numeric_limits::max)();} static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {return detail::numeric_limits::lowest();} }; } // namespace chrono } //----------------------------------------------------------------------------// // 20.9.2.3 Specializations of common_type [time.traits.specializations] // //----------------------------------------------------------------------------// namespace boost { template struct common_type, boost_ex::chrono::duration > { typedef boost_ex::chrono::duration::type, typename boost::ratio_gcd::type> type; }; } //----------------------------------------------------------------------------// // // // 20.9.3 Class template duration [time.duration] // // // //----------------------------------------------------------------------------// namespace boost_ex { namespace chrono { template class duration { BOOST_EX_CHRONO_STATIC_ASSERT(!boost_ex::chrono::detail::is_duration::value, BOOST_EX_CHRONO_A_DURATION_REPRESENTATION_CAN_NOT_BE_A_DURATION, ()); BOOST_EX_CHRONO_STATIC_ASSERT(boost::ratio_detail::is_ratio::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() { } // = default; template 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, boost::mpl::or_ < treat_as_floating_point, boost::mpl::and_ < boost::mpl::not_ < treat_as_floating_point >, boost::mpl::not_ < treat_as_floating_point > > > > >::type* = 0 #endif ) : rep_(r) { } ~duration() {} //= default; duration(const duration& rhs) : rep_(rhs.rep_) {} // = default; duration& operator=(const duration& rhs) // = default; { if (&rhs != this) rep_= rhs.rep_; return *this; } // conversions template duration(const duration& d #if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__) #else , typename boost::enable_if < boost::mpl::or_ < treat_as_floating_point, boost::mpl::and_ < boost::mpl::bool_ < boost::ratio_divide::type::den == 1>, boost::mpl::not_ < treat_as_floating_point > > > >::type* = 0 #endif ) //~ #ifdef __GNUC__ // GCC 4.2.4 refused to accept a definition at this point, // yet both VC++ 9.0 SP1 and Intel ia32 11.0 accepted the definition // without complaint. VC++ 9.0 SP1 refused to accept a later definition, // although that was fine with GCC 4.2.4 and Intel ia32 11.0. Thus we // have to support both approaches. //~ ; //~ #else //~ : rep_(chrono::duration_cast(d).count()) {} : rep_(chrono::detail::duration_cast, duration>()(d).count()) {} //~ #endif // observer rep count() const {return rep_;} // arithmetic 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;}; // 20.9.3.4 duration special values [time.duration.special] static duration zero() {return duration(duration_values::zero());} static duration min BOOST_PREVENT_MACRO_SUBSTITUTION () {return duration((duration_values::min)());} static duration max BOOST_PREVENT_MACRO_SUBSTITUTION () {return duration((duration_values::max)());} }; //----------------------------------------------------------------------------// // 20.9.3.5 duration non-member arithmetic [time.duration.nonmember] // //----------------------------------------------------------------------------// // Duration + template inline typename boost::common_type, duration >::type operator+(const duration& lhs, const duration& rhs) { typename boost::common_type, duration >::type result = lhs; result += rhs; return result; } // Duration - template inline typename boost::common_type, duration >::type operator-(const duration& lhs, const duration& rhs) { typename boost::common_type, duration >::type result = lhs; result -= rhs; return result; } // Duration * template inline #if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__) duration::type, Period> #else typename boost::enable_if < boost::mpl::and_ < boost::is_convertible::type>, boost::is_convertible::type> >, duration::type, Period> >::type #endif operator*(const duration& d, const Rep2& s) { typedef typename boost::common_type::type CR; duration r = d; r *= static_cast(s); return r; } template inline #if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__) duration::type, Period> #else typename boost::enable_if < boost::mpl::and_ < boost::is_convertible::type>, boost::is_convertible::type> >, duration::type, Period> >::type #endif operator*(const Rep1& s, const duration& d) { return d * s; } // Duration / template inline typename boost::disable_if , typename boost_ex::chrono::detail::duration_divide_result, Rep2>::type >::type operator/(const duration& d, const Rep2& s) { typedef typename boost::common_type::type CR; duration r = d; r /= static_cast(s); return r; } template inline typename boost::common_type::type operator/(const duration& lhs, const duration& rhs) { typedef typename boost::common_type, duration >::type CD; return CD(lhs).count() / CD(rhs).count(); } template inline typename boost::disable_if , typename boost_ex::chrono::detail::duration_divide_result2 >::type >::type operator/(const Rep1& s, const duration& d) { typedef typename boost::common_type::type CR; duration r = d; //return static_cast(r.count()) / static_cast(s); return static_cast(s)/r.count(); } // Duration % template typename boost::disable_if , typename boost_ex::chrono::detail::duration_modulo_result, Rep2>::type >::type operator%(const duration& d, const Rep2& s) { typedef typename boost::common_type::type CR; duration r = d; r %= static_cast(s); return r; } template typename boost::common_type, duration >::type operator%(const duration& lhs, const duration& rhs) { typedef typename boost::common_type, duration >::type CD; CD r(lhs); r%=CD(rhs); return r; } //----------------------------------------------------------------------------// // 20.9.3.6 duration comparisons [time.duration.comparisons] // //----------------------------------------------------------------------------// namespace detail { template struct duration_eq { bool operator()(const LhsDuration& lhs, const RhsDuration& rhs) { typedef typename boost::common_type::type CD; return CD(lhs).count() == CD(rhs).count(); } }; template struct duration_eq { bool operator()(const LhsDuration& lhs, const LhsDuration& rhs) {return lhs.count() == rhs.count();} }; template struct duration_lt { bool operator()(const LhsDuration& lhs, const RhsDuration& rhs) { typedef typename boost::common_type::type CD; return CD(lhs).count() < CD(rhs).count(); } }; template struct duration_lt { bool operator()(const LhsDuration& lhs, const LhsDuration& rhs) {return lhs.count() < rhs.count();} }; } // namespace detail // Duration == template inline bool operator==(const duration& lhs, const duration& rhs) { return boost_ex::chrono::detail::duration_eq, duration >()(lhs, rhs); } // Duration != template inline bool operator!=(const duration& lhs, const duration& rhs) { return !(lhs == rhs); } // Duration < template inline bool operator< (const duration& lhs, const duration& rhs) { return boost_ex::chrono::detail::duration_lt, duration >()(lhs, rhs); } // Duration > template inline bool operator> (const duration& lhs, const duration& rhs) { return rhs < lhs; } // Duration <= template inline bool operator<=(const duration& lhs, const duration& rhs) { return !(rhs < lhs); } // Duration >= template inline bool operator>=(const duration& lhs, const duration& rhs) { return !(lhs < rhs); } //----------------------------------------------------------------------------// // 20.9.3.7 duration_cast [time.duration.cast] // //----------------------------------------------------------------------------// // Compile-time select the most efficient algorithm for the conversion... template inline #if (defined(BOOST_MSVC) && (BOOST_MSVC == 1500)) || defined(__IBMCPP__) ToDuration #else typename boost::enable_if , ToDuration>::type #endif duration_cast(const duration& fd) { return boost_ex::chrono::detail::duration_cast, ToDuration>()(fd); } } } #endif // BOOST_EX_CHRONO_DURATION_HPP