annotate DEPENDENCIES/generic/include/boost/chrono/duration.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // duration.hpp --------------------------------------------------------------//
Chris@16 2
Chris@16 3 // Copyright 2008 Howard Hinnant
Chris@16 4 // Copyright 2008 Beman Dawes
Chris@16 5 // Copyright 2009-2011 Vicente J. Botet Escriba
Chris@16 6
Chris@16 7 // Distributed under the Boost Software License, Version 1.0.
Chris@16 8 // See http://www.boost.org/LICENSE_1_0.txt
Chris@16 9
Chris@16 10 /*
Chris@16 11
Chris@16 12 This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
Chris@16 13 Many thanks to Howard for making his code available under the Boost license.
Chris@16 14 The original code was modified to conform to Boost conventions and to section
Chris@16 15 20.9 Time utilities [time] of the C++ committee's working paper N2798.
Chris@16 16 See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
Chris@16 17
Chris@16 18 time2_demo contained this comment:
Chris@16 19
Chris@16 20 Much thanks to Andrei Alexandrescu,
Chris@16 21 Walter Brown,
Chris@16 22 Peter Dimov,
Chris@16 23 Jeff Garland,
Chris@16 24 Terry Golubiewski,
Chris@16 25 Daniel Krugler,
Chris@16 26 Anthony Williams.
Chris@16 27 */
Chris@16 28
Chris@16 29
Chris@16 30 #ifndef BOOST_CHRONO_DURATION_HPP
Chris@16 31 #define BOOST_CHRONO_DURATION_HPP
Chris@16 32
Chris@16 33 #include <boost/chrono/config.hpp>
Chris@16 34 #include <boost/chrono/detail/static_assert.hpp>
Chris@16 35
Chris@16 36 #include <climits>
Chris@16 37 #include <limits>
Chris@16 38
Chris@16 39
Chris@16 40 #include <boost/mpl/logical.hpp>
Chris@16 41 #include <boost/ratio/ratio.hpp>
Chris@16 42 #include <boost/type_traits/common_type.hpp>
Chris@16 43 #include <boost/type_traits/is_arithmetic.hpp>
Chris@16 44 #include <boost/type_traits/is_convertible.hpp>
Chris@16 45 #include <boost/type_traits/is_floating_point.hpp>
Chris@16 46 #include <boost/type_traits/is_unsigned.hpp>
Chris@16 47 #include <boost/chrono/detail/is_evenly_divisible_by.hpp>
Chris@16 48
Chris@16 49 #include <boost/cstdint.hpp>
Chris@16 50 #include <boost/utility/enable_if.hpp>
Chris@16 51 #include <boost/detail/workaround.hpp>
Chris@16 52 #include <boost/integer_traits.hpp>
Chris@16 53
Chris@16 54 #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) || !defined(BOOST_CHRONO_USES_MPL_ASSERT)
Chris@16 55 #define BOOST_CHRONO_A_DURATION_REPRESENTATION_CAN_NOT_BE_A_DURATION "A duration representation can not be a duration"
Chris@16 56 #define BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_DURATION_MUST_BE_A_STD_RATIO "Second template parameter of duration must be a boost::ratio"
Chris@16 57 #define BOOST_CHRONO_DURATION_PERIOD_MUST_BE_POSITIVE "duration period must be positive"
Chris@16 58 #define BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_TIME_POINT_MUST_BE_A_BOOST_CHRONO_DURATION "Second template parameter of time_point must be a boost::chrono::duration"
Chris@16 59 #endif
Chris@16 60
Chris@16 61 #ifndef BOOST_CHRONO_HEADER_ONLY
Chris@16 62 // this must occur after all of the includes and before any code appears:
Chris@16 63 #include <boost/config/abi_prefix.hpp> // must be the last #include
Chris@16 64 #endif
Chris@16 65
Chris@16 66 //----------------------------------------------------------------------------//
Chris@16 67 // //
Chris@16 68 // 20.9 Time utilities [time] //
Chris@16 69 // synopsis //
Chris@16 70 // //
Chris@16 71 //----------------------------------------------------------------------------//
Chris@16 72
Chris@16 73 namespace boost {
Chris@16 74 namespace chrono {
Chris@16 75
Chris@16 76 template <class Rep, class Period = ratio<1> >
Chris@16 77 class duration;
Chris@16 78
Chris@16 79 namespace detail
Chris@16 80 {
Chris@16 81 template <class T>
Chris@16 82 struct is_duration
Chris@16 83 : boost::false_type {};
Chris@16 84
Chris@16 85 template <class Rep, class Period>
Chris@16 86 struct is_duration<duration<Rep, Period> >
Chris@16 87 : boost::true_type {};
Chris@16 88
Chris@16 89 template <class Duration, class Rep, bool = is_duration<Rep>::value>
Chris@16 90 struct duration_divide_result
Chris@16 91 {
Chris@16 92 };
Chris@16 93
Chris@16 94 template <class Duration, class Rep2,
Chris@16 95 bool = (
Chris@16 96 ((boost::is_convertible<typename Duration::rep,
Chris@16 97 typename common_type<typename Duration::rep, Rep2>::type>::value))
Chris@16 98 && ((boost::is_convertible<Rep2,
Chris@16 99 typename common_type<typename Duration::rep, Rep2>::type>::value))
Chris@16 100 )
Chris@16 101 >
Chris@16 102 struct duration_divide_imp
Chris@16 103 {
Chris@16 104 };
Chris@16 105
Chris@16 106 template <class Rep1, class Period, class Rep2>
Chris@16 107 struct duration_divide_imp<duration<Rep1, Period>, Rep2, true>
Chris@16 108 {
Chris@16 109 typedef duration<typename common_type<Rep1, Rep2>::type, Period> type;
Chris@16 110 };
Chris@16 111
Chris@16 112 template <class Rep1, class Period, class Rep2>
Chris@16 113 struct duration_divide_result<duration<Rep1, Period>, Rep2, false>
Chris@16 114 : duration_divide_imp<duration<Rep1, Period>, Rep2>
Chris@16 115 {
Chris@16 116 };
Chris@16 117
Chris@16 118 ///
Chris@16 119 template <class Rep, class Duration, bool = is_duration<Rep>::value>
Chris@16 120 struct duration_divide_result2
Chris@16 121 {
Chris@16 122 };
Chris@16 123
Chris@16 124 template <class Rep, class Duration,
Chris@16 125 bool = (
Chris@16 126 ((boost::is_convertible<typename Duration::rep,
Chris@16 127 typename common_type<typename Duration::rep, Rep>::type>::value))
Chris@16 128 && ((boost::is_convertible<Rep,
Chris@16 129 typename common_type<typename Duration::rep, Rep>::type>::value))
Chris@16 130 )
Chris@16 131 >
Chris@16 132 struct duration_divide_imp2
Chris@16 133 {
Chris@16 134 };
Chris@16 135
Chris@16 136 template <class Rep1, class Rep2, class Period >
Chris@16 137 struct duration_divide_imp2<Rep1, duration<Rep2, Period>, true>
Chris@16 138 {
Chris@16 139 //typedef typename common_type<Rep1, Rep2>::type type;
Chris@16 140 typedef double type;
Chris@16 141 };
Chris@16 142
Chris@16 143 template <class Rep1, class Rep2, class Period >
Chris@16 144 struct duration_divide_result2<Rep1, duration<Rep2, Period>, false>
Chris@16 145 : duration_divide_imp2<Rep1, duration<Rep2, Period> >
Chris@16 146 {
Chris@16 147 };
Chris@16 148
Chris@16 149 ///
Chris@16 150 template <class Duration, class Rep, bool = is_duration<Rep>::value>
Chris@16 151 struct duration_modulo_result
Chris@16 152 {
Chris@16 153 };
Chris@16 154
Chris@16 155 template <class Duration, class Rep2,
Chris@16 156 bool = (
Chris@16 157 //boost::is_convertible<typename Duration::rep,
Chris@16 158 //typename common_type<typename Duration::rep, Rep2>::type>::value
Chris@16 159 //&&
Chris@16 160 boost::is_convertible<Rep2,
Chris@16 161 typename common_type<typename Duration::rep, Rep2>::type>::value
Chris@16 162 )
Chris@16 163 >
Chris@16 164 struct duration_modulo_imp
Chris@16 165 {
Chris@16 166 };
Chris@16 167
Chris@16 168 template <class Rep1, class Period, class Rep2>
Chris@16 169 struct duration_modulo_imp<duration<Rep1, Period>, Rep2, true>
Chris@16 170 {
Chris@16 171 typedef duration<typename common_type<Rep1, Rep2>::type, Period> type;
Chris@16 172 };
Chris@16 173
Chris@16 174 template <class Rep1, class Period, class Rep2>
Chris@16 175 struct duration_modulo_result<duration<Rep1, Period>, Rep2, false>
Chris@16 176 : duration_modulo_imp<duration<Rep1, Period>, Rep2>
Chris@16 177 {
Chris@16 178 };
Chris@16 179
Chris@16 180 } // namespace detail
Chris@16 181 } // namespace chrono
Chris@16 182
Chris@16 183
Chris@16 184 // common_type trait specializations
Chris@16 185
Chris@16 186 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 187 struct common_type<chrono::duration<Rep1, Period1>,
Chris@16 188 chrono::duration<Rep2, Period2> >;
Chris@16 189
Chris@16 190
Chris@16 191 namespace chrono {
Chris@16 192
Chris@16 193 // customization traits
Chris@16 194 template <class Rep> struct treat_as_floating_point;
Chris@16 195 template <class Rep> struct duration_values;
Chris@16 196
Chris@16 197 // convenience typedefs
Chris@16 198 typedef duration<boost::int_least64_t, nano> nanoseconds; // at least 64 bits needed
Chris@16 199 typedef duration<boost::int_least64_t, micro> microseconds; // at least 55 bits needed
Chris@16 200 typedef duration<boost::int_least64_t, milli> milliseconds; // at least 45 bits needed
Chris@16 201 typedef duration<boost::int_least64_t> seconds; // at least 35 bits needed
Chris@16 202 typedef duration<boost::int_least32_t, ratio< 60> > minutes; // at least 29 bits needed
Chris@16 203 typedef duration<boost::int_least32_t, ratio<3600> > hours; // at least 23 bits needed
Chris@16 204
Chris@16 205 //----------------------------------------------------------------------------//
Chris@16 206 // duration helpers //
Chris@16 207 //----------------------------------------------------------------------------//
Chris@16 208
Chris@16 209 namespace detail
Chris@16 210 {
Chris@16 211
Chris@16 212 // duration_cast
Chris@16 213
Chris@16 214 // duration_cast is the heart of this whole prototype. It can convert any
Chris@16 215 // duration to any other. It is also (implicitly) used in converting
Chris@16 216 // time_points. The conversion is always exact if possible. And it is
Chris@16 217 // always as efficient as hand written code. If different representations
Chris@16 218 // are involved, care is taken to never require implicit conversions.
Chris@16 219 // Instead static_cast is used explicitly for every required conversion.
Chris@16 220 // If there are a mixture of integral and floating point representations,
Chris@16 221 // the use of common_type ensures that the most logical "intermediate"
Chris@16 222 // representation is used.
Chris@16 223 template <class FromDuration, class ToDuration,
Chris@16 224 class Period,
Chris@16 225 bool PeriodNumEq1,
Chris@16 226 bool PeriodDenEq1>
Chris@16 227 struct duration_cast_aux;
Chris@16 228
Chris@16 229 // When the two periods are the same, all that is left to do is static_cast from
Chris@16 230 // the source representation to the target representation (which may be a no-op).
Chris@16 231 // This conversion is always exact as long as the static_cast from the source
Chris@16 232 // representation to the destination representation is exact.
Chris@16 233 template <class FromDuration, class ToDuration, class Period>
Chris@16 234 struct duration_cast_aux<FromDuration, ToDuration, Period, true, true>
Chris@16 235 {
Chris@16 236 BOOST_CONSTEXPR ToDuration operator()(const FromDuration& fd) const
Chris@16 237 {
Chris@16 238 return ToDuration(static_cast<typename ToDuration::rep>(fd.count()));
Chris@16 239 }
Chris@16 240 };
Chris@16 241
Chris@16 242 // When the numerator of FromPeriod / ToPeriod is 1, then all we need to do is
Chris@16 243 // divide by the denominator of FromPeriod / ToPeriod. The common_type of
Chris@16 244 // the two representations is used for the intermediate computation before
Chris@16 245 // static_cast'ing to the destination.
Chris@16 246 // This conversion is generally not exact because of the division (but could be
Chris@16 247 // if you get lucky on the run time value of fd.count()).
Chris@16 248 template <class FromDuration, class ToDuration, class Period>
Chris@16 249 struct duration_cast_aux<FromDuration, ToDuration, Period, true, false>
Chris@16 250 {
Chris@16 251 BOOST_CONSTEXPR ToDuration operator()(const FromDuration& fd) const
Chris@16 252 {
Chris@16 253 typedef typename common_type<
Chris@16 254 typename ToDuration::rep,
Chris@16 255 typename FromDuration::rep,
Chris@16 256 boost::intmax_t>::type C;
Chris@16 257 return ToDuration(static_cast<typename ToDuration::rep>(
Chris@16 258 static_cast<C>(fd.count()) / static_cast<C>(Period::den)));
Chris@16 259 }
Chris@16 260 };
Chris@16 261
Chris@16 262 // When the denominator of FromPeriod / ToPeriod is 1, then all we need to do is
Chris@16 263 // multiply by the numerator of FromPeriod / ToPeriod. The common_type of
Chris@16 264 // the two representations is used for the intermediate computation before
Chris@16 265 // static_cast'ing to the destination.
Chris@16 266 // This conversion is always exact as long as the static_cast's involved are exact.
Chris@16 267 template <class FromDuration, class ToDuration, class Period>
Chris@16 268 struct duration_cast_aux<FromDuration, ToDuration, Period, false, true>
Chris@16 269 {
Chris@16 270 BOOST_CONSTEXPR ToDuration operator()(const FromDuration& fd) const
Chris@16 271 {
Chris@16 272 typedef typename common_type<
Chris@16 273 typename ToDuration::rep,
Chris@16 274 typename FromDuration::rep,
Chris@16 275 boost::intmax_t>::type C;
Chris@16 276 return ToDuration(static_cast<typename ToDuration::rep>(
Chris@16 277 static_cast<C>(fd.count()) * static_cast<C>(Period::num)));
Chris@16 278 }
Chris@16 279 };
Chris@16 280
Chris@16 281 // When neither the numerator or denominator of FromPeriod / ToPeriod is 1, then we need to
Chris@16 282 // multiply by the numerator and divide by the denominator of FromPeriod / ToPeriod. The
Chris@16 283 // common_type of the two representations is used for the intermediate computation before
Chris@16 284 // static_cast'ing to the destination.
Chris@16 285 // This conversion is generally not exact because of the division (but could be
Chris@16 286 // if you get lucky on the run time value of fd.count()).
Chris@16 287 template <class FromDuration, class ToDuration, class Period>
Chris@16 288 struct duration_cast_aux<FromDuration, ToDuration, Period, false, false>
Chris@16 289 {
Chris@16 290 BOOST_CONSTEXPR ToDuration operator()(const FromDuration& fd) const
Chris@16 291 {
Chris@16 292 typedef typename common_type<
Chris@16 293 typename ToDuration::rep,
Chris@16 294 typename FromDuration::rep,
Chris@16 295 boost::intmax_t>::type C;
Chris@16 296 return ToDuration(static_cast<typename ToDuration::rep>(
Chris@16 297 static_cast<C>(fd.count()) * static_cast<C>(Period::num)
Chris@16 298 / static_cast<C>(Period::den)));
Chris@16 299 }
Chris@16 300 };
Chris@16 301
Chris@16 302 template <class FromDuration, class ToDuration>
Chris@16 303 struct duration_cast {
Chris@16 304 typedef typename ratio_divide<typename FromDuration::period,
Chris@16 305 typename ToDuration::period>::type Period;
Chris@16 306 typedef duration_cast_aux<
Chris@16 307 FromDuration,
Chris@16 308 ToDuration,
Chris@16 309 Period,
Chris@16 310 Period::num == 1,
Chris@16 311 Period::den == 1
Chris@16 312 > Aux;
Chris@16 313 BOOST_CONSTEXPR ToDuration operator()(const FromDuration& fd) const
Chris@16 314 {
Chris@16 315 return Aux()(fd);
Chris@16 316 }
Chris@16 317 };
Chris@16 318
Chris@16 319 } // namespace detail
Chris@16 320
Chris@16 321 //----------------------------------------------------------------------------//
Chris@16 322 // //
Chris@16 323 // 20.9.2 Time-related traits [time.traits] //
Chris@16 324 // //
Chris@16 325 //----------------------------------------------------------------------------//
Chris@16 326 //----------------------------------------------------------------------------//
Chris@16 327 // 20.9.2.1 treat_as_floating_point [time.traits.is_fp] //
Chris@16 328 // Probably should have been treat_as_floating_point. Editor notifed. //
Chris@16 329 //----------------------------------------------------------------------------//
Chris@16 330
Chris@16 331 // Support bidirectional (non-exact) conversions for floating point rep types
Chris@16 332 // (or user defined rep types which specialize treat_as_floating_point).
Chris@16 333 template <class Rep>
Chris@16 334 struct treat_as_floating_point : boost::is_floating_point<Rep> {};
Chris@16 335
Chris@16 336 //----------------------------------------------------------------------------//
Chris@16 337 // 20.9.2.2 duration_values [time.traits.duration_values] //
Chris@16 338 //----------------------------------------------------------------------------//
Chris@16 339
Chris@16 340 namespace detail {
Chris@16 341 template <class T, bool = is_arithmetic<T>::value>
Chris@16 342 struct chrono_numeric_limits {
Chris@16 343 static BOOST_CHRONO_LIB_CONSTEXPR T lowest() BOOST_CHRONO_LIB_NOEXCEPT_OR_THROW {return (std::numeric_limits<T>::min) ();}
Chris@16 344 };
Chris@16 345
Chris@16 346 template <class T>
Chris@16 347 struct chrono_numeric_limits<T,true> {
Chris@16 348 static BOOST_CHRONO_LIB_CONSTEXPR T lowest() BOOST_CHRONO_LIB_NOEXCEPT_OR_THROW {return (std::numeric_limits<T>::min) ();}
Chris@16 349 };
Chris@16 350
Chris@16 351 template <>
Chris@16 352 struct chrono_numeric_limits<float,true> {
Chris@16 353 static BOOST_CHRONO_LIB_CONSTEXPR float lowest() BOOST_CHRONO_LIB_NOEXCEPT_OR_THROW
Chris@16 354 {
Chris@16 355 return -(std::numeric_limits<float>::max) ();
Chris@16 356 }
Chris@16 357 };
Chris@16 358
Chris@16 359 template <>
Chris@16 360 struct chrono_numeric_limits<double,true> {
Chris@16 361 static BOOST_CHRONO_LIB_CONSTEXPR double lowest() BOOST_CHRONO_LIB_NOEXCEPT_OR_THROW
Chris@16 362 {
Chris@16 363 return -(std::numeric_limits<double>::max) ();
Chris@16 364 }
Chris@16 365 };
Chris@16 366
Chris@16 367 template <>
Chris@16 368 struct chrono_numeric_limits<long double,true> {
Chris@16 369 static BOOST_CHRONO_LIB_CONSTEXPR long double lowest() BOOST_CHRONO_LIB_NOEXCEPT_OR_THROW
Chris@16 370 {
Chris@16 371 return -(std::numeric_limits<long double>::max)();
Chris@16 372 }
Chris@16 373 };
Chris@16 374
Chris@16 375 template <class T>
Chris@16 376 struct numeric_limits : chrono_numeric_limits<typename remove_cv<T>::type>
Chris@16 377 {};
Chris@16 378
Chris@16 379 }
Chris@16 380 template <class Rep>
Chris@16 381 struct duration_values
Chris@16 382 {
Chris@16 383 static BOOST_CONSTEXPR Rep zero() {return Rep(0);}
Chris@16 384 static BOOST_CHRONO_LIB_CONSTEXPR Rep max BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 385 {
Chris@16 386 return (std::numeric_limits<Rep>::max)();
Chris@16 387 }
Chris@16 388
Chris@16 389 static BOOST_CHRONO_LIB_CONSTEXPR Rep min BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 390 {
Chris@16 391 return detail::numeric_limits<Rep>::lowest();
Chris@16 392 }
Chris@16 393 };
Chris@16 394
Chris@16 395 } // namespace chrono
Chris@16 396
Chris@16 397 //----------------------------------------------------------------------------//
Chris@16 398 // 20.9.2.3 Specializations of common_type [time.traits.specializations] //
Chris@16 399 //----------------------------------------------------------------------------//
Chris@16 400
Chris@16 401 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 402 struct common_type<chrono::duration<Rep1, Period1>,
Chris@16 403 chrono::duration<Rep2, Period2> >
Chris@16 404 {
Chris@16 405 typedef chrono::duration<typename common_type<Rep1, Rep2>::type,
Chris@16 406 typename boost::ratio_gcd<Period1, Period2>::type> type;
Chris@16 407 };
Chris@16 408
Chris@16 409
Chris@16 410 //----------------------------------------------------------------------------//
Chris@16 411 // //
Chris@16 412 // 20.9.3 Class template duration [time.duration] //
Chris@16 413 // //
Chris@16 414 //----------------------------------------------------------------------------//
Chris@16 415
Chris@16 416
Chris@16 417 namespace chrono {
Chris@16 418
Chris@16 419 template <class Rep, class Period>
Chris@16 420 class BOOST_SYMBOL_VISIBLE duration
Chris@16 421 {
Chris@16 422 //BOOST_CHRONO_STATIC_ASSERT(boost::is_integral<Rep>::value, BOOST_CHRONO_A_DURATION_REPRESENTATION_MUST_BE_INTEGRAL, ());
Chris@16 423 BOOST_CHRONO_STATIC_ASSERT(!boost::chrono::detail::is_duration<Rep>::value,
Chris@16 424 BOOST_CHRONO_A_DURATION_REPRESENTATION_CAN_NOT_BE_A_DURATION, ());
Chris@16 425 BOOST_CHRONO_STATIC_ASSERT(boost::ratio_detail::is_ratio<typename Period::type>::value,
Chris@16 426 BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_DURATION_MUST_BE_A_STD_RATIO, ());
Chris@16 427 BOOST_CHRONO_STATIC_ASSERT(Period::num>0,
Chris@16 428 BOOST_CHRONO_DURATION_PERIOD_MUST_BE_POSITIVE, ());
Chris@16 429 public:
Chris@16 430 typedef Rep rep;
Chris@16 431 typedef Period period;
Chris@16 432 private:
Chris@16 433 rep rep_;
Chris@16 434 public:
Chris@16 435
Chris@101 436 #if defined BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
Chris@16 437 BOOST_FORCEINLINE BOOST_CONSTEXPR
Chris@16 438 duration() : rep_(duration_values<rep>::zero()) { }
Chris@101 439 #else
Chris@101 440 BOOST_CONSTEXPR duration() BOOST_NOEXCEPT {};
Chris@101 441 #endif
Chris@16 442 template <class Rep2>
Chris@16 443 BOOST_SYMBOL_VISIBLE BOOST_FORCEINLINE BOOST_CONSTEXPR
Chris@16 444 explicit duration(const Rep2& r
Chris@16 445 , typename boost::enable_if <
Chris@16 446 mpl::and_ <
Chris@16 447 boost::is_convertible<Rep2, rep>,
Chris@16 448 mpl::or_ <
Chris@16 449 treat_as_floating_point<rep>,
Chris@16 450 mpl::and_ <
Chris@16 451 mpl::not_ < treat_as_floating_point<rep> >,
Chris@16 452 mpl::not_ < treat_as_floating_point<Rep2> >
Chris@16 453 >
Chris@16 454 >
Chris@16 455 >
Chris@16 456 >::type* = 0
Chris@16 457 ) : rep_(r) { }
Chris@101 458 #if defined BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
Chris@101 459 duration& operator=(const duration& rhs)
Chris@16 460 {
Chris@16 461 if (&rhs != this) rep_= rhs.rep_;
Chris@16 462 return *this;
Chris@16 463 }
Chris@101 464 #else
Chris@101 465 duration& operator=(const duration& rhs) = default;
Chris@101 466 #endif
Chris@16 467 // conversions
Chris@16 468 template <class Rep2, class Period2>
Chris@16 469 BOOST_FORCEINLINE BOOST_CONSTEXPR
Chris@16 470 duration(const duration<Rep2, Period2>& d
Chris@16 471 , typename boost::enable_if <
Chris@16 472 mpl::or_ <
Chris@16 473 treat_as_floating_point<rep>,
Chris@16 474 mpl::and_ <
Chris@16 475 chrono_detail::is_evenly_divisible_by<Period2, period>,
Chris@16 476 mpl::not_ < treat_as_floating_point<Rep2> >
Chris@16 477 >
Chris@16 478 >
Chris@16 479 >::type* = 0
Chris@16 480 )
Chris@16 481 : rep_(chrono::detail::duration_cast<duration<Rep2, Period2>, duration>()(d).count()) {}
Chris@16 482
Chris@16 483 // observer
Chris@16 484
Chris@16 485 BOOST_CONSTEXPR
Chris@16 486 rep count() const {return rep_;}
Chris@16 487
Chris@16 488 // arithmetic
Chris@16 489
Chris@16 490 BOOST_CONSTEXPR
Chris@16 491 duration operator+() const {return duration(rep_);;}
Chris@16 492 BOOST_CONSTEXPR
Chris@16 493 duration operator-() const {return duration(-rep_);}
Chris@16 494 duration& operator++() {++rep_; return *this;}
Chris@16 495 duration operator++(int) {return duration(rep_++);}
Chris@16 496 duration& operator--() {--rep_; return *this;}
Chris@16 497 duration operator--(int) {return duration(rep_--);}
Chris@16 498
Chris@16 499 duration& operator+=(const duration& d)
Chris@16 500 {
Chris@16 501 rep_ += d.count(); return *this;
Chris@16 502 }
Chris@16 503 duration& operator-=(const duration& d)
Chris@16 504 {
Chris@16 505 rep_ -= d.count(); return *this;
Chris@16 506 }
Chris@16 507
Chris@16 508 duration& operator*=(const rep& rhs) {rep_ *= rhs; return *this;}
Chris@16 509 duration& operator/=(const rep& rhs) {rep_ /= rhs; return *this;}
Chris@16 510 duration& operator%=(const rep& rhs) {rep_ %= rhs; return *this;}
Chris@16 511 duration& operator%=(const duration& rhs)
Chris@16 512 {
Chris@16 513 rep_ %= rhs.count(); return *this;
Chris@16 514 }
Chris@16 515 // 20.9.3.4 duration special values [time.duration.special]
Chris@16 516
Chris@16 517 static BOOST_CONSTEXPR duration zero()
Chris@16 518 {
Chris@16 519 return duration(duration_values<rep>::zero());
Chris@16 520 }
Chris@16 521 static BOOST_CHRONO_LIB_CONSTEXPR duration min BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 522 {
Chris@16 523 return duration((duration_values<rep>::min)());
Chris@16 524 }
Chris@16 525 static BOOST_CHRONO_LIB_CONSTEXPR duration max BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 526 {
Chris@16 527 return duration((duration_values<rep>::max)());
Chris@16 528 }
Chris@16 529 };
Chris@16 530
Chris@16 531 //----------------------------------------------------------------------------//
Chris@16 532 // 20.9.3.5 duration non-member arithmetic [time.duration.nonmember] //
Chris@16 533 //----------------------------------------------------------------------------//
Chris@16 534
Chris@16 535 // Duration +
Chris@16 536
Chris@16 537 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 538 inline BOOST_CONSTEXPR
Chris@16 539 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
Chris@16 540 operator+(const duration<Rep1, Period1>& lhs,
Chris@16 541 const duration<Rep2, Period2>& rhs)
Chris@16 542 {
Chris@16 543 typedef typename common_type<duration<Rep1, Period1>,
Chris@16 544 duration<Rep2, Period2> >::type CD;
Chris@16 545 return CD(CD(lhs).count()+CD(rhs).count());
Chris@16 546 }
Chris@16 547
Chris@16 548 // Duration -
Chris@16 549
Chris@16 550 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 551 inline BOOST_CONSTEXPR
Chris@16 552 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
Chris@16 553 operator-(const duration<Rep1, Period1>& lhs,
Chris@16 554 const duration<Rep2, Period2>& rhs)
Chris@16 555 {
Chris@16 556 typedef typename common_type<duration<Rep1, Period1>,
Chris@16 557 duration<Rep2, Period2> >::type CD;
Chris@16 558 return CD(CD(lhs).count()-CD(rhs).count());
Chris@16 559 }
Chris@16 560
Chris@16 561 // Duration *
Chris@16 562
Chris@16 563 template <class Rep1, class Period, class Rep2>
Chris@16 564 inline BOOST_CONSTEXPR
Chris@16 565 typename boost::enable_if <
Chris@16 566 mpl::and_ <
Chris@16 567 boost::is_convertible<Rep1, typename common_type<Rep1, Rep2>::type>,
Chris@16 568 boost::is_convertible<Rep2, typename common_type<Rep1, Rep2>::type>
Chris@16 569 >,
Chris@16 570 duration<typename common_type<Rep1, Rep2>::type, Period>
Chris@16 571 >::type
Chris@16 572 operator*(const duration<Rep1, Period>& d, const Rep2& s)
Chris@16 573 {
Chris@16 574 typedef typename common_type<Rep1, Rep2>::type CR;
Chris@16 575 typedef duration<CR, Period> CD;
Chris@16 576 return CD(CD(d).count()*static_cast<CR>(s));
Chris@16 577 }
Chris@16 578
Chris@16 579 template <class Rep1, class Period, class Rep2>
Chris@16 580 inline BOOST_CONSTEXPR
Chris@16 581 typename boost::enable_if <
Chris@16 582 mpl::and_ <
Chris@16 583 boost::is_convertible<Rep1, typename common_type<Rep1, Rep2>::type>,
Chris@16 584 boost::is_convertible<Rep2, typename common_type<Rep1, Rep2>::type>
Chris@16 585 >,
Chris@16 586 duration<typename common_type<Rep1, Rep2>::type, Period>
Chris@16 587 >::type
Chris@16 588 operator*(const Rep1& s, const duration<Rep2, Period>& d)
Chris@16 589 {
Chris@16 590 return d * s;
Chris@16 591 }
Chris@16 592
Chris@16 593 // Duration /
Chris@16 594
Chris@16 595 template <class Rep1, class Period, class Rep2>
Chris@16 596 inline BOOST_CONSTEXPR
Chris@16 597 typename boost::disable_if <boost::chrono::detail::is_duration<Rep2>,
Chris@16 598 typename boost::chrono::detail::duration_divide_result<
Chris@16 599 duration<Rep1, Period>, Rep2>::type
Chris@16 600 >::type
Chris@16 601 operator/(const duration<Rep1, Period>& d, const Rep2& s)
Chris@16 602 {
Chris@16 603 typedef typename common_type<Rep1, Rep2>::type CR;
Chris@16 604 typedef duration<CR, Period> CD;
Chris@16 605
Chris@16 606 return CD(CD(d).count()/static_cast<CR>(s));
Chris@16 607 }
Chris@16 608
Chris@16 609 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 610 inline BOOST_CONSTEXPR
Chris@16 611 typename common_type<Rep1, Rep2>::type
Chris@16 612 operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
Chris@16 613 {
Chris@16 614 typedef typename common_type<duration<Rep1, Period1>,
Chris@16 615 duration<Rep2, Period2> >::type CD;
Chris@16 616 return CD(lhs).count() / CD(rhs).count();
Chris@16 617 }
Chris@16 618
Chris@16 619 #ifdef BOOST_CHRONO_EXTENSIONS
Chris@16 620 template <class Rep1, class Rep2, class Period>
Chris@16 621 inline BOOST_CONSTEXPR
Chris@16 622 typename boost::disable_if <boost::chrono::detail::is_duration<Rep1>,
Chris@16 623 typename boost::chrono::detail::duration_divide_result2<
Chris@16 624 Rep1, duration<Rep2, Period> >::type
Chris@16 625 >::type
Chris@16 626 operator/(const Rep1& s, const duration<Rep2, Period>& d)
Chris@16 627 {
Chris@16 628 typedef typename common_type<Rep1, Rep2>::type CR;
Chris@16 629 typedef duration<CR, Period> CD;
Chris@16 630
Chris@16 631 return static_cast<CR>(s)/CD(d).count();
Chris@16 632 }
Chris@16 633 #endif
Chris@16 634 // Duration %
Chris@16 635
Chris@16 636 template <class Rep1, class Period, class Rep2>
Chris@16 637 inline BOOST_CONSTEXPR
Chris@16 638 typename boost::disable_if <boost::chrono::detail::is_duration<Rep2>,
Chris@16 639 typename boost::chrono::detail::duration_modulo_result<
Chris@16 640 duration<Rep1, Period>, Rep2>::type
Chris@16 641 >::type
Chris@16 642 operator%(const duration<Rep1, Period>& d, const Rep2& s)
Chris@16 643 {
Chris@16 644 typedef typename common_type<Rep1, Rep2>::type CR;
Chris@16 645 typedef duration<CR, Period> CD;
Chris@16 646
Chris@16 647 return CD(CD(d).count()%static_cast<CR>(s));
Chris@16 648 }
Chris@16 649
Chris@16 650 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 651 inline BOOST_CONSTEXPR
Chris@16 652 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
Chris@16 653 operator%(const duration<Rep1, Period1>& lhs,
Chris@16 654 const duration<Rep2, Period2>& rhs) {
Chris@16 655 typedef typename common_type<duration<Rep1, Period1>,
Chris@16 656 duration<Rep2, Period2> >::type CD;
Chris@16 657
Chris@16 658 return CD(CD(lhs).count()%CD(rhs).count());
Chris@16 659 }
Chris@16 660
Chris@16 661
Chris@16 662 //----------------------------------------------------------------------------//
Chris@16 663 // 20.9.3.6 duration comparisons [time.duration.comparisons] //
Chris@16 664 //----------------------------------------------------------------------------//
Chris@16 665
Chris@16 666 namespace detail
Chris@16 667 {
Chris@16 668 template <class LhsDuration, class RhsDuration>
Chris@16 669 struct duration_eq
Chris@16 670 {
Chris@16 671 BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const RhsDuration& rhs) const
Chris@16 672 {
Chris@16 673 typedef typename common_type<LhsDuration, RhsDuration>::type CD;
Chris@16 674 return CD(lhs).count() == CD(rhs).count();
Chris@16 675 }
Chris@16 676 };
Chris@16 677
Chris@16 678 template <class LhsDuration>
Chris@16 679 struct duration_eq<LhsDuration, LhsDuration>
Chris@16 680 {
Chris@16 681 BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const LhsDuration& rhs) const
Chris@16 682 {
Chris@16 683 return lhs.count() == rhs.count();
Chris@16 684 }
Chris@16 685 };
Chris@16 686
Chris@16 687 template <class LhsDuration, class RhsDuration>
Chris@16 688 struct duration_lt
Chris@16 689 {
Chris@16 690 BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const RhsDuration& rhs) const
Chris@16 691 {
Chris@16 692 typedef typename common_type<LhsDuration, RhsDuration>::type CD;
Chris@16 693 return CD(lhs).count() < CD(rhs).count();
Chris@16 694 }
Chris@16 695 };
Chris@16 696
Chris@16 697 template <class LhsDuration>
Chris@16 698 struct duration_lt<LhsDuration, LhsDuration>
Chris@16 699 {
Chris@16 700 BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const LhsDuration& rhs) const
Chris@16 701 {
Chris@16 702 return lhs.count() < rhs.count();
Chris@16 703 }
Chris@16 704 };
Chris@16 705
Chris@16 706 } // namespace detail
Chris@16 707
Chris@16 708 // Duration ==
Chris@16 709
Chris@16 710 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 711 inline BOOST_CONSTEXPR
Chris@16 712 bool
Chris@16 713 operator==(const duration<Rep1, Period1>& lhs,
Chris@16 714 const duration<Rep2, Period2>& rhs)
Chris@16 715 {
Chris@16 716 return boost::chrono::detail::duration_eq<
Chris@16 717 duration<Rep1, Period1>, duration<Rep2, Period2> >()(lhs, rhs);
Chris@16 718 }
Chris@16 719
Chris@16 720 // Duration !=
Chris@16 721
Chris@16 722 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 723 inline BOOST_CONSTEXPR
Chris@16 724 bool
Chris@16 725 operator!=(const duration<Rep1, Period1>& lhs,
Chris@16 726 const duration<Rep2, Period2>& rhs)
Chris@16 727 {
Chris@16 728 return !(lhs == rhs);
Chris@16 729 }
Chris@16 730
Chris@16 731 // Duration <
Chris@16 732
Chris@16 733 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 734 inline BOOST_CONSTEXPR
Chris@16 735 bool
Chris@16 736 operator< (const duration<Rep1, Period1>& lhs,
Chris@16 737 const duration<Rep2, Period2>& rhs)
Chris@16 738 {
Chris@16 739 return boost::chrono::detail::duration_lt<
Chris@16 740 duration<Rep1, Period1>, duration<Rep2, Period2> >()(lhs, rhs);
Chris@16 741 }
Chris@16 742
Chris@16 743 // Duration >
Chris@16 744
Chris@16 745 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 746 inline BOOST_CONSTEXPR
Chris@16 747 bool
Chris@16 748 operator> (const duration<Rep1, Period1>& lhs,
Chris@16 749 const duration<Rep2, Period2>& rhs)
Chris@16 750 {
Chris@16 751 return rhs < lhs;
Chris@16 752 }
Chris@16 753
Chris@16 754 // Duration <=
Chris@16 755
Chris@16 756 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 757 inline BOOST_CONSTEXPR
Chris@16 758 bool
Chris@16 759 operator<=(const duration<Rep1, Period1>& lhs,
Chris@16 760 const duration<Rep2, Period2>& rhs)
Chris@16 761 {
Chris@16 762 return !(rhs < lhs);
Chris@16 763 }
Chris@16 764
Chris@16 765 // Duration >=
Chris@16 766
Chris@16 767 template <class Rep1, class Period1, class Rep2, class Period2>
Chris@16 768 inline BOOST_CONSTEXPR
Chris@16 769 bool
Chris@16 770 operator>=(const duration<Rep1, Period1>& lhs,
Chris@16 771 const duration<Rep2, Period2>& rhs)
Chris@16 772 {
Chris@16 773 return !(lhs < rhs);
Chris@16 774 }
Chris@16 775
Chris@16 776 //----------------------------------------------------------------------------//
Chris@16 777 // 20.9.3.7 duration_cast [time.duration.cast] //
Chris@16 778 //----------------------------------------------------------------------------//
Chris@16 779
Chris@16 780 // Compile-time select the most efficient algorithm for the conversion...
Chris@16 781 template <class ToDuration, class Rep, class Period>
Chris@16 782 inline BOOST_CONSTEXPR
Chris@16 783 typename boost::enable_if <
Chris@16 784 boost::chrono::detail::is_duration<ToDuration>, ToDuration>::type
Chris@16 785 duration_cast(const duration<Rep, Period>& fd)
Chris@16 786 {
Chris@16 787 return boost::chrono::detail::duration_cast<
Chris@16 788 duration<Rep, Period>, ToDuration>()(fd);
Chris@16 789 }
Chris@16 790
Chris@16 791 } // namespace chrono
Chris@16 792 } // namespace boost
Chris@16 793
Chris@16 794 #ifndef BOOST_CHRONO_HEADER_ONLY
Chris@16 795 // the suffix header occurs after all of our code:
Chris@16 796 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
Chris@16 797 #endif
Chris@16 798
Chris@16 799 #endif // BOOST_CHRONO_DURATION_HPP