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 
32 template<
33  typename MessageT,
34  typename CallbackT,
35  typename AllocatorT,
36  typename CallbackMessageT,
38 // cppcheck-suppress syntaxError // bug in cppcheck 1.82 for [[deprecated]] on templated function
39 [[deprecated("use alternative rclcpp::create_subscription() signatures")]]
43  const std::string & topic_name,
44  CallbackT && callback,
45  const rmw_qos_profile_t & qos_profile,
46  const SubscriptionEventCallbacks & event_callbacks,
47  rclcpp::callback_group::CallbackGroup::SharedPtr group,
48  bool ignore_local_publications,
49  bool use_intra_process_comms,
51  CallbackMessageT, AllocatorT>::SharedPtr
52  msg_mem_strat,
53  typename std::shared_ptr<AllocatorT> allocator)
54 {
55  auto subscription_options = rcl_subscription_get_default_options();
56  subscription_options.qos = qos_profile;
57  subscription_options.ignore_local_publications = ignore_local_publications;
58 
60  <MessageT, CallbackT, AllocatorT, CallbackMessageT, SubscriptionT>(
61  std::forward<CallbackT>(callback),
62  event_callbacks,
63  msg_mem_strat,
64  allocator);
65 
66  auto sub = node_topics->create_subscription(
67  topic_name,
68  factory,
69  subscription_options,
70  use_intra_process_comms);
71  node_topics->add_subscription(sub, group);
72  return std::dynamic_pointer_cast<SubscriptionT>(sub);
73 }
74 
76 
81 template<
82  typename MessageT,
83  typename CallbackT,
84  typename AllocatorT = std::allocator<void>,
85  typename CallbackMessageT =
88  typename NodeT>
91  NodeT && node,
92  const std::string & topic_name,
93  const rclcpp::QoS & qos,
94  CallbackT && callback,
97  ),
99  CallbackMessageT, AllocatorT>::SharedPtr
100  msg_mem_strat = nullptr)
101 {
103  auto node_topics = get_node_topics_interface(std::forward<NodeT>(node));
104 
105  if (!msg_mem_strat) {
107  msg_mem_strat = MessageMemoryStrategy<CallbackMessageT, AllocatorT>::create_default();
108  }
109 
110  std::shared_ptr<AllocatorT> allocator = options.allocator;
111  if (!allocator) {
112  allocator = std::make_shared<AllocatorT>();
113  }
115  <MessageT, CallbackT, AllocatorT, CallbackMessageT, SubscriptionT>(
116  std::forward<CallbackT>(callback), options.event_callbacks, msg_mem_strat, allocator);
117 
118  bool use_intra_process;
119  switch (options.use_intra_process_comm) {
121  use_intra_process = true;
122  break;
124  use_intra_process = false;
125  break;
127  use_intra_process = node_topics->get_node_base_interface()->get_use_intra_process_default();
128  break;
129  default:
130  throw std::runtime_error("Unrecognized IntraProcessSetting value");
131  break;
132  }
133 
134  // TODO(wjwwood): convert all of the interfaces to use QoS and SubscriptionOptionsBase
135  auto sub = node_topics->create_subscription(
136  topic_name,
137  factory,
138  options.template to_rcl_subscription_options<MessageT>(qos),
139  use_intra_process);
140  node_topics->add_subscription(sub, options.callback_group);
141  return std::dynamic_pointer_cast<SubscriptionT>(sub);
142 }
143 
144 } // namespace rclcpp
145 
146 #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_topics_interface() template function.
Definition: allocator_common.hpp:24
virtual rclcpp::SubscriptionBase::SharedPtr create_subscription(const std::string &topic_name, const rclcpp::SubscriptionFactory &subscription_factory, const rcl_subscription_options_t &subscription_options, bool use_intra_process)=0
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:59
std::shared_ptr< SubscriptionT > create_subscription(rclcpp::node_interfaces::NodeTopicsInterface *node_topics, const std::string &topic_name, CallbackT &&callback, const rmw_qos_profile_t &qos_profile, const SubscriptionEventCallbacks &event_callbacks, rclcpp::callback_group::CallbackGroup::SharedPtr group, bool ignore_local_publications, bool use_intra_process_comms, typename rclcpp::message_memory_strategy::MessageMemoryStrategy< CallbackMessageT, AllocatorT >::SharedPtr msg_mem_strat, typename std::shared_ptr< AllocatorT > allocator)
Definition: create_subscription.hpp:41
typename std::remove_cv< rclcpp::function_traits::function_traits< CallbackT >::template argument_type< 0 > >::type type
Definition: subscription_traits.hpp:65
rcl_subscription_options_t rcl_subscription_get_default_options(void)
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
Explicitly enable intraprocess comm at publisher/subscription level.
Explicitly disable intraprocess comm at publisher/subscription level.
T dynamic_pointer_cast(T... args)
virtual void add_subscription(rclcpp::SubscriptionBase::SharedPtr subscription, rclcpp::callback_group::CallbackGroup::SharedPtr callback_group)=0
Structure containing optional configuration for Subscriptions.
Definition: subscription_options.hpp:46
SubscriptionFactory create_subscription_factory(CallbackT &&callback, const SubscriptionEventCallbacks &event_callbacks, typename rclcpp::message_memory_strategy::MessageMemoryStrategy< CallbackMessageT, Alloc >::SharedPtr msg_mem_strat, std::shared_ptr< Alloc > allocator)
Return a SubscriptionFactory with functions for creating a SubscriptionT<MessageT, Alloc>.
Definition: subscription_factory.hpp:76
Pure virtual interface class for the NodeTopics part of the Node API.
Definition: node_topics_interface.hpp:38
Contains callbacks for non-message events that a Subscription can receive from the middleware...
Definition: qos_event.hpp:49
Take intraprocess configuration from the node.