OpenTTD Source: src/timer/timer_window.cpp Source File

OpenTTD Source 15.0-beta1

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
19template<>
20void 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
37template<>
38void 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
51template<>
52bool 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. */
55 auto timers = TimerManager<TimerWindow>::GetTimers();
56
57 for (auto timer : timers) {
58 timer->Elapsed(delta);
59 }
60
61 return true;
62}
63
64#ifdef WITH_ASSERT
65template<>
66void TimerManager<TimerWindow>::Validate(TimerWindow::TPeriod)
67{
68}
69#endif /* WITH_ASSERT */
IntervalTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
TimeoutTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
TimerManager
The TimerManager manages a single Timer-type.
Definition timer_manager.h:27
TimerManager::GetTimers
static std::set< BaseTimer< TTimerType > *, base_timer_sorter > & GetTimers()
Singleton list, to store all the active timers.
Definition timer_manager.h:115
TimerManager::Elapsed
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
timer.h
Definition of Interval and OneShot timers.
timer_window.h
Definition of the Window system.