rclcpp  master
C++ ROS Client Library API
subscription_factory.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__SUBSCRIPTION_FACTORY_HPP_
16 #define RCLCPP__SUBSCRIPTION_FACTORY_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "rcl/subscription.h"
24 
25 #include "rosidl_typesupport_cpp/message_type_support.hpp"
26 
30 #include "rclcpp/qos.hpp"
31 #include "rclcpp/subscription.hpp"
35 
36 namespace rclcpp
37 {
38 
40 
54 {
55  // Creates a Subscription<MessageT> object and returns it as a SubscriptionBase.
59  const std::string & topic_name,
60  const rclcpp::QoS & qos)>;
61 
63 };
64 
66 template<
67  typename MessageT,
68  typename CallbackT,
69  typename AllocatorT,
70  typename CallbackMessageT =
73  typename MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy<
74  CallbackMessageT,
75  AllocatorT
76  >>
79  CallbackT && callback,
81  typename MessageMemoryStrategyT::SharedPtr msg_mem_strat)
82 {
83  auto allocator = options.get_allocator();
84 
86  AnySubscriptionCallback<CallbackMessageT, AllocatorT> any_subscription_callback(allocator);
87  any_subscription_callback.set(std::forward<CallbackT>(callback));
88 
89  SubscriptionFactory factory {
90  // factory function that creates a MessageT specific SubscriptionT
91  [options, msg_mem_strat, any_subscription_callback](
93  const std::string & topic_name,
94  const rclcpp::QoS & qos
96  {
99 
101  node_base,
102  *rosidl_typesupport_cpp::get_message_type_support_handle<MessageT>(),
103  topic_name,
104  qos,
105  any_subscription_callback,
106  options,
107  msg_mem_strat);
108  // This is used for setting up things like intra process comms which
109  // require this->shared_from_this() which cannot be called from
110  // the constructor.
111  sub->post_init_setup(node_base, qos, options);
112  auto sub_base_ptr = std::dynamic_pointer_cast<SubscriptionBase>(sub);
113  return sub_base_ptr;
114  }
115  };
116 
117  // return the factory now that it is populated
118  return factory;
119 }
120 
121 } // namespace rclcpp
122 
123 #endif // RCLCPP__SUBSCRIPTION_FACTORY_HPP_
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:40
std::shared_ptr< Allocator > get_allocator() const
Get the allocator, creating one if needed.
Definition: subscription_options.hpp:94
Encapsulation of Quality of Service settings.
Definition: qos.hpp:55
const SubscriptionFactoryFunction create_typed_subscription
Definition: subscription_factory.hpp:62
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
Definition: any_subscription_callback.hpp:36
SubscriptionFactory create_subscription_factory(CallbackT &&callback, const rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > &options, typename MessageMemoryStrategyT::SharedPtr msg_mem_strat)
Return a SubscriptionFactory setup to create a SubscriptionT<MessageT, AllocatorT>.
Definition: subscription_factory.hpp:78
Pure virtual interface class for the NodeBase part of the Node API.
Definition: node_base_interface.hpp:36
Factory containing a function used to create a Subscription<MessageT>.
Definition: subscription_factory.hpp:53
T dynamic_pointer_cast(T... args)
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
Definition: subscription_base.hpp:54
void set(CallbackT callback)
Definition: any_subscription_callback.hpp:82