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: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 31 | Timestamp() = delete; |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 32 | |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 33 | template <int64_t seconds> |
| 34 | static constexpr Timestamp Seconds() { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 35 | return FromStaticFraction<seconds, 1000000>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 36 | } |
| 37 | template <int64_t ms> |
| 38 | static constexpr Timestamp Millis() { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 39 | return FromStaticFraction<ms, 1000>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 40 | } |
| 41 | template <int64_t us> |
| 42 | static constexpr Timestamp Micros() { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 43 | return FromStaticValue<us>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 44 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 45 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 46 | template <typename T> |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 47 | static Timestamp seconds(T seconds) { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 48 | return FromFraction<1000000>(seconds); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 49 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 50 | template <typename T> |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 51 | static Timestamp ms(T milliseconds) { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 52 | return FromFraction<1000>(milliseconds); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 53 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 54 | template <typename T> |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 55 | static Timestamp us(T microseconds) { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 56 | return FromValue(microseconds); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 57 | } |
| 58 | template <typename T = int64_t> |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 59 | T seconds() const { |
| 60 | return ToFraction<1000000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 61 | } |
| 62 | template <typename T = int64_t> |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 63 | T ms() const { |
| 64 | return ToFraction<1000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 65 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 66 | template <typename T = int64_t> |
| 67 | T us() const { |
| 68 | return ToValue<T>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | constexpr int64_t seconds_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 72 | return ToFractionOr<1000000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 73 | } |
| 74 | constexpr int64_t ms_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 75 | return ToFractionOr<1000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 76 | } |
| 77 | constexpr int64_t us_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 78 | return ToValueOr(fallback_value); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 79 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 80 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 81 | Timestamp operator+(const TimeDelta delta) const { |
| 82 | if (IsPlusInfinity() || delta.IsPlusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 83 | RTC_DCHECK(!IsMinusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 84 | RTC_DCHECK(!delta.IsMinusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 85 | return PlusInfinity(); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 86 | } else if (IsMinusInfinity() || delta.IsMinusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 87 | RTC_DCHECK(!IsPlusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 88 | RTC_DCHECK(!delta.IsPlusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 89 | return MinusInfinity(); |
| 90 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 91 | return Timestamp::us(us() + delta.us()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 92 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 93 | Timestamp operator-(const TimeDelta delta) const { |
| 94 | if (IsPlusInfinity() || delta.IsMinusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 95 | RTC_DCHECK(!IsMinusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 96 | RTC_DCHECK(!delta.IsPlusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 97 | return PlusInfinity(); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 98 | } else if (IsMinusInfinity() || delta.IsPlusInfinity()) { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 99 | RTC_DCHECK(!IsPlusInfinity()); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 100 | RTC_DCHECK(!delta.IsMinusInfinity()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 101 | return MinusInfinity(); |
| 102 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 103 | return Timestamp::us(us() - delta.us()); |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 104 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 105 | TimeDelta operator-(const Timestamp other) const { |
Sebastian Jansson | 9de4ef4 | 2018-09-04 17:32:36 +0200 | [diff] [blame] | 106 | if (IsPlusInfinity() || other.IsMinusInfinity()) { |
| 107 | RTC_DCHECK(!IsMinusInfinity()); |
| 108 | RTC_DCHECK(!other.IsPlusInfinity()); |
| 109 | return TimeDelta::PlusInfinity(); |
| 110 | } else if (IsMinusInfinity() || other.IsPlusInfinity()) { |
| 111 | RTC_DCHECK(!IsPlusInfinity()); |
| 112 | RTC_DCHECK(!other.IsMinusInfinity()); |
| 113 | return TimeDelta::MinusInfinity(); |
| 114 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 115 | return TimeDelta::us(us() - other.us()); |
| 116 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 117 | Timestamp& operator-=(const TimeDelta delta) { |
| 118 | *this = *this - delta; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 119 | return *this; |
| 120 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 121 | Timestamp& operator+=(const TimeDelta delta) { |
| 122 | *this = *this + delta; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 123 | return *this; |
| 124 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 125 | |
| 126 | private: |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 127 | friend class rtc_units_impl::UnitBase<Timestamp>; |
| 128 | using UnitBase::UnitBase; |
| 129 | static constexpr bool one_sided = true; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame^] | 132 | std::string ToString(Timestamp value); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 133 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 134 | #ifdef UNIT_TEST |
| 135 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 136 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 137 | Timestamp value) { |
| 138 | return stream << ToString(value); |
| 139 | } |
| 140 | #endif // UNIT_TEST |
| 141 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 142 | } // namespace webrtc |
| 143 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 144 | #endif // API_UNITS_TIMESTAMP_H_ |