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: |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 114 | time_point(); // has value "epoch" // constexpr in C++14 |
| 115 | explicit time_point(const duration& d); // same as time_point() + d // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 116 | |
| 117 | // conversions |
| 118 | template <class Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 119 | time_point(const time_point<clock, Duration2>& t); // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | |
| 121 | // observer |
| 122 | |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 123 | duration time_since_epoch() const; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Marshall Clow | f2eabaf | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 197 | template <class ToDuration, class Rep, class Period> |
| 198 | constexpr ToDuration floor(const duration<Rep, Period>& d); // C++17 |
| 199 | template <class ToDuration, class Rep, class Period> |
| 200 | constexpr ToDuration ceil(const duration<Rep, Period>& d); // C++17 |
| 201 | template <class ToDuration, class Rep, class Period> |
| 202 | constexpr ToDuration round(const duration<Rep, Period>& d); // C++17 |
| 203 | |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 204 | // time_point arithmetic (all constexpr in C++14) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 205 | template <class Clock, class Duration1, class Rep2, class Period2> |
| 206 | time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type> |
| 207 | operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs); |
| 208 | template <class Rep1, class Period1, class Clock, class Duration2> |
| 209 | time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type> |
| 210 | operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 211 | template <class Clock, class Duration1, class Rep2, class Period2> |
| 212 | time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type> |
| 213 | operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs); |
| 214 | template <class Clock, class Duration1, class Duration2> |
| 215 | typename common_type<Duration1, Duration2>::type |
| 216 | operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 217 | |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 218 | // time_point comparisons (all constexpr in C++14) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | template <class Clock, class Duration1, class Duration2> |
| 220 | bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 221 | template <class Clock, class Duration1, class Duration2> |
| 222 | bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 223 | template <class Clock, class Duration1, class Duration2> |
| 224 | bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 225 | template <class Clock, class Duration1, class Duration2> |
| 226 | bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 227 | template <class Clock, class Duration1, class Duration2> |
| 228 | bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 229 | template <class Clock, class Duration1, class Duration2> |
| 230 | bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 231 | |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 232 | // time_point_cast (constexpr in C++14) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | |
| 234 | template <class ToDuration, class Clock, class Duration> |
| 235 | time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t); |
| 236 | |
Marshall Clow | f2eabaf | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 237 | template <class ToDuration, class Clock, class Duration> |
| 238 | constexpr time_point<Clock, ToDuration> |
| 239 | floor(const time_point<Clock, Duration>& tp); // C++17 |
| 240 | |
| 241 | template <class ToDuration, class Clock, class Duration> |
| 242 | constexpr time_point<Clock, ToDuration> |
| 243 | ceil(const time_point<Clock, Duration>& tp); // C++17 |
| 244 | |
| 245 | template <class ToDuration, class Clock, class Duration> |
| 246 | constexpr time_point<Clock, ToDuration> |
| 247 | round(const time_point<Clock, Duration>& tp); // C++17 |
| 248 | |
| 249 | template <class Rep, class Period> |
| 250 | constexpr duration<Rep, Period> abs(duration<Rep, Period> d); // C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 251 | // Clocks |
| 252 | |
| 253 | class system_clock |
| 254 | { |
| 255 | public: |
| 256 | typedef microseconds duration; |
| 257 | typedef duration::rep rep; |
| 258 | typedef duration::period period; |
| 259 | typedef chrono::time_point<system_clock> time_point; |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 260 | static const bool is_steady = false; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 261 | |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 262 | static time_point now() noexcept; |
| 263 | static time_t to_time_t (const time_point& __t) noexcept; |
| 264 | static time_point from_time_t(time_t __t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 267 | class steady_clock |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 268 | { |
| 269 | public: |
| 270 | typedef nanoseconds duration; |
| 271 | typedef duration::rep rep; |
| 272 | typedef duration::period period; |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 273 | typedef chrono::time_point<steady_clock, duration> time_point; |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 274 | static const bool is_steady = true; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 275 | |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 276 | static time_point now() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 279 | typedef steady_clock high_resolution_clock; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 280 | |
| 281 | } // chrono |
| 282 | |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 283 | constexpr chrono::hours operator "" h(unsigned long long); // C++14 |
| 284 | constexpr chrono::duration<unspecified , ratio<3600,1>> operator "" h(long double); // C++14 |
| 285 | constexpr chrono::minutes operator "" min(unsigned long long); // C++14 |
| 286 | constexpr chrono::duration<unspecified , ratio<60,1>> operator "" min(long double); // C++14 |
| 287 | constexpr chrono::seconds operator "" s(unsigned long long); // C++14 |
| 288 | constexpr chrono::duration<unspecified > operator "" s(long double); // C++14 |
| 289 | constexpr chrono::milliseconds operator "" ms(unsigned long long); // C++14 |
| 290 | constexpr chrono::duration<unspecified , milli> operator "" ms(long double); // C++14 |
| 291 | constexpr chrono::microseconds operator "" us(unsigned long long); // C++14 |
| 292 | constexpr chrono::duration<unspecified , micro> operator "" us(long double); // C++14 |
| 293 | constexpr chrono::nanoseconds operator "" ns(unsigned long long); // C++14 |
| 294 | constexpr chrono::duration<unspecified , nano> operator "" ns(long double); // C++14 |
| 295 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | } // std |
| 297 | */ |
| 298 | |
| 299 | #include <__config> |
| 300 | #include <ctime> |
| 301 | #include <type_traits> |
| 302 | #include <ratio> |
| 303 | #include <limits> |
| 304 | |
Howard Hinnant | c5a5fbd | 2011-11-29 16:45:27 +0000 | [diff] [blame] | 305 | #include <__undef_min_max> |
| 306 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 307 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 308 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 309 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 310 | |
| 311 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 312 | |
| 313 | namespace chrono |
| 314 | { |
| 315 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 316 | template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TYPE_VIS_ONLY duration; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 317 | |
Howard Hinnant | f39463b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 318 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 319 | struct __is_duration : false_type {}; |
| 320 | |
| 321 | template <class _Rep, class _Period> |
| 322 | struct __is_duration<duration<_Rep, _Period> > : true_type {}; |
| 323 | |
| 324 | template <class _Rep, class _Period> |
| 325 | struct __is_duration<const duration<_Rep, _Period> > : true_type {}; |
| 326 | |
| 327 | template <class _Rep, class _Period> |
| 328 | struct __is_duration<volatile duration<_Rep, _Period> > : true_type {}; |
| 329 | |
| 330 | template <class _Rep, class _Period> |
| 331 | struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {}; |
| 332 | |
| 333 | } // chrono |
| 334 | |
| 335 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 336 | struct _LIBCPP_TYPE_VIS_ONLY common_type<chrono::duration<_Rep1, _Period1>, |
| 337 | chrono::duration<_Rep2, _Period2> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 338 | { |
| 339 | typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, |
| 340 | typename __ratio_gcd<_Period1, _Period2>::type> type; |
| 341 | }; |
| 342 | |
| 343 | namespace chrono { |
| 344 | |
| 345 | // duration_cast |
| 346 | |
| 347 | template <class _FromDuration, class _ToDuration, |
| 348 | class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type, |
| 349 | bool = _Period::num == 1, |
| 350 | bool = _Period::den == 1> |
| 351 | struct __duration_cast; |
| 352 | |
| 353 | template <class _FromDuration, class _ToDuration, class _Period> |
| 354 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true> |
| 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 | return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count())); |
| 360 | } |
| 361 | }; |
| 362 | |
| 363 | template <class _FromDuration, class _ToDuration, class _Period> |
| 364 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false> |
| 365 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 366 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 367 | _ToDuration operator()(const _FromDuration& __fd) const |
| 368 | { |
| 369 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 370 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 371 | static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den))); |
| 372 | } |
| 373 | }; |
| 374 | |
| 375 | template <class _FromDuration, class _ToDuration, class _Period> |
| 376 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true> |
| 377 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 378 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 379 | _ToDuration operator()(const _FromDuration& __fd) const |
| 380 | { |
| 381 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 382 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 383 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num))); |
| 384 | } |
| 385 | }; |
| 386 | |
| 387 | template <class _FromDuration, class _ToDuration, class _Period> |
| 388 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> |
| 389 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 390 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | _ToDuration operator()(const _FromDuration& __fd) const |
| 392 | { |
| 393 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 394 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 395 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num) |
| 396 | / static_cast<_Ct>(_Period::den))); |
| 397 | } |
| 398 | }; |
| 399 | |
| 400 | template <class _ToDuration, class _Rep, class _Period> |
| 401 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 402 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 403 | typename enable_if |
| 404 | < |
| 405 | __is_duration<_ToDuration>::value, |
| 406 | _ToDuration |
| 407 | >::type |
| 408 | duration_cast(const duration<_Rep, _Period>& __fd) |
| 409 | { |
| 410 | return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd); |
| 411 | } |
| 412 | |
Howard Hinnant | 874ad9a | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 413 | template <class _Rep> |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 414 | struct _LIBCPP_TYPE_VIS_ONLY treat_as_floating_point : is_floating_point<_Rep> {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 415 | |
| 416 | template <class _Rep> |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 417 | struct _LIBCPP_TYPE_VIS_ONLY duration_values |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 418 | { |
| 419 | public: |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 420 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);} |
| 421 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();} |
| 422 | _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] | 423 | }; |
| 424 | |
Marshall Clow | f2eabaf | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 425 | #if _LIBCPP_STD_VER > 14 |
| 426 | template <class _ToDuration, class _Rep, class _Period> |
| 427 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 428 | typename enable_if |
| 429 | < |
| 430 | __is_duration<_ToDuration>::value, |
| 431 | _ToDuration |
| 432 | >::type |
| 433 | floor(const duration<_Rep, _Period>& __d) |
| 434 | { |
| 435 | _ToDuration __t = duration_cast<_ToDuration>(__d); |
| 436 | if (__t > __d) |
| 437 | __t = __t - _ToDuration{1}; |
| 438 | return __t; |
| 439 | } |
| 440 | |
| 441 | template <class _ToDuration, class _Rep, class _Period> |
| 442 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 443 | typename enable_if |
| 444 | < |
| 445 | __is_duration<_ToDuration>::value, |
| 446 | _ToDuration |
| 447 | >::type |
| 448 | ceil(const duration<_Rep, _Period>& __d) |
| 449 | { |
| 450 | _ToDuration __t = duration_cast<_ToDuration>(__d); |
| 451 | if (__t < __d) |
| 452 | __t = __t + _ToDuration{1}; |
| 453 | return __t; |
| 454 | } |
| 455 | |
| 456 | template <class _ToDuration, class _Rep, class _Period> |
| 457 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 458 | typename enable_if |
| 459 | < |
| 460 | __is_duration<_ToDuration>::value, |
| 461 | _ToDuration |
| 462 | >::type |
| 463 | round(const duration<_Rep, _Period>& __d) |
| 464 | { |
| 465 | _ToDuration __lower = floor<_ToDuration>(__d); |
| 466 | _ToDuration __upper = __lower + _ToDuration{1}; |
| 467 | auto __lowerDiff = __d - __lower; |
| 468 | auto __upperDiff = __upper - __d; |
| 469 | if (__lowerDiff < __upperDiff) |
| 470 | return __lower; |
| 471 | if (__lowerDiff > __upperDiff) |
| 472 | return __upper; |
| 473 | return __lower.count() & 1 ? __upper : __lower; |
| 474 | } |
| 475 | #endif |
| 476 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 477 | // duration |
| 478 | |
| 479 | template <class _Rep, class _Period> |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 480 | class _LIBCPP_TYPE_VIS_ONLY duration |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 481 | { |
| 482 | static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration"); |
| 483 | static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio"); |
| 484 | static_assert(_Period::num > 0, "duration period must be positive"); |
Howard Hinnant | 0a51e15 | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 485 | |
| 486 | template <class _R1, class _R2> |
| 487 | struct __no_overflow |
| 488 | { |
| 489 | private: |
| 490 | static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; |
| 491 | static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; |
| 492 | static const intmax_t __n1 = _R1::num / __gcd_n1_n2; |
| 493 | static const intmax_t __d1 = _R1::den / __gcd_d1_d2; |
| 494 | static const intmax_t __n2 = _R2::num / __gcd_n1_n2; |
| 495 | static const intmax_t __d2 = _R2::den / __gcd_d1_d2; |
| 496 | static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1); |
| 497 | |
| 498 | template <intmax_t _Xp, intmax_t _Yp, bool __overflow> |
| 499 | struct __mul // __overflow == false |
| 500 | { |
| 501 | static const intmax_t value = _Xp * _Yp; |
| 502 | }; |
| 503 | |
| 504 | template <intmax_t _Xp, intmax_t _Yp> |
| 505 | struct __mul<_Xp, _Yp, true> |
| 506 | { |
| 507 | static const intmax_t value = 1; |
| 508 | }; |
| 509 | |
| 510 | public: |
| 511 | static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1); |
| 512 | typedef ratio<__mul<__n1, __d2, !value>::value, |
| 513 | __mul<__n2, __d1, !value>::value> type; |
| 514 | }; |
| 515 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 516 | public: |
| 517 | typedef _Rep rep; |
| 518 | typedef _Period period; |
| 519 | private: |
| 520 | rep __rep_; |
| 521 | public: |
| 522 | |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 523 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 524 | #ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 525 | duration() = default; |
Marshall Clow | 3dbcf13 | 2013-07-31 19:39:37 +0000 | [diff] [blame] | 526 | #else |
| 527 | duration() {} |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 528 | #endif |
| 529 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 530 | template <class _Rep2> |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 531 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 532 | explicit duration(const _Rep2& __r, |
| 533 | typename enable_if |
| 534 | < |
| 535 | is_convertible<_Rep2, rep>::value && |
| 536 | (treat_as_floating_point<rep>::value || |
| 537 | !treat_as_floating_point<_Rep2>::value) |
| 538 | >::type* = 0) |
| 539 | : __rep_(__r) {} |
| 540 | |
| 541 | // conversions |
| 542 | template <class _Rep2, class _Period2> |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 543 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 544 | duration(const duration<_Rep2, _Period2>& __d, |
| 545 | typename enable_if |
| 546 | < |
Howard Hinnant | 0a51e15 | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 547 | __no_overflow<_Period2, period>::value && ( |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 548 | treat_as_floating_point<rep>::value || |
Howard Hinnant | 0a51e15 | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 549 | (__no_overflow<_Period2, period>::type::den == 1 && |
| 550 | !treat_as_floating_point<_Rep2>::value)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 551 | >::type* = 0) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 552 | : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 553 | |
| 554 | // observer |
| 555 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 556 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 557 | |
| 558 | // arithmetic |
| 559 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 560 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator+() const {return *this;} |
| 561 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator-() const {return duration(-__rep_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 562 | _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;} |
| 563 | _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);} |
| 564 | _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;} |
| 565 | _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);} |
| 566 | |
| 567 | _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;} |
| 568 | _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;} |
| 569 | |
| 570 | _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;} |
| 571 | _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;} |
| 572 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;} |
| 573 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;} |
| 574 | |
| 575 | // special values |
| 576 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 577 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());} |
| 578 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());} |
| 579 | _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] | 580 | }; |
| 581 | |
| 582 | typedef duration<long long, nano> nanoseconds; |
| 583 | typedef duration<long long, micro> microseconds; |
| 584 | typedef duration<long long, milli> milliseconds; |
| 585 | typedef duration<long long > seconds; |
| 586 | typedef duration< long, ratio< 60> > minutes; |
| 587 | typedef duration< long, ratio<3600> > hours; |
| 588 | |
| 589 | // Duration == |
| 590 | |
| 591 | template <class _LhsDuration, class _RhsDuration> |
| 592 | struct __duration_eq |
| 593 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 594 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 595 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 596 | { |
| 597 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 598 | return _Ct(__lhs).count() == _Ct(__rhs).count(); |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | template <class _LhsDuration> |
| 603 | struct __duration_eq<_LhsDuration, _LhsDuration> |
| 604 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 605 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 606 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 607 | {return __lhs.count() == __rhs.count();} |
| 608 | }; |
| 609 | |
| 610 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 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 | bool |
| 614 | operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 615 | { |
| 616 | return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 617 | } |
| 618 | |
| 619 | // Duration != |
| 620 | |
| 621 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 622 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 623 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 624 | bool |
| 625 | operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 626 | { |
| 627 | return !(__lhs == __rhs); |
| 628 | } |
| 629 | |
| 630 | // Duration < |
| 631 | |
| 632 | template <class _LhsDuration, class _RhsDuration> |
| 633 | struct __duration_lt |
| 634 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 635 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 636 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 637 | { |
| 638 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 639 | return _Ct(__lhs).count() < _Ct(__rhs).count(); |
| 640 | } |
| 641 | }; |
| 642 | |
| 643 | template <class _LhsDuration> |
| 644 | struct __duration_lt<_LhsDuration, _LhsDuration> |
| 645 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 646 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 12967fb | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 647 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 648 | {return __lhs.count() < __rhs.count();} |
| 649 | }; |
| 650 | |
| 651 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 652 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 653 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 654 | bool |
| 655 | operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 656 | { |
| 657 | return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 658 | } |
| 659 | |
| 660 | // Duration > |
| 661 | |
| 662 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 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 | bool |
| 666 | operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 667 | { |
| 668 | return __rhs < __lhs; |
| 669 | } |
| 670 | |
| 671 | // Duration <= |
| 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 | bool |
| 677 | operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 678 | { |
| 679 | return !(__rhs < __lhs); |
| 680 | } |
| 681 | |
| 682 | // Duration >= |
| 683 | |
| 684 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 685 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 686 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 687 | bool |
| 688 | operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 689 | { |
| 690 | return !(__lhs < __rhs); |
| 691 | } |
| 692 | |
| 693 | // Duration + |
| 694 | |
| 695 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 696 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 697 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 698 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 699 | operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 700 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 701 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 702 | return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // Duration - |
| 706 | |
| 707 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 708 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 709 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 710 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 711 | operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 712 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 713 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 714 | return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | // Duration * |
| 718 | |
| 719 | template <class _Rep1, class _Period, class _Rep2> |
| 720 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 721 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 722 | typename enable_if |
| 723 | < |
| 724 | is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value, |
| 725 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 726 | >::type |
| 727 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 728 | { |
| 729 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 730 | typedef duration<_Cr, _Period> _Cd; |
| 731 | return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | template <class _Rep1, class _Period, class _Rep2> |
| 735 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 736 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 737 | typename enable_if |
| 738 | < |
| 739 | is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value, |
| 740 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 741 | >::type |
| 742 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) |
| 743 | { |
| 744 | return __d * __s; |
| 745 | } |
| 746 | |
| 747 | // Duration / |
| 748 | |
| 749 | template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value> |
| 750 | struct __duration_divide_result |
| 751 | { |
| 752 | }; |
| 753 | |
| 754 | template <class _Duration, class _Rep2, |
| 755 | bool = is_convertible<_Rep2, |
| 756 | typename common_type<typename _Duration::rep, _Rep2>::type>::value> |
| 757 | struct __duration_divide_imp |
| 758 | { |
| 759 | }; |
| 760 | |
| 761 | template <class _Rep1, class _Period, class _Rep2> |
| 762 | struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true> |
| 763 | { |
| 764 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type; |
| 765 | }; |
| 766 | |
| 767 | template <class _Rep1, class _Period, class _Rep2> |
| 768 | struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false> |
| 769 | : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2> |
| 770 | { |
| 771 | }; |
| 772 | |
| 773 | template <class _Rep1, class _Period, class _Rep2> |
| 774 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 775 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 776 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 777 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 778 | { |
| 779 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 780 | typedef duration<_Cr, _Period> _Cd; |
| 781 | return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 785 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 786 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 787 | typename common_type<_Rep1, _Rep2>::type |
| 788 | operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 789 | { |
| 790 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct; |
| 791 | return _Ct(__lhs).count() / _Ct(__rhs).count(); |
| 792 | } |
| 793 | |
| 794 | // Duration % |
| 795 | |
| 796 | template <class _Rep1, class _Period, class _Rep2> |
| 797 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 798 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 799 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 800 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 801 | { |
| 802 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 803 | typedef duration<_Cr, _Period> _Cd; |
| 804 | return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 808 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 809 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 810 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 811 | operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 812 | { |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 813 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 814 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 815 | 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] | 816 | } |
| 817 | |
| 818 | ////////////////////////////////////////////////////////// |
| 819 | ///////////////////// time_point ///////////////////////// |
| 820 | ////////////////////////////////////////////////////////// |
| 821 | |
| 822 | template <class _Clock, class _Duration = typename _Clock::duration> |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 823 | class _LIBCPP_TYPE_VIS_ONLY time_point |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 824 | { |
| 825 | static_assert(__is_duration<_Duration>::value, |
| 826 | "Second template parameter of time_point must be a std::chrono::duration"); |
| 827 | public: |
| 828 | typedef _Clock clock; |
| 829 | typedef _Duration duration; |
| 830 | typedef typename duration::rep rep; |
| 831 | typedef typename duration::period period; |
| 832 | private: |
| 833 | duration __d_; |
| 834 | |
| 835 | public: |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 836 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {} |
| 837 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 838 | |
| 839 | // conversions |
| 840 | template <class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 841 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 842 | time_point(const time_point<clock, _Duration2>& t, |
| 843 | typename enable_if |
| 844 | < |
| 845 | is_convertible<_Duration2, duration>::value |
| 846 | >::type* = 0) |
| 847 | : __d_(t.time_since_epoch()) {} |
| 848 | |
| 849 | // observer |
| 850 | |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 851 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 852 | |
| 853 | // arithmetic |
| 854 | |
Howard Hinnant | ecc7ba9 | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 855 | _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;} |
| 856 | _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] | 857 | |
| 858 | // special values |
| 859 | |
Howard Hinnant | cf3143c | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 860 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());} |
| 861 | _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] | 862 | }; |
| 863 | |
| 864 | } // chrono |
| 865 | |
| 866 | template <class _Clock, class _Duration1, class _Duration2> |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 867 | struct _LIBCPP_TYPE_VIS_ONLY common_type<chrono::time_point<_Clock, _Duration1>, |
| 868 | chrono::time_point<_Clock, _Duration2> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 869 | { |
| 870 | typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type; |
| 871 | }; |
| 872 | |
| 873 | namespace chrono { |
| 874 | |
| 875 | template <class _ToDuration, class _Clock, class _Duration> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 876 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 877 | time_point<_Clock, _ToDuration> |
| 878 | time_point_cast(const time_point<_Clock, _Duration>& __t) |
| 879 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 880 | 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] | 881 | } |
| 882 | |
Marshall Clow | f2eabaf | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 883 | #if _LIBCPP_STD_VER > 14 |
| 884 | template <class _ToDuration, class _Clock, class _Duration> |
| 885 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 886 | typename enable_if |
| 887 | < |
| 888 | __is_duration<_ToDuration>::value, |
| 889 | time_point<_Clock, _ToDuration> |
| 890 | >::type |
| 891 | floor(const time_point<_Clock, _Duration>& __t) |
| 892 | { |
| 893 | return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())}; |
| 894 | } |
| 895 | |
| 896 | template <class _ToDuration, class _Clock, class _Duration> |
| 897 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 898 | typename enable_if |
| 899 | < |
| 900 | __is_duration<_ToDuration>::value, |
| 901 | time_point<_Clock, _ToDuration> |
| 902 | >::type |
| 903 | ceil(const time_point<_Clock, _Duration>& __t) |
| 904 | { |
| 905 | return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())}; |
| 906 | } |
| 907 | |
| 908 | template <class _ToDuration, class _Clock, class _Duration> |
| 909 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 910 | typename enable_if |
| 911 | < |
| 912 | __is_duration<_ToDuration>::value, |
| 913 | time_point<_Clock, _ToDuration> |
| 914 | >::type |
| 915 | round(const time_point<_Clock, _Duration>& __t) |
| 916 | { |
| 917 | return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())}; |
| 918 | } |
| 919 | |
| 920 | template <class _Rep, class _Period> |
| 921 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 922 | typename enable_if |
| 923 | < |
| 924 | numeric_limits<_Rep>::is_signed, |
| 925 | duration<_Rep, _Period> |
| 926 | >::type |
| 927 | abs(duration<_Rep, _Period> __d) |
| 928 | { |
| 929 | return __d >= __d.zero() ? __d : -__d; |
| 930 | } |
| 931 | #endif |
| 932 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 933 | // time_point == |
| 934 | |
| 935 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 936 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 937 | bool |
| 938 | operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 939 | { |
| 940 | return __lhs.time_since_epoch() == __rhs.time_since_epoch(); |
| 941 | } |
| 942 | |
| 943 | // time_point != |
| 944 | |
| 945 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 946 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 947 | bool |
| 948 | operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 949 | { |
| 950 | return !(__lhs == __rhs); |
| 951 | } |
| 952 | |
| 953 | // time_point < |
| 954 | |
| 955 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 956 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 957 | bool |
| 958 | operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 959 | { |
| 960 | return __lhs.time_since_epoch() < __rhs.time_since_epoch(); |
| 961 | } |
| 962 | |
| 963 | // time_point > |
| 964 | |
| 965 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 966 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 967 | bool |
| 968 | operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 969 | { |
| 970 | return __rhs < __lhs; |
| 971 | } |
| 972 | |
| 973 | // time_point <= |
| 974 | |
| 975 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 976 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 977 | bool |
| 978 | operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 979 | { |
| 980 | return !(__rhs < __lhs); |
| 981 | } |
| 982 | |
| 983 | // time_point >= |
| 984 | |
| 985 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 986 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 987 | bool |
| 988 | operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 989 | { |
| 990 | return !(__lhs < __rhs); |
| 991 | } |
| 992 | |
| 993 | // time_point operator+(time_point x, duration y); |
| 994 | |
| 995 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 996 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 997 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 998 | operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 999 | { |
| 1000 | typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr; |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1001 | return _Tr (__lhs.time_since_epoch() + __rhs); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | // time_point operator+(duration x, time_point y); |
| 1005 | |
| 1006 | template <class _Rep1, class _Period1, class _Clock, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1007 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1008 | time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type> |
| 1009 | operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 1010 | { |
| 1011 | return __rhs + __lhs; |
| 1012 | } |
| 1013 | |
| 1014 | // time_point operator-(time_point x, duration y); |
| 1015 | |
| 1016 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1017 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1018 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 1019 | operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 1020 | { |
| 1021 | return __lhs + (-__rhs); |
| 1022 | } |
| 1023 | |
| 1024 | // duration operator-(time_point x, time_point y); |
| 1025 | |
| 1026 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1027 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1028 | typename common_type<_Duration1, _Duration2>::type |
| 1029 | operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 1030 | { |
| 1031 | return __lhs.time_since_epoch() - __rhs.time_since_epoch(); |
| 1032 | } |
| 1033 | |
| 1034 | ////////////////////////////////////////////////////////// |
| 1035 | /////////////////////// clocks /////////////////////////// |
| 1036 | ////////////////////////////////////////////////////////// |
| 1037 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 1038 | class _LIBCPP_TYPE_VIS system_clock |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1039 | { |
| 1040 | public: |
| 1041 | typedef microseconds duration; |
| 1042 | typedef duration::rep rep; |
| 1043 | typedef duration::period period; |
| 1044 | typedef chrono::time_point<system_clock> time_point; |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1045 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1046 | |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 1047 | static time_point now() _NOEXCEPT; |
| 1048 | static time_t to_time_t (const time_point& __t) _NOEXCEPT; |
| 1049 | static time_point from_time_t(time_t __t) _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1050 | }; |
| 1051 | |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 1052 | #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 1053 | class _LIBCPP_TYPE_VIS steady_clock |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1054 | { |
| 1055 | public: |
| 1056 | typedef nanoseconds duration; |
| 1057 | typedef duration::rep rep; |
| 1058 | typedef duration::period period; |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 1059 | typedef chrono::time_point<steady_clock, duration> time_point; |
Marshall Clow | 5c459c9 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1060 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1061 | |
Howard Hinnant | aa54ac4 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 1062 | static time_point now() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1063 | }; |
| 1064 | |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 1065 | typedef steady_clock high_resolution_clock; |
Jonathan Roelofs | cce96eb | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 1066 | #else |
| 1067 | typedef system_clock high_resolution_clock; |
| 1068 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1069 | |
| 1070 | } // chrono |
| 1071 | |
Marshall Clow | ac86837 | 2013-10-05 21:18:32 +0000 | [diff] [blame] | 1072 | #if _LIBCPP_STD_VER > 11 |
| 1073 | // Suffixes for duration literals [time.duration.literals] |
| 1074 | inline namespace literals |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1075 | { |
| 1076 | inline namespace chrono_literals |
| 1077 | { |
| 1078 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1079 | constexpr chrono::hours operator"" h(unsigned long long __h) |
| 1080 | { |
| 1081 | return chrono::hours(static_cast<chrono::hours::rep>(__h)); |
| 1082 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1083 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1084 | constexpr chrono::duration<long double, ratio<3600,1>> operator"" h(long double __h) |
| 1085 | { |
| 1086 | return chrono::duration<long double, ratio<3600,1>>(__h); |
| 1087 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1088 | |
| 1089 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1090 | constexpr chrono::minutes operator"" min(unsigned long long __m) |
| 1091 | { |
| 1092 | return chrono::minutes(static_cast<chrono::minutes::rep>(__m)); |
| 1093 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1094 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1095 | constexpr chrono::duration<long double, ratio<60,1>> operator"" min(long double __m) |
| 1096 | { |
| 1097 | return chrono::duration<long double, ratio<60,1>> (__m); |
| 1098 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1099 | |
| 1100 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1101 | constexpr chrono::seconds operator"" s(unsigned long long __s) |
| 1102 | { |
| 1103 | return chrono::seconds(static_cast<chrono::seconds::rep>(__s)); |
| 1104 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1105 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1106 | constexpr chrono::duration<long double> operator"" s(long double __s) |
| 1107 | { |
| 1108 | return chrono::duration<long double> (__s); |
| 1109 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1110 | |
| 1111 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1112 | constexpr chrono::milliseconds operator"" ms(unsigned long long __ms) |
| 1113 | { |
| 1114 | return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms)); |
| 1115 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1116 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1117 | constexpr chrono::duration<long double, milli> operator"" ms(long double __ms) |
| 1118 | { |
| 1119 | return chrono::duration<long double, milli>(__ms); |
| 1120 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1121 | |
| 1122 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1123 | constexpr chrono::microseconds operator"" us(unsigned long long __us) |
| 1124 | { |
| 1125 | return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us)); |
| 1126 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1127 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1128 | constexpr chrono::duration<long double, micro> operator"" us(long double __us) |
| 1129 | { |
| 1130 | return chrono::duration<long double, micro> (__us); |
| 1131 | } |
| 1132 | |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1133 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1134 | constexpr chrono::nanoseconds operator"" ns(unsigned long long __ns) |
| 1135 | { |
| 1136 | return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns)); |
| 1137 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1138 | |
Howard Hinnant | 5c16756 | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1139 | constexpr chrono::duration<long double, nano> operator"" ns(long double __ns) |
| 1140 | { |
| 1141 | return chrono::duration<long double, nano> (__ns); |
| 1142 | } |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1143 | |
| 1144 | }} |
Marshall Clow | ac86837 | 2013-10-05 21:18:32 +0000 | [diff] [blame] | 1145 | |
| 1146 | namespace chrono { // hoist the literals into namespace std::chrono |
| 1147 | using namespace literals::chrono_literals; |
| 1148 | } |
| 1149 | |
Marshall Clow | eb41e5c | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1150 | #endif |
| 1151 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1152 | _LIBCPP_END_NAMESPACE_STD |
| 1153 | |
| 1154 | #endif // _LIBCPP_CHRONO |