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 | |
| 13 | #ifdef UNIT_TEST |
| 14 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
| 15 | #endif // 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" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> { |
| 28 | public: |
| 29 | Frequency() = delete; |
| 30 | template <int64_t hertz> |
| 31 | static constexpr Frequency Hertz() { |
| 32 | return FromStaticFraction<hertz, 1000>(); |
| 33 | } |
| 34 | template <typename T> |
| 35 | static Frequency hertz(T hertz) { |
| 36 | static_assert(std::is_arithmetic<T>::value, ""); |
| 37 | return FromFraction<1000>(hertz); |
| 38 | } |
| 39 | template <typename T> |
| 40 | static Frequency millihertz(T hertz) { |
| 41 | static_assert(std::is_arithmetic<T>::value, ""); |
| 42 | return FromValue(hertz); |
| 43 | } |
| 44 | template <typename T = int64_t> |
| 45 | T hertz() const { |
| 46 | return ToFraction<1000, T>(); |
| 47 | } |
| 48 | template <typename T = int64_t> |
| 49 | T millihertz() const { |
| 50 | return ToValue<T>(); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | friend class rtc_units_impl::UnitBase<Frequency>; |
| 55 | using RelativeUnit::RelativeUnit; |
| 56 | static constexpr bool one_sided = true; |
| 57 | }; |
| 58 | |
| 59 | inline Frequency operator/(int64_t nominator, const TimeDelta& interval) { |
| 60 | constexpr int64_t kKiloPerMicro = 1000 * 1000000; |
| 61 | RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kKiloPerMicro); |
| 62 | RTC_CHECK(interval.IsFinite()); |
| 63 | RTC_CHECK(!interval.IsZero()); |
| 64 | return Frequency::millihertz(nominator * kKiloPerMicro / interval.us()); |
| 65 | } |
| 66 | |
| 67 | inline TimeDelta operator/(int64_t nominator, const Frequency& frequency) { |
| 68 | constexpr int64_t kMegaPerMilli = 1000000 * 1000; |
| 69 | RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kMegaPerMilli); |
| 70 | RTC_CHECK(frequency.IsFinite()); |
| 71 | RTC_CHECK(!frequency.IsZero()); |
| 72 | return TimeDelta::us(nominator * kMegaPerMilli / frequency.millihertz()); |
| 73 | } |
| 74 | |
| 75 | std::string ToString(Frequency value); |
| 76 | inline std::string ToLogString(Frequency value) { |
| 77 | return ToString(value); |
| 78 | } |
| 79 | |
| 80 | #ifdef UNIT_TEST |
| 81 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 82 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 83 | Frequency value) { |
| 84 | return stream << ToString(value); |
| 85 | } |
| 86 | #endif // UNIT_TEST |
| 87 | |
| 88 | } // namespace webrtc |
| 89 | #endif // API_UNITS_FREQUENCY_H_ |