ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::Timer Class Reference

Timer class to control the execution of systems. More...

#include <Timer.hpp>

Collaboration diagram for ecstasy::Timer:

Classes

struct  Rate
 
struct  TimeInterval
 

Public Types

enum class  Type { TimeInterval , Rate }
 Possible types of timers. More...
 
using TimePoint = std::chrono::time_point< std::chrono::system_clock >
 Type of time points.
 
using Interval = std::chrono::milliseconds
 Type of time intervals.
 

Public Member Functions

 Timer ()
 Construct a new Timer.
 
 Timer (Interval interval)
 Construct a new Timer with the given interval.
 
 Timer (std::uint32_t rate)
 Construct a new Timer with the given rate.
 
 Timer (const Timer &)=default
 Copy constructor.
 
 Timer (Timer &&)=default
 Move constructor.
 
 ~Timer ()=default
 Destroy the Timer.
 
Timeroperator= (const Timer &)=default
 Copy assignment operator.
 
Timeroperator= (Timer &&)=default
 Move assignment operator.
 
constexpr Type getType () const noexcept
 Get the Type of the timer.
 
constexpr TimePoint getLastTrigger () const noexcept
 Get the last time the timer was triggered.
 
void setRate (std::uint32_t rate) noexcept
 Set the Rate of the timer.
 
std::uint32_t getRate () const
 Get the Rate of the timer.
 
void setInterval (Interval interval) noexcept
 Set the Interval of the timer.
 
Interval getInterval () const
 Get the Interval of the timer.
 
bool trigger () noexcept
 Trigger the timer if it is time to do so.
 

Private Attributes

Type _type
 Type of the timer.
 
TimePoint _lastTrigger
 Last time the timer was triggered.
 
union { 
 
   TimeInterval   _timer 
 
   Rate   _rate 
 
};  
 Interval or rate of the timer.
 

Detailed Description

Timer class to control the execution of systems.

Timers can be assigned to systems or phases to control their execution.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 32 of file Timer.hpp.

Member Typedef Documentation

◆ Interval

Type of time intervals.

Definition at line 37 of file Timer.hpp.

◆ TimePoint

Type of time points.

Definition at line 35 of file Timer.hpp.

Member Enumeration Documentation

◆ Type

enum class ecstasy::Timer::Type
strong

Possible types of timers.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)
Enumerator
TimeInterval 

Timer that triggers every time interval. Ex: every 5 seconds.

Rate 

Timer that triggers at a fixed rate. Ex: every 5 frames.

Definition at line 56 of file Timer.hpp.

56 {
57 TimeInterval,
58 Rate,
59 };

Constructor & Destructor Documentation

◆ Timer() [1/5]

ecstasy::Timer::Timer ( )

Construct a new Timer.

Note
The default timer is a Rate with a 0 interval, ie run every frame.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 17 of file Timer.cpp.

18 {
20 setRate(0);
21 }
void setRate(std::uint32_t rate) noexcept
Set the Rate of the timer.
Definition Timer.cpp:33
TimePoint _lastTrigger
Last time the timer was triggered.
Definition Timer.hpp:219

◆ Timer() [2/5]

ecstasy::Timer::Timer ( Timer::Interval  interval)

Construct a new Timer with the given interval.

Parameters
[in]intervalInterval of the timer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 23 of file Timer.cpp.

23 : Timer()
24 {
25 setInterval(interval);
26 }
void setInterval(Interval interval) noexcept
Set the Interval of the timer.
Definition Timer.cpp:51
Timer()
Construct a new Timer.
Definition Timer.cpp:17

◆ Timer() [3/5]

ecstasy::Timer::Timer ( std::uint32_t  rate)

Construct a new Timer with the given rate.

Parameters
[in]rateRate of the timer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 28 of file Timer.cpp.

28 : Timer()
29 {
30 setRate(rate);
31 }

◆ Timer() [4/5]

ecstasy::Timer::Timer ( const Timer )
default

Copy constructor.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

◆ Timer() [5/5]

ecstasy::Timer::Timer ( Timer &&  )
default

Move constructor.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

◆ ~Timer()

ecstasy::Timer::~Timer ( )
default

Destroy the Timer.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Member Function Documentation

