22
22
23
23
Provides a standard definition of a tock and a ttimer.
24
24
25
- A tock is the number of milliseconds since a platform-specific date.
25
+ A tock is the number of nanoseconds since a platform-specific date.
26
26
Tocks are never used directly, rather the difference between two tocks
27
27
(latest - previous) are used to determine the passage of time. It is
28
28
assumed that tocks always move forward, and that the base date is not
29
29
different for any instance of the compiled tock, nor does the base date change.
30
30
31
- Tocks are guaranteed to have a millisecond granularity.
31
+ Tocks are guaranteed to have a nanosecond granularity.
32
32
Tock comparisons correctly handle clock rollover.
33
33
34
34
A ttimer is a simple abstraction for typical timer usage, which is
40
40
#define _TOCK_H_
41
41
42
42
#include < QtGlobal>
43
+ #include < chrono>
43
44
44
45
class tock ;
45
46
class ttimer ;
@@ -57,20 +58,26 @@ void Tock_StopLib(); //Shuts down the tock layer.
57
58
class tock
58
59
{
59
60
public:
61
+ typedef std::chrono::nanoseconds resolution_t ;
62
+
60
63
// construction and copying
61
64
tock ();
62
- tock (quint32 ms);
63
- tock (const tock& t);
64
- tock& operator =(const tock& t);
65
+ tock (const tock&) = default ;
66
+ tock (tock&&) = default ;
67
+ tock& operator =(const tock&) = default ;
68
+ tock& operator =(tock&&) = default ;
69
+
70
+ template <typename Rep, typename Period>
71
+ tock (std::chrono::duration<Rep, Period> duration);
65
72
66
- // Returns the number of milliseconds that this tock represents
67
- quint32 Getms () ;
73
+ // Returns the number of nanoseconds that this tock represents
74
+ resolution_t Get () const ;
68
75
69
- // Used sparingly, but sets the number of milliseconds that this tock represents
70
- void Setms (quint32 ms );
76
+ // Used sparingly, but sets the number of nanoseconds that this tock represents
77
+ void Set ( resolution_t time );
71
78
72
79
protected:
73
- qint32 v; // Signed, so the wraparound calculations will work
80
+ resolution_t v;
74
81
75
82
friend bool operator >(const tock& t1, const tock& t2);
76
83
friend bool operator >=(const tock& t1, const tock& t2);
@@ -87,52 +94,28 @@ class ttimer
87
94
public:
88
95
// construction/setup
89
96
ttimer (); // Will immediately time out if timeout isn't set
90
- ttimer (qint32 ms); // The number of milliseconds before the timer will time out
91
- void SetInterval (qint32 ms); // Sets a new timeout interval (in ms) and resets the timer
92
- qint32 GetInterval (); // Returns the current timeout interval (in ms)
97
+ ttimer (const ttimer&) = default ;
98
+ ttimer (ttimer&&) = default ;
99
+ ttimer& operator =(const ttimer&) = default ;
100
+ ttimer& operator =(ttimer&&) = default ;
101
+ template <typename Rep, typename Period>
102
+ ttimer (std::chrono::duration<Rep, Period> interval); // The duration before the timer will time out
103
+
104
+ void SetInterval (tock::resolution_t interval); // Sets a new timeout interval and resets the timer
105
+
106
+ tock::resolution_t GetInterval () const ; // Returns the current timeout interval
93
107
94
108
void Reset (); // Resets the timer, using the current timeout interval
95
- bool Expired () const ; // Returns true if the timer has expired.
96
- // Call Reset() to use this timer again for a new interval.
109
+
110
+ // Returns true if the timer has expired
111
+ // Call Reset() to use this timer again for a new interval.
112
+ bool Expired () const ;
97
113
98
114
friend bool operator ==(const ttimer& t1, const ttimer& t2);
99
115
friend bool operator !=(const ttimer& t1, const ttimer& t2);
100
116
101
117
protected:
102
- qint32 interval;
118
+ tock:: resolution_t interval;
103
119
tock tockout;
104
120
};
105
-
106
-
107
- // ---------------------------------------------------------------------------------
108
- // Implementation
109
-
110
- /* ttimer implementation*/
111
- inline ttimer::ttimer ():interval(0 ) {Reset ();}
112
- inline ttimer::ttimer (qint32 ms):interval(ms) {Reset ();}
113
- inline void ttimer::SetInterval (qint32 ms) {interval = ms; Reset ();}
114
- inline qint32 ttimer::GetInterval () {return interval;}
115
- inline void ttimer::Reset () {tockout.Setms (Tock_GetTock ().Getms () + interval);}
116
- inline bool ttimer::Expired () const {return Tock_GetTock () > tockout;}
117
- inline bool operator ==(const ttimer& t1, const ttimer& t2) { return ((t1.tockout == t2.tockout ) && (t1.interval == t2.interval )); }
118
- inline bool operator !=(const ttimer& t1, const ttimer& t2) { return !(t1 == t2); }
119
-
120
- /* tock implementation*/
121
- inline tock::tock ():v(0 ) {}
122
- inline tock::tock (quint32 ms):v(ms) {}
123
- inline tock::tock (const tock& t) {v = t.v ;}
124
- inline tock& tock::operator =(const tock& t) {v = t.v ; return *this ;}
125
-
126
- inline quint32 tock::Getms () {return v;}
127
- inline void tock::Setms (quint32 ms) {v = ms;}
128
-
129
- inline bool operator >(const tock& t1, const tock& t2) {return t1.v - t2.v > 0 ;}
130
- inline bool operator >=(const tock& t1, const tock& t2) {return t1.v - t2.v >= 0 ;}
131
- inline bool operator ==(const tock& t1, const tock& t2) {return t1.v - t2.v == 0 ;}
132
- inline bool operator !=(const tock& t1, const tock& t2) {return t1.v - t2.v != 0 ;}
133
- inline bool operator <(const tock& t1, const tock& t2) {return t2.v - t1.v > 0 ;}
134
- inline bool operator <=(const tock& t1, const tock& t2) {return t2.v - t1.v >= 0 ;}
135
- inline quint32 operator -(const tock& t1, const tock& t2) {return t1.v - t2.v ;}
136
-
137
-
138
121
#endif /* _TOCK_H*/
0 commit comments