ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
qos_event.cpp
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 #include <string>
16 
17 #include "rclcpp/qos_event.hpp"
18 
19 namespace rclcpp
20 {
21 
22 UnsupportedEventTypeException::UnsupportedEventTypeException(
23  rcl_ret_t ret,
24  const rcl_error_state_t * error_state,
25  const std::string & prefix)
26 : UnsupportedEventTypeException(exceptions::RCLErrorBase(ret, error_state), prefix)
27 {}
28 
29 UnsupportedEventTypeException::UnsupportedEventTypeException(
30  const exceptions::RCLErrorBase & base_exc,
31  const std::string & prefix)
32 : exceptions::RCLErrorBase(base_exc),
33  std::runtime_error(prefix + (prefix.empty() ? "" : ": ") + base_exc.formatted_message)
34 {}
35 
36 QOSEventHandlerBase::~QOSEventHandlerBase()
37 {
38  if (on_new_event_callback_) {
39  clear_on_ready_callback();
40  }
41 
42  if (rcl_event_fini(&event_handle_) != RCL_RET_OK) {
43  RCUTILS_LOG_ERROR_NAMED(
44  "rclcpp",
45  "Error in destruction of rcl event handle: %s", rcl_get_error_string().str);
46  rcl_reset_error();
47  }
48 }
49 
51 size_t
52 QOSEventHandlerBase::get_number_of_ready_events()
53 {
54  return 1;
55 }
56 
58 void
59 QOSEventHandlerBase::add_to_wait_set(rcl_wait_set_t * wait_set)
60 {
61  rcl_ret_t ret = rcl_wait_set_add_event(wait_set, &event_handle_, &wait_set_event_index_);
62  if (RCL_RET_OK != ret) {
63  exceptions::throw_from_rcl_error(ret, "Couldn't add event to wait set");
64  }
65 }
66 
68 bool
69 QOSEventHandlerBase::is_ready(rcl_wait_set_t * wait_set)
70 {
71  return wait_set->events[wait_set_event_index_] == &event_handle_;
72 }
73 
74 void
75 QOSEventHandlerBase::set_on_new_event_callback(
76  rcl_event_callback_t callback,
77  const void * user_data)
78 {
80  &event_handle_,
81  callback,
82  user_data);
83 
84  if (RCL_RET_OK != ret) {
85  using rclcpp::exceptions::throw_from_rcl_error;
86  throw_from_rcl_error(ret, "failed to set the on new message callback for QOS Event");
87  }
88 }
89 
90 } // namespace rclcpp
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_set_callback(const rcl_event_t *event, rcl_event_callback_t callback, const void *user_data)
Set the callback function for the event.
Definition: event.c:222
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_fini(rcl_event_t *event)
Definition: event.c:176
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Container for subscription's, guard condition's, etc to be waited on.
Definition: wait.h:42
const rcl_event_t ** events
Storage for event pointers.
Definition: wait.h:64
#define RCL_RET_OK
Success return code.
Definition: types.h:26
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_event(rcl_wait_set_t *wait_set, const rcl_event_t *event, size_t *index)
Store a pointer to the event in the next empty spot in the set.
Definition: wait.c:510