Open Lighting Architecture: include/ola/Clock.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
Clock.h
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * Clock.h
17 * Provides the TimeInterval and TimeStamp classes.
18 * Copyright (C) 2005 Simon Newton
19 *
20 * The struct timeval can represent both absolute time and time intervals.
21 * We define our own wrapper classes that:
22 * - hide some of the platform differences, like the fact windows doesn't
23 * provide timeradd and timersub.
24 * - Reduces bugs by using the compiler to check if the value was supposed
25 * to be an interval or absolute time. For example, passing an absolute
26 * time instead of an Interval to RegisterTimeout would be bad.
27 */
28
29#ifndef INCLUDE_OLA_CLOCK_H_
30#define INCLUDE_OLA_CLOCK_H_
31
32#include <ola/base/Macro.h>
33#include <stdint.h>
34#include <sys/time.h>
35
36#include <iomanip>
37#include <ostream>
38#include <sstream>
39#include <string>
40
41namespace ola {
42
43static const int USEC_IN_SECONDS = 1000000;
44static const int ONE_THOUSAND = 1000;
45
50 class BaseTimeVal {
51 public:
52 // Constructors
53 BaseTimeVal() { timerclear(&m_tv); }
54 BaseTimeVal(int32_t sec, int32_t usec);
55
56 explicit BaseTimeVal(const struct timeval &timestamp) { m_tv = timestamp; }
57 explicit BaseTimeVal(const struct timespec &timestamp) { Set(timestamp); }
58 explicit BaseTimeVal(int64_t interval_useconds) { Set(interval_useconds); }
59
60 BaseTimeVal(const BaseTimeVal &other) : m_tv(other.m_tv) {}
61
62 // Assignable
63 BaseTimeVal& operator=(const BaseTimeVal& other);
64 BaseTimeVal& operator=(const struct timeval &tv);
65 BaseTimeVal& operator=(const struct timespec &ts);
66
67 // Comparables
68 bool operator==(const BaseTimeVal &other) const;
69 bool operator!=(const BaseTimeVal &other) const;
70 bool operator>(const BaseTimeVal &other) const;
71 bool operator>=(const BaseTimeVal &other) const;
72 bool operator<(const BaseTimeVal &other) const;
73 bool operator<=(const BaseTimeVal &other) const;
74
75 // Arithmetic
76 BaseTimeVal& operator+=(const BaseTimeVal& other);
77 BaseTimeVal &operator-=(const BaseTimeVal &other);
78 const BaseTimeVal operator+(const BaseTimeVal &interval) const;
79 const BaseTimeVal operator-(const BaseTimeVal &other) const;
80 BaseTimeVal operator*(unsigned int i) const;
81
82 // Various other methods.
83 bool IsSet() const;
84 void AsTimeval(struct timeval *tv) const;
85
86 // Returns the seconds portion.
87 time_t Seconds() const { return m_tv.tv_sec; }
88 // Returns the microseconds portion
89 int32_t MicroSeconds() const { return static_cast<int32_t>(m_tv.tv_usec); }
90
91 // Returns the entire BaseTimeVal as milliseconds
92 int64_t InMilliSeconds() const;
93
94 // Returns the entire BaseTimeVal as microseconds.
95 int64_t AsInt() const;
96
97 std::string ToString() const;
98
99 private:
100 struct timeval m_tv;
101
105 void TimerAdd(const struct timeval &tv1, const struct timeval &tv2,
106 struct timeval *result) const;
107
111 void TimerSub(const struct timeval &tv1, const struct timeval &tv2,
112 struct timeval *result) const;
113
114 void Set(int64_t interval_useconds);
115
120 void Set(const struct timespec &ts);
121};
122
123/*
124 * A time interval, with usecond accuracy.
125 */
126 class TimeInterval {
127 public:
128 // Constructors
129 TimeInterval() {}
130 TimeInterval(int32_t sec, int32_t usec) : m_interval(sec, usec) {}
131 explicit TimeInterval(int64_t usec) : m_interval(usec) {}
132
133 TimeInterval(const TimeInterval &other) : m_interval(other.m_interval) {}
134
135 // Assignable
136 TimeInterval& operator=(const TimeInterval& other);
137
138 // Comparables
139 bool operator==(const TimeInterval &other) const;
140 bool operator!=(const TimeInterval &other) const;
141 bool operator>(const TimeInterval &other) const;
142 bool operator>=(const TimeInterval &other) const;
143 bool operator<(const TimeInterval &other) const;
144 bool operator<=(const TimeInterval &other) const;
145
146 // Arithmetic
147 TimeInterval& operator+=(const TimeInterval& other);
148 TimeInterval operator*(unsigned int i) const;
149
150 // Various other methods.
151 bool IsZero() const { return !m_interval.IsSet(); }
152
153 void AsTimeval(struct timeval *tv) const { m_interval.AsTimeval(tv); }
154
155 time_t Seconds() const { return m_interval.Seconds(); }
156 int32_t MicroSeconds() const { return m_interval.MicroSeconds(); }
157
158 int64_t InMilliSeconds() const { return m_interval.InMilliSeconds(); }
159 int64_t AsInt() const { return m_interval.AsInt(); }
160
161 std::string ToString() const { return m_interval.ToString(); }
162
163 friend std::ostream& operator<< (std::ostream &out,
164 const TimeInterval &interval) {
165 return out << interval.m_interval.ToString();
166 }
167
168 private:
169 explicit TimeInterval(const BaseTimeVal &time_val) : m_interval(time_val) {}
170
171 BaseTimeVal m_interval;
172 friend class TimeStamp;
173};
174
175
176/*
177 * Represents a point in time with usecond accuracy.
178 */
179 class TimeStamp {
180 public:
181 // Constructors
182 TimeStamp() {}
183 explicit TimeStamp(const struct timeval &timestamp) : m_tv(timestamp) {}
184 explicit TimeStamp(const struct timespec &timestamp) : m_tv(timestamp) {}
185
186 TimeStamp(const TimeStamp &other) : m_tv(other.m_tv) {}
187
188 // Assignable
189 TimeStamp& operator=(const TimeStamp& other);
190 TimeStamp& operator=(const struct timeval &tv);
191 TimeStamp& operator=(const struct timespec &ts);
192
193 // Comparables
194 bool operator==(const TimeStamp &other) const { return m_tv == other.m_tv; }
195 bool operator!=(const TimeStamp &other) const { return m_tv != other.m_tv; }
196 bool operator>(const TimeStamp &other) const { return m_tv > other.m_tv; }
197 bool operator>=(const TimeStamp &other) const { return m_tv >= other.m_tv; }
198 bool operator<(const TimeStamp &other) const { return m_tv < other.m_tv; }
199 bool operator<=(const TimeStamp &other) const { return m_tv <= other.m_tv; }
200
201 // Arithmetic
202 TimeStamp &operator+=(const TimeInterval &interval);
203 TimeStamp &operator-=(const TimeInterval &interval);
204 const TimeStamp operator+(const TimeInterval &interval) const;
205 const TimeInterval operator-(const TimeStamp &other) const;
206 const TimeStamp operator-(const TimeInterval &interval) const;
207
208 // Various other methods.
209 bool IsSet() const { return m_tv.IsSet(); }
210
211 time_t Seconds() const { return m_tv.Seconds(); }
212 int32_t MicroSeconds() const { return m_tv.MicroSeconds(); }
213
214 std::string ToString() const { return m_tv.ToString(); }
215
216 friend std::ostream& operator<<(std::ostream &out,
217 const TimeStamp &timestamp) {
218 return out << timestamp.m_tv.ToString();
219 }
220
221 private:
222 BaseTimeVal m_tv;
223
224 explicit TimeStamp(const BaseTimeVal &time_val) : m_tv(time_val) {}
225};
226
230 class Clock {
231 public:
232 Clock() {}
233 virtual ~Clock() {}
249 virtual void CurrentMonotonicTime(TimeStamp* timestamp) const;
250
264 virtual void CurrentRealTime(TimeStamp* timestamp) const;
265
272 virtual void CurrentTime(TimeStamp* timestamp) const;
273
274 private:
275 Clock(const Clock &) = delete;
276 const Clock &operator=(const Clock &) = delete;
277};
278
282 class MockClock: public Clock {
283 public:
284 MockClock() : Clock() {}
285
286 // Advance the time
287 void AdvanceTime(const TimeInterval &interval);
288 void AdvanceTime(int32_t sec, int32_t usec);
289
290 void CurrentMonotonicTime(TimeStamp *timestamp) const;
291 void CurrentRealTime(TimeStamp *timestamp) const;
292 void CurrentTime(TimeStamp *timestamp) const;
293
294 private:
295 TimeInterval m_offset;
296};
297} // namespace ola
298#endif // INCLUDE_OLA_CLOCK_H_
Macro.h
Helper macros.
ola::BaseTimeVal
Definition Clock.h:50
ola::Clock::CurrentMonotonicTime
virtual void CurrentMonotonicTime(TimeStamp *timestamp) const
Sets timestamp to the current monotonic time.
Definition Clock.cpp:270
ola::Clock::CurrentTime
virtual void CurrentTime(TimeStamp *timestamp) const
Wrapper around CurrentRealime.
Definition Clock.cpp:286
ola::Clock::CurrentRealTime
virtual void CurrentRealTime(TimeStamp *timestamp) const
Sets timestamp to the current real time.
Definition Clock.cpp:280
ola::MockClock::CurrentMonotonicTime
void CurrentMonotonicTime(TimeStamp *timestamp) const
Sets timestamp to the current monotonic time.
Definition Clock.cpp:299
ola::MockClock::CurrentRealTime
void CurrentRealTime(TimeStamp *timestamp) const
Sets timestamp to the current real time.
Definition Clock.cpp:311
ola::MockClock::CurrentTime
void CurrentTime(TimeStamp *timestamp) const
Wrapper around CurrentRealime.
Definition Clock.cpp:318
ola::TimeInterval
Definition Clock.h:126
ola::TimeStamp
Definition Clock.h:179
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44