OpenTTD Source  20241108-master-g80f628063a
timer_manager.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 
11 #ifndef TIMER_MANAGER_H
12 #define TIMER_MANAGER_H
13 
14 #include "../stdafx.h"
15 
16 template <typename TTimerType>
17 class BaseTimer;
18 
26 template <typename TTimerType>
27 class TimerManager {
28 public:
29  using TPeriod = typename TTimerType::TPeriod;
30  using TElapsed = typename TTimerType::TElapsed;
31 
32  /* Avoid copying this object; it is a singleton object. */
33  TimerManager(TimerManager const &) = delete;
34  TimerManager &operator=(TimerManager const &) = delete;
35 
42  {
43 #ifdef WITH_ASSERT
44  Validate(timer.period);
45 #endif /* WITH_ASSERT */
46  GetTimers().insert(&timer);
47  }
48 
55  {
56  GetTimers().erase(&timer);
57  }
58 
65  static void ChangeRegisteredTimerPeriod(BaseTimer<TTimerType> &timer, TPeriod new_period)
66  {
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);
71  }
72 
73 #ifdef WITH_ASSERT
85  static void Validate(TPeriod period);
86 #endif /* WITH_ASSERT */
87 
97  static bool Elapsed(TElapsed value);
98 
99 private:
107  bool operator() (BaseTimer<TTimerType> *a, BaseTimer<TTimerType> *b) const
108  {
109  if (a->period == b->period) return a < b;
110  return a->period < b->period;
111  }
112  };
113 
115  static std::set<BaseTimer<TTimerType> *, base_timer_sorter> &GetTimers()
116  {
117  static std::set<BaseTimer<TTimerType> *, base_timer_sorter> timers;
118  return timers;
119  }
120 };
121 
122 #endif /* TIMER_MANAGER_H */
The base where every other type of timer is derived from.
Definition: timer.h:22
TPeriod period
The period of the timer.
Definition: timer.h:49
The TimerManager manages a single Timer-type.
Definition: timer_manager.h:27
static std::set< BaseTimer< TTimerType > *, base_timer_sorter > & GetTimers()
Singleton list, to store all the active timers.
static void UnregisterTimer(BaseTimer< TTimerType > &timer)
Unregister a timer.
Definition: timer_manager.h:54
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
static void RegisterTimer(BaseTimer< TTimerType > &timer)
Register a timer.
Definition: timer_manager.h:41
static void ChangeRegisteredTimerPeriod(BaseTimer< TTimerType > &timer, TPeriod new_period)
Change the period of a registered timer.
Definition: timer_manager.h:65