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>>
34 using MessageAllocatorTraits = rclcpp::allocator::AllocRebind<MessageT, AllocatorT>;
35 using MessageAllocator =
typename MessageAllocatorTraits::allocator_type;
61 std::allocator<MessageT> allocator)
64 message_allocator_(std::move(allocator))
67 void * message_ptr =
nullptr;
70 rosidl_typesupport_cpp::get_message_type_support_handle<MessageT>(),
73 rclcpp::exceptions::throw_from_rcl_error(ret);
75 message_ =
static_cast<MessageT *
>(message_ptr);
79 "Currently used middleware can't loan messages. Local allocator will be used.");
80 message_ = message_allocator_.allocate(1);
81 new (message_) MessageT();
107 deprecated(
"used the LoanedMessage constructor that does not use a shared_ptr to the allocator")
111 std::shared_ptr<std::allocator<MessageT>> allocator)
117 : pub_(std::move(other.pub_)),
118 message_(std::move(other.message_)),
119 message_allocator_(std::move(other.message_allocator_))
121 other.message_ =
nullptr;
140 if (message_ ==
nullptr) {
150 error_logger,
"rcl_deallocate_loaned_message failed: %s", rcl_get_error_string().str);
155 message_->~MessageT();
156 message_allocator_.deallocate(message_, 1);
171 return message_ !=
nullptr;
198 std::unique_ptr<MessageT, std::function<void(MessageT *)>>
205 return std::unique_ptr<MessageT, std::function<void(MessageT *)>>(msg, [](MessageT *) {});
208 return std::unique_ptr<MessageT, std::function<void(MessageT *)>>(
210 [allocator = message_allocator_](MessageT * msg_ptr)
mutable {
212 msg_ptr->~MessageT();
213 allocator.deallocate(msg_ptr, 1);
222 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, std::shared_ptr< std::allocator< MessageT >> allocator)
Constructor of the LoanedMessage class.
MessageT & get() const
Access the ROS message instance.
LoanedMessage(const rclcpp::PublisherBase &pub, std::allocator< MessageT > allocator)
Constructor of the LoanedMessage class.
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.