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_TIME_DELTA_H_ |
| 12 | #define API_UNITS_TIME_DELTA_H_ |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 13 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 14 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 15 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 16 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 17 | |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 18 | #include <cstdlib> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 19 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include <type_traits> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 21 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 22 | #include "rtc_base/units/unit_base.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 25 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 26 | // TimeDelta represents the difference between two timestamps. Commonly this can |
| 27 | // be a duration. However since two Timestamps are not guaranteed to have the |
| 28 | // same epoch (they might come from different computers, making exact |
| 29 | // synchronisation infeasible), the duration covered by a TimeDelta can be |
| 30 | // undefined. To simplify usage, it can be constructed and converted to |
| 31 | // different units, specifically seconds (s), milliseconds (ms) and |
| 32 | // microseconds (us). |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 33 | class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 34 | public: |
Danil Chapovalov | 8d94dc2 | 2020-01-28 17:54:47 +0100 | [diff] [blame] | 35 | template <typename T> |
Evan Shrubsole | d425f50 | 2022-04-28 10:18:12 +0200 | [diff] [blame^] | 36 | static constexpr TimeDelta Minutes(T value) { |
| 37 | static_assert(std::is_arithmetic<T>::value, ""); |
| 38 | return Seconds(value * 60); |
| 39 | } |
| 40 | template <typename T> |
Danil Chapovalov | 8d94dc2 | 2020-01-28 17:54:47 +0100 | [diff] [blame] | 41 | static constexpr TimeDelta Seconds(T value) { |
| 42 | static_assert(std::is_arithmetic<T>::value, ""); |
| 43 | return FromFraction(1'000'000, value); |
| 44 | } |
| 45 | template <typename T> |
| 46 | static constexpr TimeDelta Millis(T value) { |
| 47 | static_assert(std::is_arithmetic<T>::value, ""); |
| 48 | return FromFraction(1'000, value); |
| 49 | } |
| 50 | template <typename T> |
| 51 | static constexpr TimeDelta Micros(T value) { |
| 52 | static_assert(std::is_arithmetic<T>::value, ""); |
| 53 | return FromValue(value); |
| 54 | } |
| 55 | |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 56 | TimeDelta() = delete; |
Danil Chapovalov | 8d94dc2 | 2020-01-28 17:54:47 +0100 | [diff] [blame] | 57 | |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 58 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 59 | constexpr T seconds() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 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 | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 63 | constexpr T ms() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 64 | return ToFraction<1000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 65 | } |
| 66 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 67 | constexpr T us() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 68 | return ToValue<T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 69 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 70 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 71 | constexpr T ns() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 72 | return ToMultiple<1000, T>(); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 73 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 74 | |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 75 | constexpr int64_t seconds_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 76 | return ToFractionOr<1000000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 77 | } |
| 78 | constexpr int64_t ms_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 79 | return ToFractionOr<1000>(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 80 | } |
| 81 | constexpr int64_t us_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 82 | return ToValueOr(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 85 | constexpr TimeDelta Abs() const { |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 86 | return us() < 0 ? TimeDelta::Micros(-us()) : *this; |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 87 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 88 | |
| 89 | private: |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 90 | friend class rtc_units_impl::UnitBase<TimeDelta>; |
| 91 | using RelativeUnit::RelativeUnit; |
| 92 | static constexpr bool one_sided = false; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 93 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 94 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 95 | std::string ToString(TimeDelta value); |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 96 | inline std::string ToLogString(TimeDelta value) { |
| 97 | return ToString(value); |
| 98 | } |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 99 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 100 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 101 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 102 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 103 | TimeDelta value) { |
| 104 | return stream << ToString(value); |
| 105 | } |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 106 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 107 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 108 | } // namespace webrtc |
| 109 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 110 | #endif // API_UNITS_TIME_DELTA_H_ |