15 #ifndef RCLCPP__MAPPED_RING_BUFFER_HPP_ 16 #define RCLCPP__MAPPED_RING_BUFFER_HPP_ 33 namespace mapped_ring_buffer
59 template<
typename T,
typename Alloc = std::allocator<
void>>
79 : elements_(size), head_(0)
85 allocator_ = std::make_shared<ElemAlloc>();
87 allocator_ = std::make_shared<ElemAlloc>(*allocator.get());
109 auto it = get_iterator_of_key(key);
111 if (it != elements_.end() && it->in_use) {
112 if (it->unique_value) {
113 ElemDeleter deleter = it->unique_value.get_deleter();
114 auto ptr = ElemAllocTraits::allocate(*allocator_.get(), 1);
115 ElemAllocTraits::construct(*allocator_.get(), ptr, *it->unique_value);
117 }
else if (it->shared_value) {
118 ElemDeleter * deleter = std::get_deleter<ElemDeleter, const T>(it->shared_value);
119 auto ptr = ElemAllocTraits::allocate(*allocator_.get(), 1);
120 ElemAllocTraits::construct(*allocator_.get(), ptr, *it->shared_value);
147 auto it = get_iterator_of_key(key);
149 if (it != elements_.end() && it->in_use) {
150 if (!it->shared_value) {
154 if (!it->unique_value) {
157 it->shared_value =
std::move(it->unique_value);
159 value = it->shared_value;
182 auto it = get_iterator_of_key(key);
184 if (it != elements_.end() && it->in_use) {
185 if (it->unique_value) {
187 }
else if (it->shared_value) {
188 auto ptr = ElemAllocTraits::allocate(*allocator_.get(), 1);
189 ElemAllocTraits::construct(*allocator_.get(), ptr, *it->shared_value);
190 auto deleter = std::get_deleter<ElemDeleter, const T>(it->shared_value);
196 it->shared_value.reset();
219 auto it = get_iterator_of_key(key);
220 if (it != elements_.end() && it->in_use) {
221 if (it->shared_value) {
223 }
else if (it->unique_value) {
248 bool did_replace = elements_[head_].in_use;
249 Element & element = elements_[head_];
251 element.unique_value.reset();
252 element.shared_value.reset();
253 element.shared_value = value;
254 element.in_use =
true;
255 head_ = (head_ + 1) % elements_.size();
267 bool did_replace = elements_[head_].in_use;
268 Element & element = elements_[head_];
270 element.unique_value.reset();
271 element.shared_value.reset();
273 element.in_use =
true;
274 head_ = (head_ + 1) % elements_.size();
283 return elements_.end() != get_iterator_of_key(key);
300 get_iterator_of_key(uint64_t key)
303 elements_.begin(), elements_.end(),
304 [key](Element & e) ->
bool {
305 return e.key == key && e.in_use;
319 #endif // RCLCPP__MAPPED_RING_BUFFER_HPP_
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
void pop(uint64_t key, ConstElemSharedPtr &value)
Give the ownership of the stored value to the caller, at the given key.
Definition: mapped_ring_buffer.hpp:216
bool push_and_replace(uint64_t key, ElemUniquePtr value)
Insert a key-value pair, displacing an existing pair if necessary.
Definition: mapped_ring_buffer.hpp:264
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
bool push_and_replace(uint64_t key, ConstElemSharedPtr value)
Insert a key-value pair, displacing an existing pair if necessary.
Definition: mapped_ring_buffer.hpp:245
Ring buffer container of shared_ptr's or unique_ptr's of T, which can be accessed by a key...
Definition: mapped_ring_buffer.hpp:60
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
Definition: mapped_ring_buffer.hpp:36
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
virtual ~MappedRingBuffer()
Definition: mapped_ring_buffer.hpp:91
allocator::AllocRebind< T, Alloc > ElemAllocTraits
Definition: mapped_ring_buffer.hpp:64
allocator::Deleter< ElemAlloc, T > ElemDeleter
Definition: mapped_ring_buffer.hpp:66
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
void pop(uint64_t key, ElemUniquePtr &value)
Give the ownership of the stored value to the caller if possible, or copy and release.
Definition: mapped_ring_buffer.hpp:179
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
typename ElemAllocTraits::allocator_type ElemAlloc
Definition: mapped_ring_buffer.hpp:65
bool has_key(uint64_t key)
Return true if the key is found in the ring buffer, otherwise false.
Definition: mapped_ring_buffer.hpp:280