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 | |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 262 | constexpr chrono::hours operator "" h(unsigned long long); // C++14 |
| 263 | constexpr chrono::duration<unspecified , ratio<3600,1>> operator "" h(long double); // C++14 |
| 264 | constexpr chrono::minutes operator "" min(unsigned long long); // C++14 |
| 265 | constexpr chrono::duration<unspecified , ratio<60,1>> operator "" min(long double); // C++14 |
| 266 | constexpr chrono::seconds operator "" s(unsigned long long); // C++14 |
| 267 | constexpr chrono::duration<unspecified > operator "" s(long double); // C++14 |
| 268 | constexpr chrono::milliseconds operator "" ms(unsigned long long); // C++14 |
| 269 | constexpr chrono::duration<unspecified , milli> operator "" ms(long double); // C++14 |
| 270 | constexpr chrono::microseconds operator "" us(unsigned long long); // C++14 |
| 271 | constexpr chrono::duration<unspecified , micro> operator "" us(long double); // C++14 |
| 272 | constexpr chrono::nanoseconds operator "" ns(unsigned long long); // C++14 |
| 273 | constexpr chrono::duration<unspecified , nano> operator "" ns(long double); // C++14 |
| 274 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 275 | } // std |
| 276 | */ |
| 277 | |
| 278 | #include <__config> |
| 279 | #include <ctime> |
| 280 | #include <type_traits> |
| 281 | #include <ratio> |
| 282 | #include <limits> |
| 283 | |
Howard Hinnant | c5a5fbd | 2011-11-29 16:45:27 +0000 | [diff] [blame] | 284 | #include <__undef_min_max> |
| 285 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 286 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 287 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 288 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 289 | |
| 290 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 291 | |
| 292 | namespace chrono |
| 293 | { |
| 294 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 295 | template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TYPE_VIS duration; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 297 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 298 | struct __is_duration : false_type {}; |
| 299 | |
| 300 | template <class _Rep, class _Period> |
| 301 | struct __is_duration<duration<_Rep, _Period> > : true_type {}; |
| 302 | |
| 303 | template <class _Rep, class _Period> |
| 304 | struct __is_duration<const duration<_Rep, _Period> > : true_type {}; |
| 305 | |
| 306 | template <class _Rep, class _Period> |
| 307 | struct __is_duration<volatile duration<_Rep, _Period> > : true_type {}; |
| 308 | |
| 309 | template <class _Rep, class _Period> |
| 310 | struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {}; |
| 311 | |
| 312 | } // chrono |
| 313 | |
| 314 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 315 | struct _LIBCPP_TYPE_VIS common_type<chrono::duration<_Rep1, _Period1>, |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 316 | chrono::duration<_Rep2, _Period2> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 317 | { |
| 318 | typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, |
| 319 | typename __ratio_gcd<_Period1, _Period2>::type> type; |
| 320 | }; |
| 321 | |
| 322 | namespace chrono { |
| 323 | |
| 324 | // duration_cast |
| 325 | |
| 326 | template <class _FromDuration, class _ToDuration, |
| 327 | class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type, |
| 328 | bool = _Period::num == 1, |
| 329 | bool = _Period::den == 1> |
| 330 | struct __duration_cast; |
| 331 | |
| 332 | template <class _FromDuration, class _ToDuration, class _Period> |
| 333 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true> |
| 334 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 335 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 336 | _ToDuration operator()(const _FromDuration& __fd) const |
| 337 | { |
| 338 | return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count())); |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | template <class _FromDuration, class _ToDuration, class _Period> |
| 343 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false> |
| 344 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 345 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 346 | _ToDuration operator()(const _FromDuration& __fd) const |
| 347 | { |
| 348 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 349 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 350 | static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den))); |
| 351 | } |
| 352 | }; |
| 353 | |
| 354 | template <class _FromDuration, class _ToDuration, class _Period> |
| 355 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true> |
| 356 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 357 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 358 | _ToDuration operator()(const _FromDuration& __fd) const |
| 359 | { |
| 360 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 361 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 362 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num))); |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | template <class _FromDuration, class _ToDuration, class _Period> |
| 367 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> |
| 368 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 369 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 370 | _ToDuration operator()(const _FromDuration& __fd) const |
| 371 | { |
| 372 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 373 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 374 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num) |
| 375 | / static_cast<_Ct>(_Period::den))); |
| 376 | } |
| 377 | }; |
| 378 | |
| 379 | template <class _ToDuration, class _Rep, class _Period> |
| 380 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 381 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 382 | typename enable_if |
| 383 | < |
| 384 | __is_duration<_ToDuration>::value, |
| 385 | _ToDuration |
| 386 | >::type |
| 387 | duration_cast(const duration<_Rep, _Period>& __fd) |
| 388 | { |
| 389 | return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd); |
| 390 | } |
| 391 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 392 | template <class _Rep> |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 393 | struct _LIBCPP_TYPE_VIS treat_as_floating_point : is_floating_point<_Rep> {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 394 | |
| 395 | template <class _Rep> |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 396 | struct _LIBCPP_TYPE_VIS duration_values |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 397 | { |
| 398 | public: |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 399 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);} |
| 400 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();} |
| 401 | _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] | 402 | }; |
| 403 | |
| 404 | // duration |
| 405 | |
| 406 | template <class _Rep, class _Period> |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 407 | class _LIBCPP_TYPE_VIS duration |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 408 | { |
| 409 | static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration"); |
| 410 | static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio"); |
| 411 | static_assert(_Period::num > 0, "duration period must be positive"); |
| 412 | public: |
| 413 | typedef _Rep rep; |
| 414 | typedef _Period period; |
| 415 | private: |
| 416 | rep __rep_; |
| 417 | public: |
| 418 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 419 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration() {} // = default; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 420 | template <class _Rep2> |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 421 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 422 | explicit duration(const _Rep2& __r, |
| 423 | typename enable_if |
| 424 | < |
| 425 | is_convertible<_Rep2, rep>::value && |
| 426 | (treat_as_floating_point<rep>::value || |
| 427 | !treat_as_floating_point<_Rep2>::value) |
| 428 | >::type* = 0) |
| 429 | : __rep_(__r) {} |
| 430 | |
| 431 | // conversions |
| 432 | template <class _Rep2, class _Period2> |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 433 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | duration(const duration<_Rep2, _Period2>& __d, |
| 435 | typename enable_if |
| 436 | < |
| 437 | treat_as_floating_point<rep>::value || |
| 438 | (ratio_divide<_Period2, period>::type::den == 1 && |
| 439 | !treat_as_floating_point<_Rep2>::value) |
| 440 | >::type* = 0) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 441 | : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 442 | |
| 443 | // observer |
| 444 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 445 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 446 | |
| 447 | // arithmetic |
| 448 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 449 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator+() const {return *this;} |
| 450 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator-() const {return duration(-__rep_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 451 | _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;} |
| 452 | _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);} |
| 453 | _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;} |
| 454 | _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);} |
| 455 | |
| 456 | _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;} |
| 457 | _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;} |
| 458 | |
| 459 | _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;} |
| 460 | _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;} |
| 461 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;} |
| 462 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;} |
| 463 | |
| 464 | // special values |
| 465 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 466 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());} |
| 467 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());} |
| 468 | _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] | 469 | }; |
| 470 | |
| 471 | typedef duration<long long, nano> nanoseconds; |
| 472 | typedef duration<long long, micro> microseconds; |
| 473 | typedef duration<long long, milli> milliseconds; |
| 474 | typedef duration<long long > seconds; |
| 475 | typedef duration< long, ratio< 60> > minutes; |
| 476 | typedef duration< long, ratio<3600> > hours; |
| 477 | |
| 478 | // Duration == |
| 479 | |
| 480 | template <class _LhsDuration, class _RhsDuration> |
| 481 | struct __duration_eq |
| 482 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 483 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 484 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 485 | { |
| 486 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 487 | return _Ct(__lhs).count() == _Ct(__rhs).count(); |
| 488 | } |
| 489 | }; |
| 490 | |
| 491 | template <class _LhsDuration> |
| 492 | struct __duration_eq<_LhsDuration, _LhsDuration> |
| 493 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 494 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 495 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 496 | {return __lhs.count() == __rhs.count();} |
| 497 | }; |
| 498 | |
| 499 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 500 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 501 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 502 | bool |
| 503 | operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 504 | { |
| 505 | return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 506 | } |
| 507 | |
| 508 | // Duration != |
| 509 | |
| 510 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 511 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 512 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 513 | bool |
| 514 | operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 515 | { |
| 516 | return !(__lhs == __rhs); |
| 517 | } |
| 518 | |
| 519 | // Duration < |
| 520 | |
| 521 | template <class _LhsDuration, class _RhsDuration> |
| 522 | struct __duration_lt |
| 523 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 524 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 525 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 526 | { |
| 527 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 528 | return _Ct(__lhs).count() < _Ct(__rhs).count(); |
| 529 | } |
| 530 | }; |
| 531 | |
| 532 | template <class _LhsDuration> |
| 533 | struct __duration_lt<_LhsDuration, _LhsDuration> |
| 534 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 535 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 536 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 537 | {return __lhs.count() < __rhs.count();} |
| 538 | }; |
| 539 | |
| 540 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 541 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 542 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 543 | bool |
| 544 | operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 545 | { |
| 546 | return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 547 | } |
| 548 | |
| 549 | // Duration > |
| 550 | |
| 551 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 552 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 553 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 554 | bool |
| 555 | operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 556 | { |
| 557 | return __rhs < __lhs; |
| 558 | } |
| 559 | |
| 560 | // Duration <= |
| 561 | |
| 562 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 563 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 564 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 565 | bool |
| 566 | operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 567 | { |
| 568 | return !(__rhs < __lhs); |
| 569 | } |
| 570 | |
| 571 | // Duration >= |
| 572 | |
| 573 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 574 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 575 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 576 | bool |
| 577 | operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 578 | { |
| 579 | return !(__lhs < __rhs); |
| 580 | } |
| 581 | |
| 582 | // Duration + |
| 583 | |
| 584 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 585 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 586 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 587 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 588 | operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 589 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 590 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 591 | return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Duration - |
| 595 | |
| 596 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 597 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 598 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 599 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 600 | operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 601 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 602 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 603 | return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // Duration * |
| 607 | |
| 608 | template <class _Rep1, class _Period, class _Rep2> |
| 609 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 610 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 611 | typename enable_if |
| 612 | < |
| 613 | is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value, |
| 614 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 615 | >::type |
| 616 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 617 | { |
| 618 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 619 | typedef duration<_Cr, _Period> _Cd; |
| 620 | return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | template <class _Rep1, class _Period, class _Rep2> |
| 624 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 625 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 626 | typename enable_if |
| 627 | < |
| 628 | is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value, |
| 629 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 630 | >::type |
| 631 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) |
| 632 | { |
| 633 | return __d * __s; |
| 634 | } |
| 635 | |
| 636 | // Duration / |
| 637 | |
| 638 | template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value> |
| 639 | struct __duration_divide_result |
| 640 | { |
| 641 | }; |
| 642 | |
| 643 | template <class _Duration, class _Rep2, |
| 644 | bool = is_convertible<_Rep2, |
| 645 | typename common_type<typename _Duration::rep, _Rep2>::type>::value> |
| 646 | struct __duration_divide_imp |
| 647 | { |
| 648 | }; |
| 649 | |
| 650 | template <class _Rep1, class _Period, class _Rep2> |
| 651 | struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true> |
| 652 | { |
| 653 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type; |
| 654 | }; |
| 655 | |
| 656 | template <class _Rep1, class _Period, class _Rep2> |
| 657 | struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false> |
| 658 | : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2> |
| 659 | { |
| 660 | }; |
| 661 | |
| 662 | template <class _Rep1, class _Period, class _Rep2> |
| 663 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 664 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 665 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 666 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 667 | { |
| 668 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 669 | typedef duration<_Cr, _Period> _Cd; |
| 670 | return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 674 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 675 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 676 | typename common_type<_Rep1, _Rep2>::type |
| 677 | operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 678 | { |
| 679 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct; |
| 680 | return _Ct(__lhs).count() / _Ct(__rhs).count(); |
| 681 | } |
| 682 | |
| 683 | // Duration % |
| 684 | |
| 685 | template <class _Rep1, class _Period, class _Rep2> |
| 686 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 687 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 688 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 689 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 690 | { |
| 691 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 692 | typedef duration<_Cr, _Period> _Cd; |
| 693 | return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 697 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 698 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 699 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 700 | operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 701 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 702 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 703 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 704 | 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] | 705 | } |
| 706 | |
| 707 | ////////////////////////////////////////////////////////// |
| 708 | ///////////////////// time_point ///////////////////////// |
| 709 | ////////////////////////////////////////////////////////// |
| 710 | |
| 711 | template <class _Clock, class _Duration = typename _Clock::duration> |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 712 | class _LIBCPP_TYPE_VIS time_point |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 713 | { |
| 714 | static_assert(__is_duration<_Duration>::value, |
| 715 | "Second template parameter of time_point must be a std::chrono::duration"); |
| 716 | public: |
| 717 | typedef _Clock clock; |
| 718 | typedef _Duration duration; |
| 719 | typedef typename duration::rep rep; |
| 720 | typedef typename duration::period period; |
| 721 | private: |
| 722 | duration __d_; |
| 723 | |
| 724 | public: |
| 725 | _LIBCPP_INLINE_VISIBILITY time_point() : __d_(duration::zero()) {} |
| 726 | _LIBCPP_INLINE_VISIBILITY explicit time_point(const duration& __d) : __d_(__d) {} |
| 727 | |
| 728 | // conversions |
| 729 | template <class _Duration2> |
| 730 | _LIBCPP_INLINE_VISIBILITY |
| 731 | time_point(const time_point<clock, _Duration2>& t, |
| 732 | typename enable_if |
| 733 | < |
| 734 | is_convertible<_Duration2, duration>::value |
| 735 | >::type* = 0) |
| 736 | : __d_(t.time_since_epoch()) {} |
| 737 | |
| 738 | // observer |
| 739 | |
| 740 | _LIBCPP_INLINE_VISIBILITY duration time_since_epoch() const {return __d_;} |
| 741 | |
| 742 | // arithmetic |
| 743 | |
Howard Hinnant | ecc7ba9 | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 744 | _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;} |
| 745 | _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] | 746 | |
| 747 | // special values |
| 748 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 749 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());} |
| 750 | _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] | 751 | }; |
| 752 | |
| 753 | } // chrono |
| 754 | |
| 755 | template <class _Clock, class _Duration1, class _Duration2> |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 756 | struct _LIBCPP_TYPE_VIS common_type<chrono::time_point<_Clock, _Duration1>, |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 757 | chrono::time_point<_Clock, _Duration2> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 758 | { |
| 759 | typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type; |
| 760 | }; |
| 761 | |
| 762 | namespace chrono { |
| 763 | |
| 764 | template <class _ToDuration, class _Clock, class _Duration> |
| 765 | inline _LIBCPP_INLINE_VISIBILITY |
| 766 | time_point<_Clock, _ToDuration> |
| 767 | time_point_cast(const time_point<_Clock, _Duration>& __t) |
| 768 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 769 | 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] | 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 __lhs.time_since_epoch() == __rhs.time_since_epoch(); |
| 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 < |
| 793 | |
| 794 | template <class _Clock, class _Duration1, class _Duration2> |
| 795 | inline _LIBCPP_INLINE_VISIBILITY |
| 796 | bool |
| 797 | operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 798 | { |
| 799 | return __lhs.time_since_epoch() < __rhs.time_since_epoch(); |
| 800 | } |
| 801 | |
| 802 | // time_point > |
| 803 | |
| 804 | template <class _Clock, class _Duration1, class _Duration2> |
| 805 | inline _LIBCPP_INLINE_VISIBILITY |
| 806 | bool |
| 807 | operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 808 | { |
| 809 | return __rhs < __lhs; |
| 810 | } |
| 811 | |
| 812 | // time_point <= |
| 813 | |
| 814 | template <class _Clock, class _Duration1, class _Duration2> |
| 815 | inline _LIBCPP_INLINE_VISIBILITY |
| 816 | bool |
| 817 | operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 818 | { |
| 819 | return !(__rhs < __lhs); |
| 820 | } |
| 821 | |
| 822 | // time_point >= |
| 823 | |
| 824 | template <class _Clock, class _Duration1, class _Duration2> |
| 825 | inline _LIBCPP_INLINE_VISIBILITY |
| 826 | bool |
| 827 | operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 828 | { |
| 829 | return !(__lhs < __rhs); |
| 830 | } |
| 831 | |
| 832 | // time_point operator+(time_point x, duration y); |
| 833 | |
| 834 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
| 835 | inline _LIBCPP_INLINE_VISIBILITY |
| 836 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 837 | operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 838 | { |
| 839 | typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr; |
| 840 | _Tr __r(__lhs.time_since_epoch()); |
| 841 | __r += __rhs; |
| 842 | return __r; |
| 843 | } |
| 844 | |
| 845 | // time_point operator+(duration x, time_point y); |
| 846 | |
| 847 | template <class _Rep1, class _Period1, class _Clock, class _Duration2> |
| 848 | inline _LIBCPP_INLINE_VISIBILITY |
| 849 | time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type> |
| 850 | operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 851 | { |
| 852 | return __rhs + __lhs; |
| 853 | } |
| 854 | |
| 855 | // time_point operator-(time_point x, duration y); |
| 856 | |
| 857 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
| 858 | inline _LIBCPP_INLINE_VISIBILITY |
| 859 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 860 | operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 861 | { |
| 862 | return __lhs + (-__rhs); |
| 863 | } |
| 864 | |
| 865 | // duration operator-(time_point x, time_point y); |
| 866 | |
| 867 | template <class _Clock, class _Duration1, class _Duration2> |
| 868 | inline _LIBCPP_INLINE_VISIBILITY |
| 869 | typename common_type<_Duration1, _Duration2>::type |
| 870 | operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 871 | { |
| 872 | return __lhs.time_since_epoch() - __rhs.time_since_epoch(); |
| 873 | } |
| 874 | |
| 875 | ////////////////////////////////////////////////////////// |
| 876 | /////////////////////// clocks /////////////////////////// |
| 877 | ////////////////////////////////////////////////////////// |
| 878 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 879 | class _LIBCPP_TYPE_VIS system_clock |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 880 | { |
| 881 | public: |
| 882 | typedef microseconds duration; |
| 883 | typedef duration::rep rep; |
| 884 | typedef duration::period period; |
| 885 | typedef chrono::time_point<system_clock> time_point; |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 886 | static const bool is_steady = false; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 887 | |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 888 | static time_point now() _NOEXCEPT; |
| 889 | static time_t to_time_t (const time_point& __t) _NOEXCEPT; |
| 890 | static time_point from_time_t(time_t __t) _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 891 | }; |
| 892 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 893 | class _LIBCPP_TYPE_VIS steady_clock |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 894 | { |
| 895 | public: |
| 896 | typedef nanoseconds duration; |
| 897 | typedef duration::rep rep; |
| 898 | typedef duration::period period; |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 899 | typedef chrono::time_point<steady_clock, duration> time_point; |
| 900 | static const bool is_steady = true; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 901 | |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 902 | static time_point now() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 903 | }; |
| 904 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 905 | typedef steady_clock high_resolution_clock; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 906 | |
| 907 | } // chrono |
| 908 | |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 909 | #if _LIBCPP_STD_VER > 11 |
| 910 | // Literal suffixes for chrono types |
| 911 | // inline // Deviation from N3690. |
| 912 | // We believe the inline to be a defect and have submitted an LWG issue. |
| 913 | // An LWG issue number has not yet been assigned. |
| 914 | namespace literals |
| 915 | { |
| 916 | inline namespace chrono_literals |
| 917 | { |
| 918 | |
| 919 | constexpr chrono::hours operator"" h(unsigned long long __h) |
| 920 | { |
| 921 | return chrono::hours(static_cast<chrono::hours::rep>(__h)); |
| 922 | } |
| 923 | |
| 924 | constexpr chrono::duration<long double, ratio<3600,1>> operator"" h(long double __h) |
| 925 | { |
| 926 | return chrono::duration<long double, ratio<3600,1>>(__h); |
| 927 | } |
| 928 | |
| 929 | |
| 930 | constexpr chrono::minutes operator"" min(unsigned long long __m) |
| 931 | { |
| 932 | return chrono::minutes(static_cast<chrono::minutes::rep>(__m)); |
| 933 | } |
| 934 | |
| 935 | constexpr chrono::duration<long double, ratio<60,1>> operator"" min(long double __m) |
| 936 | { |
| 937 | return chrono::duration<long double, ratio<60,1>> (__m); |
| 938 | } |
| 939 | |
| 940 | |
| 941 | constexpr chrono::seconds operator"" s(unsigned long long __s) |
| 942 | { |
| 943 | return chrono::seconds(static_cast<chrono::seconds::rep>(__s)); |
| 944 | } |
| 945 | |
| 946 | constexpr chrono::duration<long double> operator"" s(long double __s) |
| 947 | { |
| 948 | return chrono::duration<long double> (__s); |
| 949 | } |
| 950 | |
| 951 | |
| 952 | constexpr chrono::milliseconds operator"" ms(unsigned long long __ms) |
| 953 | { |
| 954 | return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms)); |
| 955 | } |
| 956 | |
| 957 | constexpr chrono::duration<long double, milli> operator"" ms(long double __ms) |
| 958 | { |
| 959 | return chrono::duration<long double, milli>(__ms); |
| 960 | } |
| 961 | |
| 962 | |
| 963 | constexpr chrono::microseconds operator"" us(unsigned long long __us) |
| 964 | { |
| 965 | return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us)); |
| 966 | } |
| 967 | |
| 968 | constexpr chrono::duration<long double, micro> operator"" us(long double __us) |
| 969 | { |
| 970 | return chrono::duration<long double, micro> (__us); |
| 971 | } |
| 972 | |
| 973 | |
| 974 | constexpr chrono::nanoseconds operator"" ns(unsigned long long __ns) |
| 975 | { |
| 976 | return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns)); |
| 977 | } |
| 978 | |
| 979 | constexpr chrono::duration<long double, nano> operator"" ns(long double __ns) |
| 980 | { |
| 981 | return chrono::duration<long double, nano> (__ns); |
| 982 | } |
| 983 | |
| 984 | }} |
| 985 | #endif |
| 986 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 987 | _LIBCPP_END_NAMESPACE_STD |
| 988 | |
| 989 | #endif // _LIBCPP_CHRONO |