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 
32 template<
33  typename MessageT,
34  typename AllocatorT = std::allocator<void>,
35  typename PublisherT = ::rclcpp::Publisher<MessageT, AllocatorT>>
36 // cppcheck-suppress syntaxError // bug in cppcheck 1.82 for [[deprecated]] on templated function
37 [[deprecated("use alternative rclcpp::create_publisher() signatures")]]
41  const std::string & topic_name,
42  const rmw_qos_profile_t & qos_profile,
43  const PublisherEventCallbacks & event_callbacks,
44  rclcpp::callback_group::CallbackGroup::SharedPtr group,
45  bool use_intra_process_comms,
47 {
48  auto publisher_options = rcl_publisher_get_default_options();
49  publisher_options.qos = qos_profile;
50 
51  auto pub = node_topics->create_publisher(
52  topic_name,
53  rclcpp::create_publisher_factory<MessageT, AllocatorT, PublisherT>(event_callbacks, allocator),
54  publisher_options,
55  use_intra_process_comms);
56 
57  node_topics->add_publisher(pub, group);
58 
59  return std::dynamic_pointer_cast<PublisherT>(pub);
60 }
61 
63 
67 template<
68  typename MessageT,
69  typename AllocatorT = std::allocator<void>,
70  typename PublisherT = ::rclcpp::Publisher<MessageT, AllocatorT>,
71  typename NodeT>
74  NodeT & node,
75  const std::string & topic_name,
76  const rclcpp::QoS & qos,
79 ))
80 {
82  auto node_topics = get_node_topics_interface(node);
83 
84  std::shared_ptr<AllocatorT> allocator = options.allocator;
85  if (!allocator) {
86  allocator = std::make_shared<AllocatorT>();
87  }
88 
89  bool use_intra_process;
90  switch (options.use_intra_process_comm) {
92  use_intra_process = true;
93  break;
95  use_intra_process = false;
96  break;
98  use_intra_process = node_topics->get_node_base_interface()->get_use_intra_process_default();
99  break;
100  default:
101  throw std::runtime_error("Unrecognized IntraProcessSetting value");
102  break;
103  }
104 
105  // TODO(wjwwood): convert all of the interfaces to use QoS and PublisherOptionsBase
106  auto pub = node_topics->create_publisher(
107  topic_name,
108  rclcpp::create_publisher_factory<MessageT, AllocatorT, PublisherT>(
109  options.event_callbacks,
110  allocator
111  ),
112  options.template to_rcl_publisher_options<MessageT>(qos),
113  use_intra_process
114  );
115  node_topics->add_publisher(pub, options.callback_group);
116  return std::dynamic_pointer_cast<PublisherT>(pub);
117 }
118 
119 } // namespace rclcpp
120 
121 #endif // RCLCPP__CREATE_PUBLISHER_HPP_
virtual rclcpp::PublisherBase::SharedPtr create_publisher(const std::string &topic_name, const rclcpp::PublisherFactory &publisher_factory, const rcl_publisher_options_t &publisher_options, bool use_intra_process)=0
Encapsulation of Quality of Service settings.
Definition: qos.hpp:55
Contains callbacks for various types of events a Publisher can receive from the middleware.
Definition: qos_event.hpp:42
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
std::shared_ptr< PublisherT > create_publisher(rclcpp::node_interfaces::NodeTopicsInterface *node_topics, const std::string &topic_name, const rmw_qos_profile_t &qos_profile, const PublisherEventCallbacks &event_callbacks, rclcpp::callback_group::CallbackGroup::SharedPtr group, bool use_intra_process_comms, std::shared_ptr< AllocatorT > allocator)
Definition: create_publisher.hpp:39
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
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:46
Explicitly enable intraprocess comm at publisher/subscription level.
Explicitly disable intraprocess comm at publisher/subscription level.
T dynamic_pointer_cast(T... args)
rcl_publisher_options_t rcl_publisher_get_default_options(void)
Structure containing optional configuration for Publishers.
Definition: publisher_options.hpp:47
Pure virtual interface class for the NodeTopics part of the Node API.
Definition: node_topics_interface.hpp:38
virtual void add_publisher(rclcpp::PublisherBase::SharedPtr publisher, rclcpp::callback_group::CallbackGroup::SharedPtr callback_group)=0
Take intraprocess configuration from the node.