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