ROS 2 rclcpp + rcl - rolling  rolling-20536064
ROS 2 C++ Client Library with ROS Client Library
create_publisher.hpp
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_PUBLISHER_HPP_
16 #define RCLCPP__CREATE_PUBLISHER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include "rclcpp/node_interfaces/get_node_topics_interface.hpp"
23 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
24 #include "rclcpp/publisher_factory.hpp"
25 #include "rclcpp/publisher_options.hpp"
26 #include "rclcpp/qos.hpp"
27 #include "rclcpp/qos_overriding_options.hpp"
28 #include "rclcpp/detail/qos_parameters.hpp"
29 
30 #include "rmw/qos_profiles.h"
31 
32 namespace rclcpp
33 {
34 
35 namespace detail
36 {
38 template<
39  typename MessageT,
40  typename AllocatorT = std::allocator<void>,
41  typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>,
42  typename NodeParametersT,
43  typename NodeTopicsT>
44 std::shared_ptr<PublisherT>
46  NodeParametersT & node_parameters,
47  NodeTopicsT & node_topics,
48  const std::string & topic_name,
49  const rclcpp::QoS & qos,
52  )
53 )
54 {
55  auto node_topics_interface = rclcpp::node_interfaces::get_node_topics_interface(node_topics);
56  const rclcpp::QoS & actual_qos = options.qos_overriding_options.get_policy_kinds().size() ?
57  rclcpp::detail::declare_qos_parameters(
58  options.qos_overriding_options, node_parameters,
59  node_topics_interface->resolve_topic_name(topic_name),
61  qos;
62 
63  // Create the publisher.
64  auto pub = node_topics_interface->create_publisher(
65  topic_name,
66  rclcpp::create_publisher_factory<MessageT, AllocatorT, PublisherT>(options),
67  actual_qos
68  );
69 
70  // Add the publisher to the node topics interface.
71  node_topics_interface->add_publisher(pub, options.callback_group);
72 
73  return std::dynamic_pointer_cast<PublisherT>(pub);
74 }
75 } // namespace detail
76 
77 
79 
87 template<
88  typename MessageT,
89  typename AllocatorT = std::allocator<void>,
90  typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>,
91  typename NodeT>
92 std::shared_ptr<PublisherT>
94  NodeT && node,
95  const std::string & topic_name,
96  const rclcpp::QoS & qos,
99  )
100 )
101 {
102  return detail::create_publisher<MessageT, AllocatorT, PublisherT>(
103  node, node, topic_name, qos, options);
104 }
105 
107 template<
108  typename MessageT,
109  typename AllocatorT = std::allocator<void>,
110  typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>>
111 std::shared_ptr<PublisherT>
113  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters,
114  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr & node_topics,
115  const std::string & topic_name,
116  const rclcpp::QoS & qos,
119  )
120 )
121 {
122  return detail::create_publisher<MessageT, AllocatorT, PublisherT>(
123  node_parameters, node_topics, topic_name, qos, options);
124 }
125 
126 } // namespace rclcpp
127 
128 #endif // RCLCPP__CREATE_PUBLISHER_HPP_
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:81
Encapsulation of Quality of Service settings.
Definition: qos.hpp:114
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
std::shared_ptr< PublisherT > create_publisher(NodeT &&node, const std::string &topic_name, const rclcpp::QoS &qos, const rclcpp::PublisherOptionsWithAllocator< AllocatorT > &options=(rclcpp::PublisherOptionsWithAllocator< AllocatorT >()))
Create and return a publisher of the given MessageT type.
Structure containing optional configuration for Publishers.