15 #ifndef RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
16 #define RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
24 #include "rclcpp/allocator/allocator_common.hpp"
25 #include "rclcpp/exceptions.hpp"
26 #include "rclcpp/macros.hpp"
27 #include "rclcpp/serialized_message.hpp"
28 #include "rclcpp/visibility_control.hpp"
30 #include "rcutils/logging_macros.h"
32 #include "rmw/serialized_message.h"
36 namespace message_memory_strategy
41 template<
typename MessageT,
typename Alloc = std::allocator<
void>>
47 using MessageAllocTraits = allocator::AllocRebind<MessageT, Alloc>;
48 using MessageAlloc =
typename MessageAllocTraits::allocator_type;
49 using MessageDeleter = allocator::Deleter<MessageAlloc, MessageT>;
51 using SerializedMessageAllocTraits = allocator::AllocRebind<rclcpp::SerializedMessage, Alloc>;
52 using SerializedMessageAlloc =
typename SerializedMessageAllocTraits::allocator_type;
53 using SerializedMessageDeleter =
54 allocator::Deleter<SerializedMessageAlloc, rclcpp::SerializedMessage>;
56 using BufferAllocTraits = allocator::AllocRebind<char, Alloc>;
57 using BufferAlloc =
typename BufferAllocTraits::allocator_type;
58 using BufferDeleter = allocator::Deleter<BufferAlloc, char>;
62 message_allocator_ = std::make_shared<MessageAlloc>();
63 serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>();
64 buffer_allocator_ = std::make_shared<BufferAlloc>();
65 if constexpr (std::is_same_v<Alloc, std::allocator<void>>) {
68 rcutils_allocator_ = allocator::get_rcl_allocator<char,
69 BufferAlloc>(*buffer_allocator_.get());
75 message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get());
76 serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>(*allocator.get());
77 buffer_allocator_ = std::make_shared<BufferAlloc>(*allocator.get());
78 if constexpr (std::is_same_v<Alloc, std::allocator<void>>) {
81 rcutils_allocator_ = allocator::get_rcl_allocator<char,
82 BufferAlloc>(*buffer_allocator_.get());
91 return std::make_shared<MessageMemoryStrategy<MessageT, Alloc>>(std::make_shared<Alloc>());
98 return std::allocate_shared<MessageT, MessageAlloc>(*message_allocator_.get());
101 virtual std::shared_ptr<rclcpp::SerializedMessage> borrow_serialized_message(
size_t capacity)
103 return std::make_shared<rclcpp::SerializedMessage>(capacity);
106 virtual std::shared_ptr<rclcpp::SerializedMessage> borrow_serialized_message()
108 return borrow_serialized_message(default_buffer_capacity_);
111 virtual void set_default_buffer_capacity(
size_t capacity)
113 default_buffer_capacity_ = capacity;
123 virtual void return_serialized_message(
124 std::shared_ptr<rclcpp::SerializedMessage> & serialized_msg)
126 serialized_msg.reset();
129 std::shared_ptr<MessageAlloc> message_allocator_;
130 MessageDeleter message_deleter_;
132 std::shared_ptr<SerializedMessageAlloc> serialized_message_allocator_;
133 SerializedMessageDeleter serialized_message_deleter_;
135 std::shared_ptr<BufferAlloc> buffer_allocator_;
136 BufferDeleter buffer_deleter_;
137 size_t default_buffer_capacity_ = 0;
139 rcutils_allocator_t rcutils_allocator_;
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
Default allocation strategy for messages received by subscriptions.
static SharedPtr create_default()
Default factory method.
virtual void return_message(std::shared_ptr< MessageT > &msg)
Release ownership of the message, which will deallocate it if it has no more owners.
virtual std::shared_ptr< MessageT > borrow_message()
By default, dynamically allocate a new message.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
@ SharedPtr
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>