SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
Loading...
Searching...
No Matches
swt::Timer Class Reference

RAII timer wrapper with boost-style API and move semantics. More...

#include <Timer.h>

Collaboration diagram for swt::Timer:
Collaboration graph

Public Member Functions

 Timer (TimerId id, std::weak_ptr< SLLooper > looper)
 Constructor - creates timer handle.
 
 Timer (const Timer &)=delete
 Copy constructor - deleted (move-only semantics)
 
Timeroperator= (const Timer &)=delete
 Copy assignment - deleted (move-only semantics)
 
 Timer (Timer &&other) noexcept
 Move constructor - transfers timer ownership.
 
Timeroperator= (Timer &&other) noexcept
 Move assignment - transfers timer ownership.
 
 ~Timer ()
 Destructor - automatic timer cleanup.
 
void cancel ()
 Cancel the timer.
 
bool isActive () const
 Check if timer is currently active.
 
TimerId getId () const
 Get unique timer identifier.
 
void restart (uint64_t delay_ms)
 Restart timer with new delay (one-shot timers only)
 

Friends

class SLLooper
 Friend class declaration for internal access.
 

Detailed Description

RAII timer wrapper with boost-style API and move semantics.

Timer provides a safe, RAII-based interface for managing kernel timers. Key characteristics:

  • RAII lifecycle: Automatic cleanup when Timer object is destroyed
  • Move-only semantics: Prevents accidental copying and ensures unique ownership
  • Thread-safe operations: Safe cancellation and status checking across threads
  • Boost-style API: Familiar interface for timer operations

The Timer class acts as a handle to an underlying kernel timer managed by TimerManager. It ensures proper cleanup and provides safe access to timer operations even when the timer is moved between objects.

auto looper = std::make_shared<SLLooper>();
// Create one-shot timer
Timer timer = looper->addTimer([]() {
std::cout << "Timer fired!" << std::endl;
}, 1000);
// Check if active
if (timer.isActive()) {
std::cout << "Timer is running" << std::endl;
}
// Cancel if needed
timer.cancel();
// Move timer to another variable
Timer movedTimer = std::move(timer);
// Original timer is now in moved-from state
RAII timer wrapper with boost-style API and move semantics.
Definition Timer.h:68
bool isActive() const
Check if timer is currently active.
Definition Timer.cpp:75
void cancel()
Cancel the timer.
Definition Timer.cpp:59
Note
Timer objects are move-only and cannot be copied
Warning
Accessing moved-from Timer objects results in no-op operations
See also
SLLooper::addTimer, Timer, TimerManager

Definition at line 68 of file Timer.h.

Constructor & Destructor Documentation

◆ Timer() [1/3]

swt::Timer::Timer ( TimerId  id,
std::weak_ptr< SLLooper looper 
)

Constructor - creates timer handle.

Parameters
idUnique timer identifier from TimerManager
looperWeak reference to parent SLLooper instance

Creates a Timer handle that manages the lifecycle of a kernel timer. The weak_ptr prevents circular dependencies with the parent SLLooper.

Note
Typically called internally by SLLooper::addTimer

Definition at line 7 of file Timer.cpp.

◆ Timer() [2/3]

swt::Timer::Timer ( const Timer )
delete

Copy constructor - deleted (move-only semantics)

Timer objects cannot be copied to ensure unique ownership of the underlying kernel timer resource.

◆ Timer() [3/3]

swt::Timer::Timer ( Timer &&  other)
noexcept

Move constructor - transfers timer ownership.

Parameters
otherTimer object to move from

Safely transfers ownership of the underlying timer:

  1. Copies timer ID and looper reference
  2. Transfers cancellation state
  3. Marks source object as moved-from
  4. Updates TimerManager's internal pointer to new location
Note
Source timer becomes invalid after move
Thread-safe operation with atomic flags

Definition at line 12 of file Timer.cpp.

◆ ~Timer()

swt::Timer::~Timer ( )

Destructor - automatic timer cleanup.

Automatically cancels the timer if:

  • Timer hasn't been moved from
  • Timer hasn't been explicitly cancelled
  • SLLooper is still alive

Provides RAII guarantees for proper resource cleanup.

Definition at line 49 of file Timer.cpp.

References cancel().

Here is the call graph for this function:

Member Function Documentation

◆ cancel()

void swt::Timer::cancel ( )

Cancel the timer.

Cancels the underlying kernel timer and prevents future callback execution. Safe to call multiple times - subsequent calls are no-ops.

Note
Thread-safe operation
No-op if timer has been moved from
Safe to call even if timer has already expired
Timer timer = looper->addTimer(callback, 1000);
timer.cancel(); // Timer won't fire
timer.cancel(); // Safe - no-op
See also
Timer::cancel

Definition at line 59 of file Timer.cpp.

Referenced by ~Timer().

Here is the caller graph for this function:

◆ getId()

TimerId swt::Timer::getId ( ) const
inline

Get unique timer identifier.

Returns
TimerId for this timer instance

Returns the unique identifier assigned by TimerManager. Useful for debugging and logging purposes.

Note
Safe to call on moved-from objects (returns original ID)
See also
Timer::getId

Definition at line 199 of file Timer.h.

◆ isActive()

bool swt::Timer::isActive ( ) const

Check if timer is currently active.

Returns
true if timer is active and not cancelled, false otherwise

Returns timer status by checking both local cancellation flag and TimerManager's internal state. A timer is considered active if:

  • It hasn't been cancelled locally
  • It exists in TimerManager's active timer map
  • Timer object hasn't been moved from
Note
Thread-safe operation
Returns false for moved-from timer objects
Timer timer = looper->addTimer(callback, 1000);
if (timer.isActive()) {
std::cout << "Timer is running" << std::endl;
}
See also
Timer::isActive

Definition at line 75 of file Timer.cpp.

Referenced by swt::SLLooper::addPeriodicTimer(), and swt::SLLooper::addTimer().

Here is the caller graph for this function:

◆ operator=() [1/2]

Timer & swt::Timer::operator= ( const Timer )
delete

Copy assignment - deleted (move-only semantics)

Timer objects cannot be copy-assigned to ensure unique ownership.

◆ operator=() [2/2]

Timer & swt::Timer::operator= ( Timer &&  other)
noexcept

Move assignment - transfers timer ownership.

Parameters
otherTimer object to move from
Returns
Reference to this timer

Cancels current timer (if any) and transfers ownership from other timer. Handles self-assignment safely.

Note
Source timer becomes invalid after move
Thread-safe operation

Definition at line 18 of file Timer.cpp.

◆ restart()

void swt::Timer::restart ( uint64_t  delay_ms)

Restart timer with new delay (one-shot timers only)

Parameters
delay_msNew delay in milliseconds before timer expiration

Restarts the timer with a new delay value while maintaining all other timer properties (callback, periodic flag, etc.). Resets the cancellation flag to allow the timer to fire again.

Note
Only works for one-shot timers
No-op if timer has been moved from
Thread-safe operation
Timer timer = looper->addTimer(callback, 1000);
timer.cancel(); // Cancel current timer
timer.restart(2000); // Restart with 2 second delay
void restart(uint64_t delay_ms)
Restart timer with new delay (one-shot timers only)
Definition Timer.cpp:86
See also
Timer::restart

Definition at line 86 of file Timer.cpp.

Friends And Related Symbol Documentation

◆ SLLooper

friend class SLLooper
friend

Friend class declaration for internal access.

Allows SLLooper to access private members during timer creation and management operations.

Definition at line 229 of file Timer.h.


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