Skip to content

Commit 1cf15bf

Browse files
jonexCommit Bot
authored andcommitted
Adds product operator for TimeDelta and Frequency
Also adding kHz factory function for Frequency class. Bug: webrtc:9883 Change-Id: Ide44910d50eb9616de2bb0c66b8c62493d2be92e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/167725 Commit-Queue: Sebastian Jansson <[email protected]> Reviewed-by: Ali Tofigh <[email protected]> Cr-Commit-Position: refs/heads/master@{#30416}
1 parent 184ea66 commit 1cf15bf

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

api/units/frequency.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> {
3232
return FromFraction(1000, hertz);
3333
}
3434
template <typename T>
35+
static constexpr Frequency kHz(T hertz) {
36+
static_assert(std::is_arithmetic<T>::value, "");
37+
return FromFraction(1000000, hertz);
38+
}
39+
template <typename T>
3540
static constexpr Frequency hertz(T hertz) {
3641
static_assert(std::is_arithmetic<T>::value, "");
3742
return FromFraction(1000, hertz);
@@ -74,6 +79,13 @@ inline constexpr TimeDelta operator/(int64_t nominator,
7479
return TimeDelta::us(nominator * kMegaPerMilli / frequency.millihertz());
7580
}
7681

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+
7789
std::string ToString(Frequency value);
7890
inline std::string ToLogString(Frequency value) {
7991
return ToString(value);

api/units/frequency_unittest.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ TEST(FrequencyTest, InfinityOperations) {
154154
TEST(UnitConversionTest, TimeDeltaAndFrequency) {
155155
EXPECT_EQ(1 / Frequency::hertz(50), TimeDelta::ms(20));
156156
EXPECT_EQ(1 / TimeDelta::ms(20), Frequency::hertz(50));
157+
EXPECT_EQ(Frequency::kHz(200) * TimeDelta::ms(2), 400.0);
157158
}
158159
} // namespace test
159160
} // namespace webrtc

0 commit comments

Comments
 (0)