Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TimeSpec.h
Go to the documentation of this file.
1#pragma once
2
3#include <misc/gen/time.h>
4#include <misc/global.h>
5
6namespace sf
7{
11struct _MISC_CLASS TimeSpec : public timespec
12{
13 public:
17 typedef long nsec_type;
18
22 TimeSpec();
23
27 TimeSpec(const TimeSpec& ts);
28
32 TimeSpec(TimeSpec&& ts) = default;
33
38
42 explicit TimeSpec(const timespec& ts);
43
48 explicit TimeSpec(double sec);
49
55 explicit TimeSpec(time_t sec, nsec_type nano_sec);
56
62 TimeSpec& operator=(const timespec& ts);
63
67 bool isZero() const;
68
72 explicit operator bool() const;
73
77 void clear();
78
82 bool operator>=(const timespec& ts) const;
83
87 bool operator>(const timespec& ts) const;
88
92 bool operator<(const timespec& ts) const;
93
97 bool operator<=(const timespec& ts) const;
98
102 bool operator==(const timespec& ts) const;
103
107 bool operator!=(const timespec& ts) const;
108
114 TimeSpec& operator=(const TimeSpec& ts);
115
121 TimeSpec& operator+=(nsec_type nano_sec);
122
128 TimeSpec& operator-=(nsec_type nano_sec);
129
135 TimeSpec& operator+=(const timespec& t);
136
142 TimeSpec& operator-=(const timespec& t);
143
149 TimeSpec operator+(const timespec& t) const;
150
156 TimeSpec operator-(const timespec& t) const;
157
163 TimeSpec& assign(double sec);
164
170 TimeSpec& assign(const timespec& ts);
171
178 TimeSpec& randomize(double factor = 0.1);
179
185
192 TimeSpec& add(time_t sec, nsec_type nano_sec);
193
199 TimeSpec& add(const timespec& ts);
200
206 TimeSpec& sub(const timespec& ts);
207
214 TimeSpec& assign(time_t sec, nsec_type nano_sec);
215
220 [[nodiscard]] double toDouble() const;
221
226 [[nodiscard]] time_t toMilliSecs() const;
227
232 [[nodiscard]] std::string toString() const;
233};
234
236 : timespec{0, 0}
237{}
238
240 : timespec(ts)
241{}
242
243inline TimeSpec::TimeSpec(const timespec& ts)
244 : timespec()
245{
246 assign(ts);
247}
248
249inline TimeSpec::TimeSpec(double sec)
250 : timespec()
251{
252 assign(sec);
253}
254
255inline TimeSpec::TimeSpec(time_t sec, nsec_type nano_sec)
256 : timespec{sec, nano_sec}
257{}
258
259inline TimeSpec& TimeSpec::operator=(const timespec& ts)
260{
261 return assign(ts);
262}
263
264inline bool TimeSpec::isZero() const
265{
266 return tv_sec || tv_nsec;
267}
268
269inline TimeSpec::operator bool() const
270{
271 return tv_sec || tv_nsec;
272}
273
274inline void TimeSpec::clear()
275{
276 tv_sec = 0;
277 tv_nsec = 0;
278}
279
280inline bool TimeSpec::operator>=(const timespec& ts) const
281{
282 return timespecCompare(*this, ts) >= 0;
283}
284
285inline bool TimeSpec::operator>(const timespec& ts) const
286{
287 return timespecCompare(*this, ts) > 0;
288}
289
290inline bool TimeSpec::operator<(const timespec& ts) const
291{
292 return timespecCompare(*this, ts) < 0;
293}
294
295inline bool TimeSpec::operator<=(const timespec& ts) const
296{
297 return timespecCompare(*this, ts) <= 0;
298}
299
300inline bool TimeSpec::operator==(const timespec& ts) const
301{
302 return timespecCompare(*this, ts) == 0;
303}
304
305inline bool TimeSpec::operator!=(const timespec& ts) const
306{
307 return timespecCompare(*this, ts) != 0;
308}
309
311{
312 return assign(ts);
313}
314
316{
317 return add(0, nano_sec);
318}
319
321{
322 return add(0, -nano_sec);
323}
324
325inline TimeSpec& TimeSpec::operator+=(const timespec& t)
326{
327 return add(t);
328}
329
330inline TimeSpec& TimeSpec::operator-=(const timespec& t)
331{
332 return sub(t);
333}
334
335inline TimeSpec TimeSpec::operator+(const timespec& t) const
336{
337 return TimeSpec(*this).add(t);
338}
339
340inline TimeSpec TimeSpec::operator-(const timespec& t) const
341{
342 return TimeSpec(*this).sub(t);
343}
344
345}// namespace sf
346
353inline sf::TimeSpec operator+(const timespec& lhs, const timespec& rhs)
354{
355 return sf::TimeSpec(lhs).add(rhs);
356}
357
364inline sf::TimeSpec operator-(const timespec& lhs, const timespec& rhs)
365{
366 return sf::TimeSpec(lhs).sub(rhs);
367}
368
376inline std::ostream& operator<<(std::ostream& os, const sf::TimeSpec& ts)
377{
378 return os << ts.toString();
379}
sf::TimeSpec operator+(const timespec &lhs, const timespec &rhs)
Addition operator for std::timespec types which also work for sf::TimeSpec.
Definition TimeSpec.h:353
std::ostream & operator<<(std::ostream &os, const sf::TimeSpec &ts)
Operator for writing the sf::TimeSpec to an output-stream.
Definition TimeSpec.h:376
sf::TimeSpec operator-(const timespec &lhs, const timespec &rhs)
Subtraction operator for std::timespec types which also work for sf::TimeSpec.
Definition TimeSpec.h:364
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
_MISC_FUNC int timespecCompare(const timespec &ts1, const timespec &ts2)
Compares the 2 'timespec' time structures. Helper function.
_MISC_FUNC bool operator==(const Md5Hash::hash_type &h1, const Md5Hash::hash_type &h2)
Compare operator for storing an MD5 hash.
bool isZero(T value, T epsilon=std::numeric_limits< T >::epsilon())
Check if the passed value is zero or near zero according the given epsilon.
Definition misc/gen/math.h:263
QRect operator+=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:69
QRect operator-=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:86
Class wrapper for timespec structure to modify.
Definition TimeSpec.h:12
TimeSpec operator-(const timespec &t) const
Operator to add another timespec.
Definition TimeSpec.h:340
TimeSpec & assign(const timespec &ts)
Assigns a timespec base type.
TimeSpec & assign(time_t sec, nsec_type nano_sec)
Assigns seconds and nanoseconds to the current value.
void clear()
clear this instance.
Definition TimeSpec.h:274
bool operator>=(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:280
TimeSpec()
Default constructor.
Definition TimeSpec.h:235
TimeSpec & operator=(TimeSpec &&)=default
Move assignment operator is default.
TimeSpec operator+(const timespec &t) const
Operator to add another timespec.
Definition TimeSpec.h:335
bool operator==(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:300
std::string toString() const
Gets the time as toDouble() result converted to a string.
TimeSpec & randomize(double factor=0.1)
Modifies the current value using the passed factor where 0.1 is + or - 10%.
bool operator<(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:290
TimeSpec & setTimeOfDay()
assign the current time.
double toDouble() const
Gets the time value as a double.
bool isZero() const
Test if it has been set and thus not-zero.
Definition TimeSpec.h:264
bool operator>(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:285
TimeSpec & operator+=(nsec_type nano_sec)
Operator to add nanoseconds.
Definition TimeSpec.h:315
TimeSpec & add(time_t sec, nsec_type nano_sec)
Adds seconds and nanoseconds to the current value.
TimeSpec & operator-=(nsec_type nano_sec)
Operator to add nanoseconds.
Definition TimeSpec.h:320
bool operator!=(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:305
long nsec_type
Integer type to indicate nanoseconds.
Definition TimeSpec.h:17
time_t toMilliSecs() const
Gets the time value as a double.
bool operator<=(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:295
TimeSpec & add(const timespec &ts)
Adds a timespec base type to the current value.
TimeSpec & assign(double sec)
Assigns a double as seconds.
TimeSpec(TimeSpec &&ts)=default
Move constructor.
TimeSpec & sub(const timespec &ts)
Subtracts a timespec base type from the current value.