blob: f8c6af457e618a845dd6ced5162c41d3f8ba9897 [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 Jansson30bd4032018-04-13 13:56:17 +020025// TimeDelta represents the difference between two timestamps. Commonly this can
26// be a duration. However since two Timestamps are not guaranteed to have the
27// same epoch (they might come from different computers, making exact
28// synchronisation infeasible), the duration covered by a TimeDelta can be
29// undefined. To simplify usage, it can be constructed and converted to
30// different units, specifically seconds (s), milliseconds (ms) and
31// microseconds (us).
Sebastian Jansson72bba622018-11-19 11:17:12 +010032class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020033 public:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020034 TimeDelta() = delete;
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020035 template <int64_t seconds>
36 static constexpr TimeDelta Seconds() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010037 return FromStaticFraction<seconds, 1000000>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020038 }
39 template <int64_t ms>
40 static constexpr TimeDelta Millis() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010041 return FromStaticFraction<ms, 1000>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020042 }
43 template <int64_t us>
44 static constexpr TimeDelta Micros() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010045 return FromStaticValue<us>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020046 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010047 template <typename T>
Sebastian Jansson942b3602018-05-30 15:47:44 +020048 static TimeDelta seconds(T seconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010049 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010050 return FromFraction<1000000>(seconds);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020051 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010052 template <typename T>
Sebastian Jansson942b3602018-05-30 15:47:44 +020053 static TimeDelta ms(T milliseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010054 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010055 return FromFraction<1000>(milliseconds);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020056 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010057 template <typename T>
Sebastian Jansson942b3602018-05-30 15:47:44 +020058 static TimeDelta us(T microseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010059 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010060 return FromValue(microseconds);
Sebastian Jansson942b3602018-05-30 15:47:44 +020061 }
62 template <typename T = int64_t>
Sebastian Jansson72bba622018-11-19 11:17:12 +010063 T seconds() const {
64 return ToFraction<1000000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020065 }
66 template <typename T = int64_t>
Sebastian Jansson72bba622018-11-19 11:17:12 +010067 T ms() const {
68 return ToFraction<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020069 }
70 template <typename T = int64_t>
Sebastian Jansson72bba622018-11-19 11:17:12 +010071 T us() const {
72 return ToValue<T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020073 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010074 template <typename T = int64_t>
75 T ns() const {
76 return ToMultiple<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020077 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020078
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020079 constexpr int64_t seconds_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010080 return ToFractionOr<1000000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020081 }
82 constexpr int64_t ms_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010083 return ToFractionOr<1000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020084 }
85 constexpr int64_t us_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010086 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020087 }
88
Sebastian Jansson30bd4032018-04-13 13:56:17 +020089 TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020090
91 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +010092 friend class rtc_units_impl::UnitBase<TimeDelta>;
93 using RelativeUnit::RelativeUnit;
94 static constexpr bool one_sided = false;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020095};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020096
Sebastian Jansson72bba622018-11-19 11:17:12 +010097std::string ToString(TimeDelta value);
Sebastian Janssonb1138622019-04-11 16:48:15 +020098inline std::string ToLogString(TimeDelta value) {
99 return ToString(value);
100}
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200101
102#ifdef UNIT_TEST
103inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
104 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
105 TimeDelta value) {
106 return stream << ToString(value);
107}
108#endif // UNIT_TEST
109
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200110} // namespace webrtc
111
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200112#endif // API_UNITS_TIME_DELTA_H_