Class EventHandlerBase

Inheritance Relationships

Base Type

Derived Type

Class Documentation

class EventHandlerBase : public rclcpp::Waitable

Subclassed by rclcpp::EventHandler< EventCallbackT, ParentHandleT >

Public Types

enum class EntityType : std::size_t

Values:

enumerator Event

Public Functions

virtual ~EventHandlerBase()
virtual size_t get_number_of_ready_events() override

Get the number of ready events.

virtual void add_to_wait_set(rcl_wait_set_t *wait_set) override

Add the Waitable to a wait set.

virtual bool is_ready(rcl_wait_set_t *wait_set) override

Check if the Waitable is ready.

inline virtual void set_on_ready_callback(std::function<void(size_t, int)> callback) override

Set a callback to be called when each new event instance occurs.

The callback receives a size_t which is the number of events that occurred since the last time this callback was called. Normally this is 1, but can be > 1 if events occurred before any callback was set.

The callback also receives an int identifier argument. This is needed because a Waitable may be composed of several distinct entities, such as subscriptions, services, etc. The application should provide a generic callback function that will be then forwarded by the waitable to all of its entities. Before forwarding, a different value for the identifier argument will be bond to the function. This implies that the provided callback can use the identifier to behave differently depending on which entity triggered the waitable to become ready.

Since this callback is called from the middleware, you should aim to make it fast and not blocking. If you need to do a lot of work or wait for some other event, you should spin it off to another thread, otherwise you risk blocking the middleware.

Calling it again will clear any previously set callback.

An exception will be thrown if the callback is not callable.

This function is thread-safe.

If you want more information available in the callback, like the qos event or other information, you may use a lambda with captures or std::bind.

See also

rmw_event_set_callback

See also

rcl_event_set_callback

Parameters:

callback[in] functor to be called when a new event occurs

inline virtual void clear_on_ready_callback() override

Unset the callback registered for new events, if any.

Protected Functions

void set_on_new_event_callback(rcl_event_callback_t callback, const void *user_data)

Protected Attributes

std::recursive_mutex callback_mutex_
std::function<void(size_t)> on_new_event_callback_ = {nullptr}
rcl_event_t event_handle_
size_t wait_set_event_index_