ROS 2 rclcpp + rcl - jazzy  jazzy
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,
80  node_base,
81  *rclcpp::get_message_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib),
82  topic_name,
83  options.to_rcl_subscription_options(qos),
84  options.event_callbacks,
85  options.use_default_callbacks,
86  DeliveredMessageKind::SERIALIZED_MESSAGE),
87  any_callback_(callback),
88  ts_lib_(ts_lib)
89  {
90  TRACETOOLS_TRACEPOINT(
91  rclcpp_subscription_init,
92  static_cast<const void *>(get_subscription_handle().get()),
93  static_cast<const void *>(this));
94  TRACETOOLS_TRACEPOINT(
95  rclcpp_subscription_callback_added,
96  static_cast<const void *>(this),
97  static_cast<const void *>(&any_callback_));
98 
99 #ifndef TRACETOOLS_DISABLED
100  any_callback_.register_callback_for_tracing();
101 #endif
102  }
103 
104  RCLCPP_PUBLIC
105  virtual ~GenericSubscription() = default;
106 
107  // Same as create_serialized_message() as the subscription is to serialized_messages only
108  RCLCPP_PUBLIC
109  std::shared_ptr<void> create_message() override;
110 
111  RCLCPP_PUBLIC
112  std::shared_ptr<rclcpp::SerializedMessage> create_serialized_message() override;
113 
115  RCLCPP_PUBLIC
116  void handle_message(
117  std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) override;
118 
120  RCLCPP_PUBLIC
121  void
123  const std::shared_ptr<rclcpp::SerializedMessage> & serialized_message,
124  const rclcpp::MessageInfo & message_info) override;
125 
127  RCLCPP_PUBLIC
129  void * loaned_message, const rclcpp::MessageInfo & message_info) override;
130 
131  // Same as return_serialized_message() as the subscription is to serialized_messages only
132  RCLCPP_PUBLIC
133  void return_message(std::shared_ptr<void> & message) override;
134 
135  RCLCPP_PUBLIC
136  void return_serialized_message(std::shared_ptr<rclcpp::SerializedMessage> & message) override;
137 
138 
139  // DYNAMIC TYPE ==================================================================================
140  RCLCPP_PUBLIC
141  rclcpp::dynamic_typesupport::DynamicMessageType::SharedPtr get_shared_dynamic_message_type()
142  override;
143 
144  RCLCPP_PUBLIC
145  rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr get_shared_dynamic_message() override;
146 
147  RCLCPP_PUBLIC
148  rclcpp::dynamic_typesupport::DynamicSerializationSupport::SharedPtr
149  get_shared_dynamic_serialization_support() override;
150 
151  RCLCPP_PUBLIC
152  rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr create_dynamic_message() override;
153 
154  RCLCPP_PUBLIC
155  void return_dynamic_message(
156  rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr & message) override;
157 
158  RCLCPP_PUBLIC
159  void handle_dynamic_message(
160  const rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr & message,
161  const rclcpp::MessageInfo & message_info) override;
162 
163 private:
164  RCLCPP_DISABLE_COPY(GenericSubscription)
166  // The type support library should stay loaded, so it is stored in the GenericSubscription
167  std::shared_ptr<rcpputils::SharedLibrary> ts_lib_;
168 };
169 
170 } // namespace rclcpp
171 
172 #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 rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr create_dynamic_message() override
Borrow a new serialized message (this clones!)
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.
RCLCPP_PUBLIC std::shared_ptr< void > create_message() override
Borrow a new message.
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, AnySubscriptionCallback< rclcpp::SerializedMessage, AllocatorT > callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options)
Constructor.
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:116
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.
DeliveredMessageKind
The kind of message that the subscription delivers in its callback, used by the executor.
RCLCPP_PUBLIC const rosidl_message_type_support_t * get_message_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extracts the message type support handle from the library.
Structure containing optional configuration for Subscriptions.