ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
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/node_options.hpp"
25 #include "rclcpp/publisher_factory.hpp"
26 #include "rclcpp/publisher_options.hpp"
27 #include "rclcpp/qos.hpp"
28 #include "rclcpp/qos_overriding_options.hpp"
29 #include "rclcpp/detail/qos_parameters.hpp"
30 
31 #include "rmw/qos_profiles.h"
32 
33 namespace rclcpp
34 {
35 
36 namespace detail
37 {
39 template<
40  typename MessageT,
41  typename AllocatorT = std::allocator<void>,
42  typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>,
43  typename NodeParametersT,
44  typename NodeTopicsT>
45 std::shared_ptr<PublisherT>
47  NodeParametersT & node_parameters,
48  NodeTopicsT & node_topics,
49  const std::string & topic_name,
50  const rclcpp::QoS & qos,
53  )
54 )
55 {
56  auto node_topics_interface = rclcpp::node_interfaces::get_node_topics_interface(node_topics);
57  const rclcpp::QoS & actual_qos = options.qos_overriding_options.get_policy_kinds().size() ?
58  rclcpp::detail::declare_qos_parameters(
59  options.qos_overriding_options, node_parameters,
60  node_topics_interface->resolve_topic_name(topic_name),
62  qos;
63 
64  // Create the publisher.
65  auto pub = node_topics_interface->create_publisher(
66  topic_name,
67  rclcpp::create_publisher_factory<MessageT, AllocatorT, PublisherT>(options),
68  actual_qos
69  );
70 
71  // Add the publisher to the node topics interface.
72  node_topics_interface->add_publisher(pub, options.callback_group);
73 
74  return std::dynamic_pointer_cast<PublisherT>(pub);
75 }
76 } // namespace detail
77 
78 
80 
88 template<
89  typename MessageT,
90  typename AllocatorT = std::allocator<void>,
91  typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>,
92  typename NodeT>
93 std::shared_ptr<PublisherT>
95  NodeT && node,
96  const std::string & topic_name,
97  const rclcpp::QoS & qos,
100  )
101 )
102 {
103  return detail::create_publisher<MessageT, AllocatorT, PublisherT>(
104  node, node, topic_name, qos, options);
105 }
106 
108 template<
109  typename MessageT,
110  typename AllocatorT = std::allocator<void>,
111  typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>>
112 std::shared_ptr<PublisherT>
114  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr & node_parameters,
115  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr & node_topics,
116  const std::string & topic_name,
117  const rclcpp::QoS & qos,
120  )
121 )
122 {
123  return detail::create_publisher<MessageT, AllocatorT, PublisherT>(
124  node_parameters, node_topics, topic_name, qos, options);
125 }
126 
127 } // namespace rclcpp
128 
129 #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:116
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.