rclcpp  master
C++ ROS Client Library API
publisher_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__PUBLISHER_FACTORY_HPP_
16 #define RCLCPP__PUBLISHER_FACTORY_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 
22 #include "rcl/publisher.h"
23 
24 #include "rosidl_typesupport_cpp/message_type_support.hpp"
25 
26 #include "rclcpp/publisher.hpp"
30 
31 namespace rclcpp
32 {
33 
35 
46 {
47  // Creates a PublisherT<MessageT, ...> publisher object and returns it as a PublisherBase.
49  rclcpp::PublisherBase::SharedPtr(
51  const std::string & topic_name,
52  const rcl_publisher_options_t & publisher_options)>;
53 
55 };
56 
58 template<typename MessageT, typename Alloc, typename PublisherT>
61  const PublisherEventCallbacks & event_callbacks,
62  std::shared_ptr<Alloc> allocator)
63 {
64  PublisherFactory factory;
65 
66  // factory function that creates a MessageT specific PublisherT
67  factory.create_typed_publisher =
68  [event_callbacks, allocator](
70  const std::string & topic_name,
71  const rcl_publisher_options_t & publisher_options
73  {
74  auto options_copy = publisher_options;
75  auto message_alloc = std::make_shared<typename PublisherT::MessageAlloc>(*allocator.get());
76  options_copy.allocator = allocator::get_rcl_allocator<MessageT>(*message_alloc.get());
77 
78  return std::make_shared<PublisherT>(
79  node_base,
80  topic_name,
81  options_copy,
82  event_callbacks,
83  message_alloc);
84  };
85 
86  // return the factory now that it is populated
87  return factory;
88 }
89 
90 } // namespace rclcpp
91 
92 #endif // RCLCPP__PUBLISHER_FACTORY_HPP_
PublisherFactory create_publisher_factory(const PublisherEventCallbacks &event_callbacks, std::shared_ptr< Alloc > allocator)
Return a PublisherFactory with functions setup for creating a PublisherT<MessageT, Alloc>.
Definition: publisher_factory.hpp:60
Contains callbacks for various types of events a Publisher can receive from the middleware.
Definition: qos_event.hpp:42
PublisherFactoryFunction create_typed_publisher
Definition: publisher_factory.hpp:54
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
Factory with functions used to create a MessageT specific PublisherT.
Definition: publisher_factory.hpp:45
Pure virtual interface class for the NodeBase part of the Node API.
Definition: node_base_interface.hpp:36
T get(T... args)