duration.hpp 31 KB

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