Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 11 | #ifndef API_UNITS_TIMESTAMP_H_ |
| 12 | #define API_UNITS_TIMESTAMP_H_ |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 13 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 14 | #ifdef UNIT_TEST |
| 15 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
| 16 | #endif // UNIT_TEST |
| 17 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 18 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 19 | #include <type_traits> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 20 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 21 | #include "api/units/time_delta.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
| 23 | |
| 24 | namespace webrtc { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 25 | // Timestamp represents the time that has passed since some unspecified epoch. |
| 26 | // The epoch is assumed to be before any represented timestamps, this means that |
| 27 | // negative values are not valid. The most notable feature is that the |
| 28 | // difference of two Timestamps results in a TimeDelta. |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 29 | class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 30 | public: |
Danil Chapovalov | 8d94dc2 | 2020-01-28 17:54:47 +0100 | [diff] [blame^] | 31 | template <typename T> |
| 32 | static constexpr Timestamp Seconds(T value) { |
| 33 | static_assert(std::is_arithmetic<T>::value, ""); |
| 34 | return FromFraction(1'000'000, value); |
| 35 | } |
| 36 | template <typename T> |
| 37 | static constexpr Timestamp Millis(T value) { |
| 38 | static_assert(std::is_arithmetic<T>::value, ""); |
| 39 | return FromFraction(1'000, value); |
| 40 | } |
| 41 | template <typename T> |
| 42 | static constexpr Timestamp Micros(T value) { |
| 43 | static_assert(std::is_arithmetic<T>::value, ""); |
| 44 | return FromValue(value); |
| 45 | } |
| 46 | |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 47 | Timestamp() = delete; |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 48 | |
Danil Chapovalov | 8d94dc2 | 2020-01-28 17:54:47 +0100 | [diff] [blame^] | 49 | // TODO(danilchap): Migrate all code to the 3 factories above and delete the |
| 50 | // 6 factories below. |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 51 | template <int64_t seconds> |
| 52 | static constexpr Timestamp Seconds() { |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 53 | return FromFraction(1'000'000, seconds); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 54 | } |
| 55 | template <int64_t ms> |
| 56 | static constexpr Timestamp Millis() { |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 57 | return FromFraction(1000, ms); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 58 | } |
| 59 | template <int64_t us> |
| 60 | static constexpr Timestamp Micros() { |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 61 | return FromValue(us); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 62 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 63 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 64 | template <typename T> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 65 | static constexpr Timestamp seconds(T seconds) { |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 66 | static_assert(std::is_arithmetic<T>::value, ""); |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 67 | return FromFraction(1'000'000, seconds); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 68 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 69 | template <typename T> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 70 | static constexpr Timestamp ms(T milliseconds) { |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 71 | static_assert(std::is_arithmetic<T>::value, ""); |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 72 | return FromFraction(1000, milliseconds); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 73 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 74 | template <typename T> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 75 | static constexpr Timestamp us(T microseconds) { |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 76 | static_assert(std::is_arithmetic<T>::value, ""); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 77 | return FromValue(microseconds); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 78 | } |
| 79 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 80 | constexpr T seconds() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 81 | return ToFraction<1000000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 82 | } |
| 83 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 84 | constexpr T ms() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 85 | return ToFraction<1000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 86 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 87 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 88 | constexpr T us() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 89 | return ToValue<T>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | constexpr int64_t seconds_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 93 | return ToFractionOr<1000000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 94 | } |
| 95 | constexpr int64_t ms_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 96 | return ToFractionOr<1000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 97 | } |
| 98 | constexpr int64_t us_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 99 | return ToValueOr(fallback_value); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 100 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 101 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 102 | constexpr Timestamp operator+(const TimeDelta delta) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 103 | if (IsPlusInfinity() || delta.IsPlusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 104 | RTC_DCHECK(!IsMinusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 105 | RTC_DCHECK(!delta.IsMinusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 106 | return PlusInfinity(); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 107 | } else if (IsMinusInfinity() || delta.IsMinusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 108 | RTC_DCHECK(!IsPlusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 109 | RTC_DCHECK(!delta.IsPlusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 110 | return MinusInfinity(); |
| 111 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 112 | return Timestamp::us(us() + delta.us()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 113 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 114 | constexpr Timestamp operator-(const TimeDelta delta) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 115 | if (IsPlusInfinity() || delta.IsMinusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 116 | RTC_DCHECK(!IsMinusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 117 | RTC_DCHECK(!delta.IsPlusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 118 | return PlusInfinity(); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 119 | } else if (IsMinusInfinity() || delta.IsPlusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 120 | RTC_DCHECK(!IsPlusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 121 | RTC_DCHECK(!delta.IsMinusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 122 | return MinusInfinity(); |
| 123 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 124 | return Timestamp::us(us() - delta.us()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 125 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 126 | constexpr TimeDelta operator-(const Timestamp other) const { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 127 | if (IsPlusInfinity() || other.IsMinusInfinity()) { |
| 128 | RTC_DCHECK(!IsMinusInfinity()); |
| 129 | RTC_DCHECK(!other.IsPlusInfinity()); |
| 130 | return TimeDelta::PlusInfinity(); |
| 131 | } else if (IsMinusInfinity() || other.IsPlusInfinity()) { |
| 132 | RTC_DCHECK(!IsPlusInfinity()); |
| 133 | RTC_DCHECK(!other.IsMinusInfinity()); |
| 134 | return TimeDelta::MinusInfinity(); |
| 135 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 136 | return TimeDelta::us(us() - other.us()); |
| 137 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 138 | constexpr Timestamp& operator-=(const TimeDelta delta) { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 139 | *this = *this - delta; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 140 | return *this; |
| 141 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 142 | constexpr Timestamp& operator+=(const TimeDelta delta) { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 143 | *this = *this + delta; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 144 | return *this; |
| 145 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 146 | |
| 147 | private: |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 148 | friend class rtc_units_impl::UnitBase<Timestamp>; |
| 149 | using UnitBase::UnitBase; |
| 150 | static constexpr bool one_sided = true; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 153 | std::string ToString(Timestamp value); |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 154 | inline std::string ToLogString(Timestamp value) { |
| 155 | return ToString(value); |
| 156 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 157 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 158 | #ifdef UNIT_TEST |
| 159 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 160 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 161 | Timestamp value) { |
| 162 | return stream << ToString(value); |
| 163 | } |
| 164 | #endif // UNIT_TEST |
| 165 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 166 | } // namespace webrtc |
| 167 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 168 | #endif // API_UNITS_TIMESTAMP_H_ |