blob: a53ffee74d9cd51a7109a8b16905a81be92f39e4 [file] [log] [blame]
Sebastian Jansson30bd4032018-04-13 13:56:17 +02001/*
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 Jansson6fae6ec2018-05-08 10:43:18 +020011#ifndef API_UNITS_TIME_DELTA_H_
12#define API_UNITS_TIME_DELTA_H_
Sebastian Jansson30bd4032018-04-13 13:56:17 +020013
Sebastian Jansson2afd2812018-08-23 14:44:05 +020014#ifdef UNIT_TEST
15#include <ostream> // no-presubmit-check TODO(webrtc:8982)
16#endif // UNIT_TEST
17
Jonas Olsson941a07c2018-09-13 10:07:07 +020018#include <cstdlib>
Sebastian Jansson30bd4032018-04-13 13:56:17 +020019#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020020#include <type_traits>
Sebastian Jansson30bd4032018-04-13 13:56:17 +020021
Sebastian Jansson72bba622018-11-19 11:17:12 +010022#include "rtc_base/units/unit_base.h"
Sebastian Jansson30bd4032018-04-13 13:56:17 +020023
24namespace webrtc {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020025
Sebastian Jansson30bd4032018-04-13 13:56:17 +020026// 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 Jansson72bba622018-11-19 11:17:12 +010033class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020034 public:
Danil Chapovalov8d94dc22020-01-28 17:54:47 +010035 template <typename T>
36 static constexpr TimeDelta Seconds(T value) {
37 static_assert(std::is_arithmetic<T>::value, "");
38 return FromFraction(1'000'000, value);
39 }
40 template <typename T>
41 static constexpr TimeDelta Millis(T value) {
42 static_assert(std::is_arithmetic<T>::value, "");
43 return FromFraction(1'000, value);
44 }
45 template <typename T>
46 static constexpr TimeDelta Micros(T value) {
47 static_assert(std::is_arithmetic<T>::value, "");
48 return FromValue(value);
49 }
50
Sebastian Jansson3b69b192018-05-07 13:51:51 +020051 TimeDelta() = delete;
Danil Chapovalov8d94dc22020-01-28 17:54:47 +010052
53 // TODO(danilchap): Migrate all code to the 3 factories above and delete the
54 // 6 factories below.
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020055 template <int64_t seconds>
56 static constexpr TimeDelta Seconds() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010057 return FromFraction(1'000'000, seconds);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020058 }
59 template <int64_t ms>
60 static constexpr TimeDelta Millis() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010061 return FromFraction(1000, ms);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020062 }
63 template <int64_t us>
64 static constexpr TimeDelta Micros() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010065 return FromValue(us);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020066 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010067 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010068 static constexpr TimeDelta seconds(T seconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010069 static_assert(std::is_arithmetic<T>::value, "");
Danil Chapovalov7356a562020-01-20 13:02:44 +010070 return FromFraction(1'000'000, seconds);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020071 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010072 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010073 static constexpr TimeDelta ms(T milliseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010074 static_assert(std::is_arithmetic<T>::value, "");
Danil Chapovalov7356a562020-01-20 13:02:44 +010075 return FromFraction(1000, milliseconds);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020076 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010077 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010078 static constexpr TimeDelta us(T microseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010079 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010080 return FromValue(microseconds);
Sebastian Jansson942b3602018-05-30 15:47:44 +020081 }
82 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010083 constexpr T seconds() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010084 return ToFraction<1000000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020085 }
86 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010087 constexpr T ms() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010088 return ToFraction<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020089 }
90 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010091 constexpr T us() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010092 return ToValue<T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020093 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010094 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010095 constexpr T ns() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010096 return ToMultiple<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020097 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020098
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020099 constexpr int64_t seconds_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +0100100 return ToFractionOr<1000000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +0200101 }
102 constexpr int64_t ms_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +0100103 return ToFractionOr<1000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +0200104 }
105 constexpr int64_t us_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +0100106 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +0200107 }
108
Sebastian Janssond7fade52020-01-29 10:44:51 +0100109 constexpr TimeDelta Abs() const {
110 return us() < 0 ? TimeDelta::us(-us()) : *this;
111 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200112
113 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +0100114 friend class rtc_units_impl::UnitBase<TimeDelta>;
115 using RelativeUnit::RelativeUnit;
116 static constexpr bool one_sided = false;
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200117};
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200118
Sebastian Jansson72bba622018-11-19 11:17:12 +0100119std::string ToString(TimeDelta value);
Sebastian Janssonb1138622019-04-11 16:48:15 +0200120inline std::string ToLogString(TimeDelta value) {
121 return ToString(value);
122}
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200123
124#ifdef UNIT_TEST
125inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
126 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
127 TimeDelta value) {
128 return stream << ToString(value);
129}
130#endif // UNIT_TEST
131
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200132} // namespace webrtc
133
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200134#endif // API_UNITS_TIME_DELTA_H_