/* * Copyright 2018 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #ifndef RTC_BASE_UNITS_UNIT_BASE_H_ #define RTC_BASE_UNITS_UNIT_BASE_H_ #include #include #include #include #include #include "rtc_base/checks.h" #include "rtc_base/numerics/divide_round.h" #include "rtc_base/numerics/safe_conversions.h" namespace webrtc { namespace rtc_units_impl { // UnitBase is a base class for implementing custom value types with a specific // unit. It provides type safety and commonly useful operations. The underlying // storage is always an int64_t, it's up to the unit implementation to choose // what scale it represents. // // It's used like: // class MyUnit: public UnitBase {...}; // // Unit_T is the subclass representing the specific unit. template class UnitBase { public: UnitBase() = delete; static constexpr Unit_T Zero() { return Unit_T(0); } static constexpr Unit_T PlusInfinity() { return Unit_T(PlusInfinityVal()); } static constexpr Unit_T MinusInfinity() { return Unit_T(MinusInfinityVal()); } constexpr bool IsZero() const { return value_ == 0; } constexpr bool IsFinite() const { return !IsInfinite(); } constexpr bool IsInfinite() const { return value_ == PlusInfinityVal() || value_ == MinusInfinityVal(); } constexpr bool IsPlusInfinity() const { return value_ == PlusInfinityVal(); } constexpr bool IsMinusInfinity() const { return value_ == MinusInfinityVal(); } constexpr bool operator==(const UnitBase& other) const { return value_ == other.value_; } constexpr bool operator!=(const UnitBase& other) const { return value_ != other.value_; } constexpr bool operator<=(const UnitBase& other) const { return value_ <= other.value_; } constexpr bool operator>=(const UnitBase& other) const { return value_ >= other.value_; } constexpr bool operator>(const UnitBase& other) const { return value_ > other.value_; } constexpr bool operator<(const UnitBase& other) const { return value_ < other.value_; } constexpr Unit_T RoundTo(const Unit_T& resolution) const { RTC_DCHECK(IsFinite()); RTC_DCHECK(resolution.IsFinite()); RTC_DCHECK_GT(resolution.value_, 0); return Unit_T((value_ + resolution.value_ / 2) / resolution.value_) * resolution.value_; } constexpr Unit_T RoundUpTo(const Unit_T& resolution) const { RTC_DCHECK(IsFinite()); RTC_DCHECK(resolution.IsFinite()); RTC_DCHECK_GT(resolution.value_, 0); return Unit_T((value_ + resolution.value_ - 1) / resolution.value_) * resolution.value_; } constexpr Unit_T RoundDownTo(const Unit_T& resolution) const { RTC_DCHECK(IsFinite()); RTC_DCHECK(resolution.IsFinite()); RTC_DCHECK_GT(resolution.value_, 0); return Unit_T(value_ / resolution.value_) * resolution.value_; } protected: template < typename T, typename std::enable_if::value>::type* = nullptr> static constexpr Unit_T FromValue(T value) { if (Unit_T::one_sided) RTC_DCHECK_GE(value, 0); RTC_DCHECK_GT(value, MinusInfinityVal()); RTC_DCHECK_LT(value, PlusInfinityVal()); return Unit_T(rtc::dchecked_cast(value)); } template ::value>::type* = nullptr> static constexpr Unit_T FromValue(T value) { if (value == std::numeric_limits::infinity()) { return PlusInfinity(); } else if (value == -std::numeric_limits::infinity()) { return MinusInfinity(); } else { return FromValue(rtc::dchecked_cast(value)); } } template < typename T, typename std::enable_if::value>::type* = nullptr> 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(value * denominator)); } template ::value>::type* = nullptr> static constexpr Unit_T FromFraction(int64_t denominator, T value) { return FromValue(value * denominator); } template constexpr typename std::enable_if::value, T>::type ToValue() const { RTC_DCHECK(IsFinite()); return rtc::dchecked_cast(value_); } template constexpr typename std::enable_if::value, T>::type ToValue() const { return IsPlusInfinity() ? std::numeric_limits::infinity() : IsMinusInfinity() ? -std::numeric_limits::infinity() : value_; } template constexpr T ToValueOr(T fallback_value) const { return IsFinite() ? value_ : fallback_value; } template constexpr typename std::enable_if::value, T>::type ToFraction() const { RTC_DCHECK(IsFinite()); return rtc::dchecked_cast(DivideRoundToNearest(value_, Denominator)); } template constexpr typename std::enable_if::value, T>::type ToFraction() const { return ToValue() * (1 / static_cast(Denominator)); } template constexpr int64_t ToFractionOr(int64_t fallback_value) const { return IsFinite() ? DivideRoundToNearest(value_, Denominator) : fallback_value; } template constexpr typename std::enable_if::value, T>::type ToMultiple() const { RTC_DCHECK_GE(ToValue(), std::numeric_limits::min() / Factor); RTC_DCHECK_LE(ToValue(), std::numeric_limits::max() / Factor); return rtc::dchecked_cast(ToValue() * Factor); } template constexpr typename std::enable_if::value, T>::type ToMultiple() const { return ToValue() * Factor; } explicit constexpr UnitBase(int64_t value) : value_(value) {} private: template friend class RelativeUnit; static inline constexpr int64_t PlusInfinityVal() { return std::numeric_limits::max(); } static inline constexpr int64_t MinusInfinityVal() { return std::numeric_limits::min(); } constexpr Unit_T& AsSubClassRef() { return static_cast(*this); } constexpr const Unit_T& AsSubClassRef() const { return static_cast(*this); } int64_t value_; }; // Extends UnitBase to provide operations for relative units, that is, units // that have a meaningful relation between values such that a += b is a // sensible thing to do. For a,b <- same unit. template class RelativeUnit : public UnitBase { public: constexpr Unit_T Clamped(Unit_T min_value, Unit_T max_value) const { return std::max(min_value, std::min(UnitBase::AsSubClassRef(), max_value)); } constexpr void Clamp(Unit_T min_value, Unit_T max_value) { *this = Clamped(min_value, max_value); } constexpr Unit_T operator+(const Unit_T other) const { if (this->IsPlusInfinity() || other.IsPlusInfinity()) { RTC_DCHECK(!this->IsMinusInfinity()); RTC_DCHECK(!other.IsMinusInfinity()); return this->PlusInfinity(); } else if (this->IsMinusInfinity() || other.IsMinusInfinity()) { RTC_DCHECK(!this->IsPlusInfinity()); RTC_DCHECK(!other.IsPlusInfinity()); return this->MinusInfinity(); } return UnitBase::FromValue(this->ToValue() + other.ToValue()); } constexpr Unit_T operator-(const Unit_T other) const { if (this->IsPlusInfinity() || other.IsMinusInfinity()) { RTC_DCHECK(!this->IsMinusInfinity()); RTC_DCHECK(!other.IsPlusInfinity()); return this->PlusInfinity(); } else if (this->IsMinusInfinity() || other.IsPlusInfinity()) { RTC_DCHECK(!this->IsPlusInfinity()); RTC_DCHECK(!other.IsMinusInfinity()); return this->MinusInfinity(); } return UnitBase::FromValue(this->ToValue() - other.ToValue()); } constexpr Unit_T& operator+=(const Unit_T other) { *this = *this + other; return this->AsSubClassRef(); } constexpr Unit_T& operator-=(const Unit_T other) { *this = *this - other; return this->AsSubClassRef(); } constexpr double operator/(const Unit_T other) const { return UnitBase::template ToValue() / other.template ToValue(); } template >* = nullptr> constexpr Unit_T operator/(T scalar) const { return UnitBase::FromValue(std::llround(this->ToValue() / scalar)); } template >* = nullptr> constexpr Unit_T operator/(T scalar) const { return UnitBase::FromValue(this->ToValue() / scalar); } constexpr Unit_T operator*(double scalar) const { return UnitBase::FromValue(std::llround(this->ToValue() * scalar)); } constexpr Unit_T operator*(int64_t scalar) const { return UnitBase::FromValue(this->ToValue() * scalar); } constexpr Unit_T operator*(int32_t scalar) const { return UnitBase::FromValue(this->ToValue() * scalar); } constexpr Unit_T operator*(size_t scalar) const { return UnitBase::FromValue(this->ToValue() * scalar); } protected: using UnitBase::UnitBase; }; template inline constexpr Unit_T operator*(double scalar, RelativeUnit other) { return other * scalar; } template inline constexpr Unit_T operator*(int64_t scalar, RelativeUnit other) { return other * scalar; } template inline constexpr Unit_T operator*(int32_t scalar, RelativeUnit other) { return other * scalar; } template inline constexpr Unit_T operator*(size_t scalar, RelativeUnit other) { return other * scalar; } template inline constexpr Unit_T operator-(RelativeUnit other) { if (other.IsPlusInfinity()) return UnitBase::MinusInfinity(); if (other.IsMinusInfinity()) return UnitBase::PlusInfinity(); return -1 * other; } } // namespace rtc_units_impl } // namespace webrtc #endif // RTC_BASE_UNITS_UNIT_BASE_H_