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