brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.1 KiB · 7f2a73a Raw
178 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03___CHRONO_TIME_POINT_H11#define _LIBCPP___CXX03___CHRONO_TIME_POINT_H12 13#include <__cxx03/__chrono/duration.h>14#include <__cxx03/__config>15#include <__cxx03/__type_traits/common_type.h>16#include <__cxx03/__type_traits/enable_if.h>17#include <__cxx03/__type_traits/is_convertible.h>18#include <__cxx03/limits>19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21#  pragma GCC system_header22#endif23 24_LIBCPP_PUSH_MACROS25#include <__cxx03/__undef_macros>26 27_LIBCPP_BEGIN_NAMESPACE_STD28 29namespace chrono {30 31template <class _Clock, class _Duration = typename _Clock::duration>32class _LIBCPP_TEMPLATE_VIS time_point {33  static_assert(__is_duration<_Duration>::value,34                "Second template parameter of time_point must be a std::chrono::duration");35 36public:37  typedef _Clock clock;38  typedef _Duration duration;39  typedef typename duration::rep rep;40  typedef typename duration::period period;41 42private:43  duration __d_;44 45public:46  _LIBCPP_HIDE_FROM_ABI time_point() : __d_(duration::zero()) {}47  _LIBCPP_HIDE_FROM_ABI explicit time_point(const duration& __d) : __d_(__d) {}48 49  // conversions50  template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>51  _LIBCPP_HIDE_FROM_ABI time_point(const time_point<clock, _Duration2>& __t) : __d_(__t.time_since_epoch()) {}52 53  // observer54 55  _LIBCPP_HIDE_FROM_ABI duration time_since_epoch() const { return __d_; }56 57  // arithmetic58 59  _LIBCPP_HIDE_FROM_ABI time_point& operator+=(const duration& __d) {60    __d_ += __d;61    return *this;62  }63  _LIBCPP_HIDE_FROM_ABI time_point& operator-=(const duration& __d) {64    __d_ -= __d;65    return *this;66  }67 68  // special values69 70  _LIBCPP_HIDE_FROM_ABI static time_point min() _NOEXCEPT { return time_point(duration::min()); }71  _LIBCPP_HIDE_FROM_ABI static time_point max() _NOEXCEPT { return time_point(duration::max()); }72};73 74} // namespace chrono75 76template <class _Clock, class _Duration1, class _Duration2>77struct _LIBCPP_TEMPLATE_VIS78common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {79  typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;80};81 82namespace chrono {83 84template <class _ToDuration, class _Clock, class _Duration, __enable_if_t<__is_duration<_ToDuration>::value, int> = 0>85inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, _ToDuration> time_point_cast(const time_point<_Clock, _Duration>& __t) {86  return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));87}88 89// time_point ==90 91template <class _Clock, class _Duration1, class _Duration2>92inline _LIBCPP_HIDE_FROM_ABI bool93operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {94  return __lhs.time_since_epoch() == __rhs.time_since_epoch();95}96 97// time_point !=98 99template <class _Clock, class _Duration1, class _Duration2>100inline _LIBCPP_HIDE_FROM_ABI bool101operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {102  return !(__lhs == __rhs);103}104 105// time_point <106 107template <class _Clock, class _Duration1, class _Duration2>108inline _LIBCPP_HIDE_FROM_ABI bool109operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {110  return __lhs.time_since_epoch() < __rhs.time_since_epoch();111}112 113// time_point >114 115template <class _Clock, class _Duration1, class _Duration2>116inline _LIBCPP_HIDE_FROM_ABI bool117operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {118  return __rhs < __lhs;119}120 121// time_point <=122 123template <class _Clock, class _Duration1, class _Duration2>124inline _LIBCPP_HIDE_FROM_ABI bool125operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {126  return !(__rhs < __lhs);127}128 129// time_point >=130 131template <class _Clock, class _Duration1, class _Duration2>132inline _LIBCPP_HIDE_FROM_ABI bool133operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {134  return !(__lhs < __rhs);135}136 137// time_point operator+(time_point x, duration y);138 139template <class _Clock, class _Duration1, class _Rep2, class _Period2>140inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>141operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {142  typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;143  return _Tr(__lhs.time_since_epoch() + __rhs);144}145 146// time_point operator+(duration x, time_point y);147 148template <class _Rep1, class _Period1, class _Clock, class _Duration2>149inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>150operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {151  return __rhs + __lhs;152}153 154// time_point operator-(time_point x, duration y);155 156template <class _Clock, class _Duration1, class _Rep2, class _Period2>157inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>158operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {159  typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;160  return _Ret(__lhs.time_since_epoch() - __rhs);161}162 163// duration operator-(time_point x, time_point y);164 165template <class _Clock, class _Duration1, class _Duration2>166inline _LIBCPP_HIDE_FROM_ABI typename common_type<_Duration1, _Duration2>::type167operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {168  return __lhs.time_since_epoch() - __rhs.time_since_epoch();169}170 171} // namespace chrono172 173_LIBCPP_END_NAMESPACE_STD174 175_LIBCPP_POP_MACROS176 177#endif // _LIBCPP___CXX03___CHRONO_TIME_POINT_H178