Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:15

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CHRONO_TIME_POINT_H
0011 #define _LIBCPP___CHRONO_TIME_POINT_H
0012 
0013 #include <__chrono/duration.h>
0014 #include <__compare/ordering.h>
0015 #include <__compare/three_way_comparable.h>
0016 #include <__config>
0017 #include <__type_traits/common_type.h>
0018 #include <__type_traits/enable_if.h>
0019 #include <__type_traits/is_convertible.h>
0020 #include <limits>
0021 
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 #  pragma GCC system_header
0024 #endif
0025 
0026 _LIBCPP_PUSH_MACROS
0027 #include <__undef_macros>
0028 
0029 _LIBCPP_BEGIN_NAMESPACE_STD
0030 
0031 namespace chrono {
0032 
0033 template <class _Clock, class _Duration = typename _Clock::duration>
0034 class _LIBCPP_TEMPLATE_VIS time_point {
0035   static_assert(__is_duration_v<_Duration>, "Second template parameter of time_point must be a std::chrono::duration");
0036 
0037 public:
0038   typedef _Clock clock;
0039   typedef _Duration duration;
0040   typedef typename duration::rep rep;
0041   typedef typename duration::period period;
0042 
0043 private:
0044   duration __d_;
0045 
0046 public:
0047   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
0048   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
0049 
0050   // conversions
0051   template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
0052   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point(const time_point<clock, _Duration2>& __t)
0053       : __d_(__t.time_since_epoch()) {}
0054 
0055   // observer
0056 
0057   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const { return __d_; }
0058 
0059   // arithmetic
0060 
0061   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {
0062     __d_ += __d;
0063     return *this;
0064   }
0065   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator-=(const duration& __d) {
0066     __d_ -= __d;
0067     return *this;
0068   }
0069 
0070   // special values
0071 
0072   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT { return time_point(duration::min()); }
0073   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT { return time_point(duration::max()); }
0074 };
0075 
0076 } // namespace chrono
0077 
0078 template <class _Clock, class _Duration1, class _Duration2>
0079 struct _LIBCPP_TEMPLATE_VIS
0080 common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {
0081   typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
0082 };
0083 
0084 namespace chrono {
0085 
0086 template <class _ToDuration, class _Clock, class _Duration>
0087 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>
0088 time_point_cast(const time_point<_Clock, _Duration>& __t) {
0089   return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
0090 }
0091 
0092 #if _LIBCPP_STD_VER >= 17
0093 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
0094 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> floor(const time_point<_Clock, _Duration>& __t) {
0095   return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
0096 }
0097 
0098 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
0099 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> ceil(const time_point<_Clock, _Duration>& __t) {
0100   return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
0101 }
0102 
0103 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
0104 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> round(const time_point<_Clock, _Duration>& __t) {
0105   return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
0106 }
0107 
0108 template <class _Rep, class _Period, enable_if_t<numeric_limits<_Rep>::is_signed, int> = 0>
0109 inline _LIBCPP_HIDE_FROM_ABI constexpr duration<_Rep, _Period> abs(duration<_Rep, _Period> __d) {
0110   return __d >= __d.zero() ? +__d : -__d;
0111 }
0112 #endif // _LIBCPP_STD_VER >= 17
0113 
0114 // time_point ==
0115 
0116 template <class _Clock, class _Duration1, class _Duration2>
0117 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0118 operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0119   return __lhs.time_since_epoch() == __rhs.time_since_epoch();
0120 }
0121 
0122 #if _LIBCPP_STD_VER <= 17
0123 
0124 // time_point !=
0125 
0126 template <class _Clock, class _Duration1, class _Duration2>
0127 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0128 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0129   return !(__lhs == __rhs);
0130 }
0131 
0132 #endif // _LIBCPP_STD_VER <= 17
0133 
0134 // time_point <
0135 
0136 template <class _Clock, class _Duration1, class _Duration2>
0137 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0138 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0139   return __lhs.time_since_epoch() < __rhs.time_since_epoch();
0140 }
0141 
0142 // time_point >
0143 
0144 template <class _Clock, class _Duration1, class _Duration2>
0145 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0146 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0147   return __rhs < __lhs;
0148 }
0149 
0150 // time_point <=
0151 
0152 template <class _Clock, class _Duration1, class _Duration2>
0153 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0154 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0155   return !(__rhs < __lhs);
0156 }
0157 
0158 // time_point >=
0159 
0160 template <class _Clock, class _Duration1, class _Duration2>
0161 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0162 operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0163   return !(__lhs < __rhs);
0164 }
0165 
0166 #if _LIBCPP_STD_VER >= 20
0167 
0168 template <class _Clock, class _Duration1, three_way_comparable_with<_Duration1> _Duration2>
0169 _LIBCPP_HIDE_FROM_ABI constexpr auto
0170 operator<=>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0171   return __lhs.time_since_epoch() <=> __rhs.time_since_epoch();
0172 }
0173 
0174 #endif // _LIBCPP_STD_VER >= 20
0175 
0176 // time_point operator+(time_point x, duration y);
0177 
0178 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
0179 inline _LIBCPP_HIDE_FROM_ABI
0180 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
0181 operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
0182   typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
0183   return _Tr(__lhs.time_since_epoch() + __rhs);
0184 }
0185 
0186 // time_point operator+(duration x, time_point y);
0187 
0188 template <class _Rep1, class _Period1, class _Clock, class _Duration2>
0189 inline _LIBCPP_HIDE_FROM_ABI
0190 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
0191 operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0192   return __rhs + __lhs;
0193 }
0194 
0195 // time_point operator-(time_point x, duration y);
0196 
0197 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
0198 inline _LIBCPP_HIDE_FROM_ABI
0199 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
0200 operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
0201   typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
0202   return _Ret(__lhs.time_since_epoch() - __rhs);
0203 }
0204 
0205 // duration operator-(time_point x, time_point y);
0206 
0207 template <class _Clock, class _Duration1, class _Duration2>
0208 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename common_type<_Duration1, _Duration2>::type
0209 operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0210   return __lhs.time_since_epoch() - __rhs.time_since_epoch();
0211 }
0212 
0213 } // namespace chrono
0214 
0215 _LIBCPP_END_NAMESPACE_STD
0216 
0217 _LIBCPP_POP_MACROS
0218 
0219 #endif // _LIBCPP___CHRONO_TIME_POINT_H