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 nsec);
56
63 TimeSpec& operator=(const timespec& ts);
64
68 bool isZero() const;
69
73 explicit operator bool() const;
74
78 void clear();
79
83 bool operator>=(const timespec& ts) const;
84
88 bool operator>(const timespec& ts) const;
89
93 bool operator<(const timespec& ts) const;
94
98 bool operator<=(const timespec& ts) const;
99
103 bool operator==(const timespec& ts) const;
104
108 bool operator!=(const timespec& ts) const;
109
116 TimeSpec& operator=(const TimeSpec& ts);
117
125
133
140 TimeSpec& operator+=(const timespec& t);
141
148 TimeSpec& operator-=(const timespec& t);
149
156 TimeSpec operator+(const timespec& t) const;
157
164 TimeSpec operator-(const timespec& t) const;
165
172 TimeSpec& assign(double sec);
173
180 TimeSpec& assign(const timespec& ts);
181
188 TimeSpec& randomize(double factor = 0.1);
189
196
204 TimeSpec& add(time_t sec, nsec_type nsec);
205
212 TimeSpec& add(const timespec& ts);
213
220 TimeSpec& sub(const timespec& ts);
221
229 TimeSpec& assign(time_t sec, nsec_type nsec);
230
235 [[nodiscard]] double toDouble() const;
236
242 [[nodiscard]] time_t toMilliSecs() const;
243
249 [[nodiscard]] std::string toString() const;
250};
251
253 : timespec{0, 0}
254{}
255
257 : timespec(ts)
258{}
259
260inline TimeSpec::TimeSpec(const timespec& ts)
261 : timespec()
262{
263 assign(ts);
264}
265
266inline TimeSpec::TimeSpec(double sec)
267 : timespec()
268{
269 assign(sec);
270}
271
272inline TimeSpec::TimeSpec(time_t sec, nsec_type nsec)
273 : timespec{sec, nsec}
274{}
275
276inline TimeSpec& TimeSpec::operator=(const timespec& ts)
277{
278 return assign(ts);
279}
280
281inline bool TimeSpec::isZero() const
282{
283 return tv_sec || tv_nsec;
284}
285
286inline TimeSpec::operator bool() const
287{
288 return tv_sec || tv_nsec;
289}
290
291inline void TimeSpec::clear()
292{
293 tv_sec = 0;
294 tv_nsec = 0;
295}
296
297inline bool TimeSpec::operator>=(const timespec& ts) const
298{
299 return timespecCompare(*this, ts) >= 0;
300}
301
302inline bool TimeSpec::operator>(const timespec& ts) const
303{
304 return timespecCompare(*this, ts) > 0;
305}
306
307inline bool TimeSpec::operator<(const timespec& ts) const
308{
309 return timespecCompare(*this, ts) < 0;
310}
311
312inline bool TimeSpec::operator<=(const timespec& ts) const
313{
314 return timespecCompare(*this, ts) <= 0;
315}
316
317inline bool TimeSpec::operator==(const timespec& ts) const
318{
319 return timespecCompare(*this, ts) == 0;
320}
321
322inline bool TimeSpec::operator!=(const timespec& ts) const
323{
324 return timespecCompare(*this, ts) != 0;
325}
326
328{
329 return assign(ts);
330}
331
333{
334 return add(0, nsec);
335}
336
338{
339 return add(0, -nsec);
340}
341
342inline TimeSpec& TimeSpec::operator+=(const timespec& t)
343{
344 return add(t);
345}
346
347inline TimeSpec& TimeSpec::operator-=(const timespec& t)
348{
349 return sub(t);
350}
351
352inline TimeSpec TimeSpec::operator+(const timespec& t) const
353{
354 return TimeSpec(*this).add(t);
355}
356
357inline TimeSpec TimeSpec::operator-(const timespec& t) const
358{
359 return TimeSpec(*this).sub(t);
360}
361
362}// namespace sf
363
370inline sf::TimeSpec operator+(const timespec& lhs, const timespec& rhs)
371{
372 return sf::TimeSpec(lhs).add(rhs);
373}
374
381inline sf::TimeSpec operator-(const timespec& lhs, const timespec& rhs)
382{
383 return sf::TimeSpec(lhs).sub(rhs);
384}
385
393inline std::ostream& operator<<(std::ostream& os, const sf::TimeSpec& ts)
394{
395 return os << ts.toString();
396}
sf::TimeSpec operator+(const timespec &lhs, const timespec &rhs)
Addition operator for std::timespec types which also work for sf::TimeSpec.
Definition TimeSpec.h:370
std::ostream & operator<<(std::ostream &os, const sf::TimeSpec &ts)
Operator for writing the sf::TimeSpec to an output-stream.
Definition TimeSpec.h:393
sf::TimeSpec operator-(const timespec &lhs, const timespec &rhs)
Subtraction operator for std::timespec types which also work for sf::TimeSpec.
Definition TimeSpec.h:381
#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:245
QRect operator+=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:66
QRect operator-=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:83
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:357
TimeSpec & assign(const timespec &ts)
Assigns a timespec base type.
TimeSpec & add(time_t sec, nsec_type nsec)
Adds seconds and nanoseconds to the current value.
void clear()
clear this instance.
Definition TimeSpec.h:291
bool operator>=(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:297
TimeSpec()
Default constructor.
Definition TimeSpec.h:252
TimeSpec & operator=(TimeSpec &&)=default
Move assignment operator is default.
TimeSpec operator+(const timespec &t) const
Operator to add another timespec.
Definition TimeSpec.h:352
bool operator==(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:317
std::string toString() const
Returns 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:307
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:281
bool operator>(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:302
TimeSpec & operator-=(nsec_type nsec)
Operator to add nanoseconds.
Definition TimeSpec.h:337
bool operator!=(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:322
long nsec_type
Integer type to indicate nanoseconds.
Definition TimeSpec.h:17
TimeSpec & assign(time_t sec, nsec_type nsec)
Assigns seconds and nanoseconds to the current value.
time_t toMilliSecs() const
Gets the time value as a double.
bool operator<=(const timespec &ts) const
Compare operator.
Definition TimeSpec.h:312
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.
TimeSpec & operator+=(nsec_type nsec)
Operator to add nanoseconds.
Definition TimeSpec.h:332