00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _GLIBCXX_CHRONO
00030 #define _GLIBCXX_CHRONO 1
00031
00032 #pragma GCC system_header
00033
00034 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00035 # include <bits/c++0x_warning.h>
00036 #else
00037
00038 #ifdef _GLIBCXX_INCLUDE_AS_TR1
00039 # error C++0x header cannot be included from TR1 header
00040 #endif
00041
00042 #include <ratio>
00043 #include <type_traits>
00044 #include <limits>
00045 #include <ctime>
00046
00047 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
00048
00049 namespace std
00050 {
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 namespace chrono
00063 {
00064 template<typename _Rep, typename _Period = ratio<1>>
00065 struct duration;
00066
00067 template<typename _Clock, typename _Duration = typename _Clock::duration>
00068 struct time_point;
00069 }
00070
00071
00072 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
00073 struct common_type<chrono::duration<_Rep1, _Period1>,
00074 chrono::duration<_Rep2, _Period2>>
00075 {
00076 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
00077 ratio<__static_gcd<_Period1::num, _Period2::num>::value,
00078 (_Period1::den / __static_gcd<_Period1::den, _Period2::den>::value)
00079 * _Period2::den>> type;
00080 };
00081
00082
00083 template<typename _Clock, typename _Duration1, typename _Duration2>
00084 struct common_type<chrono::time_point<_Clock, _Duration1>,
00085 chrono::time_point<_Clock, _Duration2>>
00086 {
00087 typedef chrono::time_point<_Clock,
00088 typename common_type<_Duration1, _Duration2>::type> type;
00089 };
00090
00091 namespace chrono
00092 {
00093
00094 template<typename _ToDuration, typename _CF, typename _CR,
00095 bool _NumIsOne = false, bool _DenIsOne = false>
00096 struct __duration_cast_impl
00097 {
00098 template<typename _Rep, typename _Period>
00099 static _ToDuration __cast(const duration<_Rep, _Period>& __d)
00100 {
00101 return _ToDuration(static_cast<
00102 typename _ToDuration::rep>(static_cast<_CR>(__d.count())
00103 * static_cast<_CR>(_CF::num)
00104 / static_cast<_CR>(_CF::den)));
00105 }
00106 };
00107
00108 template<typename _ToDuration, typename _CF, typename _CR>
00109 struct __duration_cast_impl<_ToDuration, _CF, _CR, true, true>
00110 {
00111 template<typename _Rep, typename _Period>
00112 static _ToDuration __cast(const duration<_Rep, _Period>& __d)
00113 {
00114 return _ToDuration(
00115 static_cast<typename _ToDuration::rep>(__d.count()));
00116 }
00117 };
00118
00119 template<typename _ToDuration, typename _CF, typename _CR>
00120 struct __duration_cast_impl<_ToDuration, _CF, _CR, true, false>
00121 {
00122 template<typename _Rep, typename _Period>
00123 static _ToDuration __cast(const duration<_Rep, _Period>& __d)
00124 {
00125 return _ToDuration(static_cast<typename _ToDuration::rep>(
00126 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
00127 }
00128 };
00129
00130 template<typename _ToDuration, typename _CF, typename _CR>
00131 struct __duration_cast_impl<_ToDuration, _CF, _CR, false, true>
00132 {
00133 template<typename _Rep, typename _Period>
00134 static _ToDuration __cast(const duration<_Rep, _Period>& __d)
00135 {
00136 return _ToDuration(static_cast<typename _ToDuration::rep>(
00137 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
00138 }
00139 };
00140
00141 template<typename _Tp>
00142 struct __is_duration
00143 : std::false_type
00144 { };
00145
00146 template<typename _Rep, typename _Period>
00147 struct __is_duration<duration<_Rep, _Period>>
00148 : std::true_type
00149 { };
00150
00151
00152 template<typename _ToDuration, typename _Rep, typename _Period>
00153 inline typename enable_if<__is_duration<_ToDuration>::value,
00154 _ToDuration>::type
00155 duration_cast(const duration<_Rep, _Period>& __d)
00156 {
00157 typedef typename
00158 ratio_divide<_Period, typename _ToDuration::period>::type __cf;
00159 typedef typename
00160 common_type<typename _ToDuration::rep, _Rep, intmax_t>::type __cr;
00161
00162 return __duration_cast_impl<_ToDuration, __cf, __cr,
00163 __cf::num == 1, __cf::den == 1>::__cast(__d);
00164 }
00165
00166
00167 template<typename _Rep>
00168 struct treat_as_floating_point
00169 : is_floating_point<_Rep>
00170 { };
00171
00172
00173 template<typename _Rep>
00174 struct duration_values
00175 {
00176 static const _Rep
00177 zero()
00178 { return _Rep(0); }
00179
00180 static const _Rep
00181 max()
00182 { return numeric_limits<_Rep>::max(); }
00183
00184 static const _Rep
00185 min()
00186 { return numeric_limits<_Rep>::min(); }
00187 };
00188
00189 template<typename T>
00190 struct __is_ratio
00191 : std::false_type
00192 { };
00193
00194 template<intmax_t _Num, intmax_t _Den>
00195 struct __is_ratio<ratio<_Num, _Den>>
00196 : std::true_type
00197 { };
00198
00199
00200 template<typename _Rep, typename _Period>
00201 struct duration
00202 {
00203 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
00204 static_assert(__is_ratio<_Period>::value,
00205 "period must be a specialization of ratio");
00206 static_assert(_Period::num > 0, "period must be positive");
00207
00208 typedef _Rep rep;
00209 typedef _Period period;
00210
00211
00212 duration() = default;
00213
00214 template<typename _Rep2, typename = typename
00215 enable_if<is_convertible<_Rep2, rep>::value
00216 && (treat_as_floating_point<rep>::value
00217 || !treat_as_floating_point<_Rep2>::value)>::type>
00218 explicit duration(const _Rep2& __rep)
00219 : __r(static_cast<rep>(__rep)) { }
00220
00221 template<typename _Rep2, typename _Period2, typename = typename
00222 enable_if<treat_as_floating_point<rep>::value
00223 || (ratio_divide<_Period2, period>::type::den == 1
00224 && !treat_as_floating_point<_Rep2>::value)>::type>
00225 duration(const duration<_Rep2, _Period2>& __d)
00226 : __r(duration_cast<duration>(__d).count()) { }
00227
00228 ~duration() = default;
00229 duration(const duration&) = default;
00230 duration& operator=(const duration&) = default;
00231
00232
00233 rep
00234 count() const
00235 { return __r; }
00236
00237
00238 duration
00239 operator+() const
00240 { return *this; }
00241
00242 duration
00243 operator-() const
00244 { return duration(-__r); }
00245
00246 duration&
00247 operator++()
00248 {
00249 ++__r;
00250 return *this;
00251 }
00252
00253 duration
00254 operator++(int)
00255 { return duration(__r++); }
00256
00257 duration&
00258 operator--()
00259 {
00260 --__r;
00261 return *this;
00262 }
00263
00264 duration
00265 operator--(int)
00266 { return duration(__r--); }
00267
00268 duration&
00269 operator+=(const duration& __d)
00270 {
00271 __r += __d.count();
00272 return *this;
00273 }
00274
00275 duration&
00276 operator-=(const duration& __d)
00277 {
00278 __r -= __d.count();
00279 return *this;
00280 }
00281
00282 duration&
00283 operator*=(const rep& __rhs)
00284 {
00285 __r *= __rhs;
00286 return *this;
00287 }
00288
00289 duration&
00290 operator/=(const rep& __rhs)
00291 {
00292 __r /= __rhs;
00293 return *this;
00294 }
00295
00296
00297 template<typename _Rep2 = rep>
00298 typename enable_if<!treat_as_floating_point<_Rep2>::value,
00299 duration&>::type
00300 operator%=(const rep& __rhs)
00301 {
00302 __r %= __rhs;
00303 return *this;
00304 }
00305
00306 template<typename _Rep2 = rep>
00307 typename enable_if<!treat_as_floating_point<_Rep2>::value,
00308 duration&>::type
00309 operator%=(const duration& __d)
00310 {
00311 __r %= __d.count();
00312 return *this;
00313 }
00314
00315
00316
00317 static const duration
00318 zero()
00319 { return duration(duration_values<rep>::zero()); }
00320
00321 static const duration
00322 min()
00323 { return duration(duration_values<rep>::min()); }
00324
00325 static const duration
00326 max()
00327 { return duration(duration_values<rep>::max()); }
00328
00329 private:
00330 rep __r;
00331 };
00332
00333 template<typename _Rep1, typename _Period1,
00334 typename _Rep2, typename _Period2>
00335 inline typename common_type<duration<_Rep1, _Period1>,
00336 duration<_Rep2, _Period2>>::type
00337 operator+(const duration<_Rep1, _Period1>& __lhs,
00338 const duration<_Rep2, _Period2>& __rhs)
00339 {
00340 typedef typename common_type<duration<_Rep1, _Period1>,
00341 duration<_Rep2, _Period2>>::type __ct;
00342 return __ct(__lhs) += __rhs;
00343 }
00344
00345 template<typename _Rep1, typename _Period1,
00346 typename _Rep2, typename _Period2>
00347 inline typename common_type<duration<_Rep1, _Period1>,
00348 duration<_Rep2, _Period2>>::type
00349 operator-(const duration<_Rep1, _Period1>& __lhs,
00350 const duration<_Rep2, _Period2>& __rhs)
00351 {
00352 typedef typename common_type<duration<_Rep1, _Period1>,
00353 duration<_Rep2, _Period2>>::type __ct;
00354 return __ct(__lhs) -= __rhs;
00355 }
00356
00357 template<typename _Rep1, typename _Rep2, bool =
00358 is_convertible<_Rep2,
00359 typename common_type<_Rep1, _Rep2>::type>::value>
00360 struct __common_rep_type { };
00361
00362 template<typename _Rep1, typename _Rep2>
00363 struct __common_rep_type<_Rep1, _Rep2, true>
00364 { typedef typename common_type<_Rep1, _Rep2>::type type; };
00365
00366 template<typename _Rep1, typename _Period, typename _Rep2>
00367 inline duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
00368 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00369 {
00370 typedef typename common_type<_Rep1, _Rep2>::type __cr;
00371 return duration<__cr, _Period>(__d) *= __s;
00372 }
00373
00374 template<typename _Rep1, typename _Period, typename _Rep2>
00375 inline duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
00376 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
00377 { return __d * __s; }
00378
00379 template<typename _Rep1, typename _Period, typename _Rep2>
00380 inline duration<typename __common_rep_type<_Rep1, typename
00381 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
00382 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00383 {
00384 typedef typename common_type<_Rep1, _Rep2>::type __cr;
00385 return duration<__cr, _Period>(__d) /= __s;
00386 }
00387
00388 template<typename _Rep1, typename _Period1,
00389 typename _Rep2, typename _Period2>
00390 inline typename common_type<_Rep1, _Rep2>::type
00391 operator/(const duration<_Rep1, _Period1>& __lhs,
00392 const duration<_Rep2, _Period2>& __rhs)
00393 {
00394 typedef typename common_type<duration<_Rep1, _Period1>,
00395 duration<_Rep2, _Period2>>::type __ct;
00396 return __ct(__lhs).count() / __ct(__rhs).count();
00397 }
00398
00399
00400 template<typename _Rep1, typename _Period, typename _Rep2>
00401 inline duration<typename __common_rep_type<_Rep1, typename
00402 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
00403 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
00404 {
00405 typedef typename common_type<_Rep1, _Rep2>::type __cr;
00406 return duration<__cr, _Period>(__d) %= __s;
00407 }
00408
00409 template<typename _Rep1, typename _Period1,
00410 typename _Rep2, typename _Period2>
00411 inline typename common_type<duration<_Rep1, _Period1>,
00412 duration<_Rep2, _Period2>>::type
00413 operator%(const duration<_Rep1, _Period1>& __lhs,
00414 const duration<_Rep2, _Period2>& __rhs)
00415 {
00416 typedef typename common_type<duration<_Rep1, _Period1>,
00417 duration<_Rep2, _Period2>>::type __ct;
00418 return __ct(__lhs) %= __rhs;
00419 }
00420
00421
00422 template<typename _Rep1, typename _Period1,
00423 typename _Rep2, typename _Period2>
00424 inline bool
00425 operator==(const duration<_Rep1, _Period1>& __lhs,
00426 const duration<_Rep2, _Period2>& __rhs)
00427 {
00428 typedef typename common_type<duration<_Rep1, _Period1>,
00429 duration<_Rep2, _Period2>>::type __ct;
00430 return __ct(__lhs).count() == __ct(__rhs).count();
00431 }
00432
00433 template<typename _Rep1, typename _Period1,
00434 typename _Rep2, typename _Period2>
00435 inline bool
00436 operator<(const duration<_Rep1, _Period1>& __lhs,
00437 const duration<_Rep2, _Period2>& __rhs)
00438 {
00439 typedef typename common_type<duration<_Rep1, _Period1>,
00440 duration<_Rep2, _Period2>>::type __ct;
00441 return __ct(__lhs).count() < __ct(__rhs).count();
00442 }
00443
00444 template<typename _Rep1, typename _Period1,
00445 typename _Rep2, typename _Period2>
00446 inline bool
00447 operator!=(const duration<_Rep1, _Period1>& __lhs,
00448 const duration<_Rep2, _Period2>& __rhs)
00449 { return !(__lhs == __rhs); }
00450
00451 template<typename _Rep1, typename _Period1,
00452 typename _Rep2, typename _Period2>
00453 inline bool
00454 operator<=(const duration<_Rep1, _Period1>& __lhs,
00455 const duration<_Rep2, _Period2>& __rhs)
00456 { return !(__rhs < __lhs); }
00457
00458 template<typename _Rep1, typename _Period1,
00459 typename _Rep2, typename _Period2>
00460 inline bool
00461 operator>(const duration<_Rep1, _Period1>& __lhs,
00462 const duration<_Rep2, _Period2>& __rhs)
00463 { return __rhs < __lhs; }
00464
00465 template<typename _Rep1, typename _Period1,
00466 typename _Rep2, typename _Period2>
00467 inline bool
00468 operator>=(const duration<_Rep1, _Period1>& __lhs,
00469 const duration<_Rep2, _Period2>& __rhs)
00470 { return !(__lhs < __rhs); }
00471
00472
00473 typedef duration<int64_t, nano> nanoseconds;
00474
00475
00476 typedef duration<int64_t, micro> microseconds;
00477
00478
00479 typedef duration<int64_t, milli> milliseconds;
00480
00481
00482 typedef duration<int64_t > seconds;
00483
00484
00485 typedef duration<int, ratio< 60>> minutes;
00486
00487
00488 typedef duration<int, ratio<3600>> hours;
00489
00490
00491 template<typename _Clock, typename _Duration>
00492 struct time_point
00493 {
00494 typedef _Clock clock;
00495 typedef _Duration duration;
00496 typedef typename duration::rep rep;
00497 typedef typename duration::period period;
00498
00499 time_point() : __d(duration::zero())
00500 { }
00501
00502 explicit time_point(const duration& __dur)
00503 : __d(duration::zero() + __dur)
00504 { }
00505
00506
00507 template<typename _Duration2>
00508 time_point(const time_point<clock, _Duration2>& __t)
00509 : __d(__t.time_since_epoch())
00510 { }
00511
00512
00513 duration
00514 time_since_epoch() const
00515 { return __d; }
00516
00517
00518 time_point&
00519 operator+=(const duration& __dur)
00520 {
00521 __d += __dur;
00522 return *this;
00523 }
00524
00525 time_point&
00526 operator-=(const duration& __dur)
00527 {
00528 __d -= __dur;
00529 return *this;
00530 }
00531
00532
00533
00534 static const time_point
00535 min()
00536 { return time_point(duration::min()); }
00537
00538 static const time_point
00539 max()
00540 { return time_point(duration::max()); }
00541
00542 private:
00543 duration __d;
00544 };
00545
00546
00547 template<typename _ToDuration, typename _Clock, typename _Duration>
00548 inline typename enable_if<__is_duration<_ToDuration>::value,
00549 time_point<_Clock, _ToDuration>>::type
00550 time_point_cast(const time_point<_Clock, _Duration>& __t)
00551 {
00552 return time_point<_Clock, _ToDuration>(
00553 duration_cast<_ToDuration>(__t.time_since_epoch()));
00554 }
00555
00556 template<typename _Clock, typename _Duration1,
00557 typename _Rep2, typename _Period2>
00558 inline time_point<_Clock,
00559 typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
00560 operator+(const time_point<_Clock, _Duration1>& __lhs,
00561 const duration<_Rep2, _Period2>& __rhs)
00562 {
00563 typedef time_point<_Clock,
00564 typename common_type<_Duration1,
00565 duration<_Rep2, _Period2>>::type> __ct;
00566 return __ct(__lhs) += __rhs;
00567 }
00568
00569 template<typename _Rep1, typename _Period1,
00570 typename _Clock, typename _Duration2>
00571 inline time_point<_Clock,
00572 typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
00573 operator+(const duration<_Rep1, _Period1>& __lhs,
00574 const time_point<_Clock, _Duration2>& __rhs)
00575 { return __rhs + __lhs; }
00576
00577 template<typename _Clock, typename _Duration1,
00578 typename _Rep2, typename _Period2>
00579 inline time_point<_Clock,
00580 typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
00581 operator-(const time_point<_Clock, _Duration1>& __lhs,
00582 const duration<_Rep2, _Period2>& __rhs)
00583 { return __lhs + (-__rhs); }
00584
00585 template<typename _Clock, typename _Duration1, typename _Duration2>
00586 inline typename common_type<_Duration1, _Duration2>::type
00587 operator-(const time_point<_Clock, _Duration1>& __lhs,
00588 const time_point<_Clock, _Duration2>& __rhs)
00589 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
00590
00591 template<typename _Clock, typename _Duration1, typename _Duration2>
00592 inline bool
00593 operator==(const time_point<_Clock, _Duration1>& __lhs,
00594 const time_point<_Clock, _Duration2>& __rhs)
00595 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
00596
00597 template<typename _Clock, typename _Duration1, typename _Duration2>
00598 inline bool
00599 operator!=(const time_point<_Clock, _Duration1>& __lhs,
00600 const time_point<_Clock, _Duration2>& __rhs)
00601 { return !(__lhs == __rhs); }
00602
00603 template<typename _Clock, typename _Duration1, typename _Duration2>
00604 inline bool
00605 operator<(const time_point<_Clock, _Duration1>& __lhs,
00606 const time_point<_Clock, _Duration2>& __rhs)
00607 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
00608
00609 template<typename _Clock, typename _Duration1, typename _Duration2>
00610 inline bool
00611 operator<=(const time_point<_Clock, _Duration1>& __lhs,
00612 const time_point<_Clock, _Duration2>& __rhs)
00613 { return !(__rhs < __lhs); }
00614
00615 template<typename _Clock, typename _Duration1, typename _Duration2>
00616 inline bool
00617 operator>(const time_point<_Clock, _Duration1>& __lhs,
00618 const time_point<_Clock, _Duration2>& __rhs)
00619 { return __rhs < __lhs; }
00620
00621 template<typename _Clock, typename _Duration1, typename _Duration2>
00622 inline bool
00623 operator>=(const time_point<_Clock, _Duration1>& __lhs,
00624 const time_point<_Clock, _Duration2>& __rhs)
00625 { return !(__lhs < __rhs); }
00626
00627
00628 struct system_clock
00629 {
00630 #ifdef _GLIBCXX_USE_CLOCK_REALTIME
00631 typedef chrono::nanoseconds duration;
00632 #elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
00633 typedef chrono::microseconds duration;
00634 #else
00635 typedef chrono::seconds duration;
00636 #endif
00637
00638 typedef duration::rep rep;
00639 typedef duration::period period;
00640 typedef chrono::time_point<system_clock, duration> time_point;
00641
00642 static const bool is_monotonic = false;
00643
00644 static time_point
00645 now() throw ();
00646
00647
00648 static std::time_t
00649 to_time_t(const time_point& __t)
00650 {
00651 return std::time_t(
00652 duration_cast<chrono::seconds>(__t.time_since_epoch()).count());
00653 }
00654
00655 static time_point
00656 from_time_t(std::time_t __t)
00657 {
00658 return time_point_cast<system_clock::duration>(
00659 chrono::time_point<system_clock, chrono::seconds>(
00660 chrono::seconds(__t)));
00661 }
00662
00663
00664
00665
00666
00667
00668
00669
00670 };
00671
00672 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
00673
00674 struct monotonic_clock
00675 {
00676 typedef chrono::nanoseconds duration;
00677 typedef duration::rep rep;
00678 typedef duration::period period;
00679 typedef chrono::time_point<monotonic_clock, duration> time_point;
00680
00681 static const bool is_monotonic = true;
00682
00683 static time_point
00684 now();
00685 };
00686 #else
00687 typedef system_clock monotonic_clock;
00688 #endif
00689
00690 typedef system_clock high_resolution_clock;
00691 }
00692
00693
00694 }
00695
00696 #endif //_GLIBCXX_USE_C99_STDINT_TR1
00697
00698 #endif //__GXX_EXPERIMENTAL_CXX0X__
00699
00700 #endif //_GLIBCXX_CHRONO