Class TimersManager::WeakTimersHeap

Nested Relationships

This class is a nested type of Class TimersManager.

Class Documentation

class WeakTimersHeap

This class allows to store weak pointers to timers in a heap-like data structure. The root of the heap is the timer that triggers first. Since this class uses weak ownership, it is not guaranteed that it represents a valid heap at any point in time as timers could go out of scope, thus invalidating it. The “validate_and_lock” API allows to restore the heap property and also returns a locked version of the timers heap. This class is not thread safe and requires external mutexes to protect its usage.

Public Functions

inline bool add_timer(TimerPtr timer)

Add a new timer to the heap. After the addition, the heap property is enforced.

Parameters:

timer – new timer to add.

Returns:

true if timer has been added, false if it was already there.

inline bool remove_timer(TimerPtr timer)

Remove a timer from the heap. After the removal, the heap property is enforced.

Parameters:

timer – timer to remove.

Returns:

true if timer has been removed, false if it was not there.

inline TimerPtr get_timer(const rclcpp::TimerBase *timer_id)

Retrieve the timer identified by the key.

Parameters:

timer_id – The ID of the timer to retrieve.

Returns:

TimerPtr if there’s a timer associated with the ID, nullptr otherwise

inline const WeakTimerPtr &front() const

Returns a const reference to the front element.

inline bool empty() const

Returns whether the heap is empty or not.

inline TimersHeap validate_and_lock()

This function restores the current object as a valid heap and it returns a locked version of it. Timers that went out of scope are removed from the container. It is the only public API to access and manipulate the stored timers.

Returns:

TimersHeap owned timers corresponding to the current object

inline void store(const TimersHeap &heap)

This function allows to recreate the heap of weak pointers from an heap of owned pointers. It is required to be called after a locked TimersHeap generated from this object has been modified in any way (e.g. timers triggered, added, removed).

Parameters:

heap – timers heap to store as weak pointers

inline void clear()

Remove all timers from the heap.