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

@ -44,6 +44,18 @@ class DataRate {
static constexpr DataRate Infinity() {
return DataRate(data_rate_impl::kPlusInfinityVal);
}
template <int64_t bps>
static constexpr DataRate BitsPerSec() {
static_assert(bps >= 0, "");
static_assert(bps < data_rate_impl::kPlusInfinityVal, "");
return DataRate(bps);
}
template <int64_t kbps>
static constexpr DataRate KilobitsPerSec() {
static_assert(kbps >= 0, "");
static_assert(kbps < data_rate_impl::kPlusInfinityVal / 1000, "");
return DataRate(kbps * 1000);
}
template <
typename T,
@ -89,49 +101,53 @@ class DataRate {
}
template <typename T = int64_t>
typename std::enable_if<std::is_integral<T>::value, T>::type kbps() const {
return rtc::dchecked_cast<T>((bps() + 500) / 1000);
RTC_DCHECK(IsFinite());
return rtc::dchecked_cast<T>(UnsafeKilobitsPerSec());
}
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type bps()
const {
if (IsInfinite()) {
return std::numeric_limits<T>::infinity();
} else {
return bits_per_sec_;
}
typename std::enable_if<std::is_floating_point<T>::value,
T>::type constexpr bps() const {
return IsInfinite() ? std::numeric_limits<T>::infinity() : bits_per_sec_;
}
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type kbps()
const {
typename std::enable_if<std::is_floating_point<T>::value,
T>::type constexpr kbps() const {
return bps<T>() * 1e-3;
}
constexpr int64_t bps_or(int64_t fallback_value) const {
return IsFinite() ? bits_per_sec_ : fallback_value;
}
constexpr int64_t kbps_or(int64_t fallback_value) const {
return IsFinite() ? UnsafeKilobitsPerSec() : fallback_value;
}
constexpr bool IsZero() const { return bits_per_sec_ == 0; }
constexpr bool IsInfinite() const {
return bits_per_sec_ == data_rate_impl::kPlusInfinityVal;
}
constexpr bool IsFinite() const { return !IsInfinite(); }
double operator/(const DataRate& other) const {
constexpr double operator/(const DataRate& other) const {
return bps<double>() / other.bps<double>();
}
bool operator==(const DataRate& other) const {
constexpr bool operator==(const DataRate& other) const {
return bits_per_sec_ == other.bits_per_sec_;
}
bool operator!=(const DataRate& other) const {
constexpr bool operator!=(const DataRate& other) const {
return bits_per_sec_ != other.bits_per_sec_;
}
bool operator<=(const DataRate& other) const {
constexpr bool operator<=(const DataRate& other) const {
return bits_per_sec_ <= other.bits_per_sec_;
}
bool operator>=(const DataRate& other) const {
constexpr bool operator>=(const DataRate& other) const {
return bits_per_sec_ >= other.bits_per_sec_;
}
bool operator>(const DataRate& other) const {
constexpr bool operator>(const DataRate& other) const {
return bits_per_sec_ > other.bits_per_sec_;
}
bool operator<(const DataRate& other) const {
constexpr bool operator<(const DataRate& other) const {
return bits_per_sec_ < other.bits_per_sec_;
}
@ -140,6 +156,9 @@ class DataRate {
// more recognizable.
explicit constexpr DataRate(int64_t bits_per_second)
: bits_per_sec_(bits_per_second) {}
constexpr int64_t UnsafeKilobitsPerSec() const {
return (bits_per_sec_ + 500) / 1000;
}
int64_t bits_per_sec_;
};