Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / timer / timer_manager.h
blob92e45ccc17e1bd25192656d8fca550672991daa2
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_manager.h Definition of the TimerManager */
9 /** @note don't include this file; include "timer.h". */
11 #ifndef TIMER_MANAGER_H
12 #define TIMER_MANAGER_H
14 #include "../stdafx.h"
16 template <typename TTimerType>
17 class BaseTimer;
19 /**
20 * The TimerManager manages a single Timer-type.
22 * It allows for automatic registration and unregistration of timers like Interval and OneShot.
24 * Each Timer-type needs to implement the Elapsed() method, and distribute that to the timers if needed.
26 template <typename TTimerType>
27 class TimerManager {
28 public:
29 using TPeriod = typename TTimerType::TPeriod;
30 using TElapsed = typename TTimerType::TElapsed;
32 /* Avoid copying this object; it is a singleton object. */
33 TimerManager(TimerManager const &) = delete;
34 TimerManager &operator=(TimerManager const &) = delete;
36 /**
37 * Register a timer.
39 * @param timer The timer to register.
41 static void RegisterTimer(BaseTimer<TTimerType> &timer)
43 #ifdef WITH_ASSERT
44 Validate(timer.period);
45 #endif /* WITH_ASSERT */
46 GetTimers().insert(&timer);
49 /**
50 * Unregister a timer.
52 * @param timer The timer to unregister.
54 static void UnregisterTimer(BaseTimer<TTimerType> &timer)
56 GetTimers().erase(&timer);
59 /**
60 * Change the period of a registered timer.
62 * @param timer The timer to change the period of.
63 * @param new_period The new period value.
65 static void ChangeRegisteredTimerPeriod(BaseTimer<TTimerType> &timer, TPeriod new_period)
67 /* Unregistration and re-registration is necessary because the period is used as the sort key in base_timer_sorter */
68 UnregisterTimer(timer);
69 timer.period = new_period;
70 RegisterTimer(timer);
73 #ifdef WITH_ASSERT
74 /**
75 * Validate that a new period is actually valid.
77 * For most timers this is not an issue, but some want to make sure their
78 * period is unique, to ensure deterministic game-play.
80 * This is meant purely to protect a developer from making a mistake.
81 * As such, assert() when validation fails.
83 * @param period The period to validate.
85 static void Validate(TPeriod period);
86 #endif /* WITH_ASSERT */
88 /**
89 * Called when time for this timer elapsed.
91 * The implementation per type is different, but they all share a similar goal:
92 * Call the Elapsed() method of all active timers.
94 * @param value The amount of time that has elapsed.
95 * @return True iff time has progressed.
97 static bool Elapsed(TElapsed value);
99 private:
101 * Sorter for timers.
103 * It will sort based on the period, smaller first. If the period is the
104 * same, it will sort based on the pointer value.
106 struct base_timer_sorter {
107 bool operator() (BaseTimer<TTimerType> *a, BaseTimer<TTimerType> *b) const
109 if (a->period == b->period) return a < b;
110 return a->period < b->period;
114 /** Singleton list, to store all the active timers. */
115 static std::set<BaseTimer<TTimerType> *, base_timer_sorter> &GetTimers()
117 static std::set<BaseTimer<TTimerType> *, base_timer_sorter> timers;
118 return timers;
122 #endif /* TIMER_MANAGER_H */