15 #ifndef RCLCPP__LOANED_MESSAGE_HPP_
16 #define RCLCPP__LOANED_MESSAGE_HPP_
21 #include "rclcpp/allocator/allocator_common.hpp"
22 #include "rclcpp/logging.hpp"
23 #include "rclcpp/publisher_base.hpp"
31 template<
typename MessageT,
typename AllocatorT = std::allocator<
void>>
35 using MessageAllocatorTraits = rclcpp::allocator::AllocRebind<MessageT, AllocatorT>;
36 using MessageAllocator =
typename MessageAllocatorTraits::allocator_type;
60 MessageAllocator allocator)
63 message_allocator_(std::move(allocator))
66 void * message_ptr =
nullptr;
69 rosidl_typesupport_cpp::get_message_type_support_handle<MessageT>(),
72 rclcpp::exceptions::throw_from_rcl_error(ret);
74 message_ =
static_cast<MessageT *
>(message_ptr);
78 "Currently used middleware can't loan messages. Local allocator will be used.");
79 message_ = message_allocator_.allocate(1);
80 new (message_) MessageT();
86 : pub_(std::move(other.pub_)),
87 message_(std::move(other.message_)),
88 message_allocator_(std::move(other.message_allocator_))
90 other.message_ =
nullptr;
109 if (message_ ==
nullptr) {
119 error_logger,
"rcl_deallocate_loaned_message failed: %s", rcl_get_error_string().str);
124 message_->~MessageT();
125 message_allocator_.deallocate(message_, 1);
140 return message_ !=
nullptr;
167 std::unique_ptr<MessageT, std::function<void(MessageT *)>>
174 return std::unique_ptr<MessageT, std::function<void(MessageT *)>>(msg, [](MessageT *) {});
177 return std::unique_ptr<MessageT, std::function<void(MessageT *)>>(
179 [allocator = message_allocator_](MessageT * msg_ptr)
mutable {
181 msg_ptr->~MessageT();
182 allocator.deallocate(msg_ptr, 1);
191 MessageAllocator message_allocator_;
std::unique_ptr< MessageT, std::function< void(MessageT *)> > release()
Release ownership of the ROS message instance.
LoanedMessage(const LoanedMessage< MessageT > &other)=delete
Deleted copy constructor to preserve memory integrity.
LoanedMessage(const rclcpp::PublisherBase &pub, MessageAllocator allocator)
Constructor of the LoanedMessage class.
MessageT & get() const
Access the ROS message instance.
LoanedMessage(LoanedMessage< MessageT > &&other)
Move semantic for RVO.
virtual ~LoanedMessage()
Destructor of the LoanedMessage class.
bool is_valid() const
Validate if the message was correctly allocated.
RCLCPP_PUBLIC std::shared_ptr< rcl_publisher_t > get_publisher_handle()
Get the rcl publisher handle.
RCLCPP_PUBLIC bool can_loan_messages() const
Check if publisher instance can loan messages.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
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.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_return_loaned_message_from_publisher(const rcl_publisher_t *publisher, void *loaned_message)
Return a loaned message previously borrowed from a publisher.
#define RCL_RET_OK
Success return code.