rclcpp  master
C++ ROS Client Library API
qos_event.hpp
Go to the documentation of this file.
1 // Copyright 2019 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__QOS_EVENT_HPP_
16 #define RCLCPP__QOS_EVENT_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <stdexcept>
21 #include <string>
22 
23 #include "rcl/error_handling.h"
25 
26 #include "rcutils/logging_macros.h"
27 
28 #include "rclcpp/exceptions.hpp"
30 #include "rclcpp/waitable.hpp"
31 
32 namespace rclcpp
33 {
34 
42 
51 
54 {
58 };
59 
62 {
67 };
68 
70 {
71 public:
74  rcl_ret_t ret,
75  const rcl_error_state_t * error_state,
76  const std::string & prefix);
77 
80  const exceptions::RCLErrorBase & base_exc,
81  const std::string & prefix);
82 };
83 
85 {
86 public:
88  virtual ~QOSEventHandlerBase();
89 
92  size_t
93  get_number_of_ready_events() override;
94 
97  bool
98  add_to_wait_set(rcl_wait_set_t * wait_set) override;
99 
102  bool
103  is_ready(rcl_wait_set_t * wait_set) override;
104 
105 protected:
108 };
109 
110 template<typename EventCallbackT, typename ParentHandleT>
112 {
113 public:
114  template<typename InitFuncT, typename EventTypeEnum>
116  const EventCallbackT & callback,
117  InitFuncT init_func,
118  ParentHandleT parent_handle,
119  EventTypeEnum event_type)
120  : event_callback_(callback)
121  {
122  parent_handle_ = parent_handle;
124  rcl_ret_t ret = init_func(&event_handle_, parent_handle.get(), event_type);
125  if (ret != RCL_RET_OK) {
126  if (ret == RCL_RET_UNSUPPORTED) {
127  UnsupportedEventTypeException exc(ret, rcl_get_error_state(), "Failed to initialize event");
128  rcl_reset_error();
129  throw exc;
130  } else {
131  rclcpp::exceptions::throw_from_rcl_error(ret, "Failed to initialize event");
132  }
133  }
134  }
135 
138  take_data() override
139  {
140  EventCallbackInfoT callback_info;
141  rcl_ret_t ret = rcl_take_event(&event_handle_, &callback_info);
142  if (ret != RCL_RET_OK) {
144  "rclcpp",
145  "Couldn't take event info: %s", rcl_get_error_string().str);
146  return nullptr;
147  }
148  return std::static_pointer_cast<void>(std::make_shared<EventCallbackInfoT>(callback_info));
149  }
150 
152  void
154  {
155  if (!data) {
156  throw std::runtime_error("'data' is empty");
157  }
158  auto callback_ptr = std::static_pointer_cast<EventCallbackInfoT>(data);
159  event_callback_(*callback_ptr);
160  callback_ptr.reset();
161  }
162 
163 private:
164  using EventCallbackInfoT = typename std::remove_reference<typename
166 
167  ParentHandleT parent_handle_;
168  EventCallbackT event_callback_;
169 };
170 
171 } // namespace rclcpp
172 
173 #endif // RCLCPP__QOS_EVENT_HPP_
rcl_get_zero_initialized_event
rcl_event_t rcl_get_zero_initialized_event(void)
rclcpp::QOSEventHandler::take_data
std::shared_ptr< void > take_data() override
Take data so that the callback cannot be scheduled again.
Definition: qos_event.hpp:138
rclcpp::QOSEventHandlerBase::wait_set_event_index_
size_t wait_set_event_index_
Definition: qos_event.hpp:107
exceptions.hpp
rclcpp::SubscriptionEventCallbacks::message_lost_callback
QOSMessageLostCallbackType message_lost_callback
Definition: qos_event.hpp:66
rclcpp::UnsupportedEventTypeException::UnsupportedEventTypeException
UnsupportedEventTypeException(rcl_ret_t ret, const rcl_error_state_t *error_state, const std::string &prefix)
rmw_liveliness_lost_status_t
std::string
std::shared_ptr< void >
rclcpp::QOSEventHandlerBase::event_handle_
rcl_event_t event_handle_
Definition: qos_event.hpp:106
rcl_ret_t
rmw_ret_t rcl_ret_t
rclcpp::PublisherEventCallbacks::deadline_callback
QOSDeadlineOfferedCallbackType deadline_callback
Definition: qos_event.hpp:55
rclcpp::SubscriptionEventCallbacks::liveliness_callback
QOSLivelinessChangedCallbackType liveliness_callback
Definition: qos_event.hpp:64
rclcpp::function_traits::function_traits
Definition: function_traits.hpp:49
rclcpp::QOSEventHandlerBase::is_ready
bool is_ready(rcl_wait_set_t *wait_set) override
Check if the Waitable is ready.
std::function< void(QOSDeadlineRequestedInfo &)>
logging_macros.h
rclcpp::exceptions::RCLErrorBase
Definition: exceptions.hpp:135
rclcpp::Waitable
Definition: waitable.hpp:29
rclcpp::QOSEventHandlerBase::add_to_wait_set
bool add_to_wait_set(rcl_wait_set_t *wait_set) override
Add the Waitable to a wait set.
rclcpp::QOSEventHandlerBase::get_number_of_ready_events
size_t get_number_of_ready_events() override
Get the number of ready events.
rclcpp::PublisherEventCallbacks
Contains callbacks for various types of events a Publisher can receive from the middleware.
Definition: qos_event.hpp:53
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rmw_liveliness_changed_status_t
rclcpp::SubscriptionEventCallbacks::incompatible_qos_callback
QOSRequestedIncompatibleQoSCallbackType incompatible_qos_callback
Definition: qos_event.hpp:65
rmw_message_lost_status_t
RCUTILS_LOG_ERROR_NAMED
#define RCUTILS_LOG_ERROR_NAMED(name,...)
rcl_wait_set_t
std::runtime_error
rclcpp::UnsupportedEventTypeException
Definition: qos_event.hpp:69
rclcpp::PublisherEventCallbacks::incompatible_qos_callback
QOSOfferedIncompatibleQoSCallbackType incompatible_qos_callback
Definition: qos_event.hpp:57
function_traits.hpp
rclcpp::QOSEventHandler::execute
void execute(std::shared_ptr< void > &data) override
Execute any entities of the Waitable that are ready.
Definition: qos_event.hpp:153
std::remove_reference
rclcpp::exceptions::RCLErrorBase::ret
rcl_ret_t ret
Definition: exceptions.hpp:142
rclcpp::QOSEventHandlerBase::~QOSEventHandlerBase
virtual ~QOSEventHandlerBase()
rclcpp::PublisherEventCallbacks::liveliness_callback
QOSLivelinessLostCallbackType liveliness_callback
Definition: qos_event.hpp:56
rcl_take_event
rcl_ret_t rcl_take_event(const rcl_event_t *event, void *event_info)
rmw_qos_incompatible_event_status_t
rclcpp::SubscriptionEventCallbacks
Contains callbacks for non-message events that a Subscription can receive from the middleware.
Definition: qos_event.hpp:61
rclcpp::exceptions::throw_from_rcl_error
void throw_from_rcl_error(rcl_ret_t ret, const std::string &prefix="", const rcl_error_state_t *error_state=nullptr, void(*reset_error)()=rcl_reset_error)
Throw a C++ std::exception which was created based on an rcl error.
rcl_event_t
RCL_RET_UNSUPPORTED
#define RCL_RET_UNSUPPORTED
rclcpp::QOSEventHandlerBase
Definition: qos_event.hpp:84
RCL_RET_OK
#define RCL_RET_OK
rmw_requested_deadline_missed_status_t
rmw_offered_deadline_missed_status_t
rclcpp::QOSEventHandler
Definition: qos_event.hpp:111
incompatible_qos_events_statuses.h
waitable.hpp
rclcpp::QOSEventHandler::QOSEventHandler
QOSEventHandler(const EventCallbackT &callback, InitFuncT init_func, ParentHandleT parent_handle, EventTypeEnum event_type)
Definition: qos_event.hpp:115
rclcpp::SubscriptionEventCallbacks::deadline_callback
QOSDeadlineRequestedCallbackType deadline_callback
Definition: qos_event.hpp:63