ROS 2 rclcpp + rcl - rolling  rolling-26d08f28
ROS 2 C++ Client Library with ROS Client Library
create_generic_subscription.hpp
1 // Copyright 2020, Apex.AI 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__CREATE_GENERIC_SUBSCRIPTION_HPP_
16 #define RCLCPP__CREATE_GENERIC_SUBSCRIPTION_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "rcl/subscription.h"
24 #include "rclcpp/any_subscription_callback.hpp"
25 #include "rclcpp/generic_subscription.hpp"
26 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
27 #include "rclcpp/qos.hpp"
28 #include "rclcpp/serialized_message.hpp"
29 #include "rclcpp/subscription_options.hpp"
30 #include "rclcpp/typesupport_helpers.hpp"
31 
32 namespace rclcpp
33 {
34 
36 
49 template<
50  typename CallbackT,
51  typename AllocatorT = std::allocator<void>>
52 std::shared_ptr<GenericSubscription> create_generic_subscription(
53  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface,
54  const std::string & topic_name,
55  const std::string & topic_type,
56  const rclcpp::QoS & qos,
57  CallbackT && callback,
60  )
61 )
62 {
63  auto ts_lib = rclcpp::get_typesupport_library(
64  topic_type, "rosidl_typesupport_cpp");
65 
66  auto allocator = options.get_allocator();
67 
70  any_subscription_callback(*allocator);
71  any_subscription_callback.set(std::forward<CallbackT>(callback));
72 
73  auto subscription = std::make_shared<GenericSubscription>(
74  topics_interface->get_node_base_interface(),
75  std::move(ts_lib),
76  topic_name,
77  topic_type,
78  qos,
79  any_subscription_callback,
80  options);
81 
82  topics_interface->add_subscription(subscription, options.callback_group);
83 
84  return subscription;
85 }
86 
87 } // namespace rclcpp
88 
89 #endif // RCLCPP__CREATE_GENERIC_SUBSCRIPTION_HPP_
AnySubscriptionCallback< MessageT, AllocatorT > set(CallbackT callback)
Generic function for setting the callback.
Encapsulation of Quality of Service settings.
Definition: qos.hpp:114
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC std::shared_ptr< rcpputils::SharedLibrary > get_typesupport_library(const std::string &type, const std::string &typesupport_identifier)
Load the type support library for the given type.
std::shared_ptr< GenericSubscription > create_generic_subscription(rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface, const std::string &topic_name, const std::string &topic_type, const rclcpp::QoS &qos, CallbackT &&callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options=(rclcpp::SubscriptionOptionsWithAllocator< AllocatorT >()))
Create and return a GenericSubscription.
Structure containing optional configuration for Subscriptions.