You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_UNITS_UNIT_BASE_H_
  11. #define RTC_BASE_UNITS_UNIT_BASE_H_
  12. #include <stdint.h>
  13. #include <algorithm>
  14. #include <cmath>
  15. #include <limits>
  16. #include <type_traits>
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/numerics/safe_conversions.h"
  19. namespace webrtc {
  20. namespace rtc_units_impl {
  21. // UnitBase is a base class for implementing custom value types with a specific
  22. // unit. It provides type safety and commonly useful operations. The underlying
  23. // storage is always an int64_t, it's up to the unit implementation to choose
  24. // what scale it represents.
  25. //
  26. // It's used like:
  27. // class MyUnit: public UnitBase<MyUnit> {...};
  28. //
  29. // Unit_T is the subclass representing the specific unit.
  30. template <class Unit_T>
  31. class UnitBase {
  32. public:
  33. UnitBase() = delete;
  34. static constexpr Unit_T Zero() { return Unit_T(0); }
  35. static constexpr Unit_T PlusInfinity() { return Unit_T(PlusInfinityVal()); }
  36. static constexpr Unit_T MinusInfinity() { return Unit_T(MinusInfinityVal()); }
  37. constexpr bool IsZero() const { return value_ == 0; }
  38. constexpr bool IsFinite() const { return !IsInfinite(); }
  39. constexpr bool IsInfinite() const {
  40. return value_ == PlusInfinityVal() || value_ == MinusInfinityVal();
  41. }
  42. constexpr bool IsPlusInfinity() const { return value_ == PlusInfinityVal(); }
  43. constexpr bool IsMinusInfinity() const {
  44. return value_ == MinusInfinityVal();
  45. }
  46. constexpr bool operator==(const Unit_T& other) const {
  47. return value_ == other.value_;
  48. }
  49. constexpr bool operator!=(const Unit_T& other) const {
  50. return value_ != other.value_;
  51. }
  52. constexpr bool operator<=(const Unit_T& other) const {
  53. return value_ <= other.value_;
  54. }
  55. constexpr bool operator>=(const Unit_T& other) const {
  56. return value_ >= other.value_;
  57. }
  58. constexpr bool operator>(const Unit_T& other) const {
  59. return value_ > other.value_;
  60. }
  61. constexpr bool operator<(const Unit_T& other) const {
  62. return value_ < other.value_;
  63. }
  64. Unit_T RoundTo(const Unit_T& resolution) const {
  65. RTC_DCHECK(IsFinite());
  66. RTC_DCHECK(resolution.IsFinite());
  67. RTC_DCHECK_GT(resolution.value_, 0);
  68. return Unit_T((value_ + resolution.value_ / 2) / resolution.value_) *
  69. resolution.value_;
  70. }
  71. Unit_T RoundUpTo(const Unit_T& resolution) const {
  72. RTC_DCHECK(IsFinite());
  73. RTC_DCHECK(resolution.IsFinite());
  74. RTC_DCHECK_GT(resolution.value_, 0);
  75. return Unit_T((value_ + resolution.value_ - 1) / resolution.value_) *
  76. resolution.value_;
  77. }
  78. Unit_T RoundDownTo(const Unit_T& resolution) const {
  79. RTC_DCHECK(IsFinite());
  80. RTC_DCHECK(resolution.IsFinite());
  81. RTC_DCHECK_GT(resolution.value_, 0);
  82. return Unit_T(value_ / resolution.value_) * resolution.value_;
  83. }
  84. protected:
  85. template <int64_t value>
  86. static constexpr Unit_T FromStaticValue() {
  87. static_assert(value >= 0 || !Unit_T::one_sided, "");
  88. static_assert(value > MinusInfinityVal(), "");
  89. static_assert(value < PlusInfinityVal(), "");
  90. return Unit_T(value);
  91. }
  92. template <int64_t fraction_value, int64_t Denominator>
  93. static constexpr Unit_T FromStaticFraction() {
  94. static_assert(fraction_value >= 0 || !Unit_T::one_sided, "");
  95. static_assert(fraction_value > MinusInfinityVal() / Denominator, "");
  96. static_assert(fraction_value < PlusInfinityVal() / Denominator, "");
  97. return Unit_T(fraction_value * Denominator);
  98. }
  99. template <
  100. typename T,
  101. typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
  102. static Unit_T FromValue(T value) {
  103. if (Unit_T::one_sided)
  104. RTC_DCHECK_GE(value, 0);
  105. RTC_DCHECK_GT(value, MinusInfinityVal());
  106. RTC_DCHECK_LT(value, PlusInfinityVal());
  107. return Unit_T(rtc::dchecked_cast<int64_t>(value));
  108. }
  109. template <typename T,
  110. typename std::enable_if<std::is_floating_point<T>::value>::type* =
  111. nullptr>
  112. static Unit_T FromValue(T value) {
  113. if (value == std::numeric_limits<T>::infinity()) {
  114. return PlusInfinity();
  115. } else if (value == -std::numeric_limits<T>::infinity()) {
  116. return MinusInfinity();
  117. } else {
  118. RTC_DCHECK(!std::isnan(value));
  119. return FromValue(rtc::dchecked_cast<int64_t>(value));
  120. }
  121. }
  122. template <
  123. int64_t Denominator,
  124. typename T,
  125. typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
  126. static Unit_T FromFraction(T value) {
  127. if (Unit_T::one_sided)
  128. RTC_DCHECK_GE(value, 0);
  129. RTC_DCHECK_GT(value, MinusInfinityVal() / Denominator);
  130. RTC_DCHECK_LT(value, PlusInfinityVal() / Denominator);
  131. return Unit_T(rtc::dchecked_cast<int64_t>(value * Denominator));
  132. }
  133. template <int64_t Denominator,
  134. typename T,
  135. typename std::enable_if<std::is_floating_point<T>::value>::type* =
  136. nullptr>
  137. static Unit_T FromFraction(T value) {
  138. return FromValue(value * Denominator);
  139. }
  140. template <typename T = int64_t>
  141. typename std::enable_if<std::is_integral<T>::value, T>::type ToValue() const {
  142. RTC_DCHECK(IsFinite());
  143. return rtc::dchecked_cast<T>(value_);
  144. }
  145. template <typename T>
  146. constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type
  147. ToValue() const {
  148. return IsPlusInfinity()
  149. ? std::numeric_limits<T>::infinity()
  150. : IsMinusInfinity() ? -std::numeric_limits<T>::infinity()
  151. : value_;
  152. }
  153. template <typename T>
  154. constexpr T ToValueOr(T fallback_value) const {
  155. return IsFinite() ? value_ : fallback_value;
  156. }
  157. template <int64_t Denominator, typename T = int64_t>
  158. typename std::enable_if<std::is_integral<T>::value, T>::type ToFraction()
  159. const {
  160. RTC_DCHECK(IsFinite());
  161. if (Unit_T::one_sided) {
  162. return rtc::dchecked_cast<T>(
  163. DivRoundPositiveToNearest(value_, Denominator));
  164. } else {
  165. return rtc::dchecked_cast<T>(DivRoundToNearest(value_, Denominator));
  166. }
  167. }
  168. template <int64_t Denominator, typename T>
  169. constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type
  170. ToFraction() const {
  171. return ToValue<T>() * (1 / static_cast<T>(Denominator));
  172. }
  173. template <int64_t Denominator>
  174. constexpr int64_t ToFractionOr(int64_t fallback_value) const {
  175. return IsFinite() ? Unit_T::one_sided
  176. ? DivRoundPositiveToNearest(value_, Denominator)
  177. : DivRoundToNearest(value_, Denominator)
  178. : fallback_value;
  179. }
  180. template <int64_t Factor, typename T = int64_t>
  181. typename std::enable_if<std::is_integral<T>::value, T>::type ToMultiple()
  182. const {
  183. RTC_DCHECK_GE(ToValue(), std::numeric_limits<T>::min() / Factor);
  184. RTC_DCHECK_LE(ToValue(), std::numeric_limits<T>::max() / Factor);
  185. return rtc::dchecked_cast<T>(ToValue() * Factor);
  186. }
  187. template <int64_t Factor, typename T>
  188. constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type
  189. ToMultiple() const {
  190. return ToValue<T>() * Factor;
  191. }
  192. explicit constexpr UnitBase(int64_t value) : value_(value) {}
  193. private:
  194. template <class RelativeUnit_T>
  195. friend class RelativeUnit;
  196. static inline constexpr int64_t PlusInfinityVal() {
  197. return std::numeric_limits<int64_t>::max();
  198. }
  199. static inline constexpr int64_t MinusInfinityVal() {
  200. return std::numeric_limits<int64_t>::min();
  201. }
  202. Unit_T& AsSubClassRef() { return reinterpret_cast<Unit_T&>(*this); }
  203. constexpr const Unit_T& AsSubClassRef() const {
  204. return reinterpret_cast<const Unit_T&>(*this);
  205. }
  206. // Assumes that n >= 0 and d > 0.
  207. static constexpr int64_t DivRoundPositiveToNearest(int64_t n, int64_t d) {
  208. return (n + d / 2) / d;
  209. }
  210. // Assumes that d > 0.
  211. static constexpr int64_t DivRoundToNearest(int64_t n, int64_t d) {
  212. return (n + (n >= 0 ? d / 2 : -d / 2)) / d;
  213. }
  214. int64_t value_;
  215. };
  216. // Extends UnitBase to provide operations for relative units, that is, units
  217. // that have a meaningful relation between values such that a += b is a
  218. // sensible thing to do. For a,b <- same unit.
  219. template <class Unit_T>
  220. class RelativeUnit : public UnitBase<Unit_T> {
  221. public:
  222. Unit_T Clamped(Unit_T min_value, Unit_T max_value) const {
  223. return std::max(min_value,
  224. std::min(UnitBase<Unit_T>::AsSubClassRef(), max_value));
  225. }
  226. void Clamp(Unit_T min_value, Unit_T max_value) {
  227. *this = Clamped(min_value, max_value);
  228. }
  229. Unit_T operator+(const Unit_T other) const {
  230. if (this->IsPlusInfinity() || other.IsPlusInfinity()) {
  231. RTC_DCHECK(!this->IsMinusInfinity());
  232. RTC_DCHECK(!other.IsMinusInfinity());
  233. return this->PlusInfinity();
  234. } else if (this->IsMinusInfinity() || other.IsMinusInfinity()) {
  235. RTC_DCHECK(!this->IsPlusInfinity());
  236. RTC_DCHECK(!other.IsPlusInfinity());
  237. return this->MinusInfinity();
  238. }
  239. return UnitBase<Unit_T>::FromValue(this->ToValue() + other.ToValue());
  240. }
  241. Unit_T operator-(const Unit_T other) const {
  242. if (this->IsPlusInfinity() || other.IsMinusInfinity()) {
  243. RTC_DCHECK(!this->IsMinusInfinity());
  244. RTC_DCHECK(!other.IsPlusInfinity());
  245. return this->PlusInfinity();
  246. } else if (this->IsMinusInfinity() || other.IsPlusInfinity()) {
  247. RTC_DCHECK(!this->IsPlusInfinity());
  248. RTC_DCHECK(!other.IsMinusInfinity());
  249. return this->MinusInfinity();
  250. }
  251. return UnitBase<Unit_T>::FromValue(this->ToValue() - other.ToValue());
  252. }
  253. Unit_T& operator+=(const Unit_T other) {
  254. *this = *this + other;
  255. return this->AsSubClassRef();
  256. }
  257. Unit_T& operator-=(const Unit_T other) {
  258. *this = *this - other;
  259. return this->AsSubClassRef();
  260. }
  261. constexpr double operator/(const Unit_T other) const {
  262. return UnitBase<Unit_T>::template ToValue<double>() /
  263. other.template ToValue<double>();
  264. }
  265. template <typename T>
  266. typename std::enable_if<std::is_arithmetic<T>::value, Unit_T>::type operator/(
  267. const T& scalar) const {
  268. return UnitBase<Unit_T>::FromValue(
  269. std::round(UnitBase<Unit_T>::template ToValue<int64_t>() / scalar));
  270. }
  271. Unit_T operator*(const double scalar) const {
  272. return UnitBase<Unit_T>::FromValue(std::round(this->ToValue() * scalar));
  273. }
  274. Unit_T operator*(const int64_t scalar) const {
  275. return UnitBase<Unit_T>::FromValue(this->ToValue() * scalar);
  276. }
  277. Unit_T operator*(const int32_t scalar) const {
  278. return UnitBase<Unit_T>::FromValue(this->ToValue() * scalar);
  279. }
  280. protected:
  281. using UnitBase<Unit_T>::UnitBase;
  282. };
  283. template <class Unit_T>
  284. inline Unit_T operator*(const double scalar, const RelativeUnit<Unit_T> other) {
  285. return other * scalar;
  286. }
  287. template <class Unit_T>
  288. inline Unit_T operator*(const int64_t scalar,
  289. const RelativeUnit<Unit_T> other) {
  290. return other * scalar;
  291. }
  292. template <class Unit_T>
  293. inline Unit_T operator*(const int32_t& scalar,
  294. const RelativeUnit<Unit_T> other) {
  295. return other * scalar;
  296. }
  297. } // namespace rtc_units_impl
  298. } // namespace webrtc
  299. #endif // RTC_BASE_UNITS_UNIT_BASE_H_