chrono 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // <chrono> -*- C++ -*-
  2. // Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/chrono
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_CHRONO
  24. #define _GLIBCXX_CHRONO 1
  25. #pragma GCC system_header
  26. #ifndef __GXX_EXPERIMENTAL_CXX0X__
  27. # include <c++0x_warning.h>
  28. #else
  29. #ifdef _GLIBCXX_INCLUDE_AS_TR1
  30. # error C++0x header cannot be included from TR1 header
  31. #endif
  32. #include <ratio>
  33. #include <type_traits>
  34. #include <limits>
  35. #include <ctime>
  36. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  37. namespace std
  38. {
  39. /**
  40. * @defgroup chrono Time
  41. * @ingroup utilities
  42. *
  43. * Classes and functions for time.
  44. * @{
  45. */
  46. /** @namespace std::chrono
  47. * @brief ISO C++ 0x entities sub namespace for time and date.
  48. */
  49. namespace chrono
  50. {
  51. template<typename _Rep, typename _Period = ratio<1>>
  52. struct duration;
  53. template<typename _Clock, typename _Duration = typename _Clock::duration>
  54. struct time_point;
  55. }
  56. // 20.8.2.3 specialization of common_type (for duration)
  57. template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
  58. struct common_type<chrono::duration<_Rep1, _Period1>,
  59. chrono::duration<_Rep2, _Period2>>
  60. {
  61. typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
  62. ratio<__static_gcd<_Period1::num, _Period2::num>::value,
  63. (_Period1::den / __static_gcd<_Period1::den, _Period2::den>::value)
  64. * _Period2::den>> type;
  65. };
  66. // 20.8.2.3 specialization of common_type (for time_point)
  67. template<typename _Clock, typename _Duration1, typename _Duration2>
  68. struct common_type<chrono::time_point<_Clock, _Duration1>,
  69. chrono::time_point<_Clock, _Duration2>>
  70. {
  71. typedef chrono::time_point<_Clock,
  72. typename common_type<_Duration1, _Duration2>::type> type;
  73. };
  74. namespace chrono
  75. {
  76. // Primary template for duration_cast impl.
  77. template<typename _ToDuration, typename _CF, typename _CR,
  78. bool _NumIsOne = false, bool _DenIsOne = false>
  79. struct __duration_cast_impl
  80. {
  81. template<typename _Rep, typename _Period>
  82. static _ToDuration __cast(const duration<_Rep, _Period>& __d)
  83. {
  84. return _ToDuration(static_cast<
  85. typename _ToDuration::rep>(static_cast<_CR>(__d.count())
  86. * static_cast<_CR>(_CF::num)
  87. / static_cast<_CR>(_CF::den)));
  88. }
  89. };
  90. template<typename _ToDuration, typename _CF, typename _CR>
  91. struct __duration_cast_impl<_ToDuration, _CF, _CR, true, true>
  92. {
  93. template<typename _Rep, typename _Period>
  94. static _ToDuration __cast(const duration<_Rep, _Period>& __d)
  95. {
  96. return _ToDuration(
  97. static_cast<typename _ToDuration::rep>(__d.count()));
  98. }
  99. };
  100. template<typename _ToDuration, typename _CF, typename _CR>
  101. struct __duration_cast_impl<_ToDuration, _CF, _CR, true, false>
  102. {
  103. template<typename _Rep, typename _Period>
  104. static _ToDuration __cast(const duration<_Rep, _Period>& __d)
  105. {
  106. return _ToDuration(static_cast<typename _ToDuration::rep>(
  107. static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
  108. }
  109. };
  110. template<typename _ToDuration, typename _CF, typename _CR>
  111. struct __duration_cast_impl<_ToDuration, _CF, _CR, false, true>
  112. {
  113. template<typename _Rep, typename _Period>
  114. static _ToDuration __cast(const duration<_Rep, _Period>& __d)
  115. {
  116. return _ToDuration(static_cast<typename _ToDuration::rep>(
  117. static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
  118. }
  119. };
  120. /// duration_cast
  121. template<typename _ToDuration, typename _Rep, typename _Period>
  122. inline _ToDuration
  123. duration_cast(const duration<_Rep, _Period>& __d)
  124. {
  125. typedef typename
  126. ratio_divide<_Period, typename _ToDuration::period>::type __cf;
  127. typedef typename
  128. common_type<typename _ToDuration::rep, _Rep, intmax_t>::type __cr;
  129. return __duration_cast_impl<_ToDuration, __cf, __cr,
  130. __cf::num == 1, __cf::den == 1>::__cast(__d);
  131. }
  132. /// treat_as_floating_point
  133. template<typename _Rep>
  134. struct treat_as_floating_point
  135. : is_floating_point<_Rep>
  136. { };
  137. /// duration_values
  138. template<typename _Rep>
  139. struct duration_values
  140. {
  141. static const _Rep
  142. zero()
  143. { return _Rep(0); }
  144. static const _Rep
  145. max()
  146. { return numeric_limits<_Rep>::max(); }
  147. static const _Rep
  148. min()
  149. { return numeric_limits<_Rep>::min(); }
  150. };
  151. template<typename _Tp>
  152. struct __is_duration
  153. : std::false_type
  154. { };
  155. template<typename _Rep, typename _Period>
  156. struct __is_duration<duration<_Rep, _Period>>
  157. : std::true_type
  158. { };
  159. template<typename T>
  160. struct __is_ratio
  161. : std::false_type
  162. { };
  163. template<intmax_t _Num, intmax_t _Den>
  164. struct __is_ratio<ratio<_Num, _Den>>
  165. : std::true_type
  166. { };
  167. /// duration
  168. template<typename _Rep, typename _Period>
  169. struct duration
  170. {
  171. static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
  172. static_assert(__is_ratio<_Period>::value,
  173. "period must be a specialization of ratio");
  174. static_assert(_Period::num > 0, "period must be positive");
  175. typedef _Rep rep;
  176. typedef _Period period;
  177. // 20.8.3.1 construction / copy / destroy
  178. duration() = default;
  179. template<typename _Rep2>
  180. explicit duration(_Rep2 const& __rep)
  181. : __r(static_cast<rep>(__rep))
  182. {
  183. static_assert(is_convertible<_Rep2,rep>::value
  184. && (treat_as_floating_point<rep>::value
  185. || !treat_as_floating_point<_Rep2>::value),
  186. "cannot construct integral duration with floating point type");
  187. }
  188. template<typename _Rep2, typename _Period2>
  189. duration(const duration<_Rep2, _Period2>& __d)
  190. : __r(duration_cast<duration>(__d).count())
  191. {
  192. static_assert(treat_as_floating_point<rep>::value == true
  193. || ratio_divide<_Period2, period>::type::den == 1,
  194. "the resulting duration is not exactly representable");
  195. }
  196. ~duration() = default;
  197. duration(const duration&) = default;
  198. duration& operator=(const duration&) = default;
  199. // 20.8.3.2 observer
  200. rep
  201. count() const
  202. { return __r; }
  203. // 20.8.3.3 arithmetic
  204. duration
  205. operator+() const
  206. { return *this; }
  207. duration
  208. operator-() const
  209. { return duration(-__r); }
  210. duration&
  211. operator++()
  212. {
  213. ++__r;
  214. return *this;
  215. }
  216. duration
  217. operator++(int)
  218. { return duration(__r++); }
  219. duration&
  220. operator--()
  221. {
  222. --__r;
  223. return *this;
  224. }
  225. duration
  226. operator--(int)
  227. { return duration(__r--); }
  228. duration&
  229. operator+=(const duration& __d)
  230. {
  231. __r += __d.count();
  232. return *this;
  233. }
  234. duration&
  235. operator-=(const duration& __d)
  236. {
  237. __r -= __d.count();
  238. return *this;
  239. }
  240. duration&
  241. operator*=(const rep& __rhs)
  242. {
  243. __r *= __rhs;
  244. return *this;
  245. }
  246. duration&
  247. operator/=(const rep& __rhs)
  248. {
  249. __r /= __rhs;
  250. return *this;
  251. }
  252. // 20.8.3.4 special values
  253. // TODO: These should be constexprs.
  254. static const duration
  255. zero()
  256. { return duration(duration_values<rep>::zero()); }
  257. static const duration
  258. min()
  259. { return duration(duration_values<rep>::min()); }
  260. static const duration
  261. max()
  262. { return duration(duration_values<rep>::max()); }
  263. private:
  264. rep __r;
  265. };
  266. template<typename _Rep1, typename _Period1,
  267. typename _Rep2, typename _Period2>
  268. inline typename common_type<duration<_Rep1, _Period1>,
  269. duration<_Rep2, _Period2>>::type
  270. operator+(const duration<_Rep1, _Period1>& __lhs,
  271. const duration<_Rep2, _Period2>& __rhs)
  272. {
  273. typedef typename common_type<duration<_Rep1, _Period1>,
  274. duration<_Rep2, _Period2>>::type __ct;
  275. return __ct(__lhs) += __rhs;
  276. }
  277. template<typename _Rep1, typename _Period1,
  278. typename _Rep2, typename _Period2>
  279. inline typename common_type<duration<_Rep1, _Period1>,
  280. duration<_Rep2, _Period2>>::type
  281. operator-(const duration<_Rep1, _Period1>& __lhs,
  282. const duration<_Rep2, _Period2>& __rhs)
  283. {
  284. typedef typename common_type<duration<_Rep1, _Period1>,
  285. duration<_Rep2, _Period2>>::type __ct;
  286. return __ct(__lhs) -= __rhs;
  287. }
  288. template<typename _Rep1, typename _Period, typename _Rep2>
  289. inline duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  290. operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  291. {
  292. typedef typename common_type<_Rep1, _Rep2>::type __cr;
  293. return duration<__cr, _Period>(__d) *= __s;
  294. }
  295. template<typename _Rep1, typename _Period, typename _Rep2>
  296. inline duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  297. operator*(const _Rep2& __s, const duration<_Rep1, _Period>& __d)
  298. { return __d * __s; }
  299. template<typename _Tp, typename _Up, typename _Ep = void>
  300. struct __division_impl;
  301. template<typename _Rep1, typename _Period, typename _Rep2>
  302. struct __division_impl<duration<_Rep1, _Period>, _Rep2,
  303. typename enable_if<!__is_duration<_Rep2>::value>::type>
  304. {
  305. typedef typename common_type<_Rep1, _Rep2>::type __cr;
  306. typedef
  307. duration<typename common_type<_Rep1, _Rep2>::type, _Period> __rt;
  308. static __rt
  309. __divide(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  310. { return duration<__cr, _Period>(__d) /= __s; }
  311. };
  312. template<typename _Rep1, typename _Period1,
  313. typename _Rep2, typename _Period2>
  314. struct __division_impl<duration<_Rep1, _Period1>,
  315. duration<_Rep2, _Period2>>
  316. {
  317. typedef typename common_type<duration<_Rep1, _Period1>,
  318. duration<_Rep2, _Period2>>::type __ct;
  319. typedef typename common_type<_Rep1, _Rep2>::type __rt;
  320. static __rt
  321. __divide(const duration<_Rep1, _Period1>& __lhs,
  322. const duration<_Rep2, _Period2>& __rhs)
  323. { return __ct(__lhs).count() / __ct(__rhs).count(); }
  324. };
  325. template<typename _Rep, typename _Period, typename _Up>
  326. inline typename __division_impl<duration<_Rep, _Period>, _Up>::__rt
  327. operator/(const duration<_Rep, _Period>& __d, const _Up& __u)
  328. {
  329. return
  330. __division_impl<duration<_Rep, _Period>, _Up>::__divide(__d, __u);
  331. }
  332. // comparisons
  333. template<typename _Rep1, typename _Period1,
  334. typename _Rep2, typename _Period2>
  335. inline bool
  336. operator==(const duration<_Rep1, _Period1>& __lhs,
  337. const duration<_Rep2, _Period2>& __rhs)
  338. {
  339. typedef typename common_type<duration<_Rep1, _Period1>,
  340. duration<_Rep2, _Period2>>::type __ct;
  341. return __ct(__lhs).count() == __ct(__rhs).count();
  342. }
  343. template<typename _Rep1, typename _Period1,
  344. typename _Rep2, typename _Period2>
  345. inline bool
  346. operator<(const duration<_Rep1, _Period1>& __lhs,
  347. const duration<_Rep2, _Period2>& __rhs)
  348. {
  349. typedef typename common_type<duration<_Rep1, _Period1>,
  350. duration<_Rep2, _Period2>>::type __ct;
  351. return __ct(__lhs).count() < __ct(__rhs).count();
  352. }
  353. template<typename _Rep1, typename _Period1,
  354. typename _Rep2, typename _Period2>
  355. inline bool
  356. operator!=(const duration<_Rep1, _Period1>& __lhs,
  357. const duration<_Rep2, _Period2>& __rhs)
  358. { return !(__lhs == __rhs); }
  359. template<typename _Rep1, typename _Period1,
  360. typename _Rep2, typename _Period2>
  361. inline bool
  362. operator<=(const duration<_Rep1, _Period1>& __lhs,
  363. const duration<_Rep2, _Period2>& __rhs)
  364. { return !(__rhs < __lhs); }
  365. template<typename _Rep1, typename _Period1,
  366. typename _Rep2, typename _Period2>
  367. inline bool
  368. operator>(const duration<_Rep1, _Period1>& __lhs,
  369. const duration<_Rep2, _Period2>& __rhs)
  370. { return __rhs < __lhs; }
  371. template<typename _Rep1, typename _Period1,
  372. typename _Rep2, typename _Period2>
  373. inline bool
  374. operator>=(const duration<_Rep1, _Period1>& __lhs,
  375. const duration<_Rep2, _Period2>& __rhs)
  376. { return !(__lhs < __rhs); }
  377. /// nanoseconds
  378. typedef duration<int64_t, nano> nanoseconds;
  379. /// microseconds
  380. typedef duration<int64_t, micro> microseconds;
  381. /// milliseconds
  382. typedef duration<int64_t, milli> milliseconds;
  383. /// seconds
  384. typedef duration<int64_t > seconds;
  385. /// minutes
  386. typedef duration<int, ratio< 60>> minutes;
  387. /// hours
  388. typedef duration<int, ratio<3600>> hours;
  389. /// time_point
  390. template<typename _Clock, typename _Duration>
  391. struct time_point
  392. {
  393. typedef _Clock clock;
  394. typedef _Duration duration;
  395. typedef typename duration::rep rep;
  396. typedef typename duration::period period;
  397. time_point() : __d(duration::zero())
  398. { }
  399. explicit time_point(const duration& __dur)
  400. : __d(duration::zero() + __dur)
  401. { }
  402. // conversions
  403. template<typename _Duration2>
  404. time_point(const time_point<clock, _Duration2>& __t)
  405. : __d(__t.time_since_epoch())
  406. { }
  407. // observer
  408. duration
  409. time_since_epoch() const
  410. { return __d; }
  411. // arithmetic
  412. time_point&
  413. operator+=(const duration& __dur)
  414. {
  415. __d += __dur;
  416. return *this;
  417. }
  418. time_point&
  419. operator-=(const duration& __dur)
  420. {
  421. __d -= __dur;
  422. return *this;
  423. }
  424. // special values
  425. // TODO: These should be constexprs.
  426. static const time_point
  427. min()
  428. { return time_point(duration::min()); }
  429. static const time_point
  430. max()
  431. { return time_point(duration::max()); }
  432. private:
  433. duration __d;
  434. };
  435. /// time_point_cast
  436. template<typename _ToDuration, typename _Clock, typename _Duration>
  437. inline time_point<_Clock, _ToDuration>
  438. time_point_cast(const time_point<_Clock, _Duration>& __t)
  439. {
  440. return time_point<_Clock, _ToDuration>(
  441. duration_cast<_ToDuration>(__t.time_since_epoch()));
  442. }
  443. template<typename _Clock, typename _Duration1,
  444. typename _Rep2, typename _Period2>
  445. inline time_point<_Clock,
  446. typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
  447. operator+(const time_point<_Clock, _Duration1>& __lhs,
  448. const duration<_Rep2, _Period2>& __rhs)
  449. {
  450. typedef time_point<_Clock,
  451. typename common_type<_Duration1,
  452. duration<_Rep2, _Period2>>::type> __ct;
  453. return __ct(__lhs) += __rhs;
  454. }
  455. template<typename _Rep1, typename _Period1,
  456. typename _Clock, typename _Duration2>
  457. inline time_point<_Clock,
  458. typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
  459. operator+(const duration<_Rep1, _Period1>& __lhs,
  460. const time_point<_Clock, _Duration2>& __rhs)
  461. { return __rhs + __lhs; }
  462. template<typename _Clock, typename _Duration1,
  463. typename _Rep2, typename _Period2>
  464. inline time_point<_Clock,
  465. typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
  466. operator-(const time_point<_Clock, _Duration1>& __lhs,
  467. const duration<_Rep2, _Period2>& __rhs)
  468. { return __lhs + (-__rhs); }
  469. template<typename _Clock, typename _Duration1, typename _Duration2>
  470. inline typename common_type<_Duration1, _Duration2>::type
  471. operator-(const time_point<_Clock, _Duration1>& __lhs,
  472. const time_point<_Clock, _Duration2>& __rhs)
  473. { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
  474. template<typename _Clock, typename _Duration1, typename _Duration2>
  475. inline bool
  476. operator==(const time_point<_Clock, _Duration1>& __lhs,
  477. const time_point<_Clock, _Duration2>& __rhs)
  478. { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
  479. template<typename _Clock, typename _Duration1, typename _Duration2>
  480. inline bool
  481. operator!=(const time_point<_Clock, _Duration1>& __lhs,
  482. const time_point<_Clock, _Duration2>& __rhs)
  483. { return !(__lhs == __rhs); }
  484. template<typename _Clock, typename _Duration1, typename _Duration2>
  485. inline bool
  486. operator<(const time_point<_Clock, _Duration1>& __lhs,
  487. const time_point<_Clock, _Duration2>& __rhs)
  488. { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
  489. template<typename _Clock, typename _Duration1, typename _Duration2>
  490. inline bool
  491. operator<=(const time_point<_Clock, _Duration1>& __lhs,
  492. const time_point<_Clock, _Duration2>& __rhs)
  493. { return !(__rhs < __lhs); }
  494. template<typename _Clock, typename _Duration1, typename _Duration2>
  495. inline bool
  496. operator>(const time_point<_Clock, _Duration1>& __lhs,
  497. const time_point<_Clock, _Duration2>& __rhs)
  498. { return __rhs < __lhs; }
  499. template<typename _Clock, typename _Duration1, typename _Duration2>
  500. inline bool
  501. operator>=(const time_point<_Clock, _Duration1>& __lhs,
  502. const time_point<_Clock, _Duration2>& __rhs)
  503. { return !(__lhs < __rhs); }
  504. /// system_clock
  505. struct system_clock
  506. {
  507. #ifdef _GLIBCXX_USE_CLOCK_REALTIME
  508. typedef chrono::nanoseconds duration;
  509. #elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
  510. typedef chrono::microseconds duration;
  511. #else
  512. typedef chrono::seconds duration;
  513. #endif
  514. typedef duration::rep rep;
  515. typedef duration::period period;
  516. typedef chrono::time_point<system_clock, duration> time_point;
  517. static const bool is_monotonic = false;
  518. static time_point
  519. now();
  520. // Map to C API
  521. static std::time_t
  522. to_time_t(const time_point& __t)
  523. {
  524. return std::time_t(
  525. duration_cast<chrono::seconds>(__t.time_since_epoch()).count());
  526. }
  527. static time_point
  528. from_time_t(std::time_t __t)
  529. {
  530. return time_point_cast<system_clock::duration>(
  531. chrono::time_point<system_clock, chrono::seconds>(
  532. chrono::seconds(__t)));
  533. }
  534. // TODO: requires constexpr
  535. /*
  536. static_assert(
  537. system_clock::duration::min() <
  538. system_clock::duration::zero(),
  539. "a clock's minimum duration cannot be less than its epoch");
  540. */
  541. };
  542. #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
  543. /// monotonic_clock
  544. struct monotonic_clock
  545. {
  546. typedef chrono::nanoseconds duration;
  547. typedef duration::rep rep;
  548. typedef duration::period period;
  549. typedef chrono::time_point<monotonic_clock, duration> time_point;
  550. static const bool is_monotonic = true;
  551. static time_point
  552. now();
  553. };
  554. #else
  555. typedef system_clock monotonic_clock;
  556. #endif
  557. typedef system_clock high_resolution_clock;
  558. } // namespace chrono
  559. // @} group chrono
  560. } // namespace std
  561. #endif //_GLIBCXX_USE_C99_STDINT_TR1
  562. #endif //__GXX_EXPERIMENTAL_CXX0X__
  563. #endif //_GLIBCXX_CHRONO