Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 intstead 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 
84  // Returns the seconds portion.
85  time_t Seconds() const { return m_tv.tv_sec; }
86  // Returns the microseconds portion
87  int32_t MicroSeconds() const { return static_cast<int32_t>(m_tv.tv_usec); }
88 
89  // Returns the entire BaseTimeVal as milliseconds
90  int64_t InMilliSeconds() const;
91 
92  // Returns the entire BaseTimeVal as microseconds.
93  int64_t AsInt() const;
94 
95  std::string ToString() const;
96 
97  private:
98  struct timeval m_tv;
99 
103  void TimerAdd(const struct timeval &tv1, const struct timeval &tv2,
104  struct timeval *result) const;
105 
109  void TimerSub(const struct timeval &tv1, const struct timeval &tv2,
110  struct timeval *result) const;
111 
112  void Set(int64_t interval_useconds);
113 };
114 
115 /*
116  * A time interval, with usecond accuracy.
117  */
119  public:
120  // Constructors
121  TimeInterval() {}
122  TimeInterval(int32_t sec, int32_t usec) : m_interval(sec, usec) {}
123  explicit TimeInterval(int64_t usec) : m_interval(usec) {}
124 
125  TimeInterval(const TimeInterval &other) : m_interval(other.m_interval) {}
126 
127  // Assignable
128  TimeInterval& operator=(const TimeInterval& other);
129 
130  // Comparables
131  bool operator==(const TimeInterval &other) const;
132  bool operator!=(const TimeInterval &other) const;
133  bool operator>(const TimeInterval &other) const;
134  bool operator>=(const TimeInterval &other) const;
135  bool operator<(const TimeInterval &other) const;
136  bool operator<=(const TimeInterval &other) const;
137 
138  // Arithmetic
139  TimeInterval& operator+=(const TimeInterval& other);
140  TimeInterval operator*(unsigned int i) const;
141 
142  // Various other methods.
143  bool IsZero() const { return !m_interval.IsSet(); }
144 
145  void AsTimeval(struct timeval *tv) const { m_interval.AsTimeval(tv); }
146 
147  time_t Seconds() const { return m_interval.Seconds(); }
148  int32_t MicroSeconds() const { return m_interval.MicroSeconds(); }
149 
150  int64_t InMilliSeconds() const { return m_interval.InMilliSeconds(); }
151  int64_t AsInt() const { return m_interval.AsInt(); }
152 
153  std::string ToString() const { return m_interval.ToString(); }
154 
155  friend std::ostream& operator<< (std::ostream &out,
156  const TimeInterval &interval) {
157  return out << interval.m_interval.ToString();
158  }
159 
160  private:
161  explicit TimeInterval(const BaseTimeVal &time_val) : m_interval(time_val) {}
162 
163  BaseTimeVal m_interval;
164  friend class TimeStamp;
165 };
166 
167 
168 /*
169  * Represents a point in time with usecond accuracy.
170  */
171 class TimeStamp {
172  public:
173  // Constructors
174  TimeStamp() {}
175  explicit TimeStamp(const struct timeval &timestamp) : m_tv(timestamp) {}
176 
177  TimeStamp(const TimeStamp &other) : m_tv(other.m_tv) {}
178 
179  // Assignable
180  TimeStamp& operator=(const TimeStamp& other);
181  TimeStamp& operator=(const struct timeval &tv);
182 
183  // Comparables
184  bool operator==(const TimeStamp &other) const { return m_tv == other.m_tv; }
185  bool operator!=(const TimeStamp &other) const { return m_tv != other.m_tv; }
186  bool operator>(const TimeStamp &other) const { return m_tv > other.m_tv; }
187  bool operator>=(const TimeStamp &other) const { return m_tv >= other.m_tv; }
188  bool operator<(const TimeStamp &other) const { return m_tv < other.m_tv; }
189  bool operator<=(const TimeStamp &other) const { return m_tv <= other.m_tv; }
190 
191  // Arithmetic
192  TimeStamp &operator+=(const TimeInterval &interval);
193  TimeStamp &operator-=(const TimeInterval &interval);
194  const TimeStamp operator+(const TimeInterval &interval) const;
195  const TimeInterval operator-(const TimeStamp &other) const;
196  const TimeStamp operator-(const TimeInterval &interval) const;
197 
198  // Various other methods.
199  bool IsSet() const { return m_tv.IsSet(); }
200 
201  time_t Seconds() const { return m_tv.Seconds(); }
202  int32_t MicroSeconds() const { return m_tv.MicroSeconds(); }
203 
204  std::string ToString() const { return m_tv.ToString(); }
205 
206  friend std::ostream& operator<<(std::ostream &out,
207  const TimeStamp &timestamp) {
208  return out << timestamp.m_tv.ToString();
209  }
210 
211  private:
212  BaseTimeVal m_tv;
213 
214  explicit TimeStamp(const BaseTimeVal &time_val) : m_tv(time_val) {}
215 };
216 
217 
221 class Clock {
222  public:
223  Clock() {}
224  virtual ~Clock() {}
225  virtual void CurrentTime(TimeStamp *timestamp) const;
226 
227  private:
228  DISALLOW_COPY_AND_ASSIGN(Clock);
229 };
230 
231 
235 class MockClock: public Clock {
236  public:
237  MockClock() : Clock() {}
238 
239  // Advance the time
240  void AdvanceTime(const TimeInterval &interval);
241  void AdvanceTime(int32_t sec, int32_t usec);
242 
243  void CurrentTime(TimeStamp *timestamp) const;
244 
245  private:
246  TimeInterval m_offset;
247 };
248 } // namespace ola
249 #endif // INCLUDE_OLA_CLOCK_H_