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 
20 #include "rcl/error_handling.h"
21 
22 #include "rcutils/logging_macros.h"
23 
24 #include "rclcpp/exceptions.hpp"
26 #include "rclcpp/waitable.hpp"
27 
28 namespace rclcpp
29 {
30 
35 
40 
43 {
46 };
47 
50 {
53 };
54 
56 {
57 public:
59  virtual ~QOSEventHandlerBase();
60 
63  size_t
64  get_number_of_ready_events() override;
65 
68  bool
69  add_to_wait_set(rcl_wait_set_t * wait_set) override;
70 
73  bool
74  is_ready(rcl_wait_set_t * wait_set) override;
75 
76 protected:
79 };
80 
81 template<typename EventCallbackT>
83 {
84 public:
85  template<typename InitFuncT, typename ParentHandleT, typename EventTypeEnum>
87  const EventCallbackT & callback,
88  InitFuncT init_func,
89  ParentHandleT parent_handle,
90  EventTypeEnum event_type)
91  : event_callback_(callback)
92  {
93  event_handle_ = rcl_get_zero_initialized_event();
94  rcl_ret_t ret = init_func(&event_handle_, parent_handle, event_type);
95  if (ret != RCL_RET_OK) {
96  rclcpp::exceptions::throw_from_rcl_error(ret, "could not create event");
97  }
98  }
99 
101  void
102  execute() override
103  {
104  EventCallbackInfoT callback_info;
105 
106  rcl_ret_t ret = rcl_take_event(&event_handle_, &callback_info);
107  if (ret != RCL_RET_OK) {
109  "rclcpp",
110  "Couldn't take event info: %s", rcl_get_error_string().str);
111  return;
112  }
113 
114  event_callback_(callback_info);
115  }
116 
117 private:
118  using EventCallbackInfoT = typename std::remove_reference<typename
120 
121  EventCallbackT event_callback_;
122 };
123 
124 } // namespace rclcpp
125 
126 #endif // RCLCPP__QOS_EVENT_HPP_
#define rcl_get_error_string
size_t wait_set_event_index_
Definition: qos_event.hpp:78
Definition: function_traits.hpp:49
rmw_ret_t rcl_ret_t
Contains callbacks for various types of events a Publisher can receive from the middleware.
Definition: qos_event.hpp:42
rcl_ret_t rcl_take_event(const rcl_event_t *event, void *event_info)
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
QOSLivelinessLostCallbackType liveliness_callback
Definition: qos_event.hpp:45
#define RCL_RET_OK
QOSLivelinessChangedCallbackType liveliness_callback
Definition: qos_event.hpp:52
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.
QOSDeadlineRequestedCallbackType deadline_callback
Definition: qos_event.hpp:51
Definition: qos_event.hpp:55
#define RCUTILS_LOG_ERROR_NAMED(name,...)
Definition: qos_event.hpp:82
QOSDeadlineOfferedCallbackType deadline_callback
Definition: qos_event.hpp:44
void execute() override
Execute any entities of the Waitable that are ready.
Definition: qos_event.hpp:102
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Definition: waitable.hpp:26
rcl_event_t event_handle_
Definition: qos_event.hpp:77
rcl_event_t rcl_get_zero_initialized_event(void)
Contains callbacks for non-message events that a Subscription can receive from the middleware...
Definition: qos_event.hpp:49
QOSEventHandler(const EventCallbackT &callback, InitFuncT init_func, ParentHandleT parent_handle, EventTypeEnum event_type)
Definition: qos_event.hpp:86