ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
rclcpp::EventHandlerBase Class Reference
Inheritance diagram for rclcpp::EventHandlerBase:
Inheritance graph
[legend]
Collaboration diagram for rclcpp::EventHandlerBase:
Collaboration graph
[legend]

Public Types

enum class  EntityType : std::size_t { Event }
 

Public Member Functions

RCLCPP_PUBLIC size_t get_number_of_ready_events () override
 Get the number of ready events.
 
RCLCPP_PUBLIC void add_to_wait_set (rcl_wait_set_t &wait_set) override
 Add the Waitable to a wait set.
 
RCLCPP_PUBLIC bool is_ready (const rcl_wait_set_t &wait_set) override
 Check if the Waitable is ready.
 
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. More...
 
void clear_on_ready_callback () override
 Unset the callback registered for new events, if any.
 
- Public Member Functions inherited from rclcpp::Waitable
virtual RCLCPP_PUBLIC size_t get_number_of_ready_subscriptions ()
 Get the number of ready subscriptions. More...
 
virtual RCLCPP_PUBLIC size_t get_number_of_ready_timers ()
 Get the number of ready timers. More...
 
virtual RCLCPP_PUBLIC size_t get_number_of_ready_clients ()
 Get the number of ready clients. More...
 
virtual RCLCPP_PUBLIC size_t get_number_of_ready_services ()
 Get the number of ready services. More...
 
virtual RCLCPP_PUBLIC size_t get_number_of_ready_guard_conditions ()
 Get the number of ready guard_conditions. More...
 
virtual RCLCPP_PUBLIC void add_to_wait_set (rcl_wait_set_t *wait_set)
 Deprecated. More...
 
virtual RCLCPP_PUBLIC bool is_ready (rcl_wait_set_t *wait_set)
 Deprecated. More...
 
virtual RCLCPP_PUBLIC std::shared_ptr< void > take_data ()=0
 Take the data so that it can be consumed with execute. More...
 
virtual RCLCPP_PUBLIC std::shared_ptr< void > take_data_by_entity_id (size_t id)
 Take the data so that it can be consumed with execute. More...
 
virtual RCLCPP_PUBLIC void execute (std::shared_ptr< void > &data)
 Deprecated. More...
 
virtual RCLCPP_PUBLIC void execute (const std::shared_ptr< void > &data)
 Execute data that is passed in. More...
 
RCLCPP_PUBLIC bool exchange_in_use_by_wait_set_state (bool in_use_state)
 Exchange the "in use by wait set" state for this timer. More...
 

Protected Member Functions

RCLCPP_PUBLIC 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_
 

Detailed Description

Definition at line 101 of file event_handler.hpp.

Member Function Documentation

◆ set_on_ready_callback()

void rclcpp::EventHandlerBase::set_on_ready_callback ( std::function< void(size_t, int)>  callback)
inlineoverridevirtual

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
rcl_event_set_callback
Parameters
[in]callbackfunctor to be called when a new event occurs

Reimplemented from rclcpp::Waitable.

Definition at line 164 of file event_handler.hpp.

References rclcpp::get_logger().

Here is the call graph for this function:

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