ROS 2 rclcpp + rcl - jazzy  jazzy
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_message_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib),
81  options.template to_rcl_publisher_options<rclcpp::SerializedMessage>(qos),
82  // NOTE(methylDragon): Passing these args separately is necessary for event binding
83  options.event_callbacks,
84  options.use_default_callbacks),
85  ts_lib_(ts_lib)
86  {}
87 
88  RCLCPP_PUBLIC
89  virtual ~GenericPublisher() = default;
90 
92  RCLCPP_PUBLIC
93  void publish(const rclcpp::SerializedMessage & message);
94 
101  RCLCPP_PUBLIC
102  void publish_as_loaned_msg(const rclcpp::SerializedMessage & message);
103 
104 private:
105  // The type support library should stay loaded, so it is stored in the GenericPublisher
106  std::shared_ptr<rcpputils::SharedLibrary> ts_lib_;
107 
108  void * borrow_loaned_message();
109  void deserialize_message(
110  const rmw_serialized_message_t & serialized_message,
111  void * deserialized_msg);
112  void publish_loaned_message(void * loaned_message);
113 };
114 
115 } // namespace rclcpp
116 
117 #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:116
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_message_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extracts the message type support handle from the library.
Structure containing optional configuration for Publishers.