duration.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. #if defined BOOST_CHRONO_DURATION_DEFAULTS_TO_ZERO
  367. BOOST_FORCEINLINE BOOST_CONSTEXPR
  368. duration() : rep_(duration_values<rep>::zero()) { }
  369. #elif defined BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  370. BOOST_CONSTEXPR duration() {}
  371. #else
  372. BOOST_CONSTEXPR duration() = default;
  373. #endif
  374. template <class Rep2>
  375. BOOST_SYMBOL_VISIBLE BOOST_FORCEINLINE BOOST_CONSTEXPR
  376. explicit duration(const Rep2& r
  377. , typename boost::enable_if <
  378. mpl::and_ <
  379. boost::is_convertible<Rep2, rep>,
  380. mpl::or_ <
  381. treat_as_floating_point<rep>,
  382. mpl::and_ <
  383. mpl::not_ < treat_as_floating_point<rep> >,
  384. mpl::not_ < treat_as_floating_point<Rep2> >
  385. >
  386. >
  387. >
  388. >::type* = 0
  389. ) : rep_(r) { }
  390. #if defined BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  391. duration& operator=(const duration& rhs)
  392. {
  393. if (&rhs != this) rep_= rhs.rep_;
  394. return *this;
  395. }
  396. #else
  397. duration& operator=(const duration& rhs) = default;
  398. #endif
  399. // conversions
  400. template <class Rep2, class Period2>
  401. BOOST_FORCEINLINE BOOST_CONSTEXPR
  402. duration(const duration<Rep2, Period2>& d
  403. , typename boost::enable_if <
  404. mpl::or_ <
  405. treat_as_floating_point<rep>,
  406. mpl::and_ <
  407. chrono_detail::is_evenly_divisible_by<Period2, period>,
  408. mpl::not_ < treat_as_floating_point<Rep2> >
  409. >
  410. >
  411. >::type* = 0
  412. )
  413. : rep_(chrono::detail::duration_cast<duration<Rep2, Period2>, duration>()(d).count()) {}
  414. // observer
  415. BOOST_CONSTEXPR
  416. rep count() const {return rep_;}
  417. // arithmetic
  418. BOOST_CONSTEXPR
  419. duration operator+() const {return duration(rep_);;}
  420. BOOST_CONSTEXPR
  421. duration operator-() const {return duration(-rep_);}
  422. duration& operator++() {++rep_; return *this;}
  423. duration operator++(int) {return duration(rep_++);}
  424. duration& operator--() {--rep_; return *this;}
  425. duration operator--(int) {return duration(rep_--);}
  426. duration& operator+=(const duration& d)
  427. {
  428. rep_ += d.count(); return *this;
  429. }
  430. duration& operator-=(const duration& d)
  431. {
  432. rep_ -= d.count(); return *this;
  433. }
  434. duration& operator*=(const rep& rhs) {rep_ *= rhs; return *this;}
  435. duration& operator/=(const rep& rhs) {rep_ /= rhs; return *this;}
  436. duration& operator%=(const rep& rhs) {rep_ %= rhs; return *this;}
  437. duration& operator%=(const duration& rhs)
  438. {
  439. rep_ %= rhs.count(); return *this;
  440. }
  441. // 20.9.3.4 duration special values [time.duration.special]
  442. static BOOST_CONSTEXPR duration zero()
  443. {
  444. return duration(duration_values<rep>::zero());
  445. }
  446. static BOOST_CHRONO_LIB_CONSTEXPR duration min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  447. {
  448. return duration((duration_values<rep>::min)());
  449. }
  450. static BOOST_CHRONO_LIB_CONSTEXPR duration max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  451. {
  452. return duration((duration_values<rep>::max)());
  453. }
  454. };
  455. //----------------------------------------------------------------------------//
  456. // 20.9.3.5 duration non-member arithmetic [time.duration.nonmember] //
  457. //----------------------------------------------------------------------------//
  458. // Duration +
  459. template <class Rep1, class Period1, class Rep2, class Period2>
  460. inline BOOST_CONSTEXPR
  461. typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
  462. operator+(const duration<Rep1, Period1>& lhs,
  463. const duration<Rep2, Period2>& rhs)
  464. {
  465. typedef typename common_type<duration<Rep1, Period1>,
  466. duration<Rep2, Period2> >::type common_duration;
  467. return common_duration(common_duration(lhs).count()+common_duration(rhs).count());
  468. }
  469. // Duration -
  470. template <class Rep1, class Period1, class Rep2, class Period2>
  471. inline BOOST_CONSTEXPR
  472. typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
  473. operator-(const duration<Rep1, Period1>& lhs,
  474. const duration<Rep2, Period2>& rhs)
  475. {
  476. typedef typename common_type<duration<Rep1, Period1>,
  477. duration<Rep2, Period2> >::type common_duration;
  478. return common_duration(common_duration(lhs).count()-common_duration(rhs).count());
  479. }
  480. // Duration *
  481. template <class Rep1, class Period, class Rep2>
  482. inline BOOST_CONSTEXPR
  483. typename boost::enable_if <
  484. mpl::and_ <
  485. boost::is_convertible<Rep1, typename common_type<Rep1, Rep2>::type>,
  486. boost::is_convertible<Rep2, typename common_type<Rep1, Rep2>::type>
  487. >,
  488. duration<typename common_type<Rep1, Rep2>::type, Period>
  489. >::type
  490. operator*(const duration<Rep1, Period>& d, const Rep2& s)
  491. {
  492. typedef typename common_type<Rep1, Rep2>::type common_rep;
  493. typedef duration<common_rep, Period> common_duration;
  494. return common_duration(common_duration(d).count()*static_cast<common_rep>(s));
  495. }
  496. template <class Rep1, class Period, class Rep2>
  497. inline BOOST_CONSTEXPR
  498. typename boost::enable_if <
  499. mpl::and_ <
  500. boost::is_convertible<Rep1, typename common_type<Rep1, Rep2>::type>,
  501. boost::is_convertible<Rep2, typename common_type<Rep1, Rep2>::type>
  502. >,
  503. duration<typename common_type<Rep1, Rep2>::type, Period>
  504. >::type
  505. operator*(const Rep1& s, const duration<Rep2, Period>& d)
  506. {
  507. return d * s;
  508. }
  509. // Duration /
  510. template <class Rep1, class Period, class Rep2>
  511. inline BOOST_CONSTEXPR
  512. typename boost::disable_if <boost::chrono::detail::is_duration<Rep2>,
  513. typename boost::chrono::detail::duration_divide_result<
  514. duration<Rep1, Period>, Rep2>::type
  515. >::type
  516. operator/(const duration<Rep1, Period>& d, const Rep2& s)
  517. {
  518. typedef typename common_type<Rep1, Rep2>::type common_rep;
  519. typedef duration<common_rep, Period> common_duration;
  520. return common_duration(common_duration(d).count()/static_cast<common_rep>(s));
  521. }
  522. template <class Rep1, class Period1, class Rep2, class Period2>
  523. inline BOOST_CONSTEXPR
  524. typename common_type<Rep1, Rep2>::type
  525. operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs)
  526. {
  527. typedef typename common_type<duration<Rep1, Period1>,
  528. duration<Rep2, Period2> >::type common_duration;
  529. return common_duration(lhs).count() / common_duration(rhs).count();
  530. }
  531. #ifdef BOOST_CHRONO_EXTENSIONS
  532. template <class Rep1, class Rep2, class Period>
  533. inline BOOST_CONSTEXPR
  534. typename boost::disable_if <boost::chrono::detail::is_duration<Rep1>,
  535. typename boost::chrono::detail::duration_divide_result2<
  536. Rep1, duration<Rep2, Period> >::type
  537. >::type
  538. operator/(const Rep1& s, const duration<Rep2, Period>& d)
  539. {
  540. typedef typename common_type<Rep1, Rep2>::type common_rep;
  541. typedef duration<common_rep, Period> common_duration;
  542. return static_cast<common_rep>(s)/common_duration(d).count();
  543. }
  544. #endif
  545. // Duration %
  546. template <class Rep1, class Period, class Rep2>
  547. inline BOOST_CONSTEXPR
  548. typename boost::disable_if <boost::chrono::detail::is_duration<Rep2>,
  549. typename boost::chrono::detail::duration_modulo_result<
  550. duration<Rep1, Period>, Rep2>::type
  551. >::type
  552. operator%(const duration<Rep1, Period>& d, const Rep2& s)
  553. {
  554. typedef typename common_type<Rep1, Rep2>::type common_rep;
  555. typedef duration<common_rep, Period> common_duration;
  556. return common_duration(common_duration(d).count()%static_cast<common_rep>(s));
  557. }
  558. template <class Rep1, class Period1, class Rep2, class Period2>
  559. inline BOOST_CONSTEXPR
  560. typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
  561. operator%(const duration<Rep1, Period1>& lhs,
  562. const duration<Rep2, Period2>& rhs) {
  563. typedef typename common_type<duration<Rep1, Period1>,
  564. duration<Rep2, Period2> >::type common_duration;
  565. return common_duration(common_duration(lhs).count()%common_duration(rhs).count());
  566. }
  567. //----------------------------------------------------------------------------//
  568. // 20.9.3.6 duration comparisons [time.duration.comparisons] //
  569. //----------------------------------------------------------------------------//
  570. namespace detail
  571. {
  572. template <class LhsDuration, class RhsDuration>
  573. struct duration_eq
  574. {
  575. BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const RhsDuration& rhs) const
  576. {
  577. typedef typename common_type<LhsDuration, RhsDuration>::type common_duration;
  578. return common_duration(lhs).count() == common_duration(rhs).count();
  579. }
  580. };
  581. template <class LhsDuration>
  582. struct duration_eq<LhsDuration, LhsDuration>
  583. {
  584. BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const LhsDuration& rhs) const
  585. {
  586. return lhs.count() == rhs.count();
  587. }
  588. };
  589. template <class LhsDuration, class RhsDuration>
  590. struct duration_lt
  591. {
  592. BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const RhsDuration& rhs) const
  593. {
  594. typedef typename common_type<LhsDuration, RhsDuration>::type common_duration;
  595. return common_duration(lhs).count() < common_duration(rhs).count();
  596. }
  597. };
  598. template <class LhsDuration>
  599. struct duration_lt<LhsDuration, LhsDuration>
  600. {
  601. BOOST_CONSTEXPR bool operator()(const LhsDuration& lhs, const LhsDuration& rhs) const
  602. {
  603. return lhs.count() < rhs.count();
  604. }
  605. };
  606. } // namespace detail
  607. // Duration ==
  608. template <class Rep1, class Period1, class Rep2, class Period2>
  609. inline BOOST_CONSTEXPR
  610. bool
  611. operator==(const duration<Rep1, Period1>& lhs,
  612. const duration<Rep2, Period2>& rhs)
  613. {
  614. return boost::chrono::detail::duration_eq<
  615. duration<Rep1, Period1>, duration<Rep2, Period2> >()(lhs, rhs);
  616. }
  617. // Duration !=
  618. template <class Rep1, class Period1, class Rep2, class Period2>
  619. inline BOOST_CONSTEXPR
  620. bool
  621. operator!=(const duration<Rep1, Period1>& lhs,
  622. const duration<Rep2, Period2>& rhs)
  623. {
  624. return !(lhs == rhs);
  625. }
  626. // Duration <
  627. template <class Rep1, class Period1, class Rep2, class Period2>
  628. inline BOOST_CONSTEXPR
  629. bool
  630. operator< (const duration<Rep1, Period1>& lhs,
  631. const duration<Rep2, Period2>& rhs)
  632. {
  633. return boost::chrono::detail::duration_lt<
  634. duration<Rep1, Period1>, duration<Rep2, Period2> >()(lhs, rhs);
  635. }
  636. // Duration >
  637. template <class Rep1, class Period1, class Rep2, class Period2>
  638. inline BOOST_CONSTEXPR
  639. bool
  640. operator> (const duration<Rep1, Period1>& lhs,
  641. const duration<Rep2, Period2>& rhs)
  642. {
  643. return rhs < lhs;
  644. }
  645. // Duration <=
  646. template <class Rep1, class Period1, class Rep2, class Period2>
  647. inline BOOST_CONSTEXPR
  648. bool
  649. operator<=(const duration<Rep1, Period1>& lhs,
  650. const duration<Rep2, Period2>& rhs)
  651. {
  652. return !(rhs < lhs);
  653. }
  654. // Duration >=
  655. template <class Rep1, class Period1, class Rep2, class Period2>
  656. inline BOOST_CONSTEXPR
  657. bool
  658. operator>=(const duration<Rep1, Period1>& lhs,
  659. const duration<Rep2, Period2>& rhs)
  660. {
  661. return !(lhs < rhs);
  662. }
  663. //----------------------------------------------------------------------------//
  664. // 20.9.3.7 duration_cast [time.duration.cast] //
  665. //----------------------------------------------------------------------------//
  666. // Compile-time select the most efficient algorithm for the conversion...
  667. template <class ToDuration, class Rep, class Period>
  668. inline BOOST_CONSTEXPR
  669. typename boost::enable_if <
  670. boost::chrono::detail::is_duration<ToDuration>, ToDuration>::type
  671. duration_cast(const duration<Rep, Period>& fd)
  672. {
  673. return boost::chrono::detail::duration_cast<
  674. duration<Rep, Period>, ToDuration>()(fd);
  675. }
  676. } // namespace chrono
  677. } // namespace boost
  678. #ifndef BOOST_CHRONO_HEADER_ONLY
  679. // the suffix header occurs after all of our code:
  680. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  681. #endif
  682. #endif // BOOST_CHRONO_DURATION_HPP