rclcpp  master
C++ ROS Client Library API
generic_subscription.hpp
Go to the documentation of this file.
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 
26 #include "rclcpp/macros.hpp"
29 #include "rclcpp/qos.hpp"
34 
35 namespace rclcpp
36 {
37 
39 
46 {
47 public:
48  // cppcheck-suppress unknownMacro
50 
51 
52 
70  template<typename AllocatorT = std::allocator<void>>
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.
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) {
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 
125  virtual ~GenericSubscription() = default;
126 
127  // Same as create_serialized_message() as the subscription is to serialized_messages only
130 
133 
136  void handle_message(
137  std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) override;
138 
142  void * loaned_message, const rclcpp::MessageInfo & message_info) override;
143 
144  // Same as return_serialized_message() as the subscription is to serialized_messages only
146  void return_message(std::shared_ptr<void> & message) override;
147 
150 
151 private:
153 
155  // The type support library should stay loaded, so it is stored in the GenericSubscription
157 };
158 
159 } // namespace rclcpp
160 
161 #endif // RCLCPP__GENERIC_SUBSCRIPTION_HPP_
callback_group.hpp
std::string
std::shared_ptr< rcpputils::SharedLibrary >
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::GenericSubscription::create_serialized_message
std::shared_ptr< rclcpp::SerializedMessage > create_serialized_message() override
Borrow a new serialized message.
rclcpp::GenericSubscription
Subscription for serialized messages whose type is not known at compile time.
Definition: generic_subscription.hpp:45
typesupport_helpers.hpp
rclcpp::SubscriptionOptionsWithAllocator
Structure containing optional configuration for Subscriptions.
Definition: subscription_options.hpp:87
rclcpp::SubscriptionBase
Definition: subscription_base.hpp:60
std::function
subscription_base.hpp
rclcpp::SubscriptionBase::default_incompatible_qos_callback
void default_incompatible_qos_callback(QOSRequestedIncompatibleQoSInfo &info) const
serialized_message.hpp
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
qos.hpp
rclcpp::SubscriptionOptionsBase::event_callbacks
SubscriptionEventCallbacks event_callbacks
Callbacks for events related to this subscription.
Definition: subscription_options.hpp:40
node_topics_interface.hpp
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::SubscriptionOptionsBase::use_default_callbacks
bool use_default_callbacks
Whether or not to use default callbacks when user doesn't supply any in event_callbacks.
Definition: subscription_options.hpp:43
rclcpp::get_typesupport_handle
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.
rclcpp::node_interfaces::NodeBaseInterface
Pure virtual interface class for the NodeBase part of the Node API.
Definition: node_base_interface.hpp:36
rclcpp::QoS
Encapsulation of Quality of Service settings.
Definition: qos.hpp:110
rclcpp::GenericSubscription::create_message
std::shared_ptr< void > create_message() override
Borrow a new message.
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
macros.hpp
rclcpp::GenericSubscription::return_message
void return_message(std::shared_ptr< void > &message) override
Return the message borrowed in create_message.
node_base_interface.hpp
rclcpp::UnsupportedEventTypeException
Definition: qos_event.hpp:69
rclcpp::GenericSubscription::GenericSubscription
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.
Definition: generic_subscription.hpp:71
rclcpp::MessageInfo
Additional meta data about messages taken from subscriptions.
Definition: message_info.hpp:26
visibility_control.hpp
rmw_qos_incompatible_event_status_t
rclcpp::SerializedMessage
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
Definition: serialized_message.hpp:27
rclcpp::GenericSubscription::handle_message
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::GenericSubscription::return_serialized_message
void return_serialized_message(std::shared_ptr< rclcpp::SerializedMessage > &message) override
Return the message borrowed in create_serialized_message.
rclcpp::GenericSubscription::~GenericSubscription
virtual ~GenericSubscription()=default
rclcpp::SubscriptionBase::add_event_handler
void add_event_handler(const EventCallbackT &callback, const rcl_subscription_event_type_t event_type)
Definition: subscription_base.hpp:282
rclcpp::GenericSubscription::handle_loaned_message
void handle_loaned_message(void *loaned_message, const rclcpp::MessageInfo &message_info) override
This function is currently not implemented.