rclcpp  master
C++ ROS Client Library API
create_publisher.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__CREATE_PUBLISHER_HPP_
16 #define RCLCPP__CREATE_PUBLISHER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
24 #include "rclcpp/node_options.hpp"
27 #include "rclcpp/qos.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>
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() ?
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>
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>>
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_
get_node_topics_interface.hpp
std::string
rclcpp::Publisher
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:53
std::shared_ptr
rclcpp::create_publisher
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.
Definition: create_publisher.hpp:94
qos_parameters.hpp
rclcpp::detail::declare_qos_parameters
std::enable_if_t< rclcpp::node_interfaces::has_node_parameters_interface< decltype(std::declval< typename rcpputils::remove_pointer< NodeT >::type >))>::value||std::is_same< typename std::decay_t< NodeT >, rclcpp::node_interfaces::NodeParametersInterface::SharedPtr >::value, rclcpp::QoS > declare_qos_parameters(const ::rclcpp::QosOverridingOptions &options, NodeT &node, const std::string &topic_name, const ::rclcpp::QoS &default_qos, EntityQosParametersTraits)
Definition: qos_parameters.hpp:126
rclcpp::detail::create_publisher
std::shared_ptr< PublisherT > create_publisher(NodeParametersT &node_parameters, NodeTopicsT &node_topics, 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.
Definition: create_publisher.hpp:46
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
qos.hpp
node_topics_interface.hpp
rclcpp::QoS
Encapsulation of Quality of Service settings.
Definition: qos.hpp:110
publisher_options.hpp
publisher_factory.hpp
qos_overriding_options.hpp
rclcpp::node_interfaces::get_node_topics_interface
std::shared_ptr< rclcpp::node_interfaces::NodeTopicsInterface > get_node_topics_interface(NodeType &&node)
Get the NodeTopicsInterface as a shared pointer from a pointer to a "Node like" object.
Definition: get_node_topics_interface.hpp:72
rclcpp::PublisherOptionsWithAllocator
Structure containing optional configuration for Publishers.
Definition: publisher_options.hpp:65
std::allocator
rclcpp::detail::PublisherQosParametersTraits
Definition: qos_parameters.hpp:42
qos_profiles.h
node_options.hpp