BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
timer.hpp
Go to the documentation of this file.
1 #ifndef TIMER_HPP_
2 #define TIMER_HPP_
3 
4 #include <BoosterSeat/clock.hpp>
6 
7 namespace bst {
8 
10 class ITimer {
11 public:
13  virtual ~ITimer() = default;
14 
16  virtual void reset() = 0;
17 
19  virtual bool isDone() const = 0;
20 
23  virtual void setTimeout(int timeout_ms) = 0;
24 
26  virtual void setDone() = 0;
27 };
28 
30 class Timer : public ITimer {
31 public:
36  Timer(int timeout_ms);
37 
42  Timer();
43 
47  void reset() override;
48 
53  bool isDone() const override;
54 
59  void setTimeout(int timeout_ms) override;
60 
64  void setDone() override;
65 
66 private:
69 };
70 
72 class SoftwareTimer : public ITimer {
73 public:
75  SoftwareTimer() = default;
76 
78  void reset() override {
79  is_done_ = false;
80  };
81 
83  bool isDone() const override {
84  return is_done_;
85  };
86 
88  void setTimeout(int timeout_ms) override {
89  (void)timeout_ms;
90  };
91 
93  void setDone() override {
94  is_done_ = true;
95  };
96 
98  void setNotDone() {
99  is_done_ = false;
100  };
101 
102 private:
104  bool is_done_ = false;
105 };
106 
107 } // namespace bst
108 
109 #endif /* TIMER_HPP_ */
Interface for a timer class.
Definition: timer.hpp:10
virtual void reset()=0
Reset the timer to the current time.
virtual void setTimeout(int timeout_ms)=0
Set the timeout of the timer. Does not reset the timer.
virtual ~ITimer()=default
Destructor.
virtual void setDone()=0
Set the timer to be done. Changes the start time.
virtual bool isDone() const =0
Check if the timer has expired.
A software timer class for manual control.
Definition: timer.hpp:72
void setDone() override
Set the timer to done.
Definition: timer.hpp:93
void reset() override
Reset the timer - Sets the timer to not done.
Definition: timer.hpp:78
void setTimeout(int timeout_ms) override
Stubbed out - Does nothing.
Definition: timer.hpp:88
SoftwareTimer()=default
Default constructor.
bool isDone() const override
Check if the timer is done.
Definition: timer.hpp:83
void setNotDone()
Set the timer to not done.
Definition: timer.hpp:98
bool is_done_
The done state of the timer.
Definition: timer.hpp:104
A simple timer class for timing events.
Definition: timer.hpp:30
bool isDone() const override
Check if the timer has expired.
Definition: timer.cpp:18
void setDone() override
Set the timer to be done. Changes the start time.
Definition: timer.cpp:30
int timeout_ms_
Definition: timer.hpp:68
void setTimeout(int timeout_ms) override
Set the timeout of the timer.
Definition: timer.cpp:23
void reset() override
Reset the timer to the current time.
Definition: timer.cpp:14
Timer()
Create a new timer with a timeout of 0. Must call setTimeout before using.
Definition: timer.cpp:11
bst::clck::TimePoint start_
Definition: timer.hpp:67
std::chrono::time_point< Clock > TimePoint
Definition: clock.hpp:16
TimePoint now()
Definition: clock.hpp:19
Definition: filesystem.cpp:34