w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rtime.ipp
Go to the documentation of this file.
1// $Id: Rtime.ipp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2017-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-12-22 1091 1.0.2 Drop empty dtors for pod-only classes
8// Set(): add time_t cast (-Wfloat-conversion fix)
9// 2018-12-21 1090 1.0.1 use list-init
10// 2017-02-20 854 1.0 Initial version
11// ---------------------------------------------------------------------------
12
17#include <math.h>
18
19// all method definitions in namespace Retro
20namespace Retro {
21
22//------------------------------------------+-----------------------------------
24
26 : fTime{0,0} // {0,0} to make some gcc happy
27{}
28
29//------------------------------------------+-----------------------------------
31
32inline Rtime::Rtime(clockid_t clkid)
33{
34 GetClock(clkid);
35}
36
37//------------------------------------------+-----------------------------------
39
40inline Rtime::Rtime(double dt)
41{
42 Set(dt);
43}
44
45//------------------------------------------+-----------------------------------
47
48inline void Rtime::SetSec(time_t sec)
49{
50 fTime.tv_sec = sec;
51}
52
53//------------------------------------------+-----------------------------------
55
56inline void Rtime::Set(const struct timespec& ts)
57{
58 fTime = ts;
59}
60
61//------------------------------------------+-----------------------------------
63
64inline void Rtime::Set(double dt)
65{
66 double nsec = floor(1.e9*dt);
67 double sec = floor(dt);
68 fTime.tv_sec = time_t(sec);
69 fTime.tv_nsec = long(nsec - 1.e9*sec);
70 return;
71}
72
73//------------------------------------------+-----------------------------------
75
76inline void Rtime::Clear()
77{
78 fTime.tv_sec = 0;
79 fTime.tv_nsec = 0;
80}
81
82//------------------------------------------+-----------------------------------
84
85inline bool Rtime::IsZero() const
86{
87 return fTime.tv_sec==0 && fTime.tv_nsec==0;
88}
89
90//------------------------------------------+-----------------------------------
92
93inline bool Rtime::IsPositive() const
94{
95 return fTime.tv_sec > 0 || (fTime.tv_sec == 0 && fTime.tv_nsec > 0);
96}
97
98//------------------------------------------+-----------------------------------
100
101inline bool Rtime::IsNegative() const
102{
103 return fTime.tv_sec < 0;
104}
105
106//------------------------------------------+-----------------------------------
108
109inline time_t Rtime::Sec() const
110{
111 return fTime.tv_sec;
112}
113
114//------------------------------------------+-----------------------------------
116
117inline long Rtime::NSec() const
118{
119 return fTime.tv_nsec;
120}
121
122//------------------------------------------+-----------------------------------
124
125inline const struct timespec& Rtime::Timespec() const
126{
127 return fTime;
128}
129
130//------------------------------------------+-----------------------------------
132
133inline int Rtime::ToMSec() const
134{
135 // round up here !!
136 return 1000*fTime.tv_sec + (fTime.tv_nsec+999999)/1000000;
137}
138
139//------------------------------------------+-----------------------------------
141
142inline double Rtime::ToDouble() const
143{
144 return double(fTime.tv_sec) + 1.e-9*double(fTime.tv_nsec);
145}
146
147//------------------------------------------+-----------------------------------
149
150inline Rtime::operator double() const
151{
152 return ToDouble();
153}
154
155//------------------------------------------+-----------------------------------
157
158inline Rtime& Rtime::operator+=(const Rtime& rhs)
159{
160 fTime.tv_sec += rhs.fTime.tv_sec;
161 fTime.tv_nsec += rhs.fTime.tv_nsec;
162 Fixup();
163 return *this;
164}
165
166//------------------------------------------+-----------------------------------
168
169inline Rtime& Rtime::operator-=(const Rtime& rhs)
170{
171 fTime.tv_sec -= rhs.fTime.tv_sec;
172 fTime.tv_nsec -= rhs.fTime.tv_nsec;
173 Fixup();
174 return *this;
175}
176
177//------------------------------------------+-----------------------------------
179
180inline bool Rtime::operator==(const Rtime& rhs)
181{
182 return fTime.tv_sec == rhs.fTime.tv_sec &&
183 fTime.tv_nsec == rhs.fTime.tv_nsec;
184}
185
186//------------------------------------------+-----------------------------------
188
189inline bool Rtime::operator!=(const Rtime& rhs)
190{
191 return ! operator==(rhs);
192}
193
194//------------------------------------------+-----------------------------------
196
197inline bool Rtime::operator<(const Rtime& rhs)
198{
199 return fTime.tv_sec < rhs.fTime.tv_sec ||
200 (fTime.tv_sec == rhs.fTime.tv_sec &&
201 fTime.tv_nsec < rhs.fTime.tv_nsec);
202}
203
204//------------------------------------------+-----------------------------------
206
207inline bool Rtime::operator<=(const Rtime& rhs)
208{
209 return fTime.tv_sec < rhs.fTime.tv_sec ||
210 (fTime.tv_sec == rhs.fTime.tv_sec &&
211 fTime.tv_nsec <= rhs.fTime.tv_nsec);
212}
213
214//------------------------------------------+-----------------------------------
216
217inline bool Rtime::operator>(const Rtime& rhs)
218{
219 return !operator<=(rhs);
220}
221
222//------------------------------------------+-----------------------------------
224
225inline bool Rtime::operator>=(const Rtime& rhs)
226{
227 return !operator<(rhs);
228}
229
230//------------------------------------------+-----------------------------------
232
233inline void Rtime::Fixup()
234{
235 if (fTime.tv_nsec >= 1000000000) {
236 fTime.tv_nsec -= 1000000000;
237 fTime.tv_sec += 1;
238 } else if (fTime.tv_nsec < 0) {
239 fTime.tv_nsec += 1000000000;
240 fTime.tv_sec -= 1;
241 }
242 return;
243}
244
245//------------------------------------------+-----------------------------------
251inline Rtime operator+(const Rtime& x, const Rtime& y)
252{
253 Rtime res(x);
254 res += y;
255 return res;
256}
257
258//------------------------------------------+-----------------------------------
264inline Rtime operator-(const Rtime& x, const Rtime& y)
265{
266 Rtime res(x);
267 res -= y;
268 return res;
269}
270
271//------------------------------------------+-----------------------------------
277inline std::ostream& operator<<(std::ostream& os, const Rtime& obj)
278{
279 obj.Print(os);
280 return os;
281}
282
283} // end namespace Retro
FIXME_docs.
Definition: Rtime.hpp:25
bool IsZero() const
FIXME_docs.
Definition: Rtime.ipp:85
bool IsNegative() const
FIXME_docs.
Definition: Rtime.ipp:101
Rtime operator-(const Rtime &x, const Rtime &y)
operator-: Rtime - Rtime.
Definition: Rtime.ipp:264
bool operator!=(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:189
const struct timespec & Timespec() const
FIXME_docs.
Definition: Rtime.ipp:125
int ToMSec() const
FIXME_docs.
Definition: Rtime.ipp:133
void Print(std::ostream &os) const
FIXME_docs.
Definition: Rtime.cpp:82
time_t Sec() const
FIXME_docs.
Definition: Rtime.ipp:109
void Set(const struct timespec &ts)
FIXME_docs.
Definition: Rtime.ipp:56
bool IsPositive() const
FIXME_docs.
Definition: Rtime.ipp:93
void Clear()
FIXME_docs.
Definition: Rtime.ipp:76
void Fixup()
FIXME_docs.
Definition: Rtime.ipp:233
Rtime & operator+=(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:158
void SetSec(time_t sec)
FIXME_docs.
Definition: Rtime.ipp:48
bool operator<=(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:207
struct timespec fTime
time
Definition: Rtime.hpp:69
bool operator>(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:217
void GetClock(clockid_t clkid)
FIXME_docs.
Definition: Rtime.cpp:41
bool operator>=(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:225
Rtime & operator-=(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:169
Rtime()
Default constructor.
Definition: Rtime.ipp:25
bool operator<(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:197
Rtime operator+(const Rtime &x, const Rtime &y)
operator+: Rtime + Rtime.
Definition: Rtime.ipp:251
double ToDouble() const
FIXME_docs.
Definition: Rtime.ipp:142
std::ostream & operator<<(std::ostream &os, const Rtime &obj)
ostream insertion operator.
Definition: Rtime.ipp:277
bool operator==(const Rtime &rhs)
FIXME_docs.
Definition: Rtime.ipp:180
long NSec() const
FIXME_docs.
Definition: Rtime.ipp:117
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47