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(int64_t interval_useconds) { Set(interval_useconds); }
58 
59  BaseTimeVal(const BaseTimeVal &other) : m_tv(other.m_tv) {}
60 
61  // Assignable
62  BaseTimeVal& operator=(const BaseTimeVal& other);
63  BaseTimeVal& operator=(const struct timeval &tv);
64 
65  // Comparables
66  bool operator==(const BaseTimeVal &other) const;
67  bool operator!=(const BaseTimeVal &other) const;
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 
73  // Arithmetic
74  BaseTimeVal& operator+=(const BaseTimeVal& other);
75  BaseTimeVal &operator-=(const BaseTimeVal &other);
76  const BaseTimeVal operator+(const BaseTimeVal &interval) const;
77  const BaseTimeVal operator-(const BaseTimeVal &other) const;
78  BaseTimeVal operator*(unsigned int i) const;
79 
80  // Various other methods.
81  bool IsSet() const;
82  void AsTimeval(struct timeval *tv) const;
83 
88  time_t Seconds() const { return m_tv.tv_sec; }
93  int32_t MicroSeconds() const { return static_cast<int32_t>(m_tv.tv_usec); }
94 
99  int64_t InMilliSeconds() const;
100 
105  int64_t AsInt() const;
106 
107  std::string ToString() const;
108 
109  private:
110  struct timeval m_tv;
111 
115  void TimerAdd(const struct timeval &tv1, const struct timeval &tv2,
116  struct timeval *result) const;
117 
121  void TimerSub(const struct timeval &tv1, const struct timeval &tv2,
122  struct timeval *result) const;
123 
124  void Set(int64_t interval_useconds);
125 };
126 
131  public:
132  // Constructors
133  TimeInterval() {}
134  TimeInterval(int32_t sec, int32_t usec) : m_interval(sec, usec) {}
135  explicit TimeInterval(int64_t usec) : m_interval(usec) {}
136 
137  TimeInterval(const TimeInterval &other) : m_interval(other.m_interval) {}
138 
139  // Assignable
140  TimeInterval& operator=(const TimeInterval& other);
141 
142  // Comparables
143  bool operator==(const TimeInterval &other) const;
144  bool operator!=(const TimeInterval &other) const;
145  bool operator>(const TimeInterval &other) const;
146  bool operator>=(const TimeInterval &other) const;
147  bool operator<(const TimeInterval &other) const;
148  bool operator<=(const TimeInterval &other) const;
149 
150  // Arithmetic
151  TimeInterval& operator+=(const TimeInterval& other);
152  TimeInterval operator*(unsigned int i) const;
153 
154  // Various other methods.
155  bool IsZero() const { return !m_interval.IsSet(); }
156 
157  void AsTimeval(struct timeval *tv) const { m_interval.AsTimeval(tv); }
158 
159  time_t Seconds() const { return m_interval.Seconds(); }
160  int32_t MicroSeconds() const { return m_interval.MicroSeconds(); }
161 
162  int64_t InMilliSeconds() const { return m_interval.InMilliSeconds(); }
163  int64_t AsInt() const { return m_interval.AsInt(); }
164 
165  std::string ToString() const { return m_interval.ToString(); }
166 
167  friend std::ostream& operator<< (std::ostream &out,
168  const TimeInterval &interval) {
169  return out << interval.m_interval.ToString();
170  }
171 
172  private:
173  explicit TimeInterval(const BaseTimeVal &time_val) : m_interval(time_val) {}
174 
175  BaseTimeVal m_interval;
176  friend class TimeStamp;
177 };
178 
179 
183 class TimeStamp {
184  public:
185  // Constructors
186  TimeStamp() {}
187  explicit TimeStamp(const struct timeval &timestamp) : m_tv(timestamp) {}
188 
189  TimeStamp(const TimeStamp &other) : m_tv(other.m_tv) {}
190 
191  // Assignable
192  TimeStamp& operator=(const TimeStamp& other);
193  TimeStamp& operator=(const struct timeval &tv);
194 
195  // Comparables
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  bool operator<(const TimeStamp &other) const { return m_tv < other.m_tv; }
201  bool operator<=(const TimeStamp &other) const { return m_tv <= other.m_tv; }
202 
203  // Arithmetic
204  TimeStamp &operator+=(const TimeInterval &interval);
205  TimeStamp &operator-=(const TimeInterval &interval);
206  const TimeStamp operator+(const TimeInterval &interval) const;
207  const TimeInterval operator-(const TimeStamp &other) const;
208  const TimeStamp operator-(const TimeInterval &interval) const;
209 
210  // Various other methods.
211  bool IsSet() const { return m_tv.IsSet(); }
212 
213  time_t Seconds() const { return m_tv.Seconds(); }
214  int32_t MicroSeconds() const { return m_tv.MicroSeconds(); }
215 
216  std::string ToString() const { return m_tv.ToString(); }
217 
218  friend std::ostream& operator<<(std::ostream &out,
219  const TimeStamp &timestamp) {
220  return out << timestamp.m_tv.ToString();
221  }
222 
223  private:
224  BaseTimeVal m_tv;
225 
226  explicit TimeStamp(const BaseTimeVal &time_val) : m_tv(time_val) {}
227 };
228 
229 
233 class Clock {
234  public:
235  Clock() {}
236  virtual ~Clock() {}
237  virtual void CurrentTime(TimeStamp *timestamp) const;
238 
239  private:
240  DISALLOW_COPY_AND_ASSIGN(Clock);
241 };
242 
243 
247 class MockClock: public Clock {
248  public:
249  MockClock() : Clock() {}
250 
251  // Advance the time
252  void AdvanceTime(const TimeInterval &interval);
253  void AdvanceTime(int32_t sec, int32_t usec);
254 
255  void CurrentTime(TimeStamp *timestamp) const;
256 
257  private:
258  TimeInterval m_offset;
259 };
260 } // namespace ola
261 #endif // INCLUDE_OLA_CLOCK_H_
A time interval, with usecond accuracy.
Definition: Clock.h:130
int64_t InMilliSeconds() const
Returns the entire BaseTimeVal as milliseconds.
Definition: Clock.cpp:127
Definition: Clock.h:50
int64_t AsInt() const
Returns the entire BaseTimeVal as microseconds.
Definition: Clock.cpp:132
Used to get the current time.
Definition: Clock.h:233
time_t Seconds() const
Returns the seconds portion of the BaseTimeVal.
Definition: Clock.h:88
Helper macros.
int32_t MicroSeconds() const
Returns the microseconds portion of the BaseTimeVal.
Definition: Clock.h:93
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: Clock.h:247
Represents a point in time with microsecond accuracy.
Definition: Clock.h:183