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