ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
generic_publisher.hpp
1 // Copyright 2018, Bosch Software Innovations GmbH.
2 // Copyright 2021, Apex.AI Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef RCLCPP__GENERIC_PUBLISHER_HPP_
17 #define RCLCPP__GENERIC_PUBLISHER_HPP_
18 
19 #include <memory>
20 #include <string>
21 
22 #include "rcpputils/shared_library.hpp"
23 
24 #include "rclcpp/callback_group.hpp"
25 #include "rclcpp/macros.hpp"
26 #include "rclcpp/node_interfaces/node_base_interface.hpp"
27 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
28 #include "rclcpp/publisher_base.hpp"
29 #include "rclcpp/qos.hpp"
30 #include "rclcpp/serialized_message.hpp"
31 #include "rclcpp/typesupport_helpers.hpp"
32 #include "rclcpp/visibility_control.hpp"
33 
34 #include "rmw/rmw.h"
35 
36 namespace rclcpp
37 {
38 
40 
47 {
48 public:
49  // cppcheck-suppress unknownMacro
50  RCLCPP_SMART_PTR_DEFINITIONS(GenericPublisher)
51 
52 
69  template<typename AllocatorT = std::allocator<void>>
72  std::shared_ptr<rcpputils::SharedLibrary> ts_lib,
73  const std::string & topic_name,
74  const std::string & topic_type,
75  const rclcpp::QoS & qos,
78  node_base,
79  topic_name,
80  *rclcpp::get_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib),
81  options.template to_rcl_publisher_options<rclcpp::SerializedMessage>(qos)),
82  ts_lib_(ts_lib)
83  {
84  // This is unfortunately duplicated with the code in publisher.hpp.
85  // TODO(nnmm): Deduplicate by moving this into PublisherBase.
86  if (options.event_callbacks.deadline_callback) {
87  this->add_event_handler(
88  options.event_callbacks.deadline_callback,
89  RCL_PUBLISHER_OFFERED_DEADLINE_MISSED);
90  }
91  if (options.event_callbacks.liveliness_callback) {
92  this->add_event_handler(
93  options.event_callbacks.liveliness_callback,
94  RCL_PUBLISHER_LIVELINESS_LOST);
95  }
96  if (options.event_callbacks.incompatible_qos_callback) {
97  this->add_event_handler(
98  options.event_callbacks.incompatible_qos_callback,
99  RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS);
100  } else if (options.use_default_callbacks) {
101  // Register default callback when not specified
102  try {
103  this->add_event_handler(
104  [this](QOSOfferedIncompatibleQoSInfo & info) {
105  this->default_incompatible_qos_callback(info);
106  },
107  RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS);
108  } catch (UnsupportedEventTypeException & /*exc*/) {
109  // pass
110  }
111  }
112  }
113 
114  RCLCPP_PUBLIC
115  virtual ~GenericPublisher() = default;
116 
118  RCLCPP_PUBLIC
119  void publish(const rclcpp::SerializedMessage & message);
120 
127  RCLCPP_PUBLIC
128  void publish_as_loaned_msg(const rclcpp::SerializedMessage & message);
129 
130 private:
131  // The type support library should stay loaded, so it is stored in the GenericPublisher
132  std::shared_ptr<rcpputils::SharedLibrary> ts_lib_;
133 
134  void * borrow_loaned_message();
135  void deserialize_message(
136  const rmw_serialized_message_t & serialized_message,
137  void * deserialized_msg);
138  void publish_loaned_message(void * loaned_message);
139 };
140 
141 } // namespace rclcpp
142 
143 #endif // RCLCPP__GENERIC_PUBLISHER_HPP_
Publisher for serialized messages whose type is not known at compile time.
RCLCPP_PUBLIC void publish(const rclcpp::SerializedMessage &message)
Publish a rclcpp::SerializedMessage.
GenericPublisher(rclcpp::node_interfaces::NodeBaseInterface *node_base, std::shared_ptr< rcpputils::SharedLibrary > ts_lib, const std::string &topic_name, const std::string &topic_type, const rclcpp::QoS &qos, const rclcpp::PublisherOptionsWithAllocator< AllocatorT > &options)
Constructor.
RCLCPP_PUBLIC void publish_as_loaned_msg(const rclcpp::SerializedMessage &message)
Encapsulation of Quality of Service settings.
Definition: qos.hpp:111
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
Pure virtual interface class for the NodeBase part of the Node API.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC const rosidl_message_type_support_t * get_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extract the type support handle from the library.
PublisherEventCallbacks event_callbacks
Callbacks for various events related to publishers.
bool use_default_callbacks
Whether or not to use default callbacks when user doesn't supply any in event_callbacks.
Structure containing optional configuration for Publishers.