◆ getInterval()

Timer::Interval ecstasy::Timer::getInterval ( ) const

Get the Interval of the timer.

Returns
Interval Interval of the timer.
Exceptions
std::runtime_errorIf the timer is not an Interval timer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 57 of file Timer.cpp.

58 {
60 throw std::runtime_error("Timer is not of type Rate");
61 return _timer.interval;
62 }
TimeInterval _timer
Definition Timer.hpp:222
@ TimeInterval
Timer that triggers every time interval. Ex: every 5 seconds.
Type _type
Type of the timer.
Definition Timer.hpp:217

◆ getLastTrigger()

constexpr TimePoint ecstasy::Timer::getLastTrigger ( ) const
inlineconstexprnoexcept

Get the last time the timer was triggered.

Returns
TimePoint Last time the timer was triggered.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 154 of file Timer.hpp.

155 {
156 return _lastTrigger;
157 }

◆ getRate()

std::uint32_t ecstasy::Timer::getRate ( ) const

Get the Rate of the timer.

Returns
std::uint32_t Rate of the timer.
Exceptions
std::runtime_errorIf the timer is not a Rate timer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 44 of file Timer.cpp.

45 {
46 if (_type != Type::Rate)
47 throw std::runtime_error("Timer is not of type Rate");
48 return _rate.rate;
49 }
@ Rate
Timer that triggers at a fixed rate. Ex: every 5 frames.
std::uint32_t rate
Definition Timer.hpp:45

◆ getType()

constexpr Type ecstasy::Timer::getType ( ) const
inlineconstexprnoexcept

Get the Type of the timer.

Returns
Type Type of the timer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 141 of file Timer.hpp.

142 {
143 return _type;
144 }

◆ operator=() [1/2]

Timer & ecstasy::Timer::operator= ( const Timer )
default

Copy assignment operator.

Returns
Timer&
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

◆ operator=() [2/2]

Timer & ecstasy::Timer::operator= ( Timer &&  )
default

Move assignment operator.

Returns
Timer&
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

◆ setInterval()

void ecstasy::Timer::setInterval ( Timer::Interval  interval)
noexcept

Set the Interval of the timer.

This will convert the timer to an Interval timer.

Parameters
[in]intervalInterval of the timer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 51 of file Timer.cpp.

52 {
54 _timer.interval = interval;
55 }

◆ setRate()

void ecstasy::Timer::setRate ( std::uint32_t  rate)
noexcept

Set the Rate of the timer.

This will convert the timer to a Rate timer.

Parameters
[in]rateRate of the timer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 33 of file Timer.cpp.

34 {
36 if (rate == 0) {
37 rate = 1;
38 }
39 _rate.rate = rate;
40 // Reset the countdown to trigger to run the first time
42 }
std::uint32_t triggerCountdown
Definition Timer.hpp:46

◆ trigger()

bool ecstasy::Timer::trigger ( )
noexcept

Trigger the timer if it is time to do so.

Warning
This does not call the system or phase, it only checks if it is allowed to run now.
Returns
bool true if the timer was triggered, false otherwise.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-11-22)

Definition at line 64 of file Timer.cpp.

65 {
66 switch (_type) {
67 case Type::TimeInterval: {
69
70 if (tp - _timer.interval >= _lastTrigger) {
71 _lastTrigger = tp;
72 return true;
73 }
74 return false;
75 }
76 case Type::Rate: {
77 if (_rate.triggerCountdown == 0) {
80 return true;
81 }
83 return false;
84 }
85 }
86 return false;
87 }

Member Data Documentation

◆ [union]

union { ... } ecstasy::Timer

Interval or rate of the timer.

◆ _lastTrigger

TimePoint ecstasy::Timer::_lastTrigger
private

Last time the timer was triggered.

Definition at line 219 of file Timer.hpp.

◆ _rate

Rate ecstasy::Timer::_rate

Definition at line 223 of file Timer.hpp.

◆ _timer

TimeInterval ecstasy::Timer::_timer

Definition at line 222 of file Timer.hpp.

◆ _type

Type ecstasy::Timer::_type
private

Type of the timer.

Definition at line 217 of file Timer.hpp.


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