| 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 API_UNITS_FREQUENCY_H_ |
| 11 | #define API_UNITS_FREQUENCY_H_ |
| 12 |
| 13 | #ifdef WEBRTC_UNIT_TEST |
| 14 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
| 15 | #endif // WEBRTC_UNIT_TEST |
| 16 |
| 17 | #include <cstdlib> |
| 18 | #include <limits> |
| 19 | #include <string> |
| 20 | #include <type_traits> |
| 21 |
| 22 | #include "api/units/time_delta.h" |
| 23 | #include "rtc_base/units/unit_base.h" // IWYU pragma: export |
| 24 |
| 25 | namespace webrtc { |
| 26 |
| 27 | class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> { |
| 28 | public: |
| 29 | template <typename T> |
| 30 | static constexpr Frequency MilliHertz(T value) { |
| 31 | static_assert(std::is_arithmetic<T>::value, ""); |
| 32 | return FromValue(value); |
| 33 | } |
| 34 | template <typename T> |
| 35 | static constexpr Frequency Hertz(T value) { |
| 36 | static_assert(std::is_arithmetic<T>::value, ""); |
| 37 | return FromFraction(1'000, value); |
| 38 | } |
| 39 | template <typename T> |
| 40 | static constexpr Frequency KiloHertz(T value) { |
| 41 | static_assert(std::is_arithmetic<T>::value, ""); |
| 42 | return FromFraction(1'000'000, value); |
| 43 | } |
| 44 |
| 45 | Frequency() = delete; |
| 46 |
| 47 | template <typename T = int64_t> |
| 48 | constexpr T hertz() const { |
| 49 | return ToFraction<1000, T>(); |
| 50 | } |
| 51 | template <typename T = int64_t> |
| 52 | constexpr T millihertz() const { |
| 53 | return ToValue<T>(); |
| 54 | } |
| 55 |
| 56 | private: |
| 57 | friend class rtc_units_impl::UnitBase<Frequency>; |
| 58 | using RelativeUnit::RelativeUnit; |
| 59 | static constexpr bool one_sided = true; |
| 60 | }; |
| 61 |
| 62 | inline constexpr Frequency operator/(int64_t nominator, |
| 63 | const TimeDelta& interval) { |
| 64 | constexpr int64_t kKiloPerMicro = 1000 * 1000000; |
| 65 | RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kKiloPerMicro); |
| 66 | RTC_CHECK(interval.IsFinite()); |
| 67 | RTC_CHECK(!interval.IsZero()); |
| 68 | return Frequency::MilliHertz(nominator * kKiloPerMicro / interval.us()); |
| 69 | } |
| 70 |
| 71 | inline constexpr TimeDelta operator/(int64_t nominator, |
| 72 | const Frequency& frequency) { |
| 73 | constexpr int64_t kMegaPerMilli = 1000000 * 1000; |
| 74 | RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kMegaPerMilli); |
| 75 | RTC_CHECK(frequency.IsFinite()); |
| 76 | RTC_CHECK(!frequency.IsZero()); |
| 77 | return TimeDelta::Micros(nominator * kMegaPerMilli / frequency.millihertz()); |
| 78 | } |
| 79 |
| 80 | inline constexpr double operator*(Frequency frequency, TimeDelta time_delta) { |
| 81 | return frequency.hertz<double>() * time_delta.seconds<double>(); |
| 82 | } |
| 83 | inline constexpr double operator*(TimeDelta time_delta, Frequency frequency) { |
| 84 | return frequency * time_delta; |
| 85 | } |
| 86 |
| 87 | std::string ToString(Frequency value); |
| 88 | inline std::string ToLogString(Frequency value) { |
| 89 | return ToString(value); |
| 90 | } |
| 91 |
| 92 | #ifdef WEBRTC_UNIT_TEST |
| 93 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 94 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 95 | Frequency value) { |
| 96 | return stream << ToString(value); |
| 97 | } |
| 98 | #endif // WEBRTC_UNIT_TEST |
| 99 |
| 100 | } // namespace webrtc |
| 101 | #endif // API_UNITS_FREQUENCY_H_ |
| 102 |