Remove unit_base functions FromStaticX

instead make functions FromX constexpr and use them.

Bug: None
Change-Id: I826c8ad5ac8b3bd97f298a99c40b31b8c63b5f85
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/159220
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30321}
This commit is contained in:
Danil Chapovalov
2020-01-20 13:02:44 +01:00
committed by Commit Bot
parent fae6f0e87b
commit 7356a5666d
7 changed files with 30 additions and 53 deletions

View File

@ -35,11 +35,11 @@ class DataRate final : public rtc_units_impl::RelativeUnit<DataRate> {
static constexpr DataRate Infinity() { return PlusInfinity(); }
template <int64_t bps>
static constexpr DataRate BitsPerSec() {
return FromStaticValue<bps>();
return FromValue(bps);
}
template <int64_t kbps>
static constexpr DataRate KilobitsPerSec() {
return FromStaticFraction<kbps, 1000>();
return FromFraction(1000, kbps);
}
template <typename T>
static constexpr DataRate bps(T bits_per_second) {
@ -49,12 +49,12 @@ class DataRate final : public rtc_units_impl::RelativeUnit<DataRate> {
template <typename T>
static constexpr DataRate bytes_per_sec(T bytes_per_second) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<8>(bytes_per_second);
return FromFraction(8, bytes_per_second);
}
template <typename T>
static constexpr DataRate kbps(T kilobits_per_sec) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000>(kilobits_per_sec);
return FromFraction(1000, kilobits_per_sec);
}
template <typename T = int64_t>
constexpr T bps() const {

View File

@ -28,7 +28,7 @@ class DataSize final : public rtc_units_impl::RelativeUnit<DataSize> {
static constexpr DataSize Infinity() { return PlusInfinity(); }
template <int64_t bytes>
static constexpr DataSize Bytes() {
return FromStaticValue<bytes>();
return FromValue(bytes);
}
template <typename T>

View File

@ -29,12 +29,12 @@ class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> {
Frequency() = delete;
template <int64_t hertz>
static constexpr Frequency Hertz() {
return FromStaticFraction<hertz, 1000>();
return FromFraction(1000, hertz);
}
template <typename T>
static Frequency hertz(T hertz) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000>(hertz);
return FromFraction(1000, hertz);
}
template <typename T>
static Frequency millihertz(T hertz) {

View File

@ -35,25 +35,25 @@ class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
TimeDelta() = delete;
template <int64_t seconds>
static constexpr TimeDelta Seconds() {
return FromStaticFraction<seconds, 1000000>();
return FromFraction(1'000'000, seconds);
}
template <int64_t ms>
static constexpr TimeDelta Millis() {
return FromStaticFraction<ms, 1000>();
return FromFraction(1000, ms);
}
template <int64_t us>
static constexpr TimeDelta Micros() {
return FromStaticValue<us>();
return FromValue(us);
}
template <typename T>
static TimeDelta seconds(T seconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000000>(seconds);
return FromFraction(1'000'000, seconds);
}
template <typename T>
static TimeDelta ms(T milliseconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000>(milliseconds);
return FromFraction(1000, milliseconds);
}
template <typename T>
static TimeDelta us(T microseconds) {

View File

@ -32,26 +32,26 @@ class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> {
template <int64_t seconds>
static constexpr Timestamp Seconds() {
return FromStaticFraction<seconds, 1000000>();
return FromFraction(1'000'000, seconds);
}
template <int64_t ms>
static constexpr Timestamp Millis() {
return FromStaticFraction<ms, 1000>();
return FromFraction(1000, ms);
}
template <int64_t us>
static constexpr Timestamp Micros() {
return FromStaticValue<us>();
return FromValue(us);
}
template <typename T>
static Timestamp seconds(T seconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000000>(seconds);
return FromFraction(1'000'000, seconds);
}
template <typename T>
static Timestamp ms(T milliseconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000>(milliseconds);
return FromFraction(1000, milliseconds);
}
template <typename T>
static Timestamp us(T microseconds) {

View File

@ -90,26 +90,10 @@ class UnitBase {
}
protected:
template <int64_t value>
static constexpr Unit_T FromStaticValue() {
static_assert(value >= 0 || !Unit_T::one_sided, "");
static_assert(value > MinusInfinityVal(), "");
static_assert(value < PlusInfinityVal(), "");
return Unit_T(value);
}
template <int64_t fraction_value, int64_t Denominator>
static constexpr Unit_T FromStaticFraction() {
static_assert(fraction_value >= 0 || !Unit_T::one_sided, "");
static_assert(fraction_value > MinusInfinityVal() / Denominator, "");
static_assert(fraction_value < PlusInfinityVal() / Denominator, "");
return Unit_T(fraction_value * Denominator);
}
template <
typename T,
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
static Unit_T FromValue(T value) {
static constexpr Unit_T FromValue(T value) {
if (Unit_T::one_sided)
RTC_DCHECK_GE(value, 0);
RTC_DCHECK_GT(value, MinusInfinityVal());
@ -119,7 +103,7 @@ class UnitBase {
template <typename T,
typename std::enable_if<std::is_floating_point<T>::value>::type* =
nullptr>
static Unit_T FromValue(T value) {
static constexpr Unit_T FromValue(T value) {
if (value == std::numeric_limits<T>::infinity()) {
return PlusInfinity();
} else if (value == -std::numeric_limits<T>::infinity()) {
@ -131,22 +115,20 @@ class UnitBase {
}
template <
int64_t Denominator,
typename T,
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
static Unit_T FromFraction(T value) {
static constexpr Unit_T FromFraction(int64_t denominator, T value) {
if (Unit_T::one_sided)
RTC_DCHECK_GE(value, 0);
RTC_DCHECK_GT(value, MinusInfinityVal() / Denominator);
RTC_DCHECK_LT(value, PlusInfinityVal() / Denominator);
return Unit_T(rtc::dchecked_cast<int64_t>(value * Denominator));
RTC_DCHECK_GT(value, MinusInfinityVal() / denominator);
RTC_DCHECK_LT(value, PlusInfinityVal() / denominator);
return Unit_T(rtc::dchecked_cast<int64_t>(value * denominator));
}
template <int64_t Denominator,
typename T,
template <typename T,
typename std::enable_if<std::is_floating_point<T>::value>::type* =
nullptr>
static Unit_T FromFraction(T value) {
return FromValue(value * Denominator);
static constexpr Unit_T FromFraction(int64_t denominator, T value) {
return FromValue(value * denominator);
}
template <typename T = int64_t>

View File

@ -18,18 +18,13 @@ class TestUnit final : public rtc_units_impl::RelativeUnit<TestUnit> {
public:
TestUnit() = delete;
using UnitBase::FromStaticValue;
using UnitBase::FromValue;
using UnitBase::ToValue;
using UnitBase::ToValueOr;
template <int64_t kilo>
static constexpr TestUnit FromStaticKilo() {
return FromStaticFraction<kilo, 1000>();
}
template <typename T>
static TestUnit FromKilo(T kilo) {
return FromFraction<1000>(kilo);
static constexpr TestUnit FromKilo(T kilo) {
return FromFraction(1000, kilo);
}
template <typename T = int64_t>
T ToKilo() const {
@ -62,8 +57,8 @@ TEST(UnitBaseTest, ConstExpr) {
static_assert(kTestUnitPlusInf > kTestUnitZero, "");
constexpr TestUnit kTestUnitKilo = TestUnit::FromStaticKilo<kValue>();
constexpr TestUnit kTestUnitValue = TestUnit::FromStaticValue<kValue>();
constexpr TestUnit kTestUnitKilo = TestUnit::FromKilo(kValue);
constexpr TestUnit kTestUnitValue = TestUnit::FromValue(kValue);
static_assert(kTestUnitKilo.ToKiloOr(0) == kValue, "");
static_assert(kTestUnitValue.ToValueOr(0) == kValue, "");