rclcpp  master
C++ ROS Client Library API
create_subscription.hpp
Go to the documentation of this file.
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 <memory>
19 #include <string>
20 #include <utility>
21 
26 #include "rclcpp/qos.hpp"
27 #include "rmw/qos_profiles.h"
28 
29 namespace rclcpp
30 {
31 
33 
38 template<
39  typename MessageT,
40  typename CallbackT,
41  typename AllocatorT = std::allocator<void>,
42  typename CallbackMessageT =
45  typename MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy<
46  CallbackMessageT,
47  AllocatorT
48  >,
49  typename NodeT>
52  NodeT && node,
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 {
65  auto node_topics = get_node_topics_interface(std::forward<NodeT>(node));
66 
67  auto factory = rclcpp::create_subscription_factory<MessageT>(
68  std::forward<CallbackT>(callback),
69  options,
70  msg_mem_strat
71  );
72 
73  auto sub = node_topics->create_subscription(topic_name, factory, qos);
74  node_topics->add_subscription(sub, options.callback_group);
75 
76  return std::dynamic_pointer_cast<SubscriptionT>(sub);
77 }
78 
79 } // namespace rclcpp
80 
81 #endif // RCLCPP__CREATE_SUBSCRIPTION_HPP_
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:40
Encapsulation of Quality of Service settings.
Definition: qos.hpp:55
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:67
typename std::remove_cv< rclcpp::function_traits::function_traits< CallbackT >::template argument_type< 0 > >::type type
Definition: subscription_traits.hpp:65
T dynamic_pointer_cast(T... args)
rclcpp::node_interfaces::NodeTopicsInterface * get_node_topics_interface(NodeType node_pointer)
Get the NodeTopicsInterface as a pointer from a pointer to a "Node like" object.
Definition: get_node_topics_interface.hpp:126
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
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.
Definition: create_subscription.hpp:51