/Src/Dependencies/Boost/boost/chrono/duration.hpp

http://hadesmem.googlecode.com/ · C++ Header · 786 lines · 553 code · 112 blank · 121 comment · 10 complexity · 2bd62a4ee4b204fdda5da5da4174d661 MD5 · raw file

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