Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 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 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 13 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 14 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 15 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 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" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> { |
| 28 | public: |
Danil Chapovalov | 2517a47 | 2020-02-14 13:52:46 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 45 | Frequency() = delete; |
Danil Chapovalov | 2517a47 | 2020-02-14 13:52:46 +0100 | [diff] [blame] | 46 | |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 47 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 48 | constexpr T hertz() const { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 49 | return ToFraction<1000, T>(); |
| 50 | } |
| 51 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 52 | constexpr T millihertz() const { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 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 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 62 | inline constexpr Frequency operator/(int64_t nominator, |
| 63 | const TimeDelta& interval) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 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()); |
Danil Chapovalov | 2517a47 | 2020-02-14 13:52:46 +0100 | [diff] [blame] | 68 | return Frequency::MilliHertz(nominator * kKiloPerMicro / interval.us()); |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 71 | inline constexpr TimeDelta operator/(int64_t nominator, |
| 72 | const Frequency& frequency) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 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()); |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 77 | return TimeDelta::Micros(nominator * kMegaPerMilli / frequency.millihertz()); |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Sebastian Jansson | 1cf15bf | 2020-01-29 10:56:25 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 87 | std::string ToString(Frequency value); |
| 88 | inline std::string ToLogString(Frequency value) { |
| 89 | return ToString(value); |
| 90 | } |
| 91 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 92 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 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 | } |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 98 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 99 | |
| 100 | } // namespace webrtc |
| 101 | #endif // API_UNITS_FREQUENCY_H_ |