ROS 2 rclcpp + rcl - rolling  rolling-e7363f10
ROS 2 C++ Client Library with ROS Client Library
create_subscription.hpp
1 // Copyright 2016 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 #ifndef RCLCPP__CREATE_SUBSCRIPTION_HPP_
16 #define RCLCPP__CREATE_SUBSCRIPTION_HPP_
17 
18 #include <chrono>
19 #include <functional>
20 #include <memory>
21 #include <stdexcept>
22 #include <string>
23 #include <utility>
24 
25 #include "rclcpp/detail/qos_parameters.hpp"
26 #include "rclcpp/node_interfaces/get_node_topics_interface.hpp"
27 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
28 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
29 
30 #include "rclcpp/qos.hpp"
31 #include "rclcpp/subscription_factory.hpp"
32 #include "rclcpp/subscription_options.hpp"
33 #include "rmw/qos_profiles.h"
34 
35 namespace rclcpp
36 {
37 
38 namespace detail
39 {
40 template<
41  typename MessageT,
42  typename CallbackT,
43  typename AllocatorT,
44  typename SubscriptionT,
45  typename MessageMemoryStrategyT,
46  typename NodeParametersT,
47  typename NodeTopicsT
48 >
49 typename std::shared_ptr<SubscriptionT>
51  NodeParametersT & node_parameters,
52  NodeTopicsT & node_topics,
53  const std::string & topic_name,
54  const rclcpp::QoS & qos,
55  CallbackT && callback,
58  ),
59  typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
60  MessageMemoryStrategyT::create_default()
61  )
62 )
63 {
64  using rclcpp::node_interfaces::get_node_topics_interface;
65  auto node_topics_interface = get_node_topics_interface(node_topics);
66 
67  auto factory = rclcpp::create_subscription_factory<MessageT>(
68  std::forward<CallbackT>(callback),
69  options,
70  msg_mem_strat
71  );
72 
73  const rclcpp::QoS & actual_qos = options.qos_overriding_options.get_policy_kinds().size() ?
74  rclcpp::detail::declare_qos_parameters(
75  options.qos_overriding_options, node_parameters,
76  node_topics_interface->resolve_topic_name(topic_name),
78  qos;
79 
80  auto sub = node_topics_interface->create_subscription(topic_name, factory, actual_qos);
81  node_topics_interface->add_subscription(sub, options.callback_group);
82 
83  return std::dynamic_pointer_cast<SubscriptionT>(sub);
84 }
85 } // namespace detail
86 
88 
112 template<
113  typename MessageT,
114  typename CallbackT,
115  typename AllocatorT = std::allocator<void>,
116  typename SubscriptionT = rclcpp::Subscription<MessageT, AllocatorT>,
117  typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType,
118  typename NodeT>
119 typename std::shared_ptr<SubscriptionT>
121  NodeT & node,
122  const std::string & topic_name,
123  const rclcpp::QoS & qos,
124  CallbackT && callback,
127  ),
128  typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
129  MessageMemoryStrategyT::create_default()
130  )
131 )
132 {
133  return rclcpp::detail::create_subscription<
134  MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>(
135  node, node, topic_name, qos, std::forward<CallbackT>(callback), options, msg_mem_strat);
136 }
137 
139 
142 template<
143  typename MessageT,
144  typename CallbackT,
145  typename AllocatorT = std::allocator<void>,
146  typename SubscriptionT = rclcpp::Subscription<MessageT, AllocatorT>,
147  typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType>
148 typename std::shared_ptr<SubscriptionT>
150  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters,
151  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr & node_topics,
152  const std::string & topic_name,
153  const rclcpp::QoS & qos,
154  CallbackT && callback,
157  ),
158  typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
159  MessageMemoryStrategyT::create_default()
160  )
161 )
162 {
163  return rclcpp::detail::create_subscription<
164  MessageT, CallbackT, AllocatorT, SubscriptionT, MessageMemoryStrategyT>(
165  node_parameters, node_topics, topic_name, qos,
166  std::forward<CallbackT>(callback), options, msg_mem_strat);
167 }
168 
169 } // namespace rclcpp
170 
171 #endif // RCLCPP__CREATE_SUBSCRIPTION_HPP_
Encapsulation of Quality of Service settings.
Definition: qos.hpp:114
Subscription implementation, templated on the type of message this subscription receives.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
std::shared_ptr< SubscriptionT > create_subscription(NodeT &node, const std::string &topic_name, const rclcpp::QoS &qos, CallbackT &&callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options=(rclcpp::SubscriptionOptionsWithAllocator< AllocatorT >()), typename MessageMemoryStrategyT::SharedPtr msg_mem_strat=(MessageMemoryStrategyT::create_default()))
Create and return a subscription of the given MessageT type.
Structure containing optional configuration for Subscriptions.