Add conversions to and from double for units.
Bug: webrtc:8415 Change-Id: I6b1f7afb163daa327e45c51f1a3fb7cafbb1444e Reviewed-on: https://webrtc-review.googlesource.com/78183 Commit-Queue: Sebastian Jansson <srte@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23451}
This commit is contained in:
committed by
Commit Bot
parent
f859e55d9b
commit
942b360d82
@ -17,6 +17,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/numerics/safe_conversions.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace timedelta_impl {
|
||||
@ -41,33 +42,107 @@ class TimeDelta {
|
||||
static TimeDelta MinusInfinity() {
|
||||
return TimeDelta(timedelta_impl::kMinusInfinityVal);
|
||||
}
|
||||
static TimeDelta seconds(int64_t seconds) {
|
||||
return TimeDelta::us(seconds * 1000000);
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
|
||||
static TimeDelta seconds(T seconds) {
|
||||
RTC_DCHECK_GT(seconds, timedelta_impl::kMinusInfinityVal / 1000000);
|
||||
RTC_DCHECK_LT(seconds, timedelta_impl::kPlusInfinityVal / 1000000);
|
||||
return TimeDelta(rtc::dchecked_cast<int64_t>(seconds) * 1000000);
|
||||
}
|
||||
static TimeDelta ms(int64_t milliseconds) {
|
||||
return TimeDelta::us(milliseconds * 1000);
|
||||
template <
|
||||
typename T,
|
||||
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
|
||||
static TimeDelta ms(T milliseconds) {
|
||||
RTC_DCHECK_GT(milliseconds, timedelta_impl::kMinusInfinityVal / 1000);
|
||||
RTC_DCHECK_LT(milliseconds, timedelta_impl::kPlusInfinityVal / 1000);
|
||||
return TimeDelta(rtc::dchecked_cast<int64_t>(milliseconds) * 1000);
|
||||
}
|
||||
static TimeDelta us(int64_t microseconds) {
|
||||
// Infinities only allowed via use of explicit constants.
|
||||
RTC_DCHECK(microseconds > std::numeric_limits<int64_t>::min());
|
||||
RTC_DCHECK(microseconds < std::numeric_limits<int64_t>::max());
|
||||
return TimeDelta(microseconds);
|
||||
}
|
||||
int64_t seconds() const {
|
||||
return (us() + (us() >= 0 ? 500000 : -500000)) / 1000000;
|
||||
}
|
||||
int64_t ms() const { return (us() + (us() >= 0 ? 500 : -500)) / 1000; }
|
||||
int64_t us() const {
|
||||
RTC_DCHECK(IsFinite());
|
||||
return microseconds_;
|
||||
}
|
||||
int64_t ns() const {
|
||||
RTC_DCHECK(us() > std::numeric_limits<int64_t>::min() / 1000);
|
||||
RTC_DCHECK(us() < std::numeric_limits<int64_t>::max() / 1000);
|
||||
return us() * 1000;
|
||||
template <
|
||||
typename T,
|
||||
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
|
||||
static TimeDelta us(T microseconds) {
|
||||
RTC_DCHECK_GT(microseconds, timedelta_impl::kMinusInfinityVal);
|
||||
RTC_DCHECK_LT(microseconds, timedelta_impl::kPlusInfinityVal);
|
||||
return TimeDelta(rtc::dchecked_cast<int64_t>(microseconds));
|
||||
}
|
||||
|
||||
double SecondsAsDouble() const;
|
||||
template <typename T,
|
||||
typename std::enable_if<std::is_floating_point<T>::value>::type* =
|
||||
nullptr>
|
||||
static TimeDelta seconds(T seconds) {
|
||||
return TimeDelta::us(seconds * 1e6);
|
||||
}
|
||||
template <typename T,
|
||||
typename std::enable_if<std::is_floating_point<T>::value>::type* =
|
||||
nullptr>
|
||||
static TimeDelta ms(T milliseconds) {
|
||||
return TimeDelta::us(milliseconds * 1e3);
|
||||
}
|
||||
template <typename T,
|
||||
typename std::enable_if<std::is_floating_point<T>::value>::type* =
|
||||
nullptr>
|
||||
static TimeDelta us(T microseconds) {
|
||||
if (microseconds == std::numeric_limits<T>::infinity()) {
|
||||
return PlusInfinity();
|
||||
} else if (microseconds == -std::numeric_limits<T>::infinity()) {
|
||||
return MinusInfinity();
|
||||
} else {
|
||||
RTC_DCHECK(!std::isnan(microseconds));
|
||||
RTC_DCHECK_GT(microseconds, timedelta_impl::kMinusInfinityVal);
|
||||
RTC_DCHECK_LT(microseconds, timedelta_impl::kPlusInfinityVal);
|
||||
return TimeDelta(rtc::dchecked_cast<int64_t>(microseconds));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T = int64_t>
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type seconds() const {
|
||||
return rtc::dchecked_cast<T>((us() + (us() >= 0 ? 500000 : -500000)) /
|
||||
1000000);
|
||||
}
|
||||
template <typename T = int64_t>
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type ms() const {
|
||||
return rtc::dchecked_cast<T>((us() + (us() >= 0 ? 500 : -500)) / 1000);
|
||||
}
|
||||
template <typename T = int64_t>
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type us() const {
|
||||
RTC_DCHECK(IsFinite());
|
||||
return rtc::dchecked_cast<T>(microseconds_);
|
||||
}
|
||||
template <typename T = int64_t>
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type ns() const {
|
||||
RTC_DCHECK_GE(us(), std::numeric_limits<T>::min() / 1000);
|
||||
RTC_DCHECK_LE(us(), std::numeric_limits<T>::max() / 1000);
|
||||
return rtc::dchecked_cast<T>(us() * 1000);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type seconds()
|
||||
const {
|
||||
return us<T>() * 1e-6;
|
||||
}
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type ms()
|
||||
const {
|
||||
return us<T>() * 1e-3;
|
||||
}
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type us()
|
||||
const {
|
||||
if (IsPlusInfinity()) {
|
||||
return std::numeric_limits<T>::infinity();
|
||||
} else if (IsMinusInfinity()) {
|
||||
return -std::numeric_limits<T>::infinity();
|
||||
} else {
|
||||
return microseconds_;
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type ns()
|
||||
const {
|
||||
return us<T>() * 1e3;
|
||||
}
|
||||
|
||||
TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); }
|
||||
bool IsZero() const { return microseconds_ == 0; }
|
||||
@ -96,7 +171,9 @@ class TimeDelta {
|
||||
microseconds_ += other.us();
|
||||
return *this;
|
||||
}
|
||||
|
||||
double operator/(const TimeDelta& other) const {
|
||||
return us<double>() / other.us<double>();
|
||||
}
|
||||
bool operator==(const TimeDelta& other) const {
|
||||
return microseconds_ == other.microseconds_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user