]>
Commit | Line | Data |
---|---|---|
15e38d0d CF |
1 | // <chrono> -*- C++ -*- |
2 | ||
405feeb8 | 3 | // Copyright (C) 2008-2013 Free Software Foundation, Inc. |
15e38d0d CF |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
15e38d0d CF |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
15e38d0d | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
15e38d0d CF |
24 | |
25 | /** @file include/chrono | |
26 | * This is a Standard C++ Library header. | |
27 | */ | |
28 | ||
29 | #ifndef _GLIBCXX_CHRONO | |
30 | #define _GLIBCXX_CHRONO 1 | |
31 | ||
32 | #pragma GCC system_header | |
33 | ||
734f5023 | 34 | #if __cplusplus < 201103L |
ab65a4c7 | 35 | # include <bits/c++0x_warning.h> |
15e38d0d CF |
36 | #else |
37 | ||
15e38d0d CF |
38 | #include <ratio> |
39 | #include <type_traits> | |
40 | #include <limits> | |
41 | #include <ctime> | |
1c9f675f | 42 | #include <bits/parse_numbers.h> // for literals support. |
15e38d0d CF |
43 | |
44 | #ifdef _GLIBCXX_USE_C99_STDINT_TR1 | |
45 | ||
12ffa228 BK |
46 | namespace std _GLIBCXX_VISIBILITY(default) |
47 | { | |
5b9daa7e BK |
48 | /** |
49 | * @defgroup chrono Time | |
50 | * @ingroup utilities | |
51 | * | |
52 | * Classes and functions for time. | |
53 | * @{ | |
54 | */ | |
55 | ||
56 | /** @namespace std::chrono | |
9ab48d6e | 57 | * @brief ISO C++ 2011 entities sub-namespace for time and date. |
5b9daa7e | 58 | */ |
15e38d0d CF |
59 | namespace chrono |
60 | { | |
12ffa228 BK |
61 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
62 | ||
15e38d0d CF |
63 | template<typename _Rep, typename _Period = ratio<1>> |
64 | struct duration; | |
65 | ||
16684e9c | 66 | template<typename _Clock, typename _Dur = typename _Clock::duration> |
15e38d0d | 67 | struct time_point; |
12ffa228 BK |
68 | |
69 | _GLIBCXX_END_NAMESPACE_VERSION | |
15e38d0d CF |
70 | } |
71 | ||
12ffa228 | 72 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
b3618b71 DK |
73 | |
74 | // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly) | |
75 | ||
76 | template<typename _CT, typename _Period1, typename _Period2> | |
77 | struct __duration_common_type_wrapper | |
15e38d0d | 78 | { |
16684e9c | 79 | private: |
b3618b71 DK |
80 | typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num; |
81 | typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den; | |
82 | typedef typename _CT::type __cr; | |
16684e9c | 83 | typedef ratio<__gcd_num::value, |
b3618b71 | 84 | (_Period1::den / __gcd_den::value) * _Period2::den> __r; |
16684e9c | 85 | public: |
b3618b71 | 86 | typedef __success_type<chrono::duration<__cr, __r>> type; |
15e38d0d | 87 | }; |
94a86be0 | 88 | |
b3618b71 DK |
89 | template<typename _Period1, typename _Period2> |
90 | struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2> | |
91 | { typedef __failure_type type; }; | |
16684e9c | 92 | |
b3618b71 DK |
93 | template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> |
94 | struct common_type<chrono::duration<_Rep1, _Period1>, | |
95 | chrono::duration<_Rep2, _Period2>> | |
96 | : public __duration_common_type_wrapper<typename __member_type_wrapper< | |
97 | common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type | |
98 | { }; | |
99 | ||
100 | // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly) | |
101 | ||
102 | template<typename _CT, typename _Clock> | |
103 | struct __timepoint_common_type_wrapper | |
104 | { | |
105 | typedef __success_type<chrono::time_point<_Clock, typename _CT::type>> | |
106 | type; | |
15e38d0d | 107 | }; |
b3618b71 DK |
108 | |
109 | template<typename _Clock> | |
110 | struct __timepoint_common_type_wrapper<__failure_type, _Clock> | |
111 | { typedef __failure_type type; }; | |
112 | ||
113 | template<typename _Clock, typename _Duration1, typename _Duration2> | |
114 | struct common_type<chrono::time_point<_Clock, _Duration1>, | |
115 | chrono::time_point<_Clock, _Duration2>> | |
116 | : public __timepoint_common_type_wrapper<typename __member_type_wrapper< | |
117 | common_type<_Duration1, _Duration2>>::type, _Clock>::type | |
118 | { }; | |
119 | ||
12ffa228 | 120 | _GLIBCXX_END_NAMESPACE_VERSION |
15e38d0d | 121 | |
94a86be0 | 122 | namespace chrono |
15e38d0d | 123 | { |
12ffa228 BK |
124 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
125 | ||
5b9daa7e | 126 | // Primary template for duration_cast impl. |
16684e9c | 127 | template<typename _ToDur, typename _CF, typename _CR, |
94a86be0 | 128 | bool _NumIsOne = false, bool _DenIsOne = false> |
15e38d0d CF |
129 | struct __duration_cast_impl |
130 | { | |
94a86be0 | 131 | template<typename _Rep, typename _Period> |
16684e9c | 132 | static constexpr _ToDur |
94a86be0 BK |
133 | __cast(const duration<_Rep, _Period>& __d) |
134 | { | |
16684e9c BK |
135 | typedef typename _ToDur::rep __to_rep; |
136 | return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count()) | |
94a86be0 BK |
137 | * static_cast<_CR>(_CF::num) |
138 | / static_cast<_CR>(_CF::den))); | |
139 | } | |
15e38d0d CF |
140 | }; |
141 | ||
16684e9c BK |
142 | template<typename _ToDur, typename _CF, typename _CR> |
143 | struct __duration_cast_impl<_ToDur, _CF, _CR, true, true> | |
15e38d0d | 144 | { |
94a86be0 | 145 | template<typename _Rep, typename _Period> |
16684e9c | 146 | static constexpr _ToDur |
94a86be0 BK |
147 | __cast(const duration<_Rep, _Period>& __d) |
148 | { | |
16684e9c BK |
149 | typedef typename _ToDur::rep __to_rep; |
150 | return _ToDur(static_cast<__to_rep>(__d.count())); | |
94a86be0 | 151 | } |
15e38d0d CF |
152 | }; |
153 | ||
16684e9c BK |
154 | template<typename _ToDur, typename _CF, typename _CR> |
155 | struct __duration_cast_impl<_ToDur, _CF, _CR, true, false> | |
15e38d0d | 156 | { |
94a86be0 | 157 | template<typename _Rep, typename _Period> |
16684e9c | 158 | static constexpr _ToDur |
94a86be0 BK |
159 | __cast(const duration<_Rep, _Period>& __d) |
160 | { | |
16684e9c BK |
161 | typedef typename _ToDur::rep __to_rep; |
162 | return _ToDur(static_cast<__to_rep>( | |
94a86be0 BK |
163 | static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den))); |
164 | } | |
15e38d0d CF |
165 | }; |
166 | ||
16684e9c BK |
167 | template<typename _ToDur, typename _CF, typename _CR> |
168 | struct __duration_cast_impl<_ToDur, _CF, _CR, false, true> | |
15e38d0d | 169 | { |
94a86be0 | 170 | template<typename _Rep, typename _Period> |
16684e9c | 171 | static constexpr _ToDur |
94a86be0 BK |
172 | __cast(const duration<_Rep, _Period>& __d) |
173 | { | |
16684e9c BK |
174 | typedef typename _ToDur::rep __to_rep; |
175 | return _ToDur(static_cast<__to_rep>( | |
94a86be0 BK |
176 | static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num))); |
177 | } | |
15e38d0d CF |
178 | }; |
179 | ||
cc1e2504 PC |
180 | template<typename _Tp> |
181 | struct __is_duration | |
182 | : std::false_type | |
183 | { }; | |
184 | ||
185 | template<typename _Rep, typename _Period> | |
186 | struct __is_duration<duration<_Rep, _Period>> | |
187 | : std::true_type | |
188 | { }; | |
189 | ||
5b9daa7e | 190 | /// duration_cast |
16684e9c | 191 | template<typename _ToDur, typename _Rep, typename _Period> |
a4eeb822 PC |
192 | constexpr typename enable_if<__is_duration<_ToDur>::value, |
193 | _ToDur>::type | |
15e38d0d CF |
194 | duration_cast(const duration<_Rep, _Period>& __d) |
195 | { | |
16684e9c BK |
196 | typedef typename _ToDur::period __to_period; |
197 | typedef typename _ToDur::rep __to_rep; | |
c128d203 | 198 | typedef ratio_divide<_Period, __to_period> __cf; |
16684e9c BK |
199 | typedef typename common_type<__to_rep, _Rep, intmax_t>::type |
200 | __cr; | |
201 | typedef __duration_cast_impl<_ToDur, __cf, __cr, | |
202 | __cf::num == 1, __cf::den == 1> __dc; | |
203 | return __dc::__cast(__d); | |
15e38d0d CF |
204 | } |
205 | ||
5b9daa7e | 206 | /// treat_as_floating_point |
15e38d0d | 207 | template<typename _Rep> |
513c5a5b | 208 | struct treat_as_floating_point |
15e38d0d CF |
209 | : is_floating_point<_Rep> |
210 | { }; | |
211 | ||
5b9daa7e | 212 | /// duration_values |
15e38d0d CF |
213 | template<typename _Rep> |
214 | struct duration_values | |
215 | { | |
94a86be0 BK |
216 | static constexpr _Rep |
217 | zero() | |
218 | { return _Rep(0); } | |
219 | ||
220 | static constexpr _Rep | |
221 | max() | |
222 | { return numeric_limits<_Rep>::max(); } | |
223 | ||
224 | static constexpr _Rep | |
225 | min() | |
545dc5e3 | 226 | { return numeric_limits<_Rep>::lowest(); } |
15e38d0d CF |
227 | }; |
228 | ||
7ad68967 | 229 | template<typename _Tp> |
9d4e8554 CF |
230 | struct __is_ratio |
231 | : std::false_type | |
232 | { }; | |
233 | ||
234 | template<intmax_t _Num, intmax_t _Den> | |
235 | struct __is_ratio<ratio<_Num, _Den>> | |
236 | : std::true_type | |
237 | { }; | |
238 | ||
ad68e9fc | 239 | /// duration |
15e38d0d CF |
240 | template<typename _Rep, typename _Period> |
241 | struct duration | |
242 | { | |
12ffa228 | 243 | typedef _Rep rep; |
16684e9c | 244 | typedef _Period period; |
94a86be0 | 245 | |
9d4e8554 | 246 | static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration"); |
94a86be0 | 247 | static_assert(__is_ratio<_Period>::value, |
9d4e8554 | 248 | "period must be a specialization of ratio"); |
94a86be0 BK |
249 | static_assert(_Period::num > 0, "period must be positive"); |
250 | ||
9ab48d6e | 251 | // 20.11.5.1 construction / copy / destroy |
d069a690 | 252 | constexpr duration() = default; |
94a86be0 | 253 | |
a9ba8ba5 BK |
254 | // NB: Make constexpr implicit. This cannot be explicitly |
255 | // constexpr, as any UDT that is not a literal type with a | |
256 | // constexpr copy constructor will be ill-formed. | |
257 | duration(const duration&) = default; | |
6f0a9757 | 258 | |
94a86be0 | 259 | template<typename _Rep2, typename = typename |
cc1e2504 PC |
260 | enable_if<is_convertible<_Rep2, rep>::value |
261 | && (treat_as_floating_point<rep>::value | |
262 | || !treat_as_floating_point<_Rep2>::value)>::type> | |
94a86be0 | 263 | constexpr explicit duration(const _Rep2& __rep) |
85db9dcc | 264 | : __r(static_cast<rep>(__rep)) { } |
15e38d0d | 265 | |
94a86be0 BK |
266 | template<typename _Rep2, typename _Period2, typename = typename |
267 | enable_if<treat_as_floating_point<rep>::value | |
c128d203 | 268 | || (ratio_divide<_Period2, period>::den == 1 |
cc1e2504 | 269 | && !treat_as_floating_point<_Rep2>::value)>::type> |
94a86be0 BK |
270 | constexpr duration(const duration<_Rep2, _Period2>& __d) |
271 | : __r(duration_cast<duration>(__d).count()) { } | |
15e38d0d | 272 | |
9d4e8554 | 273 | ~duration() = default; |
9d4e8554 CF |
274 | duration& operator=(const duration&) = default; |
275 | ||
9ab48d6e | 276 | // 20.11.5.2 observer |
94a86be0 BK |
277 | constexpr rep |
278 | count() const | |
279 | { return __r; } | |
280 | ||
9ab48d6e | 281 | // 20.11.5.3 arithmetic |
94a86be0 BK |
282 | constexpr duration |
283 | operator+() const | |
284 | { return *this; } | |
285 | ||
286 | constexpr duration | |
287 | operator-() const | |
288 | { return duration(-__r); } | |
289 | ||
290 | duration& | |
291 | operator++() | |
292 | { | |
293 | ++__r; | |
294 | return *this; | |
295 | } | |
296 | ||
297 | duration | |
298 | operator++(int) | |
299 | { return duration(__r++); } | |
300 | ||
301 | duration& | |
302 | operator--() | |
303 | { | |
304 | --__r; | |
305 | return *this; | |
306 | } | |
307 | ||
308 | duration | |
309 | operator--(int) | |
310 | { return duration(__r--); } | |
311 | ||
312 | duration& | |
313 | operator+=(const duration& __d) | |
314 | { | |
315 | __r += __d.count(); | |
316 | return *this; | |
317 | } | |
318 | ||
319 | duration& | |
320 | operator-=(const duration& __d) | |
321 | { | |
322 | __r -= __d.count(); | |
323 | return *this; | |
324 | } | |
325 | ||
326 | duration& | |
327 | operator*=(const rep& __rhs) | |
328 | { | |
329 | __r *= __rhs; | |
330 | return *this; | |
331 | } | |
332 | ||
333 | duration& | |
334 | operator/=(const rep& __rhs) | |
335 | { | |
336 | __r /= __rhs; | |
337 | return *this; | |
338 | } | |
15e38d0d | 339 | |
513c5a5b PC |
340 | // DR 934. |
341 | template<typename _Rep2 = rep> | |
342 | typename enable_if<!treat_as_floating_point<_Rep2>::value, | |
343 | duration&>::type | |
344 | operator%=(const rep& __rhs) | |
345 | { | |
346 | __r %= __rhs; | |
347 | return *this; | |
348 | } | |
349 | ||
350 | template<typename _Rep2 = rep> | |
351 | typename enable_if<!treat_as_floating_point<_Rep2>::value, | |
352 | duration&>::type | |
353 | operator%=(const duration& __d) | |
354 | { | |
355 | __r %= __d.count(); | |
356 | return *this; | |
357 | } | |
358 | ||
9ab48d6e | 359 | // 20.11.5.4 special values |
94a86be0 BK |
360 | static constexpr duration |
361 | zero() | |
362 | { return duration(duration_values<rep>::zero()); } | |
363 | ||
364 | static constexpr duration | |
365 | min() | |
366 | { return duration(duration_values<rep>::min()); } | |
15e38d0d | 367 | |
94a86be0 BK |
368 | static constexpr duration |
369 | max() | |
370 | { return duration(duration_values<rep>::max()); } | |
513c5a5b PC |
371 | |
372 | private: | |
94a86be0 | 373 | rep __r; |
15e38d0d CF |
374 | }; |
375 | ||
376 | template<typename _Rep1, typename _Period1, | |
94a86be0 | 377 | typename _Rep2, typename _Period2> |
a4eeb822 PC |
378 | constexpr typename common_type<duration<_Rep1, _Period1>, |
379 | duration<_Rep2, _Period2>>::type | |
94a86be0 BK |
380 | operator+(const duration<_Rep1, _Period1>& __lhs, |
381 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d | 382 | { |
16684e9c BK |
383 | typedef duration<_Rep1, _Period1> __dur1; |
384 | typedef duration<_Rep2, _Period2> __dur2; | |
b850be9d PC |
385 | typedef typename common_type<__dur1,__dur2>::type __cd; |
386 | return __cd(__cd(__lhs).count() + __cd(__rhs).count()); | |
15e38d0d CF |
387 | } |
388 | ||
94a86be0 BK |
389 | template<typename _Rep1, typename _Period1, |
390 | typename _Rep2, typename _Period2> | |
a4eeb822 PC |
391 | constexpr typename common_type<duration<_Rep1, _Period1>, |
392 | duration<_Rep2, _Period2>>::type | |
94a86be0 BK |
393 | operator-(const duration<_Rep1, _Period1>& __lhs, |
394 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d | 395 | { |
16684e9c BK |
396 | typedef duration<_Rep1, _Period1> __dur1; |
397 | typedef duration<_Rep2, _Period2> __dur2; | |
b850be9d PC |
398 | typedef typename common_type<__dur1,__dur2>::type __cd; |
399 | return __cd(__cd(__lhs).count() - __cd(__rhs).count()); | |
15e38d0d CF |
400 | } |
401 | ||
cc1e2504 PC |
402 | template<typename _Rep1, typename _Rep2, bool = |
403 | is_convertible<_Rep2, | |
404 | typename common_type<_Rep1, _Rep2>::type>::value> | |
405 | struct __common_rep_type { }; | |
406 | ||
407 | template<typename _Rep1, typename _Rep2> | |
408 | struct __common_rep_type<_Rep1, _Rep2, true> | |
94a86be0 | 409 | { typedef typename common_type<_Rep1, _Rep2>::type type; }; |
cc1e2504 | 410 | |
15e38d0d | 411 | template<typename _Rep1, typename _Period, typename _Rep2> |
a4eeb822 | 412 | constexpr |
b850be9d | 413 | duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period> |
15e38d0d CF |
414 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
415 | { | |
b850be9d PC |
416 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
417 | __cd; | |
418 | return __cd(__cd(__d).count() * __s); | |
15e38d0d CF |
419 | } |
420 | ||
dbb45bf9 | 421 | template<typename _Rep1, typename _Rep2, typename _Period> |
a4eeb822 | 422 | constexpr |
b850be9d | 423 | duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period> |
cc1e2504 | 424 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) |
15e38d0d | 425 | { return __d * __s; } |
94a86be0 | 426 | |
15e38d0d | 427 | template<typename _Rep1, typename _Period, typename _Rep2> |
a4eeb822 | 428 | constexpr duration<typename __common_rep_type<_Rep1, typename |
513c5a5b PC |
429 | enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period> |
430 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) | |
15e38d0d | 431 | { |
b850be9d PC |
432 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
433 | __cd; | |
434 | return __cd(__cd(__d).count() / __s); | |
513c5a5b | 435 | } |
15e38d0d | 436 | |
a4eeb822 PC |
437 | template<typename _Rep1, typename _Period1, |
438 | typename _Rep2, typename _Period2> | |
439 | constexpr typename common_type<_Rep1, _Rep2>::type | |
94a86be0 BK |
440 | operator/(const duration<_Rep1, _Period1>& __lhs, |
441 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d | 442 | { |
16684e9c BK |
443 | typedef duration<_Rep1, _Period1> __dur1; |
444 | typedef duration<_Rep2, _Period2> __dur2; | |
b850be9d PC |
445 | typedef typename common_type<__dur1,__dur2>::type __cd; |
446 | return __cd(__lhs).count() / __cd(__rhs).count(); | |
513c5a5b | 447 | } |
15e38d0d | 448 | |
513c5a5b PC |
449 | // DR 934. |
450 | template<typename _Rep1, typename _Period, typename _Rep2> | |
a4eeb822 | 451 | constexpr duration<typename __common_rep_type<_Rep1, typename |
513c5a5b PC |
452 | enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period> |
453 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) | |
15e38d0d | 454 | { |
b850be9d PC |
455 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
456 | __cd; | |
457 | return __cd(__cd(__d).count() % __s); | |
15e38d0d | 458 | } |
513c5a5b | 459 | |
a4eeb822 PC |
460 | template<typename _Rep1, typename _Period1, |
461 | typename _Rep2, typename _Period2> | |
462 | constexpr typename common_type<duration<_Rep1, _Period1>, | |
463 | duration<_Rep2, _Period2>>::type | |
94a86be0 BK |
464 | operator%(const duration<_Rep1, _Period1>& __lhs, |
465 | const duration<_Rep2, _Period2>& __rhs) | |
513c5a5b | 466 | { |
16684e9c BK |
467 | typedef duration<_Rep1, _Period1> __dur1; |
468 | typedef duration<_Rep2, _Period2> __dur2; | |
b850be9d PC |
469 | typedef typename common_type<__dur1,__dur2>::type __cd; |
470 | return __cd(__cd(__lhs).count() % __cd(__rhs).count()); | |
513c5a5b PC |
471 | } |
472 | ||
15e38d0d CF |
473 | // comparisons |
474 | template<typename _Rep1, typename _Period1, | |
94a86be0 | 475 | typename _Rep2, typename _Period2> |
a4eeb822 | 476 | constexpr bool |
94a86be0 BK |
477 | operator==(const duration<_Rep1, _Period1>& __lhs, |
478 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d | 479 | { |
16684e9c BK |
480 | typedef duration<_Rep1, _Period1> __dur1; |
481 | typedef duration<_Rep2, _Period2> __dur2; | |
482 | typedef typename common_type<__dur1,__dur2>::type __ct; | |
94a86be0 | 483 | return __ct(__lhs).count() == __ct(__rhs).count(); |
15e38d0d CF |
484 | } |
485 | ||
486 | template<typename _Rep1, typename _Period1, | |
94a86be0 | 487 | typename _Rep2, typename _Period2> |
a4eeb822 | 488 | constexpr bool |
94a86be0 BK |
489 | operator<(const duration<_Rep1, _Period1>& __lhs, |
490 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d | 491 | { |
16684e9c BK |
492 | typedef duration<_Rep1, _Period1> __dur1; |
493 | typedef duration<_Rep2, _Period2> __dur2; | |
494 | typedef typename common_type<__dur1,__dur2>::type __ct; | |
94a86be0 | 495 | return __ct(__lhs).count() < __ct(__rhs).count(); |
15e38d0d CF |
496 | } |
497 | ||
498 | template<typename _Rep1, typename _Period1, | |
94a86be0 | 499 | typename _Rep2, typename _Period2> |
a4eeb822 | 500 | constexpr bool |
94a86be0 BK |
501 | operator!=(const duration<_Rep1, _Period1>& __lhs, |
502 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d CF |
503 | { return !(__lhs == __rhs); } |
504 | ||
505 | template<typename _Rep1, typename _Period1, | |
94a86be0 | 506 | typename _Rep2, typename _Period2> |
a4eeb822 | 507 | constexpr bool |
94a86be0 BK |
508 | operator<=(const duration<_Rep1, _Period1>& __lhs, |
509 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d CF |
510 | { return !(__rhs < __lhs); } |
511 | ||
512 | template<typename _Rep1, typename _Period1, | |
94a86be0 | 513 | typename _Rep2, typename _Period2> |
a4eeb822 | 514 | constexpr bool |
94a86be0 BK |
515 | operator>(const duration<_Rep1, _Period1>& __lhs, |
516 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d CF |
517 | { return __rhs < __lhs; } |
518 | ||
94a86be0 BK |
519 | template<typename _Rep1, typename _Period1, |
520 | typename _Rep2, typename _Period2> | |
a4eeb822 | 521 | constexpr bool |
94a86be0 BK |
522 | operator>=(const duration<_Rep1, _Period1>& __lhs, |
523 | const duration<_Rep2, _Period2>& __rhs) | |
15e38d0d CF |
524 | { return !(__lhs < __rhs); } |
525 | ||
5b9daa7e | 526 | /// nanoseconds |
94a86be0 | 527 | typedef duration<int64_t, nano> nanoseconds; |
5b9daa7e BK |
528 | |
529 | /// microseconds | |
94a86be0 | 530 | typedef duration<int64_t, micro> microseconds; |
5b9daa7e BK |
531 | |
532 | /// milliseconds | |
94a86be0 BK |
533 | typedef duration<int64_t, milli> milliseconds; |
534 | ||
5b9daa7e | 535 | /// seconds |
94a86be0 | 536 | typedef duration<int64_t> seconds; |
5b9daa7e BK |
537 | |
538 | /// minutes | |
94a86be0 | 539 | typedef duration<int, ratio< 60>> minutes; |
5b9daa7e BK |
540 | |
541 | /// hours | |
94a86be0 | 542 | typedef duration<int, ratio<3600>> hours; |
15e38d0d | 543 | |
ad68e9fc | 544 | /// time_point |
16684e9c | 545 | template<typename _Clock, typename _Dur> |
ad68e9fc | 546 | struct time_point |
15e38d0d | 547 | { |
16684e9c BK |
548 | typedef _Clock clock; |
549 | typedef _Dur duration; | |
550 | typedef typename duration::rep rep; | |
551 | typedef typename duration::period period; | |
ad68e9fc | 552 | |
94a86be0 | 553 | constexpr time_point() : __d(duration::zero()) |
ad68e9fc BK |
554 | { } |
555 | ||
94a86be0 BK |
556 | constexpr explicit time_point(const duration& __dur) |
557 | : __d(__dur) | |
ad68e9fc BK |
558 | { } |
559 | ||
560 | // conversions | |
16684e9c BK |
561 | template<typename _Dur2> |
562 | constexpr time_point(const time_point<clock, _Dur2>& __t) | |
ad68e9fc BK |
563 | : __d(__t.time_since_epoch()) |
564 | { } | |
565 | ||
566 | // observer | |
94a86be0 | 567 | constexpr duration |
ad68e9fc BK |
568 | time_since_epoch() const |
569 | { return __d; } | |
94a86be0 | 570 | |
ad68e9fc BK |
571 | // arithmetic |
572 | time_point& | |
573 | operator+=(const duration& __dur) | |
574 | { | |
575 | __d += __dur; | |
576 | return *this; | |
577 | } | |
94a86be0 | 578 | |
ad68e9fc BK |
579 | time_point& |
580 | operator-=(const duration& __dur) | |
581 | { | |
582 | __d -= __dur; | |
583 | return *this; | |
584 | } | |
94a86be0 | 585 | |
ad68e9fc | 586 | // special values |
94a86be0 | 587 | static constexpr time_point |
ad68e9fc BK |
588 | min() |
589 | { return time_point(duration::min()); } | |
94a86be0 BK |
590 | |
591 | static constexpr time_point | |
ad68e9fc BK |
592 | max() |
593 | { return time_point(duration::max()); } | |
94a86be0 | 594 | |
ad68e9fc BK |
595 | private: |
596 | duration __d; | |
597 | }; | |
94a86be0 | 598 | |
5b9daa7e | 599 | /// time_point_cast |
16684e9c | 600 | template<typename _ToDur, typename _Clock, typename _Dur> |
a4eeb822 PC |
601 | constexpr typename enable_if<__is_duration<_ToDur>::value, |
602 | time_point<_Clock, _ToDur>>::type | |
16684e9c | 603 | time_point_cast(const time_point<_Clock, _Dur>& __t) |
15e38d0d | 604 | { |
16684e9c BK |
605 | typedef time_point<_Clock, _ToDur> __time_point; |
606 | return __time_point(duration_cast<_ToDur>(__t.time_since_epoch())); | |
15e38d0d CF |
607 | } |
608 | ||
16684e9c | 609 | template<typename _Clock, typename _Dur1, |
94a86be0 | 610 | typename _Rep2, typename _Period2> |
a4eeb822 | 611 | constexpr time_point<_Clock, |
16684e9c BK |
612 | typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> |
613 | operator+(const time_point<_Clock, _Dur1>& __lhs, | |
94a86be0 | 614 | const duration<_Rep2, _Period2>& __rhs) |
15e38d0d | 615 | { |
16684e9c BK |
616 | typedef duration<_Rep2, _Period2> __dur2; |
617 | typedef typename common_type<_Dur1,__dur2>::type __ct; | |
618 | typedef time_point<_Clock, __ct> __time_point; | |
1b97ec17 | 619 | return __time_point(__lhs.time_since_epoch() + __rhs); |
15e38d0d CF |
620 | } |
621 | ||
622 | template<typename _Rep1, typename _Period1, | |
16684e9c | 623 | typename _Clock, typename _Dur2> |
a4eeb822 | 624 | constexpr time_point<_Clock, |
16684e9c | 625 | typename common_type<duration<_Rep1, _Period1>, _Dur2>::type> |
94a86be0 | 626 | operator+(const duration<_Rep1, _Period1>& __lhs, |
16684e9c | 627 | const time_point<_Clock, _Dur2>& __rhs) |
1b97ec17 BK |
628 | { |
629 | typedef duration<_Rep1, _Period1> __dur1; | |
630 | typedef typename common_type<__dur1,_Dur2>::type __ct; | |
631 | typedef time_point<_Clock, __ct> __time_point; | |
632 | return __time_point(__rhs.time_since_epoch() + __lhs); | |
633 | } | |
15e38d0d | 634 | |
16684e9c | 635 | template<typename _Clock, typename _Dur1, |
94a86be0 | 636 | typename _Rep2, typename _Period2> |
a4eeb822 | 637 | constexpr time_point<_Clock, |
16684e9c BK |
638 | typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> |
639 | operator-(const time_point<_Clock, _Dur1>& __lhs, | |
94a86be0 | 640 | const duration<_Rep2, _Period2>& __rhs) |
1b97ec17 BK |
641 | { |
642 | typedef duration<_Rep2, _Period2> __dur2; | |
643 | typedef typename common_type<_Dur1,__dur2>::type __ct; | |
644 | typedef time_point<_Clock, __ct> __time_point; | |
645 | return __time_point(__lhs.time_since_epoch() -__rhs); | |
646 | } | |
15e38d0d | 647 | |
16684e9c | 648 | template<typename _Clock, typename _Dur1, typename _Dur2> |
a4eeb822 | 649 | constexpr typename common_type<_Dur1, _Dur2>::type |
16684e9c BK |
650 | operator-(const time_point<_Clock, _Dur1>& __lhs, |
651 | const time_point<_Clock, _Dur2>& __rhs) | |
15e38d0d CF |
652 | { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); } |
653 | ||
16684e9c | 654 | template<typename _Clock, typename _Dur1, typename _Dur2> |
a4eeb822 | 655 | constexpr bool |
16684e9c BK |
656 | operator==(const time_point<_Clock, _Dur1>& __lhs, |
657 | const time_point<_Clock, _Dur2>& __rhs) | |
15e38d0d CF |
658 | { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); } |
659 | ||
16684e9c | 660 | template<typename _Clock, typename _Dur1, typename _Dur2> |
a4eeb822 | 661 | constexpr bool |
16684e9c BK |
662 | operator!=(const time_point<_Clock, _Dur1>& __lhs, |
663 | const time_point<_Clock, _Dur2>& __rhs) | |
15e38d0d CF |
664 | { return !(__lhs == __rhs); } |
665 | ||
16684e9c | 666 | template<typename _Clock, typename _Dur1, typename _Dur2> |
a4eeb822 | 667 | constexpr bool |
16684e9c BK |
668 | operator<(const time_point<_Clock, _Dur1>& __lhs, |
669 | const time_point<_Clock, _Dur2>& __rhs) | |
15e38d0d CF |
670 | { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); } |
671 | ||
16684e9c | 672 | template<typename _Clock, typename _Dur1, typename _Dur2> |
a4eeb822 | 673 | constexpr bool |
16684e9c BK |
674 | operator<=(const time_point<_Clock, _Dur1>& __lhs, |
675 | const time_point<_Clock, _Dur2>& __rhs) | |
15e38d0d CF |
676 | { return !(__rhs < __lhs); } |
677 | ||
16684e9c | 678 | template<typename _Clock, typename _Dur1, typename _Dur2> |
a4eeb822 | 679 | constexpr bool |
16684e9c BK |
680 | operator>(const time_point<_Clock, _Dur1>& __lhs, |
681 | const time_point<_Clock, _Dur2>& __rhs) | |
15e38d0d CF |
682 | { return __rhs < __lhs; } |
683 | ||
16684e9c | 684 | template<typename _Clock, typename _Dur1, typename _Dur2> |
a4eeb822 | 685 | constexpr bool |
16684e9c BK |
686 | operator>=(const time_point<_Clock, _Dur1>& __lhs, |
687 | const time_point<_Clock, _Dur2>& __rhs) | |
15e38d0d CF |
688 | { return !(__lhs < __rhs); } |
689 | ||
1f08a749 BK |
690 | |
691 | // Clocks. | |
692 | ||
693 | // Why nanosecond resolution as the default? | |
694 | // Why have std::system_clock always count in the higest | |
695 | // resolution (ie nanoseconds), even if on some OSes the low 3 | |
696 | // or 9 decimal digits will be always zero? This allows later | |
697 | // implementations to change the system_clock::now() | |
698 | // implementation any time to provide better resolution without | |
699 | // changing function signature or units. | |
700 | ||
701 | // To support the (forward) evolution of the library's defined | |
702 | // clocks, wrap inside inline namespace so that the current | |
703 | // defintions of system_clock, steady_clock, and | |
704 | // high_resolution_clock types are uniquely mangled. This way, new | |
705 | // code can use the latests clocks, while the library can contain | |
706 | // compatibility definitions for previous versions. At some | |
707 | // point, when these clocks settle down, the inlined namespaces | |
708 | // can be removed. XXX GLIBCXX_ABI Deprecated | |
709 | inline namespace _V2 { | |
710 | ||
711 | /** | |
712 | * @brief System clock. | |
713 | * | |
714 | * Time returned represents wall time from the system-wide clock. | |
715 | */ | |
716 | struct system_clock | |
15e38d0d | 717 | { |
85db9dcc | 718 | typedef chrono::nanoseconds duration; |
85db9dcc BK |
719 | typedef duration::rep rep; |
720 | typedef duration::period period; | |
721 | typedef chrono::time_point<system_clock, duration> time_point; | |
15e38d0d | 722 | |
94a86be0 BK |
723 | static_assert(system_clock::duration::min() |
724 | < system_clock::duration::zero(), | |
725 | "a clock's minimum duration cannot be less than its epoch"); | |
726 | ||
1b97ec17 | 727 | static constexpr bool is_steady = false; |
15e38d0d CF |
728 | |
729 | static time_point | |
8535715d | 730 | now() noexcept; |
15e38d0d CF |
731 | |
732 | // Map to C API | |
733 | static std::time_t | |
8535715d | 734 | to_time_t(const time_point& __t) noexcept |
15e38d0d | 735 | { |
16684e9c BK |
736 | return std::time_t(duration_cast<chrono::seconds> |
737 | (__t.time_since_epoch()).count()); | |
15e38d0d CF |
738 | } |
739 | ||
740 | static time_point | |
8535715d | 741 | from_time_t(std::time_t __t) noexcept |
94a86be0 | 742 | { |
16684e9c BK |
743 | typedef chrono::time_point<system_clock, seconds> __from; |
744 | return time_point_cast<system_clock::duration> | |
745 | (__from(chrono::seconds(__t))); | |
15e38d0d | 746 | } |
15e38d0d CF |
747 | }; |
748 | ||
1f08a749 BK |
749 | |
750 | /** | |
751 | * @brief Monotonic clock | |
752 | * | |
753 | * Time returned has the property of only increasing at a uniform rate. | |
754 | */ | |
1b97ec17 | 755 | struct steady_clock |
88399079 | 756 | { |
85db9dcc | 757 | typedef chrono::nanoseconds duration; |
12ffa228 BK |
758 | typedef duration::rep rep; |
759 | typedef duration::period period; | |
1b97ec17 | 760 | typedef chrono::time_point<steady_clock, duration> time_point; |
88399079 | 761 | |
1b97ec17 | 762 | static constexpr bool is_steady = true; |
88399079 CF |
763 | |
764 | static time_point | |
8535715d | 765 | now() noexcept; |
88399079 | 766 | }; |
88399079 | 767 | |
1f08a749 BK |
768 | |
769 | /** | |
770 | * @brief Highest-resolution clock | |
771 | * | |
772 | * This is the clock "with the shortest tick period." Alias to | |
773 | * std::system_clock until higher-than-nanosecond definitions | |
774 | * become feasible. | |
775 | */ | |
776 | using high_resolution_clock = system_clock; | |
777 | ||
778 | } // end inline namespace _V2 | |
12ffa228 BK |
779 | |
780 | _GLIBCXX_END_NAMESPACE_VERSION | |
5b9daa7e BK |
781 | } // namespace chrono |
782 | ||
783 | // @} group chrono | |
12ffa228 | 784 | } // namespace |
15e38d0d CF |
785 | |
786 | #endif //_GLIBCXX_USE_C99_STDINT_TR1 | |
787 | ||
734f5023 | 788 | #endif // C++11 |
15e38d0d | 789 | |
1c9f675f ESR |
790 | #if __cplusplus > 201103L |
791 | ||
792 | #ifdef _GLIBCXX_USE_C99_STDINT_TR1 | |
793 | ||
794 | namespace std _GLIBCXX_VISIBILITY(default) | |
795 | { | |
796 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
797 | ||
798 | inline namespace literals { | |
799 | inline namespace chrono_literals { | |
800 | ||
801 | namespace __detail { | |
802 | ||
803 | using namespace __parse_int; | |
804 | ||
805 | template<unsigned long long _Val, typename _Dur> | |
806 | struct _Select_type | |
807 | : conditional< | |
808 | _Val <= static_cast<unsigned long long> | |
809 | (numeric_limits<typename _Dur::rep>::max()), | |
810 | _Dur, void> | |
811 | { | |
812 | static constexpr typename _Select_type::type | |
813 | value{static_cast<typename _Select_type::type>(_Val)}; | |
814 | }; | |
815 | ||
816 | template<unsigned long long _Val, typename _Dur> | |
817 | constexpr typename _Select_type<_Val, _Dur>::type | |
818 | _Select_type<_Val, _Dur>::value; | |
819 | ||
820 | } // __detail | |
821 | ||
fa409833 | 822 | constexpr chrono::duration<long double, ratio<3600,1>> |
1c9f675f ESR |
823 | operator"" h(long double __hours) |
824 | { return chrono::duration<long double, ratio<3600,1>>{__hours}; } | |
825 | ||
826 | template <char... _Digits> | |
fa409833 | 827 | constexpr typename |
1c9f675f ESR |
828 | __detail::_Select_type<__select_int::_Select_int<_Digits...>::value, |
829 | chrono::hours>::type | |
830 | operator"" h() | |
831 | { | |
832 | return __detail::_Select_type< | |
833 | __select_int::_Select_int<_Digits...>::value, | |
834 | chrono::hours>::value; | |
835 | } | |
836 | ||
fa409833 | 837 | constexpr chrono::duration<long double, ratio<60,1>> |
1c9f675f ESR |
838 | operator"" min(long double __mins) |
839 | { return chrono::duration<long double, ratio<60,1>>{__mins}; } | |
840 | ||
841 | template <char... _Digits> | |
fa409833 | 842 | constexpr typename |
1c9f675f ESR |
843 | __detail::_Select_type<__select_int::_Select_int<_Digits...>::value, |
844 | chrono::minutes>::type | |
845 | operator"" min() | |
846 | { | |
847 | return __detail::_Select_type< | |
848 | __select_int::_Select_int<_Digits...>::value, | |
849 | chrono::minutes>::value; | |
850 | } | |
851 | ||
fa409833 | 852 | constexpr chrono::duration<long double> |
1c9f675f ESR |
853 | operator"" s(long double __secs) |
854 | { return chrono::duration<long double>{__secs}; } | |
855 | ||
856 | template <char... _Digits> | |
fa409833 | 857 | constexpr typename |
1c9f675f ESR |
858 | __detail::_Select_type<__select_int::_Select_int<_Digits...>::value, |
859 | chrono::seconds>::type | |
860 | operator"" s() | |
861 | { | |
862 | return __detail::_Select_type< | |
863 | __select_int::_Select_int<_Digits...>::value, | |
864 | chrono::seconds>::value; | |
865 | } | |
866 | ||
fa409833 | 867 | constexpr chrono::duration<long double, milli> |
1c9f675f ESR |
868 | operator"" ms(long double __msecs) |
869 | { return chrono::duration<long double, milli>{__msecs}; } | |
870 | ||
871 | template <char... _Digits> | |
fa409833 | 872 | constexpr typename |
1c9f675f ESR |
873 | __detail::_Select_type<__select_int::_Select_int<_Digits...>::value, |
874 | chrono::milliseconds>::type | |
875 | operator"" ms() | |
876 | { | |
877 | return __detail::_Select_type< | |
878 | __select_int::_Select_int<_Digits...>::value, | |
879 | chrono::milliseconds>::value; | |
880 | } | |
881 | ||
fa409833 | 882 | constexpr chrono::duration<long double, micro> |
1c9f675f ESR |
883 | operator"" us(long double __usecs) |
884 | { return chrono::duration<long double, micro>{__usecs}; } | |
885 | ||
886 | template <char... _Digits> | |
fa409833 | 887 | constexpr typename |
1c9f675f ESR |
888 | __detail::_Select_type<__select_int::_Select_int<_Digits...>::value, |
889 | chrono::microseconds>::type | |
890 | operator"" us() | |
891 | { | |
892 | return __detail::_Select_type< | |
893 | __select_int::_Select_int<_Digits...>::value, | |
894 | chrono::microseconds>::value; | |
895 | } | |
896 | ||
fa409833 | 897 | constexpr chrono::duration<long double, nano> |
1c9f675f ESR |
898 | operator"" ns(long double __nsecs) |
899 | { return chrono::duration<long double, nano>{__nsecs}; } | |
900 | ||
901 | template <char... _Digits> | |
fa409833 | 902 | constexpr typename |
1c9f675f ESR |
903 | __detail::_Select_type<__select_int::_Select_int<_Digits...>::value, |
904 | chrono::nanoseconds>::type | |
905 | operator"" ns() | |
906 | { | |
907 | return __detail::_Select_type< | |
908 | __select_int::_Select_int<_Digits...>::value, | |
909 | chrono::nanoseconds>::value; | |
910 | } | |
911 | ||
912 | } // inline namespace chrono_literals | |
913 | } // inline namespace literals | |
914 | ||
915 | _GLIBCXX_END_NAMESPACE_VERSION | |
916 | } // namespace std | |
917 | ||
918 | #endif //_GLIBCXX_USE_C99_STDINT_TR1 | |
919 | ||
920 | #endif // __cplusplus > 201103L | |
921 | ||
15e38d0d | 922 | #endif //_GLIBCXX_CHRONO |