Adds constexpr create functions for units.

This adds new constexpr create function for DataSize, DataRate,
TimeDelta and Timestamp. The names are capitalized to mirror the
naming scheme of the previously constexpr methods (Zero and
Infinity create functions). They are also kept longer since they
are not expected to be used in complex expressions.

Bug: webrtc:9574
Change-Id: I5950548718675050fc5d66699de295455c310861
Reviewed-on: https://webrtc-review.googlesource.com/91161
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24218}
This commit is contained in:
Sebastian Jansson
2018-08-07 15:29:04 +02:00
committed by Commit Bot
parent 133cff009b
commit c1c8b8e836
8 changed files with 213 additions and 76 deletions

View File

@ -35,6 +35,24 @@ class Timestamp {
static constexpr Timestamp Infinity() {
return Timestamp(timestamp_impl::kPlusInfinityVal);
}
template <int64_t seconds>
static constexpr Timestamp Seconds() {
static_assert(seconds >= 0, "");
static_assert(seconds < timestamp_impl::kPlusInfinityVal / 1000000, "");
return Timestamp(seconds * 1000000);
}
template <int64_t ms>
static constexpr Timestamp Millis() {
static_assert(ms >= 0, "");
static_assert(ms < timestamp_impl::kPlusInfinityVal / 1000, "");
return Timestamp(ms * 1000);
}
template <int64_t us>
static constexpr Timestamp Micros() {
static_assert(us >= 0, "");
static_assert(us < timestamp_impl::kPlusInfinityVal, "");
return Timestamp(us);
}
template <
typename T,
@ -92,11 +110,13 @@ class Timestamp {
template <typename T = int64_t>
typename std::enable_if<std::is_integral<T>::value, T>::type seconds() const {
return rtc::dchecked_cast<T>((us() + 500000) / 1000000);
RTC_DCHECK(IsFinite());
return rtc::dchecked_cast<T>(UnsafeSeconds());
}
template <typename T = int64_t>
typename std::enable_if<std::is_integral<T>::value, T>::type ms() const {
return rtc::dchecked_cast<T>((us() + 500) / 1000);
RTC_DCHECK(IsFinite());
return rtc::dchecked_cast<T>(UnsafeMillis());
}
template <typename T = int64_t>
typename std::enable_if<std::is_integral<T>::value, T>::type us() const {
@ -105,23 +125,29 @@ class Timestamp {
}
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type seconds()
const {
constexpr 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 {
constexpr 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 (IsInfinite()) {
return std::numeric_limits<T>::infinity();
} else {
return microseconds_;
}
constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type
us() const {
return IsInfinite() ? std::numeric_limits<T>::infinity() : microseconds_;
}
constexpr int64_t seconds_or(int64_t fallback_value) const {
return IsFinite() ? UnsafeSeconds() : fallback_value;
}
constexpr int64_t ms_or(int64_t fallback_value) const {
return IsFinite() ? UnsafeMillis() : fallback_value;
}
constexpr int64_t us_or(int64_t fallback_value) const {
return IsFinite() ? microseconds_ : fallback_value;
}
constexpr bool IsInfinite() const {
@ -145,27 +171,33 @@ class Timestamp {
microseconds_ += other.us();
return *this;
}
bool operator==(const Timestamp& other) const {
constexpr bool operator==(const Timestamp& other) const {
return microseconds_ == other.microseconds_;
}
bool operator!=(const Timestamp& other) const {
constexpr bool operator!=(const Timestamp& other) const {
return microseconds_ != other.microseconds_;
}
bool operator<=(const Timestamp& other) const {
constexpr bool operator<=(const Timestamp& other) const {
return microseconds_ <= other.microseconds_;
}
bool operator>=(const Timestamp& other) const {
constexpr bool operator>=(const Timestamp& other) const {
return microseconds_ >= other.microseconds_;
}
bool operator>(const Timestamp& other) const {
constexpr bool operator>(const Timestamp& other) const {
return microseconds_ > other.microseconds_;
}
bool operator<(const Timestamp& other) const {
constexpr bool operator<(const Timestamp& other) const {
return microseconds_ < other.microseconds_;
}
private:
explicit constexpr Timestamp(int64_t us) : microseconds_(us) {}
constexpr int64_t UnsafeSeconds() const {
return (microseconds_ + 500000) / 1000000;
}
constexpr int64_t UnsafeMillis() const {
return (microseconds_ + 500) / 1000;
}
int64_t microseconds_;
};