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 
27 #include "rclcpp/subscription.hpp"
32 
33 namespace rclcpp
34 {
35 
37 
48 {
49  // Creates a Subscription<MessageT> object and returns it as a SubscriptionBase.
51  rclcpp::SubscriptionBase::SharedPtr(
53  const std::string & topic_name,
54  const rcl_subscription_options_t & subscription_options)>;
55 
57 
58  // Function that takes a MessageT from the intra process manager
60  void (
61  rclcpp::intra_process_manager::IntraProcessManager::SharedPtr ipm,
62  rclcpp::SubscriptionBase::SharedPtr subscription,
63  const rcl_subscription_options_t & subscription_options)>;
64 
66 };
67 
69 template<
70  typename MessageT,
71  typename CallbackT,
72  typename Alloc,
73  typename CallbackMessageT,
74  typename SubscriptionT>
77  CallbackT && callback,
78  const SubscriptionEventCallbacks & event_callbacks,
80  CallbackMessageT, Alloc>::SharedPtr
81  msg_mem_strat,
82  std::shared_ptr<Alloc> allocator)
83 {
84  SubscriptionFactory factory;
85 
87  AnySubscriptionCallback<CallbackMessageT, Alloc> any_subscription_callback(allocator);
88  any_subscription_callback.set(std::forward<CallbackT>(callback));
89 
90  auto message_alloc =
91  std::make_shared<typename Subscription<CallbackMessageT, Alloc>::MessageAlloc>();
92 
93  // factory function that creates a MessageT specific SubscriptionT
95  [allocator, msg_mem_strat, any_subscription_callback, event_callbacks, message_alloc](
97  const std::string & topic_name,
98  const rcl_subscription_options_t & subscription_options
99  ) -> rclcpp::SubscriptionBase::SharedPtr
100  {
101  auto options_copy = subscription_options;
102  options_copy.allocator =
103  rclcpp::allocator::get_rcl_allocator<CallbackMessageT>(*message_alloc.get());
104 
105  using rclcpp::Subscription;
107 
109  node_base->get_shared_rcl_node_handle(),
110  *rosidl_typesupport_cpp::get_message_type_support_handle<MessageT>(),
111  topic_name,
112  options_copy,
113  any_subscription_callback,
114  event_callbacks,
115  msg_mem_strat);
116  auto sub_base_ptr = std::dynamic_pointer_cast<SubscriptionBase>(sub);
117  return sub_base_ptr;
118  };
119 
120  // return the factory now that it is populated
121  return factory;
122 }
123 
124 } // namespace rclcpp
125 
126 #endif // RCLCPP__SUBSCRIPTION_FACTORY_HPP_
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:40
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:59
Definition: any_subscription_callback.hpp:34
SubscriptionFactoryFunction create_typed_subscription
Definition: subscription_factory.hpp:56
Pure virtual interface class for the NodeBase part of the Node API.
Definition: node_base_interface.hpp:36
Factory with functions used to create a Subscription<MessageT>.
Definition: subscription_factory.hpp:47
T dynamic_pointer_cast(T... args)
SetupIntraProcessFunction setup_intra_process
Definition: subscription_factory.hpp:65
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
Definition: subscription_base.hpp:53
Contains callbacks for non-message events that a Subscription can receive from the middleware...
Definition: qos_event.hpp:49
void set(CallbackT callback)
Definition: any_subscription_callback.hpp:80