duration.hpp 30 KB

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