rclcpp  master
C++ ROS Client Library API
generic_publisher.hpp
Go to the documentation of this file.
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 
25 #include "rclcpp/macros.hpp"
29 #include "rclcpp/qos.hpp"
33 
34 namespace rclcpp
35 {
36 
38 
45 {
46 public:
47  // cppcheck-suppress unknownMacro
49 
50 
51 
68  template<typename AllocatorT = std::allocator<void>>
72  const std::string & topic_name,
73  const std::string & topic_type,
74  const rclcpp::QoS & qos,
77  node_base,
78  topic_name,
79  *rclcpp::get_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib),
80  options.template to_rcl_publisher_options<rclcpp::SerializedMessage>(qos)),
81  ts_lib_(ts_lib)
82  {
83  // This is unfortunately duplicated with the code in publisher.hpp.
84  // TODO(nnmm): Deduplicate by moving this into PublisherBase.
85  if (options.event_callbacks.deadline_callback) {
86  this->add_event_handler(
87  options.event_callbacks.deadline_callback,
88  RCL_PUBLISHER_OFFERED_DEADLINE_MISSED);
89  }
90  if (options.event_callbacks.liveliness_callback) {
91  this->add_event_handler(
92  options.event_callbacks.liveliness_callback,
93  RCL_PUBLISHER_LIVELINESS_LOST);
94  }
95  if (options.event_callbacks.incompatible_qos_callback) {
96  this->add_event_handler(
97  options.event_callbacks.incompatible_qos_callback,
98  RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS);
99  } else if (options.use_default_callbacks) {
100  // Register default callback when not specified
101  try {
102  this->add_event_handler(
103  [this](QOSOfferedIncompatibleQoSInfo & info) {
105  },
106  RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS);
107  } catch (UnsupportedEventTypeException & /*exc*/) {
108  // pass
109  }
110  }
111  }
112 
114  virtual ~GenericPublisher() = default;
115 
118  void publish(const rclcpp::SerializedMessage & message);
119 
120 private:
121  // The type support library should stay loaded, so it is stored in the GenericPublisher
123 };
124 
125 } // namespace rclcpp
126 
127 #endif // RCLCPP__GENERIC_PUBLISHER_HPP_
callback_group.hpp
std::string
std::shared_ptr< rcpputils::SharedLibrary >
rclcpp::PublisherBase::default_incompatible_qos_callback
void default_incompatible_qos_callback(QOSOfferedIncompatibleQoSInfo &info) const
typesupport_helpers.hpp
serialized_message.hpp
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
qos.hpp
node_topics_interface.hpp
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::get_typesupport_handle
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.
rclcpp::node_interfaces::NodeBaseInterface
Pure virtual interface class for the NodeBase part of the Node API.
Definition: node_base_interface.hpp:36
rclcpp::QoS
Encapsulation of Quality of Service settings.
Definition: qos.hpp:110
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
macros.hpp
node_base_interface.hpp
rclcpp::GenericPublisher::~GenericPublisher
virtual ~GenericPublisher()=default
rclcpp::PublisherBase
Definition: publisher_base.hpp:56
rclcpp::UnsupportedEventTypeException
Definition: qos_event.hpp:69
rclcpp::PublisherBase::add_event_handler
void add_event_handler(const EventCallbackT &callback, const rcl_publisher_event_type_t event_type)
Definition: publisher_base.hpp:209
rclcpp::PublisherOptionsBase::use_default_callbacks
bool use_default_callbacks
Whether or not to use default callbacks when user doesn't supply any in event_callbacks.
Definition: publisher_options.hpp:46
publisher_base.hpp
rclcpp::PublisherOptionsBase::event_callbacks
PublisherEventCallbacks event_callbacks
Callbacks for various events related to publishers.
Definition: publisher_options.hpp:43
rclcpp::PublisherOptionsWithAllocator
Structure containing optional configuration for Publishers.
Definition: publisher_options.hpp:65
rclcpp::GenericPublisher::GenericPublisher
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.
Definition: generic_publisher.hpp:69
visibility_control.hpp
rclcpp::GenericPublisher::publish
void publish(const rclcpp::SerializedMessage &message)
Publish a rclcpp::SerializedMessage.
rmw_qos_incompatible_event_status_t
rclcpp::SerializedMessage
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
Definition: serialized_message.hpp:27
rclcpp::GenericPublisher
Publisher for serialized messages whose type is not known at compile time.
Definition: generic_publisher.hpp:44