generic_publisher.h
Go to the documentation of this file.
1 // Copyright 2018, Bosch Software Innovations GmbH.
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 GENERIC_PUBLISHER_H
16 #define GENERIC_PUBLISHER_H
17 
18 #include <memory>
19 #include <string>
20 #include <rclcpp/rclcpp.hpp>
21 #include <rclcpp/publisher_base.hpp>
22 
23 class GenericPublisher : public rclcpp::PublisherBase
24 {
25 public:
26  GenericPublisher(rclcpp::node_interfaces::NodeBaseInterface* node_base,
27  const std::string& topic_name,
28  const rosidl_message_type_support_t& type_support)
29 #ifdef ROS_HUMBLE
30  : rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options())
31 #else
32  : rclcpp::PublisherBase(node_base, topic_name, type_support, rcl_publisher_get_default_options(), callbacks_, true)
33 #endif
34  {}
35 
36  virtual ~GenericPublisher() = default;
37 
38  void publish(std::shared_ptr<rmw_serialized_message_t> message)
39  {
40  auto return_code = rcl_publish_serialized_message(get_publisher_handle().get(), message.get(), NULL);
41 
42  if (return_code != RCL_RET_OK)
43  {
44  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to publish serialized message");
45  }
46  }
47 
48  static std::shared_ptr<GenericPublisher> create(rclcpp::Node& node,
49  const std::string& topic_name,
50  const std::string& topic_type)
51  {
52 
53  auto library = std::move(rosbag2_cpp::get_typesupport_library(topic_type, "rosidl_typesupport_cpp"));
54  auto type_support = rosbag2_cpp::get_typesupport_handle(topic_type, "rosidl_typesupport_cpp", library);
55 
56 
57  return std::make_shared<GenericPublisher>(node.get_node_base_interface().get(), topic_name, *type_support);
58  }
59 
60  #ifndef ROS_HUMBLE
61  rclcpp::PublisherEventCallbacks callbacks_;
62  #endif
63 };
64 
65 #endif // GENERIC_PUBLISHER_H
GenericPublisher::publish
void publish(std::shared_ptr< rmw_serialized_message_t > message)
Definition: generic_publisher.h:38
GenericPublisher::create
static std::shared_ptr< GenericPublisher > create(rclcpp::Node &node, const std::string &topic_name, const std::string &topic_type)
Definition: generic_publisher.h:48
GenericPublisher::GenericPublisher
GenericPublisher(rclcpp::node_interfaces::NodeBaseInterface *node_base, const std::string &topic_name, const rosidl_message_type_support_t &type_support)
Definition: generic_publisher.h:26
get
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
GenericPublisher
Definition: generic_publisher.h:23
GenericPublisher::callbacks_
rclcpp::PublisherEventCallbacks callbacks_
Definition: generic_publisher.h:61
GenericPublisher::~GenericPublisher
virtual ~GenericPublisher()=default


plotjuggler_ros
Author(s): Davide Faconti
autogenerated on Wed Feb 21 2024 03:22:55