| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef API_UNITS_TIME_DELTA_H_ |
| 12 |
#define API_UNITS_TIME_DELTA_H_ |
| 13 |
|
| 14 |
#ifdef WEBRTC_UNIT_TEST |
| 15 |
#include <ostream> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <cstdlib> |
| 19 |
#include <string> |
| 20 |
#include <type_traits> |
| 21 |
|
| 22 |
#include "rtc_base/units/unit_base.h" |
| 23 |
|
| 24 |
namespace webrtc { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> { |
| 34 |
public: |
| 35 |
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 |
|
| 51 |
TimeDelta() = delete; |
| 52 |
|
| 53 |
template <typename T = int64_t> |
| 54 |
constexpr T seconds() const { |
| 55 |
return ToFraction<1000000, T>(); |
| 56 |
} |
| 57 |
template <typename T = int64_t> |
| 58 |
constexpr T ms() const { |
| 59 |
return ToFraction<1000, T>(); |
| 60 |
} |
| 61 |
template <typename T = int64_t> |
| 62 |
constexpr T us() const { |
| 63 |
return ToValue<T>(); |
| 64 |
} |
| 65 |
template <typename T = int64_t> |
| 66 |
constexpr T ns() const { |
| 67 |
return ToMultiple<1000, T>(); |
| 68 |
} |
| 69 |
|
| 70 |
constexpr int64_t seconds_or(int64_t fallback_value) const { |
| 71 |
return ToFractionOr<1000000>(fallback_value); |
| 72 |
} |
| 73 |
constexpr int64_t ms_or(int64_t fallback_value) const { |
| 74 |
return ToFractionOr<1000>(fallback_value); |
| 75 |
} |
| 76 |
constexpr int64_t us_or(int64_t fallback_value) const { |
| 77 |
return ToValueOr(fallback_value); |
| 78 |
} |
| 79 |
|
| 80 |
constexpr TimeDelta Abs() const { |
| 81 |
return us() < 0 ? TimeDelta::Micros(-us()) : *this; |
| 82 |
} |
| 83 |
|
| 84 |
private: |
| 85 |
friend class rtc_units_impl::UnitBase<TimeDelta>; |
| 86 |
using RelativeUnit::RelativeUnit; |
| 87 |
static constexpr bool one_sided = false; |
| 88 |
}; |
| 89 |
|
| 90 |
std::string ToString(TimeDelta value); |
| 91 |
inline std::string ToLogString(TimeDelta value) { |
| 92 |
return ToString(value); |
| 93 |
} |
| 94 |
|
| 95 |
#ifdef WEBRTC_UNIT_TEST |
| 96 |
inline std::ostream& operator<<( |
| 97 |
std::ostream& stream, |
| 98 |
TimeDelta value) { |
| 99 |
return stream << ToString(value); |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
#endif |