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 
23 #include "rclcpp/node_options.hpp"
26 #include "rclcpp/qos.hpp"
27 #include "rmw/qos_profiles.h"
28 
29 namespace rclcpp
30 {
31 
33 
37 template<
38  typename MessageT,
39  typename AllocatorT = std::allocator<void>,
40  typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>,
41  typename NodeT>
44  NodeT & node,
45  const std::string & topic_name,
46  const rclcpp::QoS & qos,
49  )
50 )
51 {
52  // Extract the NodeTopicsInterface from the NodeT.
54  auto node_topics = get_node_topics_interface(node);
55 
56  // Create the publisher.
57  auto pub = node_topics->create_publisher(
58  topic_name,
59  rclcpp::create_publisher_factory<MessageT, AllocatorT, PublisherT>(options),
60  qos
61  );
62 
63  // Add the publisher to the node topics interface.
64  node_topics->add_publisher(pub, options.callback_group);
65 
66  return std::dynamic_pointer_cast<PublisherT>(pub);
67 }
68 
69 } // namespace rclcpp
70 
71 #endif // RCLCPP__CREATE_PUBLISHER_HPP_
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:43
Encapsulation of Quality of Service settings.
Definition: qos.hpp:55
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:51
T dynamic_pointer_cast(T... args)
rclcpp::node_interfaces::NodeTopicsInterface * get_node_topics_interface(NodeType node_pointer)
Get the NodeTopicsInterface as a pointer from a pointer to a "Node like" object.
Definition: get_node_topics_interface.hpp:126