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 
22 #include "rclcpp/macros.hpp"
24 
25 namespace rclcpp
26 {
27 namespace message_memory_strategy
28 {
29 
31 
32 template<typename MessageT, typename Alloc = std::allocator<void>>
34 {
35 public:
37 
38  using MessageAllocTraits = allocator::AllocRebind<MessageT, Alloc>;
39  using MessageAlloc = typename MessageAllocTraits::allocator_type;
40  using MessageDeleter = allocator::Deleter<MessageAlloc, MessageT>;
41 
43  {
44  message_allocator_ = std::make_shared<MessageAlloc>();
45  }
46 
48  {
49  message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get());
50  }
51 
53  static SharedPtr create_default()
54  {
55  return std::make_shared<MessageMemoryStrategy<MessageT, Alloc>>(std::make_shared<Alloc>());
56  }
57 
59 
61  {
62  return std::allocate_shared<MessageT, MessageAlloc>(*message_allocator_.get());
63  }
64 
66 
68  {
69  msg.reset();
70  }
71 
74 };
75 
76 } // namespace message_memory_strategy
77 } // namespace rclcpp
78 
79 #endif // RCLCPP__MESSAGE_MEMORY_STRATEGY_HPP_
static SharedPtr create_default()
Default factory method.
Definition: message_memory_strategy.hpp:53
Definition: allocator_common.hpp:24
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:67
std::shared_ptr< MessageAlloc > message_allocator_
Definition: message_memory_strategy.hpp:72
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
T reset(T... args)
allocator::AllocRebind< MessageT, std::allocator< void > > MessageAllocTraits
Definition: message_memory_strategy.hpp:38
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:33
MessageMemoryStrategy(std::shared_ptr< Alloc > allocator)
Definition: message_memory_strategy.hpp:47
T get(T... args)
allocator::Deleter< MessageAlloc, MessageT > MessageDeleter
Definition: message_memory_strategy.hpp:40
typename MessageAllocTraits::allocator_type MessageAlloc
Definition: message_memory_strategy.hpp:39
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
virtual std::shared_ptr< MessageT > borrow_message()
By default, dynamically allocate a new message.
Definition: message_memory_strategy.hpp:60
MessageDeleter message_deleter_
Definition: message_memory_strategy.hpp:73