duration.hpp 29 KB

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