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 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 226 | static const bool is_steady = false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 227 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 228 | static time_point now() noexcept; |
| 229 | static time_t to_time_t (const time_point& __t) noexcept; |
| 230 | static time_point from_time_t(time_t __t) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 233 | class steady_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 234 | { |
| 235 | public: |
| 236 | typedef nanoseconds duration; |
| 237 | typedef duration::rep rep; |
| 238 | typedef duration::period period; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 239 | typedef chrono::time_point<steady_clock, duration> time_point; |
| 240 | static const bool is_steady = true; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 241 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 242 | static time_point now() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 245 | typedef steady_clock high_resolution_clock; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 265 | template <class _Rep, class _Period = ratio<1> > class _LIBCPP_VISIBLE duration; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 266 | |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 267 | template <class _Tp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 285 | struct _LIBCPP_VISIBLE common_type<chrono::duration<_Rep1, _Period1>, |
| 286 | chrono::duration<_Rep2, _Period2> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 287 | { |
| 288 | typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, |
| 289 | typename __ratio_gcd<_Period1, _Period2>::type> type; |
| 290 | }; |
| 291 | |
| 292 | namespace chrono { |
| 293 | |
| 294 | // duration_cast |
| 295 | |
| 296 | template <class _FromDuration, class _ToDuration, |
| 297 | class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type, |
| 298 | bool = _Period::num == 1, |
| 299 | bool = _Period::den == 1> |
| 300 | struct __duration_cast; |
| 301 | |
| 302 | template <class _FromDuration, class _ToDuration, class _Period> |
| 303 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true> |
| 304 | { |
| 305 | _LIBCPP_INLINE_VISIBILITY |
| 306 | _ToDuration operator()(const _FromDuration& __fd) const |
| 307 | { |
| 308 | return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count())); |
| 309 | } |
| 310 | }; |
| 311 | |
| 312 | template <class _FromDuration, class _ToDuration, class _Period> |
| 313 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false> |
| 314 | { |
| 315 | _LIBCPP_INLINE_VISIBILITY |
| 316 | _ToDuration operator()(const _FromDuration& __fd) const |
| 317 | { |
| 318 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 319 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 320 | static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den))); |
| 321 | } |
| 322 | }; |
| 323 | |
| 324 | template <class _FromDuration, class _ToDuration, class _Period> |
| 325 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true> |
| 326 | { |
| 327 | _LIBCPP_INLINE_VISIBILITY |
| 328 | _ToDuration operator()(const _FromDuration& __fd) const |
| 329 | { |
| 330 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 331 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 332 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num))); |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | template <class _FromDuration, class _ToDuration, class _Period> |
| 337 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> |
| 338 | { |
| 339 | _LIBCPP_INLINE_VISIBILITY |
| 340 | _ToDuration operator()(const _FromDuration& __fd) const |
| 341 | { |
| 342 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 343 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 344 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num) |
| 345 | / static_cast<_Ct>(_Period::den))); |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | template <class _ToDuration, class _Rep, class _Period> |
| 350 | inline _LIBCPP_INLINE_VISIBILITY |
| 351 | typename enable_if |
| 352 | < |
| 353 | __is_duration<_ToDuration>::value, |
| 354 | _ToDuration |
| 355 | >::type |
| 356 | duration_cast(const duration<_Rep, _Period>& __fd) |
| 357 | { |
| 358 | return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd); |
| 359 | } |
| 360 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 361 | template <class _Rep> |
| 362 | struct _LIBCPP_VISIBLE treat_as_floating_point : is_floating_point<_Rep> {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 363 | |
| 364 | template <class _Rep> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 365 | struct _LIBCPP_VISIBLE duration_values |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 366 | { |
| 367 | public: |
| 368 | _LIBCPP_INLINE_VISIBILITY static _Rep zero() {return _Rep(0);} |
| 369 | _LIBCPP_INLINE_VISIBILITY static _Rep max() {return numeric_limits<_Rep>::max();} |
| 370 | _LIBCPP_INLINE_VISIBILITY static _Rep min() {return numeric_limits<_Rep>::lowest();} |
| 371 | }; |
| 372 | |
| 373 | // duration |
| 374 | |
| 375 | template <class _Rep, class _Period> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 376 | class _LIBCPP_VISIBLE duration |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 377 | { |
| 378 | static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration"); |
| 379 | static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio"); |
| 380 | static_assert(_Period::num > 0, "duration period must be positive"); |
| 381 | public: |
| 382 | typedef _Rep rep; |
| 383 | typedef _Period period; |
| 384 | private: |
| 385 | rep __rep_; |
| 386 | public: |
| 387 | |
| 388 | _LIBCPP_INLINE_VISIBILITY duration() {} // = default; |
| 389 | template <class _Rep2> |
| 390 | _LIBCPP_INLINE_VISIBILITY |
| 391 | explicit duration(const _Rep2& __r, |
| 392 | typename enable_if |
| 393 | < |
| 394 | is_convertible<_Rep2, rep>::value && |
| 395 | (treat_as_floating_point<rep>::value || |
| 396 | !treat_as_floating_point<_Rep2>::value) |
| 397 | >::type* = 0) |
| 398 | : __rep_(__r) {} |
| 399 | |
| 400 | // conversions |
| 401 | template <class _Rep2, class _Period2> |
| 402 | _LIBCPP_INLINE_VISIBILITY |
| 403 | duration(const duration<_Rep2, _Period2>& __d, |
| 404 | typename enable_if |
| 405 | < |
| 406 | treat_as_floating_point<rep>::value || |
| 407 | (ratio_divide<_Period2, period>::type::den == 1 && |
| 408 | !treat_as_floating_point<_Rep2>::value) |
| 409 | >::type* = 0) |
| 410 | : __rep_(_STD::chrono::duration_cast<duration>(__d).count()) {} |
| 411 | |
| 412 | // observer |
| 413 | |
| 414 | _LIBCPP_INLINE_VISIBILITY rep count() const {return __rep_;} |
| 415 | |
| 416 | // arithmetic |
| 417 | |
| 418 | _LIBCPP_INLINE_VISIBILITY duration operator+() const {return *this;} |
| 419 | _LIBCPP_INLINE_VISIBILITY duration operator-() const {return duration(-__rep_);} |
| 420 | _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;} |
| 421 | _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);} |
| 422 | _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;} |
| 423 | _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);} |
| 424 | |
| 425 | _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;} |
| 426 | _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;} |
| 427 | |
| 428 | _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;} |
| 429 | _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;} |
| 430 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;} |
| 431 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;} |
| 432 | |
| 433 | // special values |
| 434 | |
| 435 | _LIBCPP_INLINE_VISIBILITY static duration zero() {return duration(duration_values<rep>::zero());} |
| 436 | _LIBCPP_INLINE_VISIBILITY static duration min() {return duration(duration_values<rep>::min());} |
| 437 | _LIBCPP_INLINE_VISIBILITY static duration max() {return duration(duration_values<rep>::max());} |
| 438 | }; |
| 439 | |
| 440 | typedef duration<long long, nano> nanoseconds; |
| 441 | typedef duration<long long, micro> microseconds; |
| 442 | typedef duration<long long, milli> milliseconds; |
| 443 | typedef duration<long long > seconds; |
| 444 | typedef duration< long, ratio< 60> > minutes; |
| 445 | typedef duration< long, ratio<3600> > hours; |
| 446 | |
| 447 | // Duration == |
| 448 | |
| 449 | template <class _LhsDuration, class _RhsDuration> |
| 450 | struct __duration_eq |
| 451 | { |
| 452 | _LIBCPP_INLINE_VISIBILITY |
| 453 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) |
| 454 | { |
| 455 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 456 | return _Ct(__lhs).count() == _Ct(__rhs).count(); |
| 457 | } |
| 458 | }; |
| 459 | |
| 460 | template <class _LhsDuration> |
| 461 | struct __duration_eq<_LhsDuration, _LhsDuration> |
| 462 | { |
| 463 | _LIBCPP_INLINE_VISIBILITY |
| 464 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) |
| 465 | {return __lhs.count() == __rhs.count();} |
| 466 | }; |
| 467 | |
| 468 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 469 | inline _LIBCPP_INLINE_VISIBILITY |
| 470 | bool |
| 471 | operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 472 | { |
| 473 | return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 474 | } |
| 475 | |
| 476 | // Duration != |
| 477 | |
| 478 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 479 | inline _LIBCPP_INLINE_VISIBILITY |
| 480 | bool |
| 481 | operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 482 | { |
| 483 | return !(__lhs == __rhs); |
| 484 | } |
| 485 | |
| 486 | // Duration < |
| 487 | |
| 488 | template <class _LhsDuration, class _RhsDuration> |
| 489 | struct __duration_lt |
| 490 | { |
| 491 | _LIBCPP_INLINE_VISIBILITY |
| 492 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) |
| 493 | { |
| 494 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 495 | return _Ct(__lhs).count() < _Ct(__rhs).count(); |
| 496 | } |
| 497 | }; |
| 498 | |
| 499 | template <class _LhsDuration> |
| 500 | struct __duration_lt<_LhsDuration, _LhsDuration> |
| 501 | { |
| 502 | _LIBCPP_INLINE_VISIBILITY |
| 503 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) |
| 504 | {return __lhs.count() < __rhs.count();} |
| 505 | }; |
| 506 | |
| 507 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 508 | inline _LIBCPP_INLINE_VISIBILITY |
| 509 | bool |
| 510 | operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 511 | { |
| 512 | return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 513 | } |
| 514 | |
| 515 | // Duration > |
| 516 | |
| 517 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 518 | inline _LIBCPP_INLINE_VISIBILITY |
| 519 | bool |
| 520 | operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 521 | { |
| 522 | return __rhs < __lhs; |
| 523 | } |
| 524 | |
| 525 | // Duration <= |
| 526 | |
| 527 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 528 | inline _LIBCPP_INLINE_VISIBILITY |
| 529 | bool |
| 530 | operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 531 | { |
| 532 | return !(__rhs < __lhs); |
| 533 | } |
| 534 | |
| 535 | // Duration >= |
| 536 | |
| 537 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 538 | inline _LIBCPP_INLINE_VISIBILITY |
| 539 | bool |
| 540 | operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 541 | { |
| 542 | return !(__lhs < __rhs); |
| 543 | } |
| 544 | |
| 545 | // Duration + |
| 546 | |
| 547 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 548 | inline _LIBCPP_INLINE_VISIBILITY |
| 549 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 550 | operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 551 | { |
| 552 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs; |
| 553 | __r += __rhs; |
| 554 | return __r; |
| 555 | } |
| 556 | |
| 557 | // Duration - |
| 558 | |
| 559 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 560 | inline _LIBCPP_INLINE_VISIBILITY |
| 561 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 562 | operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 563 | { |
| 564 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs; |
| 565 | __r -= __rhs; |
| 566 | return __r; |
| 567 | } |
| 568 | |
| 569 | // Duration * |
| 570 | |
| 571 | template <class _Rep1, class _Period, class _Rep2> |
| 572 | inline _LIBCPP_INLINE_VISIBILITY |
| 573 | typename enable_if |
| 574 | < |
| 575 | is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value, |
| 576 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 577 | >::type |
| 578 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 579 | { |
| 580 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 581 | duration<_Cr, _Period> __r = __d; |
| 582 | __r *= static_cast<_Cr>(__s); |
| 583 | return __r; |
| 584 | } |
| 585 | |
| 586 | template <class _Rep1, class _Period, class _Rep2> |
| 587 | inline _LIBCPP_INLINE_VISIBILITY |
| 588 | typename enable_if |
| 589 | < |
| 590 | is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value, |
| 591 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 592 | >::type |
| 593 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) |
| 594 | { |
| 595 | return __d * __s; |
| 596 | } |
| 597 | |
| 598 | // Duration / |
| 599 | |
| 600 | template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value> |
| 601 | struct __duration_divide_result |
| 602 | { |
| 603 | }; |
| 604 | |
| 605 | template <class _Duration, class _Rep2, |
| 606 | bool = is_convertible<_Rep2, |
| 607 | typename common_type<typename _Duration::rep, _Rep2>::type>::value> |
| 608 | struct __duration_divide_imp |
| 609 | { |
| 610 | }; |
| 611 | |
| 612 | template <class _Rep1, class _Period, class _Rep2> |
| 613 | struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true> |
| 614 | { |
| 615 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type; |
| 616 | }; |
| 617 | |
| 618 | template <class _Rep1, class _Period, class _Rep2> |
| 619 | struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false> |
| 620 | : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2> |
| 621 | { |
| 622 | }; |
| 623 | |
| 624 | template <class _Rep1, class _Period, class _Rep2> |
| 625 | inline _LIBCPP_INLINE_VISIBILITY |
| 626 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 627 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 628 | { |
| 629 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 630 | duration<_Cr, _Period> __r = __d; |
| 631 | __r /= static_cast<_Cr>(__s); |
| 632 | return __r; |
| 633 | } |
| 634 | |
| 635 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 636 | inline _LIBCPP_INLINE_VISIBILITY |
| 637 | typename common_type<_Rep1, _Rep2>::type |
| 638 | operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 639 | { |
| 640 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct; |
| 641 | return _Ct(__lhs).count() / _Ct(__rhs).count(); |
| 642 | } |
| 643 | |
| 644 | // Duration % |
| 645 | |
| 646 | template <class _Rep1, class _Period, class _Rep2> |
| 647 | inline _LIBCPP_INLINE_VISIBILITY |
| 648 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 649 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 650 | { |
| 651 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 652 | duration<_Cr, _Period> __r = __d; |
| 653 | __r %= static_cast<_Cr>(__s); |
| 654 | return __r; |
| 655 | } |
| 656 | |
| 657 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 658 | inline _LIBCPP_INLINE_VISIBILITY |
| 659 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 660 | operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 661 | { |
| 662 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs; |
| 663 | __r %= __rhs; |
| 664 | return __r; |
| 665 | } |
| 666 | |
| 667 | ////////////////////////////////////////////////////////// |
| 668 | ///////////////////// time_point ///////////////////////// |
| 669 | ////////////////////////////////////////////////////////// |
| 670 | |
| 671 | template <class _Clock, class _Duration = typename _Clock::duration> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 672 | class _LIBCPP_VISIBLE time_point |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 673 | { |
| 674 | static_assert(__is_duration<_Duration>::value, |
| 675 | "Second template parameter of time_point must be a std::chrono::duration"); |
| 676 | public: |
| 677 | typedef _Clock clock; |
| 678 | typedef _Duration duration; |
| 679 | typedef typename duration::rep rep; |
| 680 | typedef typename duration::period period; |
| 681 | private: |
| 682 | duration __d_; |
| 683 | |
| 684 | public: |
| 685 | _LIBCPP_INLINE_VISIBILITY time_point() : __d_(duration::zero()) {} |
| 686 | _LIBCPP_INLINE_VISIBILITY explicit time_point(const duration& __d) : __d_(__d) {} |
| 687 | |
| 688 | // conversions |
| 689 | template <class _Duration2> |
| 690 | _LIBCPP_INLINE_VISIBILITY |
| 691 | time_point(const time_point<clock, _Duration2>& t, |
| 692 | typename enable_if |
| 693 | < |
| 694 | is_convertible<_Duration2, duration>::value |
| 695 | >::type* = 0) |
| 696 | : __d_(t.time_since_epoch()) {} |
| 697 | |
| 698 | // observer |
| 699 | |
| 700 | _LIBCPP_INLINE_VISIBILITY duration time_since_epoch() const {return __d_;} |
| 701 | |
| 702 | // arithmetic |
| 703 | |
| 704 | _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d;} |
| 705 | _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d;} |
| 706 | |
| 707 | // special values |
| 708 | |
| 709 | _LIBCPP_INLINE_VISIBILITY static time_point min() {return time_point(duration::min());} |
| 710 | _LIBCPP_INLINE_VISIBILITY static time_point max() {return time_point(duration::max());} |
| 711 | }; |
| 712 | |
| 713 | } // chrono |
| 714 | |
| 715 | template <class _Clock, class _Duration1, class _Duration2> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 716 | struct _LIBCPP_VISIBLE common_type<chrono::time_point<_Clock, _Duration1>, |
| 717 | chrono::time_point<_Clock, _Duration2> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 718 | { |
| 719 | typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type; |
| 720 | }; |
| 721 | |
| 722 | namespace chrono { |
| 723 | |
| 724 | template <class _ToDuration, class _Clock, class _Duration> |
| 725 | inline _LIBCPP_INLINE_VISIBILITY |
| 726 | time_point<_Clock, _ToDuration> |
| 727 | time_point_cast(const time_point<_Clock, _Duration>& __t) |
| 728 | { |
| 729 | return time_point<_Clock, _ToDuration>(_STD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch())); |
| 730 | } |
| 731 | |
| 732 | // time_point == |
| 733 | |
| 734 | template <class _Clock, class _Duration1, class _Duration2> |
| 735 | inline _LIBCPP_INLINE_VISIBILITY |
| 736 | bool |
| 737 | operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 738 | { |
| 739 | return __lhs.time_since_epoch() == __rhs.time_since_epoch(); |
| 740 | } |
| 741 | |
| 742 | // time_point != |
| 743 | |
| 744 | template <class _Clock, class _Duration1, class _Duration2> |
| 745 | inline _LIBCPP_INLINE_VISIBILITY |
| 746 | bool |
| 747 | operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 748 | { |
| 749 | return !(__lhs == __rhs); |
| 750 | } |
| 751 | |
| 752 | // time_point < |
| 753 | |
| 754 | template <class _Clock, class _Duration1, class _Duration2> |
| 755 | inline _LIBCPP_INLINE_VISIBILITY |
| 756 | bool |
| 757 | operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 758 | { |
| 759 | return __lhs.time_since_epoch() < __rhs.time_since_epoch(); |
| 760 | } |
| 761 | |
| 762 | // time_point > |
| 763 | |
| 764 | template <class _Clock, class _Duration1, class _Duration2> |
| 765 | inline _LIBCPP_INLINE_VISIBILITY |
| 766 | bool |
| 767 | operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 768 | { |
| 769 | return __rhs < __lhs; |
| 770 | } |
| 771 | |
| 772 | // time_point <= |
| 773 | |
| 774 | template <class _Clock, class _Duration1, class _Duration2> |
| 775 | inline _LIBCPP_INLINE_VISIBILITY |
| 776 | bool |
| 777 | operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 778 | { |
| 779 | return !(__rhs < __lhs); |
| 780 | } |
| 781 | |
| 782 | // time_point >= |
| 783 | |
| 784 | template <class _Clock, class _Duration1, class _Duration2> |
| 785 | inline _LIBCPP_INLINE_VISIBILITY |
| 786 | bool |
| 787 | operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 788 | { |
| 789 | return !(__lhs < __rhs); |
| 790 | } |
| 791 | |
| 792 | // time_point operator+(time_point x, duration y); |
| 793 | |
| 794 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
| 795 | inline _LIBCPP_INLINE_VISIBILITY |
| 796 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 797 | operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 798 | { |
| 799 | typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr; |
| 800 | _Tr __r(__lhs.time_since_epoch()); |
| 801 | __r += __rhs; |
| 802 | return __r; |
| 803 | } |
| 804 | |
| 805 | // time_point operator+(duration x, time_point y); |
| 806 | |
| 807 | template <class _Rep1, class _Period1, class _Clock, class _Duration2> |
| 808 | inline _LIBCPP_INLINE_VISIBILITY |
| 809 | time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type> |
| 810 | operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 811 | { |
| 812 | return __rhs + __lhs; |
| 813 | } |
| 814 | |
| 815 | // time_point operator-(time_point x, duration y); |
| 816 | |
| 817 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
| 818 | inline _LIBCPP_INLINE_VISIBILITY |
| 819 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 820 | operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 821 | { |
| 822 | return __lhs + (-__rhs); |
| 823 | } |
| 824 | |
| 825 | // duration operator-(time_point x, time_point y); |
| 826 | |
| 827 | template <class _Clock, class _Duration1, class _Duration2> |
| 828 | inline _LIBCPP_INLINE_VISIBILITY |
| 829 | typename common_type<_Duration1, _Duration2>::type |
| 830 | operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 831 | { |
| 832 | return __lhs.time_since_epoch() - __rhs.time_since_epoch(); |
| 833 | } |
| 834 | |
| 835 | ////////////////////////////////////////////////////////// |
| 836 | /////////////////////// clocks /////////////////////////// |
| 837 | ////////////////////////////////////////////////////////// |
| 838 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 839 | class _LIBCPP_VISIBLE system_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 840 | { |
| 841 | public: |
| 842 | typedef microseconds duration; |
| 843 | typedef duration::rep rep; |
| 844 | typedef duration::period period; |
| 845 | typedef chrono::time_point<system_clock> time_point; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 846 | static const bool is_steady = false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 847 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 848 | static time_point now() _NOEXCEPT; |
| 849 | static time_t to_time_t (const time_point& __t) _NOEXCEPT; |
| 850 | static time_point from_time_t(time_t __t) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 851 | }; |
| 852 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 853 | class _LIBCPP_VISIBLE steady_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 854 | { |
| 855 | public: |
| 856 | typedef nanoseconds duration; |
| 857 | typedef duration::rep rep; |
| 858 | typedef duration::period period; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 859 | typedef chrono::time_point<steady_clock, duration> time_point; |
| 860 | static const bool is_steady = true; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 861 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 862 | static time_point now() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 863 | }; |
| 864 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 865 | typedef steady_clock high_resolution_clock; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 866 | |
| 867 | } // chrono |
| 868 | |
| 869 | _LIBCPP_END_NAMESPACE_STD |
| 870 | |
| 871 | #endif // _LIBCPP_CHRONO |