OpenTTD Source  14.0-beta1
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 
59 #ifdef WITH_ASSERT
60 
71  static void Validate(TPeriod period);
72 #endif /* WITH_ASSERT */
73 
83  static bool Elapsed(TElapsed value);
84 
85 private:
93  bool operator() (BaseTimer<TTimerType> *a, BaseTimer<TTimerType> *b) const
94  {
95  if (a->period == b->period) return a < b;
96  return a->period < b->period;
97  }
98  };
99 
101  static std::set<BaseTimer<TTimerType> *, base_timer_sorter> &GetTimers()
102  {
103  static std::set<BaseTimer<TTimerType> *, base_timer_sorter> timers;
104  return timers;
105  }
106 };
107 
108 #endif /* TIMER_MANAGER_H */
BaseTimer
The base where every other type of timer is derived from.
Definition: timer.h:22
TimerManager::GetTimers
static std::set< BaseTimer< TTimerType > *, base_timer_sorter > & GetTimers()
Singleton list, to store all the active timers.
Definition: timer_manager.h:101
BaseTimer::period
TPeriod period
The period of the timer.
Definition: timer.h:49
TimerManager::base_timer_sorter
Sorter for timers.
Definition: timer_manager.h:92
TimerManager
The TimerManager manages a single Timer-type.
Definition: timer_manager.h:27
TimerManager::Elapsed
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
TimerManager::UnregisterTimer
static void UnregisterTimer(BaseTimer< TTimerType > &timer)
Unregister a timer.
Definition: timer_manager.h:54
TimerManager::RegisterTimer
static void RegisterTimer(BaseTimer< TTimerType > &timer)
Register a timer.
Definition: timer_manager.h:41