Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- chrono ----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame^] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_CHRONO |
| 12 | #define _LIBCPP_CHRONO |
| 13 | |
| 14 | /* |
| 15 | chrono synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | namespace chrono |
| 20 | { |
| 21 | |
| 22 | template <class ToDuration, class Rep, class Period> |
| 23 | ToDuration |
| 24 | duration_cast(const duration<Rep, Period>& fd); |
| 25 | |
| 26 | template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {}; |
| 27 | |
| 28 | template <class Rep> |
| 29 | struct duration_values |
| 30 | { |
| 31 | public: |
| 32 | static Rep zero(); |
| 33 | static Rep max(); |
| 34 | static Rep min(); |
| 35 | }; |
| 36 | |
| 37 | // duration |
| 38 | |
| 39 | template <class Rep, class Period = ratio<1>> |
| 40 | class duration |
| 41 | { |
| 42 | static_assert(!__is_duration<Rep>::value, "A duration representation can not be a duration"); |
| 43 | static_assert(__is_ratio<Period>::value, "Second template parameter of duration must be a std::ratio"); |
| 44 | static_assert(Period::num > 0, "duration period must be positive"); |
| 45 | public: |
| 46 | typedef Rep rep; |
| 47 | typedef Period period; |
| 48 | |
| 49 | duration() = default; |
| 50 | template <class Rep2> |
| 51 | explicit duration(const Rep2& r, |
| 52 | typename enable_if |
| 53 | < |
| 54 | is_convertible<Rep2, rep>::value && |
| 55 | (treat_as_floating_point<rep>::value || |
| 56 | !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::value) |
| 57 | >::type* = 0); |
| 58 | |
| 59 | // conversions |
| 60 | template <class Rep2, class Period2> |
| 61 | duration(const duration<Rep2, Period2>& d, |
| 62 | typename enable_if |
| 63 | < |
| 64 | treat_as_floating_point<rep>::value || |
| 65 | ratio_divide<Period2, period>::type::den == 1 |
| 66 | >::type* = 0); |
| 67 | |
| 68 | // observer |
| 69 | |
| 70 | rep count() const; |
| 71 | |
| 72 | // arithmetic |
| 73 | |
| 74 | duration operator+() const; |
| 75 | duration operator-() const; |
| 76 | duration& operator++(); |
| 77 | duration operator++(int); |
| 78 | duration& operator--(); |
| 79 | duration operator--(int); |
| 80 | |
| 81 | duration& operator+=(const duration& d); |
| 82 | duration& operator-=(const duration& d); |
| 83 | |
| 84 | duration& operator*=(const rep& rhs); |
| 85 | duration& operator/=(const rep& rhs); |
| 86 | |
| 87 | // special values |
| 88 | |
| 89 | static duration zero(); |
| 90 | static duration min(); |
| 91 | static duration max(); |
| 92 | }; |
| 93 | |
| 94 | typedef duration<long long, nano> nanoseconds; |
| 95 | typedef duration<long long, micro> microseconds; |
| 96 | typedef duration<long long, milli> milliseconds; |
| 97 | typedef duration<long long > seconds; |
| 98 | typedef duration< long, ratio< 60> > minutes; |
| 99 | typedef duration< long, ratio<3600> > hours; |
| 100 | |
| 101 | template <class Clock, class Duration = typename Clock::duration> |
| 102 | class time_point |
| 103 | { |
| 104 | public: |
| 105 | typedef Clock clock; |
| 106 | typedef Duration duration; |
| 107 | typedef typename duration::rep rep; |
| 108 | typedef typename duration::period period; |
| 109 | private: |
| 110 | duration d_; // exposition only |
| 111 | |
| 112 | public: |
| 113 | time_point(); // has value "epoch" |
| 114 | explicit time_point(const duration& d); // same as time_point() + d |
| 115 | |
| 116 | // conversions |
| 117 | template <class Duration2> |
| 118 | time_point(const time_point<clock, Duration2>& t); |
| 119 | |
| 120 | // observer |
| 121 | |
| 122 | duration time_since_epoch() const; |
| 123 | |
| 124 | // arithmetic |
| 125 | |
| 126 | time_point& operator+=(const duration& d); |
| 127 | time_point& operator-=(const duration& d); |
| 128 | |
| 129 | // special values |
| 130 | |
| 131 | static constexpr time_point min(); |
| 132 | static constexpr time_point max(); |
| 133 | }; |
| 134 | |
| 135 | } // chrono |
| 136 | |
| 137 | // common_type traits |
| 138 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 139 | struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>; |
| 140 | |
| 141 | template <class Clock, class Duration1, class Duration2> |
| 142 | struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>; |
| 143 | |
| 144 | namespace chrono { |
| 145 | |
| 146 | // duration arithmetic |
| 147 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 148 | typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type |
| 149 | operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 150 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 151 | typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type |
| 152 | operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 153 | template <class Rep1, class Period, class Rep2> |
| 154 | duration<typename common_type<Rep1, Rep2>::type, Period> |
| 155 | operator*(const duration<Rep1, Period>& d, const Rep2& s); |
| 156 | template <class Rep1, class Period, class Rep2> |
| 157 | duration<typename common_type<Rep1, Rep2>::type, Period> |
| 158 | operator*(const Rep1& s, const duration<Rep2, Period>& d); |
| 159 | template <class Rep1, class Period, class Rep2> |
| 160 | duration<typename common_type<Rep1, Rep2>::type, Period> |
| 161 | operator/(const duration<Rep1, Period>& d, const Rep2& s); |
| 162 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 163 | typename common_type<Rep1, Rep2>::type |
| 164 | operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 165 | |
| 166 | // duration comparisons |
| 167 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 168 | bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 169 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 170 | bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 171 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 172 | bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 173 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 174 | bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 175 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 176 | bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 177 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 178 | bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 179 | |
| 180 | // duration_cast |
| 181 | template <class ToDuration, class Rep, class Period> |
| 182 | ToDuration duration_cast(const duration<Rep, Period>& d); |
| 183 | |
| 184 | // time_point arithmetic |
| 185 | template <class Clock, class Duration1, class Rep2, class Period2> |
| 186 | time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type> |
| 187 | operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs); |
| 188 | template <class Rep1, class Period1, class Clock, class Duration2> |
| 189 | time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type> |
| 190 | operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 191 | template <class Clock, class Duration1, class Rep2, class Period2> |
| 192 | time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type> |
| 193 | operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs); |
| 194 | template <class Clock, class Duration1, class Duration2> |
| 195 | typename common_type<Duration1, Duration2>::type |
| 196 | operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 197 | |
| 198 | // time_point comparisons |
| 199 | template <class Clock, class Duration1, class Duration2> |
| 200 | bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 201 | template <class Clock, class Duration1, class Duration2> |
| 202 | bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 203 | template <class Clock, class Duration1, class Duration2> |
| 204 | bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 205 | template <class Clock, class Duration1, class Duration2> |
| 206 | bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 207 | template <class Clock, class Duration1, class Duration2> |
| 208 | bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 209 | template <class Clock, class Duration1, class Duration2> |
| 210 | bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 211 | |
| 212 | // time_point_cast |
| 213 | |
| 214 | template <class ToDuration, class Clock, class Duration> |
| 215 | time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t); |
| 216 | |
| 217 | // Clocks |
| 218 | |
| 219 | class system_clock |
| 220 | { |
| 221 | public: |
| 222 | typedef microseconds duration; |
| 223 | typedef duration::rep rep; |
| 224 | typedef duration::period period; |
| 225 | typedef chrono::time_point<system_clock> time_point; |
| 226 | static const bool is_monotonic = false; |
| 227 | |
| 228 | static time_point now(); |
| 229 | static time_t to_time_t (const time_point& __t); |
| 230 | static time_point from_time_t(time_t __t); |
| 231 | }; |
| 232 | |
| 233 | class monotonic_clock |
| 234 | { |
| 235 | public: |
| 236 | typedef nanoseconds duration; |
| 237 | typedef duration::rep rep; |
| 238 | typedef duration::period period; |
| 239 | typedef chrono::time_point<monotonic_clock, duration> time_point; |
| 240 | static const bool is_monotonic = true; |
| 241 | |
| 242 | static time_point now(); |
| 243 | }; |
| 244 | |
| 245 | typedef monotonic_clock high_resolution_clock; |
| 246 | |
| 247 | } // chrono |
| 248 | |
| 249 | } // std |
| 250 | */ |
| 251 | |
| 252 | #include <__config> |
| 253 | #include <ctime> |
| 254 | #include <type_traits> |
| 255 | #include <ratio> |
| 256 | #include <limits> |
| 257 | |
| 258 | #pragma GCC system_header |
| 259 | |
| 260 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 261 | |
| 262 | namespace chrono |
| 263 | { |
| 264 | |
| 265 | template <class _Rep, class _Period = ratio<1> > class duration; |
| 266 | |
| 267 | template <class T> |
| 268 | struct __is_duration : false_type {}; |
| 269 | |
| 270 | template <class _Rep, class _Period> |
| 271 | struct __is_duration<duration<_Rep, _Period> > : true_type {}; |
| 272 | |
| 273 | template <class _Rep, class _Period> |
| 274 | struct __is_duration<const duration<_Rep, _Period> > : true_type {}; |
| 275 | |
| 276 | template <class _Rep, class _Period> |
| 277 | struct __is_duration<volatile duration<_Rep, _Period> > : true_type {}; |
| 278 | |
| 279 | template <class _Rep, class _Period> |
| 280 | struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {}; |
| 281 | |
| 282 | } // chrono |
| 283 | |
| 284 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 285 | struct common_type<chrono::duration<_Rep1, _Period1>, chrono::duration<_Rep2, _Period2> > |
| 286 | { |
| 287 | typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, |
| 288 | typename __ratio_gcd<_Period1, _Period2>::type> type; |
| 289 | }; |
| 290 | |
| 291 | namespace chrono { |
| 292 | |
| 293 | // duration_cast |
| 294 | |
| 295 | template <class _FromDuration, class _ToDuration, |
| 296 | class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type, |
| 297 | bool = _Period::num == 1, |
| 298 | bool = _Period::den == 1> |
| 299 | struct __duration_cast; |
| 300 | |
| 301 | template <class _FromDuration, class _ToDuration, class _Period> |
| 302 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true> |
| 303 | { |
| 304 | _LIBCPP_INLINE_VISIBILITY |
| 305 | _ToDuration operator()(const _FromDuration& __fd) const |
| 306 | { |
| 307 | return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count())); |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | template <class _FromDuration, class _ToDuration, class _Period> |
| 312 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false> |
| 313 | { |
| 314 | _LIBCPP_INLINE_VISIBILITY |
| 315 | _ToDuration operator()(const _FromDuration& __fd) const |
| 316 | { |
| 317 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 318 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 319 | static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den))); |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | template <class _FromDuration, class _ToDuration, class _Period> |
| 324 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true> |
| 325 | { |
| 326 | _LIBCPP_INLINE_VISIBILITY |
| 327 | _ToDuration operator()(const _FromDuration& __fd) const |
| 328 | { |
| 329 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 330 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 331 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num))); |
| 332 | } |
| 333 | }; |
| 334 | |
| 335 | template <class _FromDuration, class _ToDuration, class _Period> |
| 336 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> |
| 337 | { |
| 338 | _LIBCPP_INLINE_VISIBILITY |
| 339 | _ToDuration operator()(const _FromDuration& __fd) const |
| 340 | { |
| 341 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 342 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 343 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num) |
| 344 | / static_cast<_Ct>(_Period::den))); |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | template <class _ToDuration, class _Rep, class _Period> |
| 349 | inline _LIBCPP_INLINE_VISIBILITY |
| 350 | typename enable_if |
| 351 | < |
| 352 | __is_duration<_ToDuration>::value, |
| 353 | _ToDuration |
| 354 | >::type |
| 355 | duration_cast(const duration<_Rep, _Period>& __fd) |
| 356 | { |
| 357 | return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd); |
| 358 | } |
| 359 | |
| 360 | template <class _Rep> struct treat_as_floating_point : is_floating_point<_Rep> {}; |
| 361 | |
| 362 | template <class _Rep> |
| 363 | struct duration_values |
| 364 | { |
| 365 | public: |
| 366 | _LIBCPP_INLINE_VISIBILITY static _Rep zero() {return _Rep(0);} |
| 367 | _LIBCPP_INLINE_VISIBILITY static _Rep max() {return numeric_limits<_Rep>::max();} |
| 368 | _LIBCPP_INLINE_VISIBILITY static _Rep min() {return numeric_limits<_Rep>::lowest();} |
| 369 | }; |
| 370 | |
| 371 | // duration |
| 372 | |
| 373 | template <class _Rep, class _Period> |
| 374 | class duration |
| 375 | { |
| 376 | static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration"); |
| 377 | static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio"); |
| 378 | static_assert(_Period::num > 0, "duration period must be positive"); |
| 379 | public: |
| 380 | typedef _Rep rep; |
| 381 | typedef _Period period; |
| 382 | private: |
| 383 | rep __rep_; |
| 384 | public: |
| 385 | |
| 386 | _LIBCPP_INLINE_VISIBILITY duration() {} // = default; |
| 387 | template <class _Rep2> |
| 388 | _LIBCPP_INLINE_VISIBILITY |
| 389 | explicit duration(const _Rep2& __r, |
| 390 | typename enable_if |
| 391 | < |
| 392 | is_convertible<_Rep2, rep>::value && |
| 393 | (treat_as_floating_point<rep>::value || |
| 394 | !treat_as_floating_point<_Rep2>::value) |
| 395 | >::type* = 0) |
| 396 | : __rep_(__r) {} |
| 397 | |
| 398 | // conversions |
| 399 | template <class _Rep2, class _Period2> |
| 400 | _LIBCPP_INLINE_VISIBILITY |
| 401 | duration(const duration<_Rep2, _Period2>& __d, |
| 402 | typename enable_if |
| 403 | < |
| 404 | treat_as_floating_point<rep>::value || |
| 405 | (ratio_divide<_Period2, period>::type::den == 1 && |
| 406 | !treat_as_floating_point<_Rep2>::value) |
| 407 | >::type* = 0) |
| 408 | : __rep_(_STD::chrono::duration_cast<duration>(__d).count()) {} |
| 409 | |
| 410 | // observer |
| 411 | |
| 412 | _LIBCPP_INLINE_VISIBILITY rep count() const {return __rep_;} |
| 413 | |
| 414 | // arithmetic |
| 415 | |
| 416 | _LIBCPP_INLINE_VISIBILITY duration operator+() const {return *this;} |
| 417 | _LIBCPP_INLINE_VISIBILITY duration operator-() const {return duration(-__rep_);} |
| 418 | _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;} |
| 419 | _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);} |
| 420 | _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;} |
| 421 | _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);} |
| 422 | |
| 423 | _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;} |
| 424 | _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;} |
| 425 | |
| 426 | _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;} |
| 427 | _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;} |
| 428 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;} |
| 429 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;} |
| 430 | |
| 431 | // special values |
| 432 | |
| 433 | _LIBCPP_INLINE_VISIBILITY static duration zero() {return duration(duration_values<rep>::zero());} |
| 434 | _LIBCPP_INLINE_VISIBILITY static duration min() {return duration(duration_values<rep>::min());} |
| 435 | _LIBCPP_INLINE_VISIBILITY static duration max() {return duration(duration_values<rep>::max());} |
| 436 | }; |
| 437 | |
| 438 | typedef duration<long long, nano> nanoseconds; |
| 439 | typedef duration<long long, micro> microseconds; |
| 440 | typedef duration<long long, milli> milliseconds; |
| 441 | typedef duration<long long > seconds; |
| 442 | typedef duration< long, ratio< 60> > minutes; |
| 443 | typedef duration< long, ratio<3600> > hours; |
| 444 | |
| 445 | // Duration == |
| 446 | |
| 447 | template <class _LhsDuration, class _RhsDuration> |
| 448 | struct __duration_eq |
| 449 | { |
| 450 | _LIBCPP_INLINE_VISIBILITY |
| 451 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) |
| 452 | { |
| 453 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 454 | return _Ct(__lhs).count() == _Ct(__rhs).count(); |
| 455 | } |
| 456 | }; |
| 457 | |
| 458 | template <class _LhsDuration> |
| 459 | struct __duration_eq<_LhsDuration, _LhsDuration> |
| 460 | { |
| 461 | _LIBCPP_INLINE_VISIBILITY |
| 462 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) |
| 463 | {return __lhs.count() == __rhs.count();} |
| 464 | }; |
| 465 | |
| 466 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 467 | inline _LIBCPP_INLINE_VISIBILITY |
| 468 | bool |
| 469 | operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 470 | { |
| 471 | return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 472 | } |
| 473 | |
| 474 | // Duration != |
| 475 | |
| 476 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 477 | inline _LIBCPP_INLINE_VISIBILITY |
| 478 | bool |
| 479 | operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 480 | { |
| 481 | return !(__lhs == __rhs); |
| 482 | } |
| 483 | |
| 484 | // Duration < |
| 485 | |
| 486 | template <class _LhsDuration, class _RhsDuration> |
| 487 | struct __duration_lt |
| 488 | { |
| 489 | _LIBCPP_INLINE_VISIBILITY |
| 490 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) |
| 491 | { |
| 492 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 493 | return _Ct(__lhs).count() < _Ct(__rhs).count(); |
| 494 | } |
| 495 | }; |
| 496 | |
| 497 | template <class _LhsDuration> |
| 498 | struct __duration_lt<_LhsDuration, _LhsDuration> |
| 499 | { |
| 500 | _LIBCPP_INLINE_VISIBILITY |
| 501 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) |
| 502 | {return __lhs.count() < __rhs.count();} |
| 503 | }; |
| 504 | |
| 505 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 506 | inline _LIBCPP_INLINE_VISIBILITY |
| 507 | bool |
| 508 | operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 509 | { |
| 510 | return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 511 | } |
| 512 | |
| 513 | // Duration > |
| 514 | |
| 515 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 516 | inline _LIBCPP_INLINE_VISIBILITY |
| 517 | bool |
| 518 | operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 519 | { |
| 520 | return __rhs < __lhs; |
| 521 | } |
| 522 | |
| 523 | // Duration <= |
| 524 | |
| 525 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 526 | inline _LIBCPP_INLINE_VISIBILITY |
| 527 | bool |
| 528 | operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 529 | { |
| 530 | return !(__rhs < __lhs); |
| 531 | } |
| 532 | |
| 533 | // Duration >= |
| 534 | |
| 535 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 536 | inline _LIBCPP_INLINE_VISIBILITY |
| 537 | bool |
| 538 | operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 539 | { |
| 540 | return !(__lhs < __rhs); |
| 541 | } |
| 542 | |
| 543 | // Duration + |
| 544 | |
| 545 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 546 | inline _LIBCPP_INLINE_VISIBILITY |
| 547 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 548 | operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 549 | { |
| 550 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs; |
| 551 | __r += __rhs; |
| 552 | return __r; |
| 553 | } |
| 554 | |
| 555 | // Duration - |
| 556 | |
| 557 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 558 | inline _LIBCPP_INLINE_VISIBILITY |
| 559 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 560 | operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 561 | { |
| 562 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs; |
| 563 | __r -= __rhs; |
| 564 | return __r; |
| 565 | } |
| 566 | |
| 567 | // Duration * |
| 568 | |
| 569 | template <class _Rep1, class _Period, class _Rep2> |
| 570 | inline _LIBCPP_INLINE_VISIBILITY |
| 571 | typename enable_if |
| 572 | < |
| 573 | is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value, |
| 574 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 575 | >::type |
| 576 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 577 | { |
| 578 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 579 | duration<_Cr, _Period> __r = __d; |
| 580 | __r *= static_cast<_Cr>(__s); |
| 581 | return __r; |
| 582 | } |
| 583 | |
| 584 | template <class _Rep1, class _Period, class _Rep2> |
| 585 | inline _LIBCPP_INLINE_VISIBILITY |
| 586 | typename enable_if |
| 587 | < |
| 588 | is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value, |
| 589 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 590 | >::type |
| 591 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) |
| 592 | { |
| 593 | return __d * __s; |
| 594 | } |
| 595 | |
| 596 | // Duration / |
| 597 | |
| 598 | template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value> |
| 599 | struct __duration_divide_result |
| 600 | { |
| 601 | }; |
| 602 | |
| 603 | template <class _Duration, class _Rep2, |
| 604 | bool = is_convertible<_Rep2, |
| 605 | typename common_type<typename _Duration::rep, _Rep2>::type>::value> |
| 606 | struct __duration_divide_imp |
| 607 | { |
| 608 | }; |
| 609 | |
| 610 | template <class _Rep1, class _Period, class _Rep2> |
| 611 | struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true> |
| 612 | { |
| 613 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type; |
| 614 | }; |
| 615 | |
| 616 | template <class _Rep1, class _Period, class _Rep2> |
| 617 | struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false> |
| 618 | : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2> |
| 619 | { |
| 620 | }; |
| 621 | |
| 622 | template <class _Rep1, class _Period, class _Rep2> |
| 623 | inline _LIBCPP_INLINE_VISIBILITY |
| 624 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 625 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 626 | { |
| 627 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 628 | duration<_Cr, _Period> __r = __d; |
| 629 | __r /= static_cast<_Cr>(__s); |
| 630 | return __r; |
| 631 | } |
| 632 | |
| 633 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 634 | inline _LIBCPP_INLINE_VISIBILITY |
| 635 | typename common_type<_Rep1, _Rep2>::type |
| 636 | operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 637 | { |
| 638 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct; |
| 639 | return _Ct(__lhs).count() / _Ct(__rhs).count(); |
| 640 | } |
| 641 | |
| 642 | // Duration % |
| 643 | |
| 644 | template <class _Rep1, class _Period, class _Rep2> |
| 645 | inline _LIBCPP_INLINE_VISIBILITY |
| 646 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 647 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 648 | { |
| 649 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 650 | duration<_Cr, _Period> __r = __d; |
| 651 | __r %= static_cast<_Cr>(__s); |
| 652 | return __r; |
| 653 | } |
| 654 | |
| 655 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 656 | inline _LIBCPP_INLINE_VISIBILITY |
| 657 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 658 | operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 659 | { |
| 660 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs; |
| 661 | __r %= __rhs; |
| 662 | return __r; |
| 663 | } |
| 664 | |
| 665 | ////////////////////////////////////////////////////////// |
| 666 | ///////////////////// time_point ///////////////////////// |
| 667 | ////////////////////////////////////////////////////////// |
| 668 | |
| 669 | template <class _Clock, class _Duration = typename _Clock::duration> |
| 670 | class time_point |
| 671 | { |
| 672 | static_assert(__is_duration<_Duration>::value, |
| 673 | "Second template parameter of time_point must be a std::chrono::duration"); |
| 674 | public: |
| 675 | typedef _Clock clock; |
| 676 | typedef _Duration duration; |
| 677 | typedef typename duration::rep rep; |
| 678 | typedef typename duration::period period; |
| 679 | private: |
| 680 | duration __d_; |
| 681 | |
| 682 | public: |
| 683 | _LIBCPP_INLINE_VISIBILITY time_point() : __d_(duration::zero()) {} |
| 684 | _LIBCPP_INLINE_VISIBILITY explicit time_point(const duration& __d) : __d_(__d) {} |
| 685 | |
| 686 | // conversions |
| 687 | template <class _Duration2> |
| 688 | _LIBCPP_INLINE_VISIBILITY |
| 689 | time_point(const time_point<clock, _Duration2>& t, |
| 690 | typename enable_if |
| 691 | < |
| 692 | is_convertible<_Duration2, duration>::value |
| 693 | >::type* = 0) |
| 694 | : __d_(t.time_since_epoch()) {} |
| 695 | |
| 696 | // observer |
| 697 | |
| 698 | _LIBCPP_INLINE_VISIBILITY duration time_since_epoch() const {return __d_;} |
| 699 | |
| 700 | // arithmetic |
| 701 | |
| 702 | _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d;} |
| 703 | _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d;} |
| 704 | |
| 705 | // special values |
| 706 | |
| 707 | _LIBCPP_INLINE_VISIBILITY static time_point min() {return time_point(duration::min());} |
| 708 | _LIBCPP_INLINE_VISIBILITY static time_point max() {return time_point(duration::max());} |
| 709 | }; |
| 710 | |
| 711 | } // chrono |
| 712 | |
| 713 | template <class _Clock, class _Duration1, class _Duration2> |
| 714 | struct common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > |
| 715 | { |
| 716 | typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type; |
| 717 | }; |
| 718 | |
| 719 | namespace chrono { |
| 720 | |
| 721 | template <class _ToDuration, class _Clock, class _Duration> |
| 722 | inline _LIBCPP_INLINE_VISIBILITY |
| 723 | time_point<_Clock, _ToDuration> |
| 724 | time_point_cast(const time_point<_Clock, _Duration>& __t) |
| 725 | { |
| 726 | return time_point<_Clock, _ToDuration>(_STD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch())); |
| 727 | } |
| 728 | |
| 729 | // time_point == |
| 730 | |
| 731 | template <class _Clock, class _Duration1, class _Duration2> |
| 732 | inline _LIBCPP_INLINE_VISIBILITY |
| 733 | bool |
| 734 | operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 735 | { |
| 736 | return __lhs.time_since_epoch() == __rhs.time_since_epoch(); |
| 737 | } |
| 738 | |
| 739 | // time_point != |
| 740 | |
| 741 | template <class _Clock, class _Duration1, class _Duration2> |
| 742 | inline _LIBCPP_INLINE_VISIBILITY |
| 743 | bool |
| 744 | operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 745 | { |
| 746 | return !(__lhs == __rhs); |
| 747 | } |
| 748 | |
| 749 | // time_point < |
| 750 | |
| 751 | template <class _Clock, class _Duration1, class _Duration2> |
| 752 | inline _LIBCPP_INLINE_VISIBILITY |
| 753 | bool |
| 754 | operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 755 | { |
| 756 | return __lhs.time_since_epoch() < __rhs.time_since_epoch(); |
| 757 | } |
| 758 | |
| 759 | // time_point > |
| 760 | |
| 761 | template <class _Clock, class _Duration1, class _Duration2> |
| 762 | inline _LIBCPP_INLINE_VISIBILITY |
| 763 | bool |
| 764 | operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 765 | { |
| 766 | return __rhs < __lhs; |
| 767 | } |
| 768 | |
| 769 | // time_point <= |
| 770 | |
| 771 | template <class _Clock, class _Duration1, class _Duration2> |
| 772 | inline _LIBCPP_INLINE_VISIBILITY |
| 773 | bool |
| 774 | operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 775 | { |
| 776 | return !(__rhs < __lhs); |
| 777 | } |
| 778 | |
| 779 | // time_point >= |
| 780 | |
| 781 | template <class _Clock, class _Duration1, class _Duration2> |
| 782 | inline _LIBCPP_INLINE_VISIBILITY |
| 783 | bool |
| 784 | operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 785 | { |
| 786 | return !(__lhs < __rhs); |
| 787 | } |
| 788 | |
| 789 | // time_point operator+(time_point x, duration y); |
| 790 | |
| 791 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
| 792 | inline _LIBCPP_INLINE_VISIBILITY |
| 793 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 794 | operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 795 | { |
| 796 | typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr; |
| 797 | _Tr __r(__lhs.time_since_epoch()); |
| 798 | __r += __rhs; |
| 799 | return __r; |
| 800 | } |
| 801 | |
| 802 | // time_point operator+(duration x, time_point y); |
| 803 | |
| 804 | template <class _Rep1, class _Period1, class _Clock, class _Duration2> |
| 805 | inline _LIBCPP_INLINE_VISIBILITY |
| 806 | time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type> |
| 807 | operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 808 | { |
| 809 | return __rhs + __lhs; |
| 810 | } |
| 811 | |
| 812 | // time_point operator-(time_point x, duration y); |
| 813 | |
| 814 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
| 815 | inline _LIBCPP_INLINE_VISIBILITY |
| 816 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 817 | operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 818 | { |
| 819 | return __lhs + (-__rhs); |
| 820 | } |
| 821 | |
| 822 | // duration operator-(time_point x, time_point y); |
| 823 | |
| 824 | template <class _Clock, class _Duration1, class _Duration2> |
| 825 | inline _LIBCPP_INLINE_VISIBILITY |
| 826 | typename common_type<_Duration1, _Duration2>::type |
| 827 | operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 828 | { |
| 829 | return __lhs.time_since_epoch() - __rhs.time_since_epoch(); |
| 830 | } |
| 831 | |
| 832 | ////////////////////////////////////////////////////////// |
| 833 | /////////////////////// clocks /////////////////////////// |
| 834 | ////////////////////////////////////////////////////////// |
| 835 | |
| 836 | class system_clock |
| 837 | { |
| 838 | public: |
| 839 | typedef microseconds duration; |
| 840 | typedef duration::rep rep; |
| 841 | typedef duration::period period; |
| 842 | typedef chrono::time_point<system_clock> time_point; |
| 843 | static const bool is_monotonic = false; |
| 844 | |
| 845 | static time_point now(); |
| 846 | static time_t to_time_t (const time_point& __t); |
| 847 | static time_point from_time_t(time_t __t); |
| 848 | }; |
| 849 | |
| 850 | class monotonic_clock |
| 851 | { |
| 852 | public: |
| 853 | typedef nanoseconds duration; |
| 854 | typedef duration::rep rep; |
| 855 | typedef duration::period period; |
| 856 | typedef chrono::time_point<monotonic_clock, duration> time_point; |
| 857 | static const bool is_monotonic = true; |
| 858 | |
| 859 | static time_point now(); |
| 860 | }; |
| 861 | |
| 862 | typedef monotonic_clock high_resolution_clock; |
| 863 | |
| 864 | } // chrono |
| 865 | |
| 866 | _LIBCPP_END_NAMESPACE_STD |
| 867 | |
| 868 | #endif // _LIBCPP_CHRONO |