OpenTTD Source  20241108-master-g80f628063a
timer_window.cpp
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 
13 #include "../stdafx.h"
14 #include "timer.h"
15 #include "timer_window.h"
16 
17 #include "../safeguards.h"
18 
19 template<>
20 void IntervalTimer<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
21 {
22  if (this->period == std::chrono::milliseconds::zero()) return;
23 
24  this->storage.elapsed += delta;
25 
26  uint count = 0;
27  while (this->storage.elapsed >= this->period) {
28  this->storage.elapsed -= this->period;
29  count++;
30  }
31 
32  if (count > 0) {
33  this->callback(count);
34  }
35 }
36 
37 template<>
38 void TimeoutTimer<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
39 {
40  if (this->fired) return;
41  if (this->period == std::chrono::milliseconds::zero()) return;
42 
43  this->storage.elapsed += delta;
44 
45  if (this->storage.elapsed >= this->period) {
46  this->callback();
47  this->fired = true;
48  }
49 }
50 
51 template<>
52 bool TimerManager<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
53 {
54  /* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */
56 
57  for (auto timer : timers) {
58  timer->Elapsed(delta);
59  }
60 
61  return true;
62 }
63 
64 #ifdef WITH_ASSERT
65 template<>
66 void TimerManager<TimerWindow>::Validate(TimerWindow::TPeriod)
67 {
68 }
69 #endif /* WITH_ASSERT */
TStorage storage
The storage of the timer.
Definition: timer.h:50
TPeriod period
The period of the timer.
Definition: timer.h:49
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
bool fired
Whether the timeout has occurred.
Definition: timer.h:178
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
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 bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
Definition of Interval and OneShot timers.
Definition of the Window system.