ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
generic_subscription.hpp
1 // Copyright 2018, Bosch Software Innovations GmbH.
2 // Copyright 2021, Apex.AI Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef RCLCPP__GENERIC_SUBSCRIPTION_HPP_
17 #define RCLCPP__GENERIC_SUBSCRIPTION_HPP_
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 
23 #include "rcpputils/shared_library.hpp"
24 
25 #include "rclcpp/callback_group.hpp"
26 #include "rclcpp/macros.hpp"
27 #include "rclcpp/node_interfaces/node_base_interface.hpp"
28 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
29 #include "rclcpp/qos.hpp"
30 #include "rclcpp/serialized_message.hpp"
31 #include "rclcpp/subscription_base.hpp"
32 #include "rclcpp/typesupport_helpers.hpp"
33 #include "rclcpp/visibility_control.hpp"
34 
35 namespace rclcpp
36 {
37 
39 
46 {
47 public:
48  // cppcheck-suppress unknownMacro
49  RCLCPP_SMART_PTR_DEFINITIONS(GenericSubscription)
50 
51 
70  template<typename AllocatorT = std::allocator<void>>
73  const std::shared_ptr<rcpputils::SharedLibrary> ts_lib,
74  const std::string & topic_name,
75  const std::string & topic_type,
76  const rclcpp::QoS & qos,
77  // TODO(nnmm): Add variant for callback with message info. See issue #1604.
78  std::function<void(std::shared_ptr<rclcpp::SerializedMessage>)> callback,
81  node_base,
82  *rclcpp::get_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib),
83  topic_name,
84  options.template to_rcl_subscription_options<rclcpp::SerializedMessage>(qos),
85  true),
86  callback_(callback),
87  ts_lib_(ts_lib)
88  {
89  // This is unfortunately duplicated with the code in subscription.hpp.
90  // TODO(nnmm): Deduplicate by moving this into SubscriptionBase.
91  if (options.event_callbacks.deadline_callback) {
92  this->add_event_handler(
93  options.event_callbacks.deadline_callback,
94  RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED);
95  }
96  if (options.event_callbacks.liveliness_callback) {
97  this->add_event_handler(
98  options.event_callbacks.liveliness_callback,
99  RCL_SUBSCRIPTION_LIVELINESS_CHANGED);
100  }
101  if (options.event_callbacks.incompatible_qos_callback) {
102  this->add_event_handler(
103  options.event_callbacks.incompatible_qos_callback,
104  RCL_SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS);
105  } else if (options.use_default_callbacks) {
106  // Register default callback when not specified
107  try {
108  this->add_event_handler(
109  [this](QOSRequestedIncompatibleQoSInfo & info) {
110  this->default_incompatible_qos_callback(info);
111  },
112  RCL_SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS);
113  } catch (UnsupportedEventTypeException & /*exc*/) {
114  // pass
115  }
116  }
117  if (options.event_callbacks.message_lost_callback) {
118  this->add_event_handler(
119  options.event_callbacks.message_lost_callback,
120  RCL_SUBSCRIPTION_MESSAGE_LOST);
121  }
122  }
123 
124  RCLCPP_PUBLIC
125  virtual ~GenericSubscription() = default;
126 
127  // Same as create_serialized_message() as the subscription is to serialized_messages only
128  RCLCPP_PUBLIC
129  std::shared_ptr<void> create_message() override;
130 
131  RCLCPP_PUBLIC
132  std::shared_ptr<rclcpp::SerializedMessage> create_serialized_message() override;
133 
135  RCLCPP_PUBLIC
136  void handle_message(
137  std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) override;
138 
140  RCLCPP_PUBLIC
141  void
143  const std::shared_ptr<rclcpp::SerializedMessage> & serialized_message,
144  const rclcpp::MessageInfo & message_info) override;
145 
147  RCLCPP_PUBLIC
149  void * loaned_message, const rclcpp::MessageInfo & message_info) override;
150 
151  // Same as return_serialized_message() as the subscription is to serialized_messages only
152  RCLCPP_PUBLIC
153  void return_message(std::shared_ptr<void> & message) override;
154 
155  RCLCPP_PUBLIC
156  void return_serialized_message(std::shared_ptr<rclcpp::SerializedMessage> & message) override;
157 
158 private:
159  RCLCPP_DISABLE_COPY(GenericSubscription)
160 
161  std::function<void(std::shared_ptr<rclcpp::SerializedMessage>)> callback_;
162  // The type support library should stay loaded, so it is stored in the GenericSubscription
163  std::shared_ptr<rcpputils::SharedLibrary> ts_lib_;
164 };
165 
166 } // namespace rclcpp
167 
168 #endif // RCLCPP__GENERIC_SUBSCRIPTION_HPP_
Subscription for serialized messages whose type is not known at compile time.
RCLCPP_PUBLIC void return_message(std::shared_ptr< void > &message) override
Return the message borrowed in create_message.
RCLCPP_PUBLIC void return_serialized_message(std::shared_ptr< rclcpp::SerializedMessage > &message) override
Return the message borrowed in create_serialized_message.
RCLCPP_PUBLIC void handle_message(std::shared_ptr< void > &message, const rclcpp::MessageInfo &message_info) override
Cast the message to a rclcpp::SerializedMessage and call the callback.
RCLCPP_PUBLIC std::shared_ptr< rclcpp::SerializedMessage > create_serialized_message() override
Borrow a new serialized message.
RCLCPP_PUBLIC void handle_loaned_message(void *loaned_message, const rclcpp::MessageInfo &message_info) override
This function is currently not implemented.
GenericSubscription(rclcpp::node_interfaces::NodeBaseInterface *node_base, const std::shared_ptr< rcpputils::SharedLibrary > ts_lib, const std::string &topic_name, const std::string &topic_type, const rclcpp::QoS &qos, std::function< void(std::shared_ptr< rclcpp::SerializedMessage >)> callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options)
Constructor.
RCLCPP_PUBLIC std::shared_ptr< void > create_message() override
Borrow a new message.
RCLCPP_PUBLIC void handle_serialized_message(const std::shared_ptr< rclcpp::SerializedMessage > &serialized_message, const rclcpp::MessageInfo &message_info) override
Handle dispatching rclcpp::SerializedMessage to user callback.
Additional meta data about messages taken from subscriptions.
Encapsulation of Quality of Service settings.
Definition: qos.hpp:111
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
Pure virtual interface class for the NodeBase part of the Node API.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC const rosidl_message_type_support_t * get_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extract the type support handle from the library.
SubscriptionEventCallbacks event_callbacks
Callbacks for events related to this subscription.
bool use_default_callbacks
Whether or not to use default callbacks when user doesn't supply any in event_callbacks.
Structure containing optional configuration for Subscriptions.