Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
41 namespace ola {
42 
43 static const int USEC_IN_SECONDS = 1000000;
44 static 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 
90  time_t Seconds() const { return m_tv.tv_sec; }
95  int32_t MicroSeconds() const { return static_cast<int32_t>(m_tv.tv_usec); }
96 
101  int64_t InMilliSeconds() const;
102 
107  int64_t AsInt() const;
108 
109  std::string ToString() const;
110 
111  private:
112  struct timeval m_tv;
113 
117  void TimerAdd(const struct timeval &tv1, const struct timeval &tv2,
118  struct timeval *result) const;
119 
123  void TimerSub(const struct timeval &tv1, const struct timeval &tv2,
124  struct timeval *result) const;
125 
126  void Set(int64_t interval_useconds);
127 
132  void Set(const struct timespec &ts);
133 };
134 
139  public:
140  // Constructors
141  TimeInterval() {}
142  TimeInterval(int32_t sec, int32_t usec) : m_interval(sec, usec) {}
143  explicit TimeInterval(int64_t usec) : m_interval(usec) {}
144 
145  TimeInterval(const TimeInterval &other) : m_interval(other.m_interval) {}
146 
147  // Assignable
148  TimeInterval& operator=(const TimeInterval& other);
149 
150  // Comparables
151  bool operator==(const TimeInterval &other) const;
152  bool operator!=(const TimeInterval &other) const;
153  bool operator>(const TimeInterval &other) const;
154  bool operator>=(const TimeInterval &other) const;
155  bool operator<(const TimeInterval &other) const;
156  bool operator<=(const TimeInterval &other) const;
157 
158  // Arithmetic
159  TimeInterval& operator+=(const TimeInterval& other);
160  TimeInterval operator*(unsigned int i) const;
161 
162  // Various other methods.
163  bool IsZero() const { return !m_interval.IsSet(); }
164 
165  void AsTimeval(struct timeval *tv) const { m_interval.AsTimeval(tv); }
166 
167  time_t Seconds() const { return m_interval.Seconds(); }
168  int32_t MicroSeconds() const { return m_interval.MicroSeconds(); }
169 
170  int64_t InMilliSeconds() const { return m_interval.InMilliSeconds(); }
171  int64_t AsInt() const { return m_interval.AsInt(); }
172 
173  std::string ToString() const { return m_interval.ToString(); }
174 
175  friend std::ostream& operator<< (std::ostream &out,
176  const TimeInterval &interval) {
177  return out << interval.m_interval.ToString();
178  }
179 
180  private:
181  explicit TimeInterval(const BaseTimeVal &time_val) : m_interval(time_val) {}
182 
183  BaseTimeVal m_interval;
184  friend class TimeStamp;
185 };
186 
187 
191 class TimeStamp {
192  public:
193  // Constructors
194  TimeStamp() {}
195  explicit TimeStamp(const struct timeval &timestamp) : m_tv(timestamp) {}
196  explicit TimeStamp(const struct timespec &timestamp) : m_tv(timestamp) {}
197 
198  TimeStamp(const TimeStamp &other) : m_tv(other.m_tv) {}
199 
200  // Assignable
201  TimeStamp& operator=(const TimeStamp& other);
202  TimeStamp& operator=(const struct timeval &tv);
203  TimeStamp& operator=(const struct timespec &ts);
204 
205  // Comparables
206  bool operator==(const TimeStamp &other) const { return m_tv == other.m_tv; }
207  bool operator!=(const TimeStamp &other) const { return m_tv != other.m_tv; }
208  bool operator>(const TimeStamp &other) const { return m_tv > other.m_tv; }
209  bool operator>=(const TimeStamp &other) const { return m_tv >= other.m_tv; }
210  bool operator<(const TimeStamp &other) const { return m_tv < other.m_tv; }
211  bool operator<=(const TimeStamp &other) const { return m_tv <= other.m_tv; }
212 
213  // Arithmetic
214  TimeStamp &operator+=(const TimeInterval &interval);
215  TimeStamp &operator-=(const TimeInterval &interval);
216  const TimeStamp operator+(const TimeInterval &interval) const;
217  const TimeInterval operator-(const TimeStamp &other) const;
218  const TimeStamp operator-(const TimeInterval &interval) const;
219 
220  // Various other methods.
221  bool IsSet() const { return m_tv.IsSet(); }
222 
223  time_t Seconds() const { return m_tv.Seconds(); }
224  int32_t MicroSeconds() const { return m_tv.MicroSeconds(); }
225 
226  std::string ToString() const { return m_tv.ToString(); }
227 
228  friend std::ostream& operator<<(std::ostream &out,
229  const TimeStamp &timestamp) {
230  return out << timestamp.m_tv.ToString();
231  }
232 
233  private:
234  BaseTimeVal m_tv;
235 
236  explicit TimeStamp(const BaseTimeVal &time_val) : m_tv(time_val) {}
237 };
238 
242 class Clock {
243  public:
244  Clock() {}
245  virtual ~Clock() {}
261  virtual void CurrentMonotonicTime(TimeStamp* timestamp) const;
262 
276  virtual void CurrentRealTime(TimeStamp* timestamp) const;
277 
284  virtual void CurrentTime(TimeStamp* timestamp) const;
285 
286  private:
287  DISALLOW_COPY_AND_ASSIGN(Clock);
288 };
289 
293 class MockClock: public Clock {
294  public:
295  MockClock() : Clock() {}
296 
297  // Advance the time
298  void AdvanceTime(const TimeInterval &interval);
299  void AdvanceTime(int32_t sec, int32_t usec);
300 
301  void CurrentMonotonicTime(TimeStamp *timestamp) const;
302  void CurrentRealTime(TimeStamp *timestamp) const;
303  void CurrentTime(TimeStamp *timestamp) const;
304 
305  private:
306  TimeInterval m_offset;
307 };
308 } // namespace ola
309 #endif // INCLUDE_OLA_CLOCK_H_
A time interval, with usecond accuracy.
Definition: Clock.h:138
virtual void CurrentRealTime(TimeStamp *timestamp) const
Sets timestamp to the current real time.
Definition: Clock.cpp:280
virtual void CurrentMonotonicTime(TimeStamp *timestamp) const
Sets timestamp to the current monotonic time.
Definition: Clock.cpp:270
void CurrentMonotonicTime(TimeStamp *timestamp) const
Sets timestamp to the current monotonic time.
Definition: Clock.cpp:299
void CurrentTime(TimeStamp *timestamp) const
Wrapper around CurrentRealime.
Definition: Clock.cpp:318
int64_t InMilliSeconds() const
Returns the entire BaseTimeVal as milliseconds.
Definition: Clock.cpp:133
virtual void CurrentTime(TimeStamp *timestamp) const
Wrapper around CurrentRealime.
Definition: Clock.cpp:286
Definition: Clock.h:50
int64_t AsInt() const
Returns the entire BaseTimeVal as microseconds.
Definition: Clock.cpp:138
Used to get the current time.
Definition: Clock.h:242
time_t Seconds() const
Returns the seconds portion of the BaseTimeVal.
Definition: Clock.h:90
void CurrentRealTime(TimeStamp *timestamp) const
Sets timestamp to the current real time.
Definition: Clock.cpp:311
Helper macros.
int32_t MicroSeconds() const
Returns the microseconds portion of the BaseTimeVal.
Definition: Clock.h:95
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: Clock.h:293
Represents a point in time with microsecond accuracy.
Definition: Clock.h:191