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 
24 #include "rmw/qos_profiles.h"
25 
26 namespace rclcpp
27 {
28 
29 template<
30  typename MessageT,
31  typename CallbackT,
32  typename AllocatorT,
33  typename CallbackMessageT,
38  const std::string & topic_name,
39  CallbackT && callback,
40  const rmw_qos_profile_t & qos_profile,
41  rclcpp::callback_group::CallbackGroup::SharedPtr group,
42  bool ignore_local_publications,
43  bool use_intra_process_comms,
45  CallbackMessageT, AllocatorT>::SharedPtr
46  msg_mem_strat,
47  typename std::shared_ptr<AllocatorT> allocator)
48 {
49  auto subscription_options = rcl_subscription_get_default_options();
50  subscription_options.qos = qos_profile;
51  subscription_options.ignore_local_publications = ignore_local_publications;
52 
54  <MessageT, CallbackT, AllocatorT, CallbackMessageT, SubscriptionT>(
55  std::forward<CallbackT>(callback), msg_mem_strat, allocator);
56 
57  auto sub = node_topics->create_subscription(
58  topic_name,
59  factory,
60  subscription_options,
61  use_intra_process_comms);
62  node_topics->add_subscription(sub, group);
63  return std::dynamic_pointer_cast<SubscriptionT>(sub);
64 }
65 
66 } // namespace rclcpp
67 
68 #endif // RCLCPP__CREATE_SUBSCRIPTION_HPP_
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, 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:36
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:147
Definition: allocator_common.hpp:24
rcl_subscription_options_t rcl_subscription_get_default_options(void)
Pure virtual interface class for the NodeTopics part of the Node API.
Definition: node_topics_interface.hpp:38
T dynamic_pointer_cast(T... args)
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:38
SubscriptionFactory create_subscription_factory(CallbackT &&callback, 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
virtual rclcpp::SubscriptionBase::SharedPtr create_subscription(const std::string &topic_name, const rclcpp::SubscriptionFactory &subscription_factory, rcl_subscription_options_t &subscription_options, bool use_intra_process)=0
virtual void add_subscription(rclcpp::SubscriptionBase::SharedPtr subscription, rclcpp::callback_group::CallbackGroup::SharedPtr callback_group)=0