libstdc++
|
00001 // <chrono> -*- C++ -*- 00002 00003 // Copyright (C) 2008-2019 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/chrono 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _GLIBCXX_CHRONO 00030 #define _GLIBCXX_CHRONO 1 00031 00032 #pragma GCC system_header 00033 00034 #if __cplusplus < 201103L 00035 # include <bits/c++0x_warning.h> 00036 #else 00037 00038 #include <ratio> 00039 #include <type_traits> 00040 #include <limits> 00041 #include <ctime> 00042 #include <bits/parse_numbers.h> // for literals support. 00043 00044 namespace std _GLIBCXX_VISIBILITY(default) 00045 { 00046 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00047 00048 /** 00049 * @defgroup chrono Time 00050 * @ingroup utilities 00051 * 00052 * Classes and functions for time. 00053 * @{ 00054 */ 00055 00056 /** @namespace std::chrono 00057 * @brief ISO C++ 2011 entities sub-namespace for time and date. 00058 */ 00059 namespace chrono 00060 { 00061 template<typename _Rep, typename _Period = ratio<1>> 00062 struct duration; 00063 00064 template<typename _Clock, typename _Dur = typename _Clock::duration> 00065 struct time_point; 00066 } 00067 00068 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly) 00069 00070 template<typename _CT, typename _Period1, typename _Period2> 00071 struct __duration_common_type_wrapper 00072 { 00073 private: 00074 typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num; 00075 typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den; 00076 typedef typename _CT::type __cr; 00077 typedef ratio<__gcd_num::value, 00078 (_Period1::den / __gcd_den::value) * _Period2::den> __r; 00079 public: 00080 typedef __success_type<chrono::duration<__cr, __r>> type; 00081 }; 00082 00083 template<typename _Period1, typename _Period2> 00084 struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2> 00085 { typedef __failure_type type; }; 00086 00087 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> 00088 struct common_type<chrono::duration<_Rep1, _Period1>, 00089 chrono::duration<_Rep2, _Period2>> 00090 : public __duration_common_type_wrapper<typename __member_type_wrapper< 00091 common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type 00092 { }; 00093 00094 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly) 00095 00096 template<typename _CT, typename _Clock> 00097 struct __timepoint_common_type_wrapper 00098 { 00099 typedef __success_type<chrono::time_point<_Clock, typename _CT::type>> 00100 type; 00101 }; 00102 00103 template<typename _Clock> 00104 struct __timepoint_common_type_wrapper<__failure_type, _Clock> 00105 { typedef __failure_type type; }; 00106 00107 template<typename _Clock, typename _Duration1, typename _Duration2> 00108 struct common_type<chrono::time_point<_Clock, _Duration1>, 00109 chrono::time_point<_Clock, _Duration2>> 00110 : public __timepoint_common_type_wrapper<typename __member_type_wrapper< 00111 common_type<_Duration1, _Duration2>>::type, _Clock>::type 00112 { }; 00113 00114 namespace chrono 00115 { 00116 // Primary template for duration_cast impl. 00117 template<typename _ToDur, typename _CF, typename _CR, 00118 bool _NumIsOne = false, bool _DenIsOne = false> 00119 struct __duration_cast_impl 00120 { 00121 template<typename _Rep, typename _Period> 00122 static constexpr _ToDur 00123 __cast(const duration<_Rep, _Period>& __d) 00124 { 00125 typedef typename _ToDur::rep __to_rep; 00126 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count()) 00127 * static_cast<_CR>(_CF::num) 00128 / static_cast<_CR>(_CF::den))); 00129 } 00130 }; 00131 00132 template<typename _ToDur, typename _CF, typename _CR> 00133 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true> 00134 { 00135 template<typename _Rep, typename _Period> 00136 static constexpr _ToDur 00137 __cast(const duration<_Rep, _Period>& __d) 00138 { 00139 typedef typename _ToDur::rep __to_rep; 00140 return _ToDur(static_cast<__to_rep>(__d.count())); 00141 } 00142 }; 00143 00144 template<typename _ToDur, typename _CF, typename _CR> 00145 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false> 00146 { 00147 template<typename _Rep, typename _Period> 00148 static constexpr _ToDur 00149 __cast(const duration<_Rep, _Period>& __d) 00150 { 00151 typedef typename _ToDur::rep __to_rep; 00152 return _ToDur(static_cast<__to_rep>( 00153 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den))); 00154 } 00155 }; 00156 00157 template<typename _ToDur, typename _CF, typename _CR> 00158 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true> 00159 { 00160 template<typename _Rep, typename _Period> 00161 static constexpr _ToDur 00162 __cast(const duration<_Rep, _Period>& __d) 00163 { 00164 typedef typename _ToDur::rep __to_rep; 00165 return _ToDur(static_cast<__to_rep>( 00166 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num))); 00167 } 00168 }; 00169 00170 template<typename _Tp> 00171 struct __is_duration 00172 : std::false_type 00173 { }; 00174 00175 template<typename _Rep, typename _Period> 00176 struct __is_duration<duration<_Rep, _Period>> 00177 : std::true_type 00178 { }; 00179 00180 template<typename _Tp> 00181 using __enable_if_is_duration 00182 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type; 00183 00184 template<typename _Tp> 00185 using __disable_if_is_duration 00186 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type; 00187 00188 /// duration_cast 00189 template<typename _ToDur, typename _Rep, typename _Period> 00190 constexpr __enable_if_is_duration<_ToDur> 00191 duration_cast(const duration<_Rep, _Period>& __d) 00192 { 00193 typedef typename _ToDur::period __to_period; 00194 typedef typename _ToDur::rep __to_rep; 00195 typedef ratio_divide<_Period, __to_period> __cf; 00196 typedef typename common_type<__to_rep, _Rep, intmax_t>::type 00197 __cr; 00198 typedef __duration_cast_impl<_ToDur, __cf, __cr, 00199 __cf::num == 1, __cf::den == 1> __dc; 00200 return __dc::__cast(__d); 00201 } 00202 00203 /// treat_as_floating_point 00204 template<typename _Rep> 00205 struct treat_as_floating_point 00206 : is_floating_point<_Rep> 00207 { }; 00208 00209 #if __cplusplus > 201402L 00210 template <typename _Rep> 00211 inline constexpr bool treat_as_floating_point_v = 00212 treat_as_floating_point<_Rep>::value; 00213 #endif // C++17 00214 00215 #if __cplusplus >= 201703L 00216 # define __cpp_lib_chrono 201611 00217 00218 template<typename _ToDur, typename _Rep, typename _Period> 00219 constexpr __enable_if_is_duration<_ToDur> 00220 floor(const duration<_Rep, _Period>& __d) 00221 { 00222 auto __to = chrono::duration_cast<_ToDur>(__d); 00223 if (__to > __d) 00224 return __to - _ToDur{1}; 00225 return __to; 00226 } 00227 00228 template<typename _ToDur, typename _Rep, typename _Period> 00229 constexpr __enable_if_is_duration<_ToDur> 00230 ceil(const duration<_Rep, _Period>& __d) 00231 { 00232 auto __to = chrono::duration_cast<_ToDur>(__d); 00233 if (__to < __d) 00234 return __to + _ToDur{1}; 00235 return __to; 00236 } 00237 00238 template <typename _ToDur, typename _Rep, typename _Period> 00239 constexpr enable_if_t< 00240 __and_<__is_duration<_ToDur>, 00241 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value, 00242 _ToDur> 00243 round(const duration<_Rep, _Period>& __d) 00244 { 00245 _ToDur __t0 = chrono::floor<_ToDur>(__d); 00246 _ToDur __t1 = __t0 + _ToDur{1}; 00247 auto __diff0 = __d - __t0; 00248 auto __diff1 = __t1 - __d; 00249 if (__diff0 == __diff1) 00250 { 00251 if (__t0.count() & 1) 00252 return __t1; 00253 return __t0; 00254 } 00255 else if (__diff0 < __diff1) 00256 return __t0; 00257 return __t1; 00258 } 00259 00260 template<typename _Rep, typename _Period> 00261 constexpr 00262 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>> 00263 abs(duration<_Rep, _Period> __d) 00264 { 00265 if (__d >= __d.zero()) 00266 return __d; 00267 return -__d; 00268 } 00269 #endif // C++17 00270 00271 /// duration_values 00272 template<typename _Rep> 00273 struct duration_values 00274 { 00275 static constexpr _Rep 00276 zero() noexcept 00277 { return _Rep(0); } 00278 00279 static constexpr _Rep 00280 max() noexcept 00281 { return numeric_limits<_Rep>::max(); } 00282 00283 static constexpr _Rep 00284 min() noexcept 00285 { return numeric_limits<_Rep>::lowest(); } 00286 }; 00287 00288 template<typename _Tp> 00289 struct __is_ratio 00290 : std::false_type 00291 { }; 00292 00293 template<intmax_t _Num, intmax_t _Den> 00294 struct __is_ratio<ratio<_Num, _Den>> 00295 : std::true_type 00296 { }; 00297 00298 /// duration 00299 template<typename _Rep, typename _Period> 00300 struct duration 00301 { 00302 private: 00303 template<typename _Rep2> 00304 using __is_float = treat_as_floating_point<_Rep2>; 00305 00306 // _Period2 is an exact multiple of _Period 00307 template<typename _Period2> 00308 using __is_harmonic 00309 = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>; 00310 00311 public: 00312 00313 typedef _Rep rep; 00314 typedef _Period period; 00315 00316 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration"); 00317 static_assert(__is_ratio<_Period>::value, 00318 "period must be a specialization of ratio"); 00319 static_assert(_Period::num > 0, "period must be positive"); 00320 00321 // 20.11.5.1 construction / copy / destroy 00322 constexpr duration() = default; 00323 00324 duration(const duration&) = default; 00325 00326 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00327 // 3050. Conversion specification problem in chrono::duration 00328 template<typename _Rep2, typename = _Require< 00329 is_convertible<const _Rep2&, rep>, 00330 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>> 00331 constexpr explicit duration(const _Rep2& __rep) 00332 : __r(static_cast<rep>(__rep)) { } 00333 00334 template<typename _Rep2, typename _Period2, typename = _Require< 00335 __or_<__is_float<rep>, 00336 __and_<__is_harmonic<_Period2>, 00337 __not_<__is_float<_Rep2>>>>>> 00338 constexpr duration(const duration<_Rep2, _Period2>& __d) 00339 : __r(duration_cast<duration>(__d).count()) { } 00340 00341 ~duration() = default; 00342 duration& operator=(const duration&) = default; 00343 00344 // 20.11.5.2 observer 00345 constexpr rep 00346 count() const 00347 { return __r; } 00348 00349 // 20.11.5.3 arithmetic 00350 constexpr duration 00351 operator+() const 00352 { return *this; } 00353 00354 constexpr duration 00355 operator-() const 00356 { return duration(-__r); } 00357 00358 _GLIBCXX17_CONSTEXPR duration& 00359 operator++() 00360 { 00361 ++__r; 00362 return *this; 00363 } 00364 00365 _GLIBCXX17_CONSTEXPR duration 00366 operator++(int) 00367 { return duration(__r++); } 00368 00369 _GLIBCXX17_CONSTEXPR duration& 00370 operator--() 00371 { 00372 --__r; 00373 return *this; 00374 } 00375 00376 _GLIBCXX17_CONSTEXPR duration 00377 operator--(int) 00378 { return duration(__r--); } 00379 00380 _GLIBCXX17_CONSTEXPR duration& 00381 operator+=(const duration& __d) 00382 { 00383 __r += __d.count(); 00384 return *this; 00385 } 00386 00387 _GLIBCXX17_CONSTEXPR duration& 00388 operator-=(const duration& __d) 00389 { 00390 __r -= __d.count(); 00391 return *this; 00392 } 00393 00394 _GLIBCXX17_CONSTEXPR duration& 00395 operator*=(const rep& __rhs) 00396 { 00397 __r *= __rhs; 00398 return *this; 00399 } 00400 00401 _GLIBCXX17_CONSTEXPR duration& 00402 operator/=(const rep& __rhs) 00403 { 00404 __r /= __rhs; 00405 return *this; 00406 } 00407 00408 // DR 934. 00409 template<typename _Rep2 = rep> 00410 _GLIBCXX17_CONSTEXPR 00411 typename enable_if<!treat_as_floating_point<_Rep2>::value, 00412 duration&>::type 00413 operator%=(const rep& __rhs) 00414 { 00415 __r %= __rhs; 00416 return *this; 00417 } 00418 00419 template<typename _Rep2 = rep> 00420 _GLIBCXX17_CONSTEXPR 00421 typename enable_if<!treat_as_floating_point<_Rep2>::value, 00422 duration&>::type 00423 operator%=(const duration& __d) 00424 { 00425 __r %= __d.count(); 00426 return *this; 00427 } 00428 00429 // 20.11.5.4 special values 00430 static constexpr duration 00431 zero() noexcept 00432 { return duration(duration_values<rep>::zero()); } 00433 00434 static constexpr duration 00435 min() noexcept 00436 { return duration(duration_values<rep>::min()); } 00437 00438 static constexpr duration 00439 max() noexcept 00440 { return duration(duration_values<rep>::max()); } 00441 00442 private: 00443 rep __r; 00444 }; 00445 00446 template<typename _Rep1, typename _Period1, 00447 typename _Rep2, typename _Period2> 00448 constexpr typename common_type<duration<_Rep1, _Period1>, 00449 duration<_Rep2, _Period2>>::type 00450 operator+(const duration<_Rep1, _Period1>& __lhs, 00451 const duration<_Rep2, _Period2>& __rhs) 00452 { 00453 typedef duration<_Rep1, _Period1> __dur1; 00454 typedef duration<_Rep2, _Period2> __dur2; 00455 typedef typename common_type<__dur1,__dur2>::type __cd; 00456 return __cd(__cd(__lhs).count() + __cd(__rhs).count()); 00457 } 00458 00459 template<typename _Rep1, typename _Period1, 00460 typename _Rep2, typename _Period2> 00461 constexpr typename common_type<duration<_Rep1, _Period1>, 00462 duration<_Rep2, _Period2>>::type 00463 operator-(const duration<_Rep1, _Period1>& __lhs, 00464 const duration<_Rep2, _Period2>& __rhs) 00465 { 00466 typedef duration<_Rep1, _Period1> __dur1; 00467 typedef duration<_Rep2, _Period2> __dur2; 00468 typedef typename common_type<__dur1,__dur2>::type __cd; 00469 return __cd(__cd(__lhs).count() - __cd(__rhs).count()); 00470 } 00471 00472 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2 00473 // is implicitly convertible to it. 00474 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00475 // 3050. Conversion specification problem in chrono::duration constructor 00476 template<typename _Rep1, typename _Rep2, 00477 typename _CRep = typename common_type<_Rep1, _Rep2>::type> 00478 using __common_rep_t = typename 00479 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type; 00480 00481 template<typename _Rep1, typename _Period, typename _Rep2> 00482 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period> 00483 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 00484 { 00485 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 00486 __cd; 00487 return __cd(__cd(__d).count() * __s); 00488 } 00489 00490 template<typename _Rep1, typename _Rep2, typename _Period> 00491 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period> 00492 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) 00493 { return __d * __s; } 00494 00495 template<typename _Rep1, typename _Period, typename _Rep2> 00496 constexpr 00497 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period> 00498 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 00499 { 00500 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 00501 __cd; 00502 return __cd(__cd(__d).count() / __s); 00503 } 00504 00505 template<typename _Rep1, typename _Period1, 00506 typename _Rep2, typename _Period2> 00507 constexpr typename common_type<_Rep1, _Rep2>::type 00508 operator/(const duration<_Rep1, _Period1>& __lhs, 00509 const duration<_Rep2, _Period2>& __rhs) 00510 { 00511 typedef duration<_Rep1, _Period1> __dur1; 00512 typedef duration<_Rep2, _Period2> __dur2; 00513 typedef typename common_type<__dur1,__dur2>::type __cd; 00514 return __cd(__lhs).count() / __cd(__rhs).count(); 00515 } 00516 00517 // DR 934. 00518 template<typename _Rep1, typename _Period, typename _Rep2> 00519 constexpr 00520 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period> 00521 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 00522 { 00523 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 00524 __cd; 00525 return __cd(__cd(__d).count() % __s); 00526 } 00527 00528 template<typename _Rep1, typename _Period1, 00529 typename _Rep2, typename _Period2> 00530 constexpr typename common_type<duration<_Rep1, _Period1>, 00531 duration<_Rep2, _Period2>>::type 00532 operator%(const duration<_Rep1, _Period1>& __lhs, 00533 const duration<_Rep2, _Period2>& __rhs) 00534 { 00535 typedef duration<_Rep1, _Period1> __dur1; 00536 typedef duration<_Rep2, _Period2> __dur2; 00537 typedef typename common_type<__dur1,__dur2>::type __cd; 00538 return __cd(__cd(__lhs).count() % __cd(__rhs).count()); 00539 } 00540 00541 // comparisons 00542 template<typename _Rep1, typename _Period1, 00543 typename _Rep2, typename _Period2> 00544 constexpr bool 00545 operator==(const duration<_Rep1, _Period1>& __lhs, 00546 const duration<_Rep2, _Period2>& __rhs) 00547 { 00548 typedef duration<_Rep1, _Period1> __dur1; 00549 typedef duration<_Rep2, _Period2> __dur2; 00550 typedef typename common_type<__dur1,__dur2>::type __ct; 00551 return __ct(__lhs).count() == __ct(__rhs).count(); 00552 } 00553 00554 template<typename _Rep1, typename _Period1, 00555 typename _Rep2, typename _Period2> 00556 constexpr bool 00557 operator<(const duration<_Rep1, _Period1>& __lhs, 00558 const duration<_Rep2, _Period2>& __rhs) 00559 { 00560 typedef duration<_Rep1, _Period1> __dur1; 00561 typedef duration<_Rep2, _Period2> __dur2; 00562 typedef typename common_type<__dur1,__dur2>::type __ct; 00563 return __ct(__lhs).count() < __ct(__rhs).count(); 00564 } 00565 00566 template<typename _Rep1, typename _Period1, 00567 typename _Rep2, typename _Period2> 00568 constexpr bool 00569 operator!=(const duration<_Rep1, _Period1>& __lhs, 00570 const duration<_Rep2, _Period2>& __rhs) 00571 { return !(__lhs == __rhs); } 00572 00573 template<typename _Rep1, typename _Period1, 00574 typename _Rep2, typename _Period2> 00575 constexpr bool 00576 operator<=(const duration<_Rep1, _Period1>& __lhs, 00577 const duration<_Rep2, _Period2>& __rhs) 00578 { return !(__rhs < __lhs); } 00579 00580 template<typename _Rep1, typename _Period1, 00581 typename _Rep2, typename _Period2> 00582 constexpr bool 00583 operator>(const duration<_Rep1, _Period1>& __lhs, 00584 const duration<_Rep2, _Period2>& __rhs) 00585 { return __rhs < __lhs; } 00586 00587 template<typename _Rep1, typename _Period1, 00588 typename _Rep2, typename _Period2> 00589 constexpr bool 00590 operator>=(const duration<_Rep1, _Period1>& __lhs, 00591 const duration<_Rep2, _Period2>& __rhs) 00592 { return !(__lhs < __rhs); } 00593 00594 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 00595 # define _GLIBCXX_CHRONO_INT64_T int64_t 00596 #elif defined __INT64_TYPE__ 00597 # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__ 00598 #else 00599 static_assert(std::numeric_limits<unsigned long long>::digits >= 64, 00600 "Representation type for nanoseconds must have at least 64 bits"); 00601 # define _GLIBCXX_CHRONO_INT64_T long long 00602 #endif 00603 00604 /// nanoseconds 00605 typedef duration<_GLIBCXX_CHRONO_INT64_T, nano> nanoseconds; 00606 00607 /// microseconds 00608 typedef duration<_GLIBCXX_CHRONO_INT64_T, micro> microseconds; 00609 00610 /// milliseconds 00611 typedef duration<_GLIBCXX_CHRONO_INT64_T, milli> milliseconds; 00612 00613 /// seconds 00614 typedef duration<_GLIBCXX_CHRONO_INT64_T> seconds; 00615 00616 /// minutes 00617 typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>> minutes; 00618 00619 /// hours 00620 typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>> hours; 00621 00622 #undef _GLIBCXX_CHRONO_INT64_T 00623 00624 /// time_point 00625 template<typename _Clock, typename _Dur> 00626 struct time_point 00627 { 00628 typedef _Clock clock; 00629 typedef _Dur duration; 00630 typedef typename duration::rep rep; 00631 typedef typename duration::period period; 00632 00633 constexpr time_point() : __d(duration::zero()) 00634 { } 00635 00636 constexpr explicit time_point(const duration& __dur) 00637 : __d(__dur) 00638 { } 00639 00640 // conversions 00641 template<typename _Dur2, 00642 typename = _Require<is_convertible<_Dur2, _Dur>>> 00643 constexpr time_point(const time_point<clock, _Dur2>& __t) 00644 : __d(__t.time_since_epoch()) 00645 { } 00646 00647 // observer 00648 constexpr duration 00649 time_since_epoch() const 00650 { return __d; } 00651 00652 // arithmetic 00653 _GLIBCXX17_CONSTEXPR time_point& 00654 operator+=(const duration& __dur) 00655 { 00656 __d += __dur; 00657 return *this; 00658 } 00659 00660 _GLIBCXX17_CONSTEXPR time_point& 00661 operator-=(const duration& __dur) 00662 { 00663 __d -= __dur; 00664 return *this; 00665 } 00666 00667 // special values 00668 static constexpr time_point 00669 min() noexcept 00670 { return time_point(duration::min()); } 00671 00672 static constexpr time_point 00673 max() noexcept 00674 { return time_point(duration::max()); } 00675 00676 private: 00677 duration __d; 00678 }; 00679 00680 /// time_point_cast 00681 template<typename _ToDur, typename _Clock, typename _Dur> 00682 constexpr typename enable_if<__is_duration<_ToDur>::value, 00683 time_point<_Clock, _ToDur>>::type 00684 time_point_cast(const time_point<_Clock, _Dur>& __t) 00685 { 00686 typedef time_point<_Clock, _ToDur> __time_point; 00687 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch())); 00688 } 00689 00690 #if __cplusplus > 201402L 00691 template<typename _ToDur, typename _Clock, typename _Dur> 00692 constexpr 00693 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> 00694 floor(const time_point<_Clock, _Dur>& __tp) 00695 { 00696 return time_point<_Clock, _ToDur>{ 00697 chrono::floor<_ToDur>(__tp.time_since_epoch())}; 00698 } 00699 00700 template<typename _ToDur, typename _Clock, typename _Dur> 00701 constexpr 00702 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> 00703 ceil(const time_point<_Clock, _Dur>& __tp) 00704 { 00705 return time_point<_Clock, _ToDur>{ 00706 chrono::ceil<_ToDur>(__tp.time_since_epoch())}; 00707 } 00708 00709 template<typename _ToDur, typename _Clock, typename _Dur> 00710 constexpr enable_if_t< 00711 __and_<__is_duration<_ToDur>, 00712 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value, 00713 time_point<_Clock, _ToDur>> 00714 round(const time_point<_Clock, _Dur>& __tp) 00715 { 00716 return time_point<_Clock, _ToDur>{ 00717 chrono::round<_ToDur>(__tp.time_since_epoch())}; 00718 } 00719 #endif // C++17 00720 00721 template<typename _Clock, typename _Dur1, 00722 typename _Rep2, typename _Period2> 00723 constexpr time_point<_Clock, 00724 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> 00725 operator+(const time_point<_Clock, _Dur1>& __lhs, 00726 const duration<_Rep2, _Period2>& __rhs) 00727 { 00728 typedef duration<_Rep2, _Period2> __dur2; 00729 typedef typename common_type<_Dur1,__dur2>::type __ct; 00730 typedef time_point<_Clock, __ct> __time_point; 00731 return __time_point(__lhs.time_since_epoch() + __rhs); 00732 } 00733 00734 template<typename _Rep1, typename _Period1, 00735 typename _Clock, typename _Dur2> 00736 constexpr time_point<_Clock, 00737 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type> 00738 operator+(const duration<_Rep1, _Period1>& __lhs, 00739 const time_point<_Clock, _Dur2>& __rhs) 00740 { 00741 typedef duration<_Rep1, _Period1> __dur1; 00742 typedef typename common_type<__dur1,_Dur2>::type __ct; 00743 typedef time_point<_Clock, __ct> __time_point; 00744 return __time_point(__rhs.time_since_epoch() + __lhs); 00745 } 00746 00747 template<typename _Clock, typename _Dur1, 00748 typename _Rep2, typename _Period2> 00749 constexpr time_point<_Clock, 00750 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> 00751 operator-(const time_point<_Clock, _Dur1>& __lhs, 00752 const duration<_Rep2, _Period2>& __rhs) 00753 { 00754 typedef duration<_Rep2, _Period2> __dur2; 00755 typedef typename common_type<_Dur1,__dur2>::type __ct; 00756 typedef time_point<_Clock, __ct> __time_point; 00757 return __time_point(__lhs.time_since_epoch() -__rhs); 00758 } 00759 00760 template<typename _Clock, typename _Dur1, typename _Dur2> 00761 constexpr typename common_type<_Dur1, _Dur2>::type 00762 operator-(const time_point<_Clock, _Dur1>& __lhs, 00763 const time_point<_Clock, _Dur2>& __rhs) 00764 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); } 00765 00766 template<typename _Clock, typename _Dur1, typename _Dur2> 00767 constexpr bool 00768 operator==(const time_point<_Clock, _Dur1>& __lhs, 00769 const time_point<_Clock, _Dur2>& __rhs) 00770 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); } 00771 00772 template<typename _Clock, typename _Dur1, typename _Dur2> 00773 constexpr bool 00774 operator!=(const time_point<_Clock, _Dur1>& __lhs, 00775 const time_point<_Clock, _Dur2>& __rhs) 00776 { return !(__lhs == __rhs); } 00777 00778 template<typename _Clock, typename _Dur1, typename _Dur2> 00779 constexpr bool 00780 operator<(const time_point<_Clock, _Dur1>& __lhs, 00781 const time_point<_Clock, _Dur2>& __rhs) 00782 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); } 00783 00784 template<typename _Clock, typename _Dur1, typename _Dur2> 00785 constexpr bool 00786 operator<=(const time_point<_Clock, _Dur1>& __lhs, 00787 const time_point<_Clock, _Dur2>& __rhs) 00788 { return !(__rhs < __lhs); } 00789 00790 template<typename _Clock, typename _Dur1, typename _Dur2> 00791 constexpr bool 00792 operator>(const time_point<_Clock, _Dur1>& __lhs, 00793 const time_point<_Clock, _Dur2>& __rhs) 00794 { return __rhs < __lhs; } 00795 00796 template<typename _Clock, typename _Dur1, typename _Dur2> 00797 constexpr bool 00798 operator>=(const time_point<_Clock, _Dur1>& __lhs, 00799 const time_point<_Clock, _Dur2>& __rhs) 00800 { return !(__lhs < __rhs); } 00801 00802 00803 // Clocks. 00804 00805 // Why nanosecond resolution as the default? 00806 // Why have std::system_clock always count in the highest 00807 // resolution (ie nanoseconds), even if on some OSes the low 3 00808 // or 9 decimal digits will be always zero? This allows later 00809 // implementations to change the system_clock::now() 00810 // implementation any time to provide better resolution without 00811 // changing function signature or units. 00812 00813 // To support the (forward) evolution of the library's defined 00814 // clocks, wrap inside inline namespace so that the current 00815 // defintions of system_clock, steady_clock, and 00816 // high_resolution_clock types are uniquely mangled. This way, new 00817 // code can use the latests clocks, while the library can contain 00818 // compatibility definitions for previous versions. At some 00819 // point, when these clocks settle down, the inlined namespaces 00820 // can be removed. XXX GLIBCXX_ABI Deprecated 00821 inline namespace _V2 { 00822 00823 /** 00824 * @brief System clock. 00825 * 00826 * Time returned represents wall time from the system-wide clock. 00827 */ 00828 struct system_clock 00829 { 00830 typedef chrono::nanoseconds duration; 00831 typedef duration::rep rep; 00832 typedef duration::period period; 00833 typedef chrono::time_point<system_clock, duration> time_point; 00834 00835 static_assert(system_clock::duration::min() 00836 < system_clock::duration::zero(), 00837 "a clock's minimum duration cannot be less than its epoch"); 00838 00839 static constexpr bool is_steady = false; 00840 00841 static time_point 00842 now() noexcept; 00843 00844 // Map to C API 00845 static std::time_t 00846 to_time_t(const time_point& __t) noexcept 00847 { 00848 return std::time_t(duration_cast<chrono::seconds> 00849 (__t.time_since_epoch()).count()); 00850 } 00851 00852 static time_point 00853 from_time_t(std::time_t __t) noexcept 00854 { 00855 typedef chrono::time_point<system_clock, seconds> __from; 00856 return time_point_cast<system_clock::duration> 00857 (__from(chrono::seconds(__t))); 00858 } 00859 }; 00860 00861 00862 /** 00863 * @brief Monotonic clock 00864 * 00865 * Time returned has the property of only increasing at a uniform rate. 00866 */ 00867 struct steady_clock 00868 { 00869 typedef chrono::nanoseconds duration; 00870 typedef duration::rep rep; 00871 typedef duration::period period; 00872 typedef chrono::time_point<steady_clock, duration> time_point; 00873 00874 static constexpr bool is_steady = true; 00875 00876 static time_point 00877 now() noexcept; 00878 }; 00879 00880 00881 /** 00882 * @brief Highest-resolution clock 00883 * 00884 * This is the clock "with the shortest tick period." Alias to 00885 * std::system_clock until higher-than-nanosecond definitions 00886 * become feasible. 00887 */ 00888 using high_resolution_clock = system_clock; 00889 00890 } // end inline namespace _V2 00891 } // namespace chrono 00892 00893 #if __cplusplus > 201103L 00894 00895 #define __cpp_lib_chrono_udls 201304 00896 00897 inline namespace literals 00898 { 00899 inline namespace chrono_literals 00900 { 00901 #pragma GCC diagnostic push 00902 #pragma GCC diagnostic ignored "-Wliteral-suffix" 00903 template<typename _Dur, char... _Digits> 00904 constexpr _Dur __check_overflow() 00905 { 00906 using _Val = __parse_int::_Parse_int<_Digits...>; 00907 constexpr typename _Dur::rep __repval = _Val::value; 00908 static_assert(__repval >= 0 && __repval == _Val::value, 00909 "literal value cannot be represented by duration type"); 00910 return _Dur(__repval); 00911 } 00912 00913 constexpr chrono::duration<long double, ratio<3600,1>> 00914 operator""h(long double __hours) 00915 { return chrono::duration<long double, ratio<3600,1>>{__hours}; } 00916 00917 template <char... _Digits> 00918 constexpr chrono::hours 00919 operator""h() 00920 { return __check_overflow<chrono::hours, _Digits...>(); } 00921 00922 constexpr chrono::duration<long double, ratio<60,1>> 00923 operator""min(long double __mins) 00924 { return chrono::duration<long double, ratio<60,1>>{__mins}; } 00925 00926 template <char... _Digits> 00927 constexpr chrono::minutes 00928 operator""min() 00929 { return __check_overflow<chrono::minutes, _Digits...>(); } 00930 00931 constexpr chrono::duration<long double> 00932 operator""s(long double __secs) 00933 { return chrono::duration<long double>{__secs}; } 00934 00935 template <char... _Digits> 00936 constexpr chrono::seconds 00937 operator""s() 00938 { return __check_overflow<chrono::seconds, _Digits...>(); } 00939 00940 constexpr chrono::duration<long double, milli> 00941 operator""ms(long double __msecs) 00942 { return chrono::duration<long double, milli>{__msecs}; } 00943 00944 template <char... _Digits> 00945 constexpr chrono::milliseconds 00946 operator""ms() 00947 { return __check_overflow<chrono::milliseconds, _Digits...>(); } 00948 00949 constexpr chrono::duration<long double, micro> 00950 operator""us(long double __usecs) 00951 { return chrono::duration<long double, micro>{__usecs}; } 00952 00953 template <char... _Digits> 00954 constexpr chrono::microseconds 00955 operator""us() 00956 { return __check_overflow<chrono::microseconds, _Digits...>(); } 00957 00958 constexpr chrono::duration<long double, nano> 00959 operator""ns(long double __nsecs) 00960 { return chrono::duration<long double, nano>{__nsecs}; } 00961 00962 template <char... _Digits> 00963 constexpr chrono::nanoseconds 00964 operator""ns() 00965 { return __check_overflow<chrono::nanoseconds, _Digits...>(); } 00966 00967 #pragma GCC diagnostic pop 00968 } // inline namespace chrono_literals 00969 } // inline namespace literals 00970 00971 namespace chrono 00972 { 00973 using namespace literals::chrono_literals; 00974 } // namespace chrono 00975 00976 #endif // C++14 00977 00978 // @} group chrono 00979 00980 _GLIBCXX_END_NAMESPACE_VERSION 00981 } // namespace std 00982 00983 #endif // C++11 00984 00985 #endif //_GLIBCXX_CHRONO