OpenTTD Source: src/timer/timer.h Source File

OpenTTD Source 14.1

timer.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef TIMER_H
11 #define TIMER_H
12
13 #include "timer_manager.h"
14
15
21 template <typename TTimerType>
22 class BaseTimer {
23 public:
24 using TPeriod = typename TTimerType::TPeriod;
25 using TElapsed = typename TTimerType::TElapsed;
26 using TStorage = typename TTimerType::TStorage;
27
33 [[nodiscard]] BaseTimer(const TPeriod period) :
34 period(period)
35 {
36 TimerManager<TTimerType>::RegisterTimer(*this);
37 }
38
42 virtual ~BaseTimer()
43 {
44 TimerManager<TTimerType>::UnregisterTimer(*this);
45 }
46
47 /* Although these variables are public, they are only public to make saveload easier; not for common use. */
48
49 TPeriod period;
50 TStorage storage = {};
51
52 protected:
58 virtual void Elapsed(TElapsed delta) = 0;
59
60 /* To ensure only TimerManager can access Elapsed. */
61 friend class TimerManager<TTimerType>;
62 };
63
75 template <typename TTimerType>
76 class IntervalTimer : public BaseTimer<TTimerType> {
77 public:
78 using TPeriod = typename TTimerType::TPeriod;
79 using TElapsed = typename TTimerType::TElapsed;
80
87 [[nodiscard]] IntervalTimer(const TPeriod interval, std::function<void(uint)> callback) :
88 BaseTimer<TTimerType>(interval),
89 callback(callback)
90 {
91 }
92
99 void SetInterval(const TPeriod interval, bool reset = true)
100 {
101 TimerManager<TTimerType>::ChangeRegisteredTimerPeriod(*this, interval);
102 if (reset) this->storage = {};
103 }
104
105 private:
106 std::function<void(uint)> callback;
107
108 void Elapsed(TElapsed count) override;
109 };
110
115 template <typename TTimerType>
116 class TimeoutTimer : public BaseTimer<TTimerType> {
117 public:
118 using TPeriod = typename TTimerType::TPeriod;
119 using TElapsed = typename TTimerType::TElapsed;
120
130 [[nodiscard]] TimeoutTimer(const TPeriod timeout, std::function<void()> callback, bool start = false) :
131 BaseTimer<TTimerType>(timeout),
132 fired(!start),
133 callback(callback)
134 {
135 }
136
140 void Reset()
141 {
142 this->fired = false;
143 this->storage = {};
144 }
145
151 void Reset(const TPeriod timeout)
152 {
153 TimerManager<TTimerType>::ChangeRegisteredTimerPeriod(*this, timeout);
154 this->fired = false;
155 this->storage = {};
156 }
157
161 void Abort()
162 {
163 this->fired = true;
164 }
165
171 bool HasFired() const
172 {
173 return this->fired;
174 }
175
176 /* Although these variables are public, they are only public to make saveload easier; not for common use. */
177
178 bool fired;
179
180 private:
181 std::function<void()> callback;
182
183 void Elapsed(TElapsed count) override;
184 };
185
186 #endif /* TIMER_H */
BaseTimer
The base where every other type of timer is derived from.
Definition: timer.h:22
TimeoutTimer::TimeoutTimer
TimeoutTimer(const TPeriod timeout, std::function< void()> callback, bool start=false)
Create a new timeout timer.
Definition: timer.h:130
IntervalTimer
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition: timer.h:76
BaseTimer::period
TPeriod period
The period of the timer.
Definition: timer.h:49
TimeoutTimer::HasFired
bool HasFired() const
Check whether the timeout occurred.
Definition: timer.h:171
TimeoutTimer::Reset
void Reset()
Reset the timer, so it will fire again after the timeout.
Definition: timer.h:140
TimerManager::ChangeRegisteredTimerPeriod
static void ChangeRegisteredTimerPeriod(BaseTimer< TTimerType > &timer, TPeriod new_period)
Change the period of a registered timer.
Definition: timer_manager.h:65
TimeoutTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
IntervalTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
timer_manager.h
TimeoutTimer::fired
bool fired
Whether the timeout has occurred.
Definition: timer.h:178
TimerManager
The TimerManager manages a single Timer-type.
Definition: timer_manager.h:27
BaseTimer::BaseTimer
BaseTimer(const TPeriod period)
Create a new timer.
Definition: timer.h:33
IntervalTimer::IntervalTimer
IntervalTimer(const TPeriod interval, std::function< void(uint)> callback)
Create a new interval timer.
Definition: timer.h:87
TimeoutTimer
A timeout timer will fire once after the interval.
Definition: timer.h:116
TimeoutTimer::Reset
void Reset(const TPeriod timeout)
Reset the timer, so it will fire again after the timeout.
Definition: timer.h:151
TimerManager::UnregisterTimer
static void UnregisterTimer(BaseTimer< TTimerType > &timer)
Unregister a timer.
Definition: timer_manager.h:54
BaseTimer::storage
TStorage storage
The storage of the timer.
Definition: timer.h:50
IntervalTimer::SetInterval
void SetInterval(const TPeriod interval, bool reset=true)
Set a new interval for the timer.
Definition: timer.h:99
TimerManager::RegisterTimer
static void RegisterTimer(BaseTimer< TTimerType > &timer)
Register a timer.
Definition: timer_manager.h:41
BaseTimer::~BaseTimer
virtual ~BaseTimer()
Delete the timer.
Definition: timer.h:42
BaseTimer::Elapsed
virtual void Elapsed(TElapsed delta)=0
Called by the timer manager to notify the timer that the given amount of time has elapsed.
TimeoutTimer::Abort
void Abort()
Abort the timer so it doesn't fire if it hasn't yet.
Definition: timer.h:161