Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / timer / timer.h
blob982b86732c0c3a3a8447936a68f6a877b500242f
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 */
8 /** @file timer.h Definition of Interval and OneShot timers */
10 #ifndef TIMER_H
11 #define TIMER_H
13 #include "timer_manager.h"
16 /**
17 * The base where every other type of timer is derived from.
19 * Never use this class directly yourself.
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;
28 /**
29 * Create a new timer.
31 * @param period The period of the timer.
33 [[nodiscard]] BaseTimer(const TPeriod period) :
34 period(period)
36 TimerManager<TTimerType>::RegisterTimer(*this);
39 /**
40 * Delete the timer.
42 virtual ~BaseTimer()
44 TimerManager<TTimerType>::UnregisterTimer(*this);
47 /* Although these variables are public, they are only public to make saveload easier; not for common use. */
49 TPeriod period; ///< The period of the timer.
50 TStorage storage = {}; ///< The storage of the timer.
52 protected:
53 /**
54 * Called by the timer manager to notify the timer that the given amount of time has elapsed.
56 * @param delta Depending on the time type, this is either in milliseconds or in ticks.
58 virtual void Elapsed(TElapsed delta) = 0;
60 /* To ensure only TimerManager can access Elapsed. */
61 friend class TimerManager<TTimerType>;
64 /**
65 * An interval timer will fire every interval, and will continue to fire until it is deleted.
67 * The callback receives how many times the timer has fired since the last time it fired.
68 * It will always try to fire every interval, but in times of severe stress it might be late.
70 * Each Timer-type needs to implement the Elapsed() method, and call the callback if needed.
72 * Setting the period to zero disables the interval. It can be reenabled at any time by
73 * calling SetInterval() with a non-zero period.
75 template <typename TTimerType>
76 class IntervalTimer : public BaseTimer<TTimerType> {
77 public:
78 using TPeriod = typename TTimerType::TPeriod;
79 using TElapsed = typename TTimerType::TElapsed;
81 /**
82 * Create a new interval timer.
84 * @param interval The interval between each callback.
85 * @param callback The callback to call when the interval has passed.
87 [[nodiscard]] IntervalTimer(const TPeriod interval, std::function<void(uint)> callback) :
88 BaseTimer<TTimerType>(interval),
89 callback(callback)
93 /**
94 * Set a new interval for the timer.
96 * @param interval The interval between each callback.
97 * @param reset Whether to reset the timer to zero.
99 void SetInterval(const TPeriod interval, bool reset = true)
101 TimerManager<TTimerType>::ChangeRegisteredTimerPeriod(*this, interval);
102 if (reset) this->storage = {};
105 private:
106 std::function<void(uint)> callback;
108 void Elapsed(TElapsed count) override;
112 * A timeout timer will fire once after the interval. You can reset it to fire again.
113 * The timer will never fire before the interval has passed, but in times of severe stress it might be late.
115 template <typename TTimerType>
116 class TimeoutTimer : public BaseTimer<TTimerType> {
117 public:
118 using TPeriod = typename TTimerType::TPeriod;
119 using TElapsed = typename TTimerType::TElapsed;
122 * Create a new timeout timer.
124 * By default the timeout starts aborted; you will have to call Reset() before it starts.
126 * @param timeout The timeout after which the timer will fire.
127 * @param callback The callback to call when the timeout has passed.
128 * @param start Whether to start the timer immediately. If false, you can call Reset() to start it.
130 [[nodiscard]] TimeoutTimer(const TPeriod timeout, std::function<void()> callback, bool start = false) :
131 BaseTimer<TTimerType>(timeout),
132 fired(!start),
133 callback(callback)
138 * Reset the timer, so it will fire again after the timeout.
140 void Reset()
142 this->fired = false;
143 this->storage = {};
147 * Reset the timer, so it will fire again after the timeout.
149 * @param timeout Set a new timeout for the next trigger.
151 void Reset(const TPeriod timeout)
153 TimerManager<TTimerType>::ChangeRegisteredTimerPeriod(*this, timeout);
154 this->fired = false;
155 this->storage = {};
159 * Abort the timer so it doesn't fire if it hasn't yet.
161 void Abort()
163 this->fired = true;
167 * Check whether the timeout occurred.
169 * @return True iff the timeout occurred.
171 bool HasFired() const
173 return this->fired;
176 /* Although these variables are public, they are only public to make saveload easier; not for common use. */
178 bool fired; ///< Whether the timeout has occurred.
180 private:
181 std::function<void()> callback;
183 void Elapsed(TElapsed count) override;
186 #endif /* TIMER_H */