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 <string>
20 
21 #include "rcl/error_handling.h"
23 
24 #include "rcutils/logging_macros.h"
25 
26 #include "rclcpp/exceptions.hpp"
28 #include "rclcpp/waitable.hpp"
29 
30 namespace rclcpp
31 {
32 
39 
47 
50 {
54 };
55 
58 {
62 };
63 
65 {
66 public:
69  rcl_ret_t ret,
70  const rcl_error_state_t * error_state,
71  const std::string & prefix);
72 
75  const exceptions::RCLErrorBase & base_exc,
76  const std::string & prefix);
77 };
78 
80 {
81 public:
83  virtual ~QOSEventHandlerBase();
84 
87  size_t
88  get_number_of_ready_events() override;
89 
92  bool
93  add_to_wait_set(rcl_wait_set_t * wait_set) override;
94 
97  bool
98  is_ready(rcl_wait_set_t * wait_set) override;
99 
100 protected:
103 };
104 
105 template<typename EventCallbackT, typename ParentHandleT>
107 {
108 public:
109  template<typename InitFuncT, typename EventTypeEnum>
111  const EventCallbackT & callback,
112  InitFuncT init_func,
113  ParentHandleT parent_handle,
114  EventTypeEnum event_type)
115  : event_callback_(callback)
116  {
117  parent_handle_ = parent_handle;
118  event_handle_ = rcl_get_zero_initialized_event();
119  rcl_ret_t ret = init_func(&event_handle_, parent_handle.get(), event_type);
120  if (ret != RCL_RET_OK) {
121  if (ret == RCL_RET_UNSUPPORTED) {
122  UnsupportedEventTypeException exc(ret, rcl_get_error_state(), "Failed to initialize event");
123  rcl_reset_error();
124  throw exc;
125  } else {
126  rclcpp::exceptions::throw_from_rcl_error(ret, "Failed to initialize event");
127  }
128  }
129  }
130 
132  void
133  execute() override
134  {
135  EventCallbackInfoT callback_info;
136 
137  rcl_ret_t ret = rcl_take_event(&event_handle_, &callback_info);
138  if (ret != RCL_RET_OK) {
140  "rclcpp",
141  "Couldn't take event info: %s", rcl_get_error_string().str);
142  return;
143  }
144 
145  event_callback_(callback_info);
146  }
147 
148 private:
149  using EventCallbackInfoT = typename std::remove_reference<typename
151 
152  ParentHandleT parent_handle_;
153  EventCallbackT event_callback_;
154 };
155 
156 } // namespace rclcpp
157 
158 #endif // RCLCPP__QOS_EVENT_HPP_
rclcpp::QOSEventHandlerBase::wait_set_event_index_
size_t wait_set_event_index_
Definition: qos_event.hpp:102
exceptions.hpp
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
rclcpp::QOSEventHandlerBase::event_handle_
rcl_event_t event_handle_
Definition: qos_event.hpp:101
rclcpp::PublisherEventCallbacks::deadline_callback
QOSDeadlineOfferedCallbackType deadline_callback
Definition: qos_event.hpp:51
rclcpp::SubscriptionEventCallbacks::liveliness_callback
QOSLivelinessChangedCallbackType liveliness_callback
Definition: qos_event.hpp:60
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:126
rclcpp::Waitable
Definition: waitable.hpp:28
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:49
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:61
RCUTILS_LOG_ERROR_NAMED
#define RCUTILS_LOG_ERROR_NAMED(name,...)
rcl_wait_set_t
std::runtime_error
rclcpp::UnsupportedEventTypeException
Definition: qos_event.hpp:64
rclcpp::PublisherEventCallbacks::incompatible_qos_callback
QOSOfferedIncompatibleQoSCallbackType incompatible_qos_callback
Definition: qos_event.hpp:53
function_traits.hpp
std::remove_reference
rclcpp::exceptions::RCLErrorBase::ret
rcl_ret_t ret
Definition: exceptions.hpp:133
rclcpp::QOSEventHandlerBase::~QOSEventHandlerBase
virtual ~QOSEventHandlerBase()
rclcpp::PublisherEventCallbacks::liveliness_callback
QOSLivelinessLostCallbackType liveliness_callback
Definition: qos_event.hpp:52
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:57
rclcpp::QOSEventHandler::execute
void execute() override
Execute any entities of the Waitable that are ready.
Definition: qos_event.hpp:133
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
rclcpp::QOSEventHandlerBase
Definition: qos_event.hpp:79
rmw_requested_deadline_missed_status_t
rmw_offered_deadline_missed_status_t
rclcpp::QOSEventHandler
Definition: qos_event.hpp:106
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:110
rclcpp::SubscriptionEventCallbacks::deadline_callback
QOSDeadlineRequestedCallbackType deadline_callback
Definition: qos_event.hpp:59