ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
generic_publisher.cpp
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 #include "rclcpp/generic_publisher.hpp"
17 
18 #include <memory>
19 #include <string>
20 
21 namespace rclcpp
22 {
23 
25 {
26  auto return_code = rcl_publish_serialized_message(
27  get_publisher_handle().get(), &message.get_rcl_serialized_message(), NULL);
28 
29  if (return_code != RCL_RET_OK) {
30  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to publish serialized message");
31  }
32 }
33 
35 {
36  auto loaned_message = borrow_loaned_message();
37  deserialize_message(message.get_rcl_serialized_message(), loaned_message);
38  publish_loaned_message(loaned_message);
39 }
40 
41 void * GenericPublisher::borrow_loaned_message()
42 {
43  void * loaned_message = nullptr;
44  auto return_code = rcl_borrow_loaned_message(
45  get_publisher_handle().get(), &type_support_, &loaned_message);
46 
47  if (return_code != RMW_RET_OK) {
48  if (return_code == RCL_RET_UNSUPPORTED) {
49  rclcpp::exceptions::throw_from_rcl_error(
50  return_code,
51  "current middleware cannot support loan messages");
52  } else {
53  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to borrow loaned msg");
54  }
55  }
56  return loaned_message;
57 }
58 
59 void GenericPublisher::deserialize_message(
60  const rmw_serialized_message_t & serialized_message,
61  void * deserialized_msg)
62 {
63  auto return_code = rmw_deserialize(&serialized_message, &type_support_, deserialized_msg);
64  if (return_code != RMW_RET_OK) {
65  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to deserialize msg");
66  }
67 }
68 
69 void GenericPublisher::publish_loaned_message(void * loaned_message)
70 {
71  auto return_code = rcl_publish_loaned_message(
72  get_publisher_handle().get(), loaned_message, NULL);
73 
74  if (return_code != RCL_RET_OK) {
75  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to publish loaned message");
76  }
77 }
78 
79 } // namespace rclcpp
RCLCPP_PUBLIC void publish(const rclcpp::SerializedMessage &message)
Publish a rclcpp::SerializedMessage.
RCLCPP_PUBLIC void publish_as_loaned_msg(const rclcpp::SerializedMessage &message)
RCLCPP_PUBLIC std::shared_ptr< rcl_publisher_t > get_publisher_handle()
Get the rcl publisher handle.
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
rcl_serialized_message_t & get_rcl_serialized_message()
Get the underlying rcl_serialized_t handle.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publish_loaned_message(const rcl_publisher_t *publisher, void *ros_message, rmw_publisher_allocation_t *allocation)
Publish a loaned message on a topic using a publisher.
Definition: publisher.c:279
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publish_serialized_message(const rcl_publisher_t *publisher, const rcl_serialized_message_t *serialized_message, rmw_publisher_allocation_t *allocation)
Publish a serialized message on a topic using a publisher.
Definition: publisher.c:257
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_borrow_loaned_message(const rcl_publisher_t *publisher, const rosidl_message_type_support_t *type_support, void **ros_message)
Borrow a loaned message.
Definition: publisher.c:210
#define RCL_RET_UNSUPPORTED
Unsupported return code.
Definition: types.h:36
#define RCL_RET_OK
Success return code.
Definition: types.h:26