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"
27 
28 #include "rcutils/logging_macros.h"
29 
30 #include "rmw/serialized_message.h"
31 
32 namespace rclcpp
33 {
34 namespace message_memory_strategy
35 {
36 
38 
39 template<typename MessageT, typename Alloc = std::allocator<void>>
41 {
42 public:
44 
45  using MessageAllocTraits = allocator::AllocRebind<MessageT, Alloc>;
46  using MessageAlloc = typename MessageAllocTraits::allocator_type;
47  using MessageDeleter = allocator::Deleter<MessageAlloc, MessageT>;
48 
50  using SerializedMessageAlloc = typename SerializedMessageAllocTraits::allocator_type;
52  allocator::Deleter<SerializedMessageAlloc, rcl_serialized_message_t>;
53 
54  using BufferAllocTraits = allocator::AllocRebind<char, Alloc>;
55  using BufferAlloc = typename BufferAllocTraits::allocator_type;
56  using BufferDeleter = allocator::Deleter<BufferAlloc, char>;
57 
59  {
60  message_allocator_ = std::make_shared<MessageAlloc>();
61  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>();
62  buffer_allocator_ = std::make_shared<BufferAlloc>();
63  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
64  }
65 
67  {
68  message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get());
69  serialized_message_allocator_ = std::make_shared<SerializedMessageAlloc>(*allocator.get());
70  buffer_allocator_ = std::make_shared<BufferAlloc>(*allocator.get());
71  rcutils_allocator_ = allocator::get_rcl_allocator<char, BufferAlloc>(*buffer_allocator_.get());
72  }
73 
74  virtual ~MessageMemoryStrategy() = default;
75 
77  static SharedPtr create_default()
78  {
79  return std::make_shared<MessageMemoryStrategy<MessageT, Alloc>>(std::make_shared<Alloc>());
80  }
81 
83 
85  {
86  return std::allocate_shared<MessageT, MessageAlloc>(*message_allocator_.get());
87  }
88 
90  {
91  auto msg = new rcl_serialized_message_t;
93  auto ret = rmw_serialized_message_init(msg, capacity, &rcutils_allocator_);
94  if (ret != RCL_RET_OK) {
96  }
97 
98  auto serialized_msg = std::shared_ptr<rcl_serialized_message_t>(msg,
99  [](rmw_serialized_message_t * msg) {
100  auto ret = rmw_serialized_message_fini(msg);
101  delete msg;
102  if (ret != RCL_RET_OK) {
104  "rclcpp",
105  "failed to destroy serialized message: %s", rcl_get_error_string().str);
106  }
107  });
108 
109  return serialized_msg;
110  }
111 
113  {
115  }
116 
117  virtual void set_default_buffer_capacity(size_t capacity)
118  {
119  default_buffer_capacity_ = capacity;
120  }
121 
123 
125  {
126  msg.reset();
127  }
128 
130  {
131  serialized_msg.reset();
132  }
133 
136 
139 
143 
145 };
146 
147 } // namespace message_memory_strategy
148 } // namespace rclcpp
149 
150 #endif // RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
#define rcl_get_error_string
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:40
virtual std::shared_ptr< MessageT > borrow_message()
By default, dynamically allocate a new message.
Definition: message_memory_strategy.hpp:84
allocator::Deleter< MessageAlloc, MessageT > MessageDeleter
Definition: message_memory_strategy.hpp:47
allocator::Deleter< BufferAlloc, char > BufferDeleter
Definition: message_memory_strategy.hpp:56
allocator::AllocRebind< rcl_serialized_message_t, std::allocator< void > > SerializedMessageAllocTraits
Definition: message_memory_strategy.hpp:49
typename SerializedMessageAllocTraits::allocator_type SerializedMessageAlloc
Definition: message_memory_strategy.hpp:50
virtual void set_default_buffer_capacity(size_t capacity)
Definition: message_memory_strategy.hpp:117
Definition: allocator_common.hpp:24
allocator::AllocRebind< char, std::allocator< void > > BufferAllocTraits
Definition: message_memory_strategy.hpp:54
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:124
#define RCL_RET_OK
#define rmw_serialized_message_fini
static SharedPtr create_default()
Default factory method.
Definition: message_memory_strategy.hpp:77
void throw_from_rcl_error(rcl_ret_t ret, const std::string &prefix="", const rcl_error_state_t *error_state=nullptr, void(*reset_error)()=rcl_reset_error)
Throw a C++ std::exception which was created based on an rcl error.
std::shared_ptr< BufferAlloc > buffer_allocator_
Definition: message_memory_strategy.hpp:140
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
virtual std::shared_ptr< rcl_serialized_message_t > borrow_serialized_message(size_t capacity)
Definition: message_memory_strategy.hpp:89
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
BufferDeleter buffer_deleter_
Definition: message_memory_strategy.hpp:141
virtual std::shared_ptr< rcl_serialized_message_t > borrow_serialized_message()
Definition: message_memory_strategy.hpp:112
T reset(T... args)
size_t default_buffer_capacity_
Definition: message_memory_strategy.hpp:142
SerializedMessageDeleter serialized_message_deleter_
Definition: message_memory_strategy.hpp:138
typename BufferAllocTraits::allocator_type BufferAlloc
Definition: message_memory_strategy.hpp:55
#define rmw_get_zero_initialized_serialized_message
#define RCUTILS_LOG_ERROR_NAMED(name,...)
T get(T... args)
allocator::AllocRebind< MessageT, std::allocator< void > > MessageAllocTraits
Definition: message_memory_strategy.hpp:45
rcutils_allocator_t rcutils_allocator_
Definition: message_memory_strategy.hpp:144
std::shared_ptr< SerializedMessageAlloc > serialized_message_allocator_
Definition: message_memory_strategy.hpp:137
std::shared_ptr< MessageAlloc > message_allocator_
Definition: message_memory_strategy.hpp:134
MessageDeleter message_deleter_
Definition: message_memory_strategy.hpp:135
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
allocator::Deleter< SerializedMessageAlloc, rcl_serialized_message_t > SerializedMessageDeleter
Definition: message_memory_strategy.hpp:52
MessageMemoryStrategy(std::shared_ptr< Alloc > allocator)
Definition: message_memory_strategy.hpp:66
#define rmw_serialized_message_init
virtual void return_serialized_message(std::shared_ptr< rcl_serialized_message_t > &serialized_msg)
Definition: message_memory_strategy.hpp:129
typename MessageAllocTraits::allocator_type MessageAlloc
Definition: message_memory_strategy.hpp:46