Frequency.cpp
Go to the documentation of this file.
1 #include "Frequency.h"
2 
3 #include <iomanip>
4 #include <limits>
5 #include <sstream>
6 
7 #include <SimoxUtility/algorithm/string/string_tools.h>
8 
10 
11 namespace armarx::core::time
12 {
13 
14  Frequency::Frequency(const Duration& cycleDuration) : _cycleDuration{cycleDuration}
15  {
16  ;
17  }
18 
19  Frequency
20  Frequency::Hertz(const std::int64_t hertz)
21  {
22  if (hertz == 0)
23  {
25  }
26  return Frequency(Duration::Seconds(1) / hertz);
27  }
28 
29  Frequency
30  Frequency::HertzDouble(double hertz)
31  {
32  if (hertz == 0)
33  {
35  }
36  return Frequency(Duration::Seconds(1) / hertz);
37  }
38 
39  std::int64_t
41  {
42  if (_cycleDuration.isZero())
43  {
45  }
46  return static_cast<std::int64_t>(toHertzDouble() + 0.5);
47  }
48 
49  double
51  {
52  if (_cycleDuration.isZero())
53  {
54  return std::numeric_limits<double>::infinity();
55  }
56  return 1 / _cycleDuration.toSecondsDouble();
57  }
58 
59  Duration
61  {
62  return _cycleDuration;
63  }
64 
65  std::string
67  {
68  if (_cycleDuration.isZero())
69  {
70  return "infinite";
71  }
72 
73  return std::to_string(toHertz()) + "Hz";
74  }
75 
76  Frequency
77  Frequency::operator+(const Frequency& rhs) const
78  {
80  }
81 
82  Frequency&
84  {
86  return *this;
87  }
88 
89  Frequency
90  Frequency::operator-(const Frequency& rhs) const
91  {
93  }
94 
95  Frequency&
97  {
99  return *this;
100  }
101 
102  Frequency
103  Frequency::operator*(double rhs) const
104  {
105  return Frequency(_cycleDuration / rhs);
106  }
107 
108  Frequency
109  Frequency::operator*(int rhs) const
110  {
111  return Frequency(_cycleDuration / rhs);
112  }
113 
114  Frequency
115  Frequency::operator*(std::int64_t rhs) const
116  {
117  return Frequency(_cycleDuration / rhs);
118  }
119 
120  Frequency&
122  {
123  _cycleDuration /= rhs;
124  return *this;
125  }
126 
127  Frequency&
129  {
130  _cycleDuration /= rhs;
131  return *this;
132  }
133 
134  Frequency&
135  Frequency::operator*=(std::int64_t rhs)
136  {
137  _cycleDuration /= rhs;
138  return *this;
139  }
140 
141  double
142  Frequency::operator/(const Frequency& rhs) const
143  {
144  return toHertzDouble() / rhs.toHertzDouble();
145  }
146 
147  Frequency
148  Frequency::operator/(double rhs) const
149  {
150  return Frequency(_cycleDuration * rhs);
151  }
152 
153  Frequency
154  Frequency::operator/(int rhs) const
155  {
156  return Frequency(_cycleDuration * rhs);
157  }
158 
159  Frequency
160  Frequency::operator/(std::int64_t rhs) const
161  {
162  return Frequency(_cycleDuration * rhs);
163  }
164 
165  Frequency&
167  {
168  _cycleDuration *= rhs;
169  return *this;
170  }
171 
172  Frequency&
174  {
175  _cycleDuration *= rhs;
176  return *this;
177  }
178 
179  Frequency&
180  Frequency::operator/=(std::int64_t rhs)
181  {
182  _cycleDuration *= rhs;
183  return *this;
184  }
185 
186  bool
187  Frequency::operator<(const Frequency& rhs) const
188  {
189  return _cycleDuration > rhs._cycleDuration;
190  }
191 
192  bool
194  {
195  return _cycleDuration >= rhs._cycleDuration;
196  }
197 
198  bool
200  {
201  return _cycleDuration == rhs._cycleDuration;
202  }
203 
204  bool
206  {
207  return _cycleDuration != rhs._cycleDuration;
208  }
209 
210  bool
212  {
213  return _cycleDuration <= rhs._cycleDuration;
214  }
215 
216  bool
217  Frequency::operator>(const Frequency& rhs) const
218  {
219  return _cycleDuration < rhs._cycleDuration;
220  }
221 
222  Frequency
223  operator/(const double cyclesPerDuration, const Duration& duration)
224  {
225  const double cyclesPerSecond = cyclesPerDuration / duration.toSecondsDouble();
226  return Frequency(Duration::Seconds(1) / cyclesPerSecond);
227  }
228 
229  Frequency
230  operator/(const int cyclesPerDuration, const Duration& duration)
231  {
232  return static_cast<double>(cyclesPerDuration) / duration;
233  }
234 
235  Frequency
236  operator/(const std::int64_t cyclesPerDuration, const Duration& duration)
237  {
238  return static_cast<double>(cyclesPerDuration) / duration;
239  }
240 
241  std::ostream&
242  operator<<(std::ostream& out, const Frequency& rhs)
243  {
244  out << rhs.toFrequencyString();
245  return out;
246  }
247 
248 } // namespace armarx::core::time
armarx::core::time::Frequency::operator!=
bool operator!=(const Frequency &rhs) const
Definition: Frequency.cpp:205
armarx::core::time::Duration::toSecondsDouble
double toSecondsDouble() const
Returns the amount of seconds.
Definition: Duration.cpp:90
armarx::core::time::Frequency::operator>=
bool operator>=(const Frequency &rhs) const
Definition: Frequency.cpp:211
Frequency.h
armarx::core::time
Definition: Clock.cpp:13
armarx::core::time::Frequency
Represents a frequency.
Definition: Frequency.h:16
armarx::core::time::Frequency::operator<
bool operator<(const Frequency &rhs) const
Definition: Frequency.cpp:187
armarx::core::time::Frequency::operator/
double operator/(const Frequency &rhs) const
Definition: Frequency.cpp:142
armarx::core::time::operator<<
std::ostream & operator<<(std::ostream &out, const DateTime &rhs)
Definition: DateTime.cpp:203
armarx::core::time::Frequency::_cycleDuration
Duration _cycleDuration
Current cycle duration.
Definition: Frequency.h:86
Clock.h
armarx::core::time::Frequency::operator>
bool operator>(const Frequency &rhs) const
Definition: Frequency.cpp:217
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:72
armarx::core::time::Frequency::toCycleDuration
Duration toCycleDuration() const
Definition: Frequency.cpp:60
armarx::core::time::Frequency::Frequency
Frequency(const Duration &cycleDuration)
Definition: Frequency.cpp:14
armarx::core::time::Frequency::operator<=
bool operator<=(const Frequency &rhs) const
Definition: Frequency.cpp:193
armarx::core::time::Frequency::HertzDouble
static Frequency HertzDouble(double hertz)
Definition: Frequency.cpp:30
armarx::core::time::Frequency::operator==
bool operator==(const Frequency &rhs) const
Definition: Frequency.cpp:199
armarx::core::time::Frequency::operator+=
Frequency & operator+=(const Frequency &rhs)
Definition: Frequency.cpp:83
max
T max(T t1, T t2)
Definition: gdiam.h:51
armarx::core::time::Frequency::operator+
Frequency operator+(const Frequency &rhs) const
Definition: Frequency.cpp:77
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::core::time::Frequency::operator-
Frequency operator-(const Frequency &rhs) const
Definition: Frequency.cpp:90
armarx::core::time::Frequency::toHertz
std::int64_t toHertz() const
Definition: Frequency.cpp:40
armarx::core::time::Frequency::operator-=
Frequency & operator-=(const Frequency &rhs)
Definition: Frequency.cpp:96
armarx::core::time::Frequency::operator*
Frequency operator*(double rhs) const
Definition: Frequency.cpp:103
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:16
armarx::core::time::Frequency::Hertz
static Frequency Hertz(std::int64_t hertz)
Definition: Frequency.cpp:20
armarx::core::time::Frequency::operator*=
Frequency & operator*=(double rhs)
Definition: Frequency.cpp:121
armarx::core::time::Frequency::toHertzDouble
double toHertzDouble() const
Definition: Frequency.cpp:50
armarx::core::time::Frequency::toFrequencyString
std::string toFrequencyString() const
Definition: Frequency.cpp:66
armarx::core::time::Frequency::operator/=
Frequency & operator/=(double rhs)
Definition: Frequency.cpp:166
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:24
armarx::core::time::operator/
Frequency operator/(const double cyclesPerDuration, const Duration &duration)
Definition: Frequency.cpp:223
armarx::core::time::Duration::isZero
bool isZero() const
Definition: Duration.cpp:174