rclcpp  master
C++ ROS Client Library API
message_memory_strategy.hpp
Go to the documentation of this file.
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
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 RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
16 #define RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
17 
18 #include <memory>
19 #include <stdexcept>
20 
21 #include "rcl/types.h"
22 
24 #include "rclcpp/exceptions.hpp"
25 #include "rclcpp/macros.hpp"
28 
29 #include "rcutils/logging_macros.h"
30 
31 #include "rmw/serialized_message.h"
32 
33 namespace rclcpp
34 {
35 namespace message_memory_strategy
36 {
37 
39 
40 template<typename MessageT, typename Alloc = std::allocator<void>>
42 {
43 public:
45 
46  using MessageAllocTraits = allocator::AllocRebind<MessageT, Alloc>;
47  using MessageAlloc = typename MessageAllocTraits::allocator_type;
48  using MessageDeleter = allocator::Deleter<MessageAlloc, MessageT>;
49 
51  using SerializedMessageAlloc = typename SerializedMessageAllocTraits::allocator_type;
54 
55  using BufferAllocTraits = allocator::AllocRebind<char, Alloc>;
56  using BufferAlloc = typename BufferAllocTraits::allocator_type;
57  using BufferDeleter = allocator::Deleter<BufferAlloc, char>;
58 
60  {
61  message_allocator_ = std::make_shared<MessageAlloc>();
62  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>();
63  buffer_allocator_ = std::make_shared<BufferAlloc>();
64  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
65  }
66 
68  {
69  message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get());
70  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>(*allocator.get());
71  buffer_allocator_ = std::make_shared<BufferAlloc>(*allocator.get());
72  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
73  }
74 
75  virtual ~MessageMemoryStrategy() = default;
76 
79  {
80  return std::make_shared<MessageMemoryStrategy<MessageT, Alloc>>(std::make_shared<Alloc>());
81  }
82 
84 
86  {
87  return std::allocate_shared<MessageT, MessageAlloc>(*message_allocator_.get());
88  }
89 
91  {
92  return std::make_shared<rclcpp::SerializedMessage>(capacity);
93  }
94 
96  {
98  }
99 
100  virtual void set_default_buffer_capacity(size_t capacity)
101  {
102  default_buffer_capacity_ = capacity;
103  }
104 
106 
108  {
109  msg.reset();
110  }
111 
114  {
115  serialized_msg.reset();
116  }
117 
120 
123 
127 
129 };
130 
131 } // namespace message_memory_strategy
132 } // namespace rclcpp
133 
134 #endif // RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
rclcpp::message_memory_strategy::MessageMemoryStrategy::borrow_message
virtual std::shared_ptr< MessageT > borrow_message()
By default, dynamically allocate a new message.
Definition: message_memory_strategy.hpp:85
exceptions.hpp
rclcpp::message_memory_strategy::MessageMemoryStrategy::message_deleter_
MessageDeleter message_deleter_
Definition: message_memory_strategy.hpp:119
std::shared_ptr
rclcpp::message_memory_strategy::MessageMemoryStrategy::return_message
virtual void return_message(std::shared_ptr< MessageT > &msg)
Release ownership of the message, which will deallocate it if it has no more owners.
Definition: message_memory_strategy.hpp:107
rclcpp::message_memory_strategy::MessageMemoryStrategy::borrow_serialized_message
virtual std::shared_ptr< rclcpp::SerializedMessage > borrow_serialized_message()
Definition: message_memory_strategy.hpp:95
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::MessageAlloc
typename MessageAllocTraits::allocator_type MessageAlloc
Definition: message_memory_strategy.hpp:47
rclcpp::message_memory_strategy::MessageMemoryStrategy::return_serialized_message
virtual void return_serialized_message(std::shared_ptr< rclcpp::SerializedMessage > &serialized_msg)
Definition: message_memory_strategy.hpp:112
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::BufferDeleter
allocator::Deleter< BufferAlloc, char > BufferDeleter
Definition: message_memory_strategy.hpp:57
std::shared_ptr::get
T get(T... args)
logging_macros.h
serialized_message.h
rclcpp::message_memory_strategy::MessageMemoryStrategy::default_buffer_capacity_
size_t default_buffer_capacity_
Definition: message_memory_strategy.hpp:126
rclcpp::message_memory_strategy::MessageMemoryStrategy::serialized_message_deleter_
SerializedMessageDeleter serialized_message_deleter_
Definition: message_memory_strategy.hpp:122
std::shared_ptr::reset
T reset(T... args)
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::MessageDeleter
allocator::Deleter< MessageAlloc, MessageT > MessageDeleter
Definition: message_memory_strategy.hpp:48
serialized_message.hpp
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::message_memory_strategy::MessageMemoryStrategy::serialized_message_allocator_
std::shared_ptr< SerializedMessageAlloc > serialized_message_allocator_
Definition: message_memory_strategy.hpp:121
rclcpp::message_memory_strategy::MessageMemoryStrategy::create_default
static SharedPtr create_default()
Default factory method.
Definition: message_memory_strategy.hpp:78
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
macros.hpp
rclcpp::message_memory_strategy::MessageMemoryStrategy::buffer_allocator_
std::shared_ptr< BufferAlloc > buffer_allocator_
Definition: message_memory_strategy.hpp:124
allocator_common.hpp
rclcpp::message_memory_strategy::MessageMemoryStrategy::set_default_buffer_capacity
virtual void set_default_buffer_capacity(size_t capacity)
Definition: message_memory_strategy.hpp:100
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::MessageAllocTraits
allocator::AllocRebind< MessageT, std::allocator< void > > MessageAllocTraits
Definition: message_memory_strategy.hpp:46
rclcpp::message_memory_strategy::MessageMemoryStrategy::borrow_serialized_message
virtual std::shared_ptr< rclcpp::SerializedMessage > borrow_serialized_message(size_t capacity)
Definition: message_memory_strategy.hpp:90
rclcpp::allocator::AllocRebind
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::SerializedMessageAlloc
typename SerializedMessageAllocTraits::allocator_type SerializedMessageAlloc
Definition: message_memory_strategy.hpp:51
rclcpp::IntraProcessBufferType::SharedPtr
@ SharedPtr
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::BufferAllocTraits
allocator::AllocRebind< char, std::allocator< void > > BufferAllocTraits
Definition: message_memory_strategy.hpp:55
rclcpp::message_memory_strategy::MessageMemoryStrategy::rcutils_allocator_
rcutils_allocator_t rcutils_allocator_
Definition: message_memory_strategy.hpp:128
rcutils_allocator_t
visibility_control.hpp
rclcpp::allocator::Deleter
typename std::conditional< std::is_same< typename std::allocator_traits< Alloc >::template rebind_alloc< T >, typename std::allocator< void >::template rebind< T >::other >::value, std::default_delete< T >, AllocatorDeleter< Alloc > >::type Deleter
Definition: allocator_deleter.hpp:101
rclcpp::message_memory_strategy::MessageMemoryStrategy
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:41
rclcpp::SerializedMessage
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
Definition: serialized_message.hpp:27
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::SerializedMessageAllocTraits
allocator::AllocRebind< rclcpp::SerializedMessage, std::allocator< void > > SerializedMessageAllocTraits
Definition: message_memory_strategy.hpp:50
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::BufferAlloc
typename BufferAllocTraits::allocator_type BufferAlloc
Definition: message_memory_strategy.hpp:56
rclcpp::message_memory_strategy::MessageMemoryStrategy::message_allocator_
std::shared_ptr< MessageAlloc > message_allocator_
Definition: message_memory_strategy.hpp:118
rclcpp::message_memory_strategy::MessageMemoryStrategy::buffer_deleter_
BufferDeleter buffer_deleter_
Definition: message_memory_strategy.hpp:125
types.h
rclcpp::message_memory_strategy::MessageMemoryStrategy::MessageMemoryStrategy
MessageMemoryStrategy(std::shared_ptr< Alloc > allocator)
Definition: message_memory_strategy.hpp:67
rclcpp::message_memory_strategy::MessageMemoryStrategy::~MessageMemoryStrategy
virtual ~MessageMemoryStrategy()=default
rclcpp::message_memory_strategy::MessageMemoryStrategy< MessageT >::SerializedMessageDeleter
allocator::Deleter< SerializedMessageAlloc, rclcpp::SerializedMessage > SerializedMessageDeleter
Definition: message_memory_strategy.hpp:53