BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
stopwatch.hpp
Go to the documentation of this file.
1 
9 #ifndef STOPWATCH_HPP_
10 #define STOPWATCH_HPP_
11 
12 #include "clock.hpp"
13 #include "exception.hpp"
14 
15 namespace bst {
16 
17 class IStopwatch {
18 public:
19  virtual void start() = 0;
20  virtual void stop() = 0;
21  virtual void reset(bool stop = true) = 0;
22  virtual bool isRunning() const = 0;
23  virtual int64_t elapsedMicroseconds() const = 0;
24  virtual double elapsedMicrosecondsF() const = 0;
25  virtual int64_t elapsedMilliseconds() const = 0;
26  virtual double elapsedMillisecondsF() const = 0;
27  virtual int64_t elapsedSeconds() const = 0;
28  virtual double elapsedSecondsF() const = 0;
29  virtual void pause() = 0;
30  virtual void resume() = 0;
31  virtual ~IStopwatch() = default;
32 
33 protected:
35  enum class State { RUNNING, STOPPED };
36 
39 };
40 
43 class Stopwatch : public IStopwatch {
44 public:
46  Stopwatch() = default;
47 
49  ~Stopwatch() = default;
50 
53  void start() {
54  if (state_ == State::STOPPED) {
55  start_ = clock_.now();
57  }
58 #ifndef BOOSTERSEAT_NO_EXCEPTION
59  else {
60  throw BoosterSeatException("Stopwatch is already running.");
61  }
62 #endif
63  }
64 
66  void stop() {
67  if (state_ == State::RUNNING) {
68  stop_time_ = clock_.now();
71  }
72 #ifndef BOOSTERSEAT_NO_EXCEPTION
73  else {
74  throw BoosterSeatException("Stopwatch is not running.");
75  }
76 #endif
77  }
78 
82  void reset(bool stop = true) {
83  elapsed_ = clck::Duration::zero();
84  start_ = clock_.now();
85 
86  if (stop) {
88  }
89  }
90 
92  bool isRunning() const {
93  return state_ == State::RUNNING;
94  }
95 
98 
100  int64_t elapsedMicroseconds() const {
101  return std::chrono::duration_cast<clck::units::Microseconds>(getElapsed())
102  .count();
103  }
105  double elapsedMicrosecondsF() const {
106  return std::chrono::duration_cast<clck::units::Microseconds>(getElapsed())
107  .count();
108  }
109 
111  int64_t elapsedMilliseconds() const {
112  return std::chrono::duration_cast<clck::units::Milliseconds>(getElapsed())
113  .count();
114  }
116  double elapsedMillisecondsF() const {
117  return std::chrono::duration_cast<clck::units::Milliseconds>(getElapsed())
118  .count();
119  }
120 
122  int64_t elapsedSeconds() const {
123  return std::chrono::duration_cast<clck::units::Seconds>(getElapsed())
124  .count();
125  }
126 
128  double elapsedSecondsF() const {
129  return std::chrono::duration_cast<clck::units::Seconds>(getElapsed())
130  .count();
131  }
132 
134 
136  inline void pause() {
137  stop();
138  }
139 
141  inline void resume() {
142  start();
143  }
144 
145 protected:
149  clck::Duration elapsed = elapsed_;
150  if (state_ == State::RUNNING) {
151  elapsed += clock_.now() - start_;
152  }
153  return elapsed;
154  }
155 
156 private:
158  clck::TimePoint start_ = clck::TimePoint::min();
159  clck::TimePoint stop_time_ = clck::TimePoint::min();
160  clck::Duration elapsed_ = clck::Duration::zero();
162 };
163 
166 public:
167  SoftwareStopwatch() = default;
168  ~SoftwareStopwatch() = default;
169 
170  void start() {
171  if (state_ == State::STOPPED) {
172  start_ = 0;
174  }
175 #ifndef BOOSTERSEAT_NO_EXCEPTION
176  else {
177  throw BoosterSeatException("Stopwatch is already running.");
178  }
179 #endif
180  }
181 
182  void stop() {
183  if (state_ == State::RUNNING) {
184  stop_time_ = 0;
187  }
188 #ifndef BOOSTERSEAT_NO_EXCEPTION
189  else {
190  throw BoosterSeatException("Stopwatch is not running.");
191  }
192 #endif
193  }
194 
195  void reset(bool stop = true) {
196  elapsed_ = 0;
197 
198  if (stop) {
200  }
201  }
202 
203  bool isRunning() const {
204  return state_ == State::RUNNING;
205  }
206 
207  int64_t elapsedMicroseconds() const {
208  return elapsed_ * 1000;
209  }
210 
211  double elapsedMicrosecondsF() const {
212  return elapsed_ * 1000.0;
213  }
214 
215  int64_t elapsedMilliseconds() const {
216  return elapsed_;
217  }
218 
219  double elapsedMillisecondsF() const {
220  return elapsed_;
221  }
222 
223  int64_t elapsedSeconds() const {
224  return elapsed_ / 1000;
225  }
226 
227  double elapsedSecondsF() const {
228  return elapsed_ / 1000.0;
229  }
230 
231  void pause() {
232  stop();
233  }
234 
235  void resume() {
236  start();
237  }
238 
239  void tickIfRunning(int64_t microseconds) {
240  if (state_ == State::RUNNING) {
241  elapsed_ += microseconds;
242  }
243  }
244 
245  void tick(int64_t microseconds) {
246  elapsed_ += microseconds;
247  }
248 
249 protected:
250  int64_t getElapsed() const {
251  int64_t elapsed = elapsed_;
252  if (state_ == State::RUNNING) {
253  elapsed += stop_time_ - start_;
254  }
255  return elapsed;
256  }
257 
258 private:
259  int64_t start_ = 0;
260  int64_t stop_time_ = 0;
261  int64_t elapsed_ = 0;
263 };
264 
265 } // namespace bst
266 
267 #endif // BOOST_ERSEAT_HPP_
Definition: exception.hpp:99
Definition: stopwatch.hpp:17
virtual int64_t elapsedSeconds() const =0
virtual int64_t elapsedMicroseconds() const =0
virtual double elapsedSecondsF() const =0
State
The state of the stopwatch.
Definition: stopwatch.hpp:35
virtual ~IStopwatch()=default
virtual double elapsedMillisecondsF() const =0
virtual void stop()=0
virtual void resume()=0
virtual double elapsedMicrosecondsF() const =0
virtual int64_t elapsedMilliseconds() const =0
virtual void pause()=0
virtual bool isRunning() const =0
State state_
The state of the stopwatch.
Definition: stopwatch.hpp:38
virtual void reset(bool stop=true)=0
virtual void start()=0
A manually controlled stopwatch that does not use the system clock.
Definition: stopwatch.hpp:165
int64_t elapsed_
Definition: stopwatch.hpp:261
void start()
Definition: stopwatch.hpp:170
int64_t elapsedMicroseconds() const
Definition: stopwatch.hpp:207
double elapsedMillisecondsF() const
Definition: stopwatch.hpp:219
double elapsedSecondsF() const
Definition: stopwatch.hpp:227
int64_t elapsedMilliseconds() const
Definition: stopwatch.hpp:215
void tickIfRunning(int64_t microseconds)
Definition: stopwatch.hpp:239
void pause()
Definition: stopwatch.hpp:231
bool isRunning() const
Definition: stopwatch.hpp:203
State state_
Definition: stopwatch.hpp:262
int64_t getElapsed() const
Definition: stopwatch.hpp:250
void stop()
Definition: stopwatch.hpp:182
double elapsedMicrosecondsF() const
Definition: stopwatch.hpp:211
int64_t stop_time_
Definition: stopwatch.hpp:260
int64_t start_
Definition: stopwatch.hpp:259
void resume()
Definition: stopwatch.hpp:235
~SoftwareStopwatch()=default
int64_t elapsedSeconds() const
Definition: stopwatch.hpp:223
void reset(bool stop=true)
Definition: stopwatch.hpp:195
void tick(int64_t microseconds)
Definition: stopwatch.hpp:245
A simple stopwatch class. Basic start (resume), stop (pause), and reset functionality.
Definition: stopwatch.hpp:43
int64_t elapsedMicroseconds() const
Get the elapsed time in microseconds.
Definition: stopwatch.hpp:100
double elapsedSecondsF() const
Get the elapsed time in seconds in a floating point format.
Definition: stopwatch.hpp:128
clck::TimePoint stop_time_
Definition: stopwatch.hpp:159
void reset(bool stop=true)
Resets the elapsed time. The stopwatch will keep running by default.
Definition: stopwatch.hpp:82
clck::Duration elapsed_
Definition: stopwatch.hpp:160
bool isRunning() const
Returns the current state of the stopwatch.
Definition: stopwatch.hpp:92
double elapsedMillisecondsF() const
Get the elapsed time in milliseconds in a floating point format.
Definition: stopwatch.hpp:116
void resume()
alias for start() as it does what you want it to do.
Definition: stopwatch.hpp:141
clck::Clock clock_
Definition: stopwatch.hpp:157
~Stopwatch()=default
Default destructor.
Stopwatch()=default
Construct a new Stopwatch object. The stopwatch is stopped.
void pause()
alias for stop() as it does what you want it to do.
Definition: stopwatch.hpp:136
void start()
Start the stopwatch.
Definition: stopwatch.hpp:53
clck::TimePoint start_
Definition: stopwatch.hpp:158
State state_
Definition: stopwatch.hpp:161
Scale
The resolution/scale of the time returned by elapsed()
Definition: stopwatch.hpp:97
void stop()
Stops/Pauses the stopwatch. Does not reset the elapsed time.
Definition: stopwatch.hpp:66
int64_t elapsedMilliseconds() const
Get the elapsed time in milliseconds.
Definition: stopwatch.hpp:111
double elapsedMicrosecondsF() const
Get the elapsed time in microseconds in a floating point format.
Definition: stopwatch.hpp:105
int64_t elapsedSeconds() const
Get the elapsed time in seconds.
Definition: stopwatch.hpp:122
bst::clck::Duration getElapsed() const
Handles the timekeeping.
Definition: stopwatch.hpp:148
std::chrono::time_point< Clock > TimePoint
Definition: clock.hpp:16
std::chrono::high_resolution_clock Clock
Definition: clock.hpp:15
Clock::duration Duration
Definition: clock.hpp:17
Definition: filesystem.cpp:34