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