Adds signed infinity to timestamps.
Negative infinity is useful in comparisons as it represents a value guaranteed to be less than any other timestamp. This avoid using Timestamp::ms(0) to represent such a value or having to use optional for the special case of first time execution. To avoid ambiguity, Timestamp::Infinity is renamed to PlusInfinity. Bug: webrtc:9709 Change-Id: Ib2debff91036e94d1dc903ec0c6faae30febde4e Reviewed-on: https://webrtc-review.googlesource.com/79440 Commit-Queue: Sebastian Jansson <srte@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#24556}
This commit is contained in:
committed by
Commit Bot
parent
376e1147e6
commit
9de4ef4b24
@ -36,9 +36,12 @@ constexpr int64_t kMinusInfinityVal = std::numeric_limits<int64_t>::min();
|
||||
class Timestamp {
|
||||
public:
|
||||
Timestamp() = delete;
|
||||
static constexpr Timestamp Infinity() {
|
||||
static constexpr Timestamp PlusInfinity() {
|
||||
return Timestamp(timestamp_impl::kPlusInfinityVal);
|
||||
}
|
||||
static constexpr Timestamp MinusInfinity() {
|
||||
return Timestamp(timestamp_impl::kMinusInfinityVal);
|
||||
}
|
||||
template <int64_t seconds>
|
||||
static constexpr Timestamp Seconds() {
|
||||
static_assert(seconds >= 0, "");
|
||||
@ -103,7 +106,9 @@ class Timestamp {
|
||||
nullptr>
|
||||
static Timestamp us(T microseconds) {
|
||||
if (microseconds == std::numeric_limits<double>::infinity()) {
|
||||
return Infinity();
|
||||
return PlusInfinity();
|
||||
} else if (microseconds == -std::numeric_limits<double>::infinity()) {
|
||||
return MinusInfinity();
|
||||
} else {
|
||||
RTC_DCHECK(!std::isnan(microseconds));
|
||||
RTC_DCHECK_GE(microseconds, 0);
|
||||
@ -141,7 +146,10 @@ class Timestamp {
|
||||
template <typename T>
|
||||
constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type
|
||||
us() const {
|
||||
return IsInfinite() ? std::numeric_limits<T>::infinity() : microseconds_;
|
||||
return IsPlusInfinity()
|
||||
? std::numeric_limits<T>::infinity()
|
||||
: IsMinusInfinity() ? -std::numeric_limits<T>::infinity()
|
||||
: microseconds_;
|
||||
}
|
||||
|
||||
constexpr int64_t seconds_or(int64_t fallback_value) const {
|
||||
@ -154,25 +162,53 @@ class Timestamp {
|
||||
return IsFinite() ? microseconds_ : fallback_value;
|
||||
}
|
||||
|
||||
constexpr bool IsInfinite() const {
|
||||
return microseconds_ == timestamp_impl::kPlusInfinityVal;
|
||||
}
|
||||
constexpr bool IsFinite() const { return !IsInfinite(); }
|
||||
constexpr bool IsInfinite() const {
|
||||
return microseconds_ == timedelta_impl::kPlusInfinityVal ||
|
||||
microseconds_ == timedelta_impl::kMinusInfinityVal;
|
||||
}
|
||||
constexpr bool IsPlusInfinity() const {
|
||||
return microseconds_ == timedelta_impl::kPlusInfinityVal;
|
||||
}
|
||||
constexpr bool IsMinusInfinity() const {
|
||||
return microseconds_ == timedelta_impl::kMinusInfinityVal;
|
||||
}
|
||||
Timestamp operator+(const TimeDelta& other) const {
|
||||
if (IsPlusInfinity() || other.IsPlusInfinity()) {
|
||||
RTC_DCHECK(!IsMinusInfinity());
|
||||
RTC_DCHECK(!other.IsMinusInfinity());
|
||||
return PlusInfinity();
|
||||
} else if (IsMinusInfinity() || other.IsMinusInfinity()) {
|
||||
RTC_DCHECK(!IsPlusInfinity());
|
||||
RTC_DCHECK(!other.IsPlusInfinity());
|
||||
return MinusInfinity();
|
||||
}
|
||||
return Timestamp::us(us() + other.us());
|
||||
}
|
||||
Timestamp operator-(const TimeDelta& other) const {
|
||||
if (IsPlusInfinity() || other.IsMinusInfinity()) {
|
||||
RTC_DCHECK(!IsMinusInfinity());
|
||||
RTC_DCHECK(!other.IsPlusInfinity());
|
||||
return PlusInfinity();
|
||||
} else if (IsMinusInfinity() || other.IsPlusInfinity()) {
|
||||
RTC_DCHECK(!IsPlusInfinity());
|
||||
RTC_DCHECK(!other.IsMinusInfinity());
|
||||
return MinusInfinity();
|
||||
}
|
||||
return Timestamp::us(us() - other.us());
|
||||
}
|
||||
TimeDelta operator-(const Timestamp& other) const {
|
||||
if (IsPlusInfinity() || other.IsMinusInfinity()) {
|
||||
RTC_DCHECK(!IsMinusInfinity());
|
||||
RTC_DCHECK(!other.IsPlusInfinity());
|
||||
return TimeDelta::PlusInfinity();
|
||||
} else if (IsMinusInfinity() || other.IsPlusInfinity()) {
|
||||
RTC_DCHECK(!IsPlusInfinity());
|
||||
RTC_DCHECK(!other.IsMinusInfinity());
|
||||
return TimeDelta::MinusInfinity();
|
||||
}
|
||||
return TimeDelta::us(us() - other.us());
|
||||
}
|
||||
Timestamp operator-(const TimeDelta& delta) const {
|
||||
RTC_DCHECK(!delta.IsPlusInfinity());
|
||||
if (IsInfinite() || delta.IsMinusInfinity())
|
||||
return Infinity();
|
||||
return Timestamp::us(us() - delta.us());
|
||||
}
|
||||
Timestamp operator+(const TimeDelta& delta) const {
|
||||
RTC_DCHECK(!delta.IsMinusInfinity());
|
||||
if (IsInfinite() || delta.IsPlusInfinity())
|
||||
return Infinity();
|
||||
return Timestamp::us(us() + delta.us());
|
||||
}
|
||||
Timestamp& operator-=(const TimeDelta& other) {
|
||||
*this = *this - other;
|
||||
return *this;
|
||||
|
||||
Reference in New Issue
Block a user