Louvre  v2.13.0-1
C++ library for Wayland compositors
Public Types | Public Member Functions | Static Public Member Functions | List of all members
LTimer Class Reference

Delayed callbacks. More...

+ Inheritance diagram for LTimer:

Public Types

using Callback = std::function< void(LTimer *)>
 Type definition for the timer's timeout callback function. More...
 

Public Member Functions

 LTimer (const Callback &onTimeout=nullptr) noexcept
 Construct a re-usable LTimer instance with the provided timeout callback. More...
 
 ~LTimer ()
 Destructor for the LTimer class. More...
 
void setCallback (const Callback &onTimeout) noexcept
 Set the callback function to be executed when the timer expires. More...
 
UInt32 interval () const noexcept
 Retrieves the interval of the timer in milliseconds. More...
 
bool running () const noexcept
 Checks if the timer is currently running. More...
 
void cancel () noexcept
 Cancels the timer without invoking the callback. More...
 
void stop () noexcept
 Stops the timer invoking the callback immediatly. More...
 
bool start (UInt32 intervalMs) noexcept
 Starts the timer with the specified interval. More...
 
- Public Member Functions inherited from LObject
 LObject (const LObject &) noexcept
 Copy constructor. More...
 
LObjectoperator= (const LObject &) noexcept
 Assignment operator (each object has its own individual LWeak reference count). More...
 
void setUserData (UIntPtr data) const noexcept
 Store an unsigned integer value/pointer. More...
 
UIntPtr userData () const noexcept
 Retrieves the stored unsigned integer value/pointer. More...
 

Static Public Member Functions

static bool oneShot (UInt32 intervalMs, const Callback &onTimeout) noexcept
 Create a one-shot timer that is automatically started and destroyed on timeout or cancellation. More...
 

Additional Inherited Members

- Protected Member Functions inherited from LObject
 LObject () noexcept=default
 Constructor of the LObject class. More...
 
virtual ~LObject () noexcept
 Destructor of the LObject class. More...
 
void notifyDestruction () noexcept
 Notifies the object destruction. More...
 

Detailed Description

Delayed callbacks.

This class provides the capability to create and manage timers, allowing you to schedule time intervals in milliseconds and execute a specified callback function when the timer expires. It supports both re-usable timers and one-shot timers that can be automatically destroyed after timeout or cancellation.

Member Typedef Documentation

◆ Callback

using Callback = std::function<void(LTimer*)>

Type definition for the timer's timeout callback function.

Parameters
timerPointer to the LTimer instance that triggered the callback.

Constructor & Destructor Documentation

◆ LTimer()

LTimer ( const Callback onTimeout = nullptr)
noexcept

Construct a re-usable LTimer instance with the provided timeout callback.

Parameters
onTimeoutThe callback function to be executed when the timer expires.

◆ ~LTimer()

~LTimer ( )

Destructor for the LTimer class.

If the timer is destroyed while it is running, the associated callback function is not invoked.

Member Function Documentation

◆ oneShot()

bool oneShot ( UInt32  intervalMs,
const Callback onTimeout 
)
staticnoexcept

Create a one-shot timer that is automatically started and destroyed on timeout or cancellation.

Parameters
intervalMsThe interval for the timer in milliseconds.
onTimeoutThe callback function to be executed when the timer expires.
Returns
true on successfully, false otherwise.

◆ setCallback()

void setCallback ( const Callback onTimeout)
inlinenoexcept

Set the callback function to be executed when the timer expires.

Parameters
onTimeoutThe callback function to be executed when the timer expires.

◆ interval()

UInt32 interval ( ) const
inlinenoexcept

Retrieves the interval of the timer in milliseconds.

Returns
The interval of the timer.

◆ running()

bool running ( ) const
inlinenoexcept

Checks if the timer is currently running.

Returns
true if the timer is running, false otherwise.

◆ cancel()

void cancel ( )
noexcept

Cancels the timer without invoking the callback.

If the timer was created with oneShot() it is destroyed and the onTimeout callback is not called.

◆ stop()

void stop ( )
noexcept

Stops the timer invoking the callback immediatly.

If the timer was created with oneShot() it is destroyed after the callback is triggered.

◆ start()

bool start ( UInt32  intervalMs)
noexcept

Starts the timer with the specified interval.

Note
Calling start() on an already started timer will restart the timer without calling the callback. If this method is called within the timeout callback the timer is restarted even if it was created using oneShot().
Parameters
intervalMsThe interval for the timer in milliseconds.
Returns
true if the timer successfully started, false otherwise.