OpenTTD Source 20241224-master-gee860a5c8e
IntervalTimer< TTimerType > Class Template Reference

An interval timer will fire every interval, and will continue to fire until it is deleted. More...

#include <timer.h>

Inheritance diagram for IntervalTimer< TTimerType >:
BaseTimer< TTimerType >

Public Types

using TPeriod = typename TTimerType::TPeriod
 
using TElapsed = typename TTimerType::TElapsed
 
- Public Types inherited from BaseTimer< TTimerType >
using TPeriod = typename TTimerType::TPeriod
 
using TElapsed = typename TTimerType::TElapsed
 
using TStorage = typename TTimerType::TStorage
 

Public Member Functions

 IntervalTimer (const TPeriod interval, std::function< void(uint)> callback)
 Create a new interval timer.
 
void SetInterval (const TPeriod interval, bool reset=true)
 Set a new interval for the timer.
 
- Public Member Functions inherited from BaseTimer< TTimerType >
 BaseTimer (const TPeriod period)
 Create a new timer.
 
virtual ~BaseTimer ()
 Delete the timer.
 

Private Member Functions

void Elapsed (TElapsed count) override
 Called by the timer manager to notify the timer that the given amount of time has elapsed.
 
void Elapsed (TimerGameEconomy::TElapsed trigger)
 
void Elapsed (TimerGameRealtime::TElapsed delta)
 

Private Attributes

std::function< void(uint)> callback
 

Additional Inherited Members

- Data Fields inherited from BaseTimer< TTimerType >
TPeriod period
 The period of the timer.
 
TStorage storage = {}
 The storage of the timer.
 

Detailed Description

template<typename TTimerType>
class IntervalTimer< TTimerType >

An interval timer will fire every interval, and will continue to fire until it is deleted.

The callback receives how many times the timer has fired since the last time it fired. It will always try to fire every interval, but in times of severe stress it might be late.

Each Timer-type needs to implement the Elapsed() method, and call the callback if needed.

Setting the period to zero disables the interval. It can be reenabled at any time by calling SetInterval() with a non-zero period.

Definition at line 76 of file timer.h.

Member Typedef Documentation

◆ TElapsed

template<typename TTimerType >
using IntervalTimer< TTimerType >::TElapsed = typename TTimerType::TElapsed

Definition at line 79 of file timer.h.

◆ TPeriod

template<typename TTimerType >
using IntervalTimer< TTimerType >::TPeriod = typename TTimerType::TPeriod

Definition at line 78 of file timer.h.

Constructor & Destructor Documentation

◆ IntervalTimer()

template<typename TTimerType >
IntervalTimer< TTimerType >::IntervalTimer ( const TPeriod  interval,
std::function< void(uint)>  callback 
)
inline

Create a new interval timer.

Parameters
intervalThe interval between each callback.
callbackThe callback to call when the interval has passed.

Definition at line 87 of file timer.h.

Member Function Documentation

◆ Elapsed() [1/3]

template<typename TTimerType >
void IntervalTimer< TTimerType >::Elapsed ( TElapsed  delta)
overrideprivatevirtual

Called by the timer manager to notify the timer that the given amount of time has elapsed.

Parameters
deltaDepending on the time type, this is either in milliseconds or in ticks.

Implements BaseTimer< TTimerType >.

◆ Elapsed() [2/3]

void IntervalTimer< TimerGameEconomy >::Elapsed ( TimerGameEconomy::TElapsed  trigger)
private

Definition at line 105 of file timer_game_economy.cpp.

◆ Elapsed() [3/3]

void IntervalTimer< TimerGameRealtime >::Elapsed ( TimerGameRealtime::TElapsed  delta)
private

Definition at line 21 of file timer_game_realtime.cpp.

◆ SetInterval()

template<typename TTimerType >
void IntervalTimer< TTimerType >::SetInterval ( const TPeriod  interval,
bool  reset = true 
)
inline

Set a new interval for the timer.

Parameters
intervalThe interval between each callback.
resetWhether to reset the timer to zero.

Definition at line 99 of file timer.h.

References TimerManager< TTimerType >::ChangeRegisteredTimerPeriod(), and BaseTimer< TTimerType >::storage.

Referenced by ChangeAutosaveFrequency(), and ChangeNetworkRestartTime().

Field Documentation

◆ callback

template<typename TTimerType >
std::function<void(uint)> IntervalTimer< TTimerType >::callback
private

Definition at line 106 of file timer.h.


The documentation for this class was generated from the following file: