ROS 2 rclcpp + rcl - lyrical  lyrical
ROS 2 C++ Client Library with ROS Client Library
ring_buffer_implementation.hpp
1 // Copyright 2019 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__EXPERIMENTAL__BUFFERS__RING_BUFFER_IMPLEMENTATION_HPP_
16 #define RCLCPP__EXPERIMENTAL__BUFFERS__RING_BUFFER_IMPLEMENTATION_HPP_
17 
18 #include <memory>
19 #include <mutex>
20 #include <stdexcept>
21 #include <utility>
22 #include <vector>
23 
24 #include "rclcpp/experimental/buffers/buffer_implementation_base.hpp"
25 #include "rclcpp/logger.hpp"
26 #include "rclcpp/logging.hpp"
27 #include "rclcpp/macros.hpp"
28 #include "rclcpp/visibility_control.hpp"
29 #include "tracetools/tracetools.h"
30 
31 namespace rclcpp
32 {
33 namespace experimental
34 {
35 namespace buffers
36 {
37 
39 
42 template<typename BufferT>
44 {
45 public:
46  explicit RingBufferImplementation(size_t capacity)
47  : capacity_(capacity),
48  ring_buffer_(capacity),
49  write_index_(capacity_ - 1),
50  read_index_(0),
51  size_(0)
52  {
53  if (capacity == 0) {
54  throw std::invalid_argument("capacity must be a positive, non-zero value");
55  }
56  TRACETOOLS_TRACEPOINT(
57  rclcpp_construct_ring_buffer,
58  static_cast<const void *>(this),
59  capacity_);
60  }
61 
62  virtual ~RingBufferImplementation() {}
63 
65 
70  void enqueue(BufferT request) override
71  {
72  std::lock_guard<std::mutex> lock(mutex_);
73 
74  write_index_ = next_(write_index_);
75  ring_buffer_[write_index_] = std::move(request);
76  TRACETOOLS_TRACEPOINT(
77  rclcpp_ring_buffer_enqueue,
78  static_cast<const void *>(this),
79  write_index_,
80  size_ + 1,
81  is_full_());
82 
83  if (is_full_()) {
84  read_index_ = next_(read_index_);
85  } else {
86  size_++;
87  }
88  }
89 
91 
96  BufferT dequeue() override
97  {
98  std::lock_guard<std::mutex> lock(mutex_);
99 
100  if (!has_data_()) {
101  return BufferT();
102  }
103 
104  auto request = std::move(ring_buffer_[read_index_]);
105  TRACETOOLS_TRACEPOINT(
106  rclcpp_ring_buffer_dequeue,
107  static_cast<const void *>(this),
108  read_index_,
109  size_ - 1);
110  read_index_ = next_(read_index_);
111 
112  size_--;
113 
114  return request;
115  }
116 
118 
123  std::vector<BufferT> get_all_data() override
124  {
125  return get_all_data_impl();
126  }
127 
129 
135  inline size_t next(size_t val)
136  {
137  std::lock_guard<std::mutex> lock(mutex_);
138  return next_(val);
139  }
140 
142 
147  inline bool has_data() const override
148  {
149  std::lock_guard<std::mutex> lock(mutex_);
150  return has_data_();
151  }
152 
154 
160  inline bool is_full() const
161  {
162  std::lock_guard<std::mutex> lock(mutex_);
163  return is_full_();
164  }
165 
167 
172  size_t available_capacity() const override
173  {
174  std::lock_guard<std::mutex> lock(mutex_);
175  return available_capacity_();
176  }
177 
178  void clear() override
179  {
180  TRACETOOLS_TRACEPOINT(rclcpp_ring_buffer_clear, static_cast<const void *>(this));
181  std::lock_guard<std::mutex> lock(mutex_);
182  clear_();
183  }
184 
185 private:
187 
193  inline size_t next_(size_t val)
194  {
195  return (val + 1) % capacity_;
196  }
197 
199 
204  inline bool has_data_() const
205  {
206  return size_ != 0;
207  }
208 
210 
216  inline bool is_full_() const
217  {
218  return size_ == capacity_;
219  }
220 
222 
227  inline size_t available_capacity_() const
228  {
229  return capacity_ - size_;
230  }
231 
232  inline void clear_()
233  {
234  size_ = 0;
235  read_index_ = 0;
236  write_index_ = capacity_ - 1;
237  }
238 
240  template<typename ...>
241  struct is_std_unique_ptr final : std::false_type {};
242  template<class T, typename ... Args>
243  struct is_std_unique_ptr<std::unique_ptr<T, Args...>> final : std::true_type
244  {
245  typedef T Ptr_type;
246  };
247 
249 
256  template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
257  std::is_copy_constructible<
258  typename is_std_unique_ptr<T>::Ptr_type
259  >::value,
260  void> * = nullptr>
261  std::vector<BufferT> get_all_data_impl()
262  {
263  std::lock_guard<std::mutex> lock(mutex_);
264  std::vector<BufferT> result_vtr;
265  result_vtr.reserve(size_);
266  for (size_t id = 0; id < size_; ++id) {
267  const auto & elem(ring_buffer_[(read_index_ + id) % capacity_]);
268  if (elem != nullptr) {
269  result_vtr.emplace_back(new typename is_std_unique_ptr<T>::Ptr_type(
270  *elem));
271  } else {
272  result_vtr.emplace_back(nullptr);
273  }
274  }
275  return result_vtr;
276  }
277 
278  template<typename T = BufferT, std::enable_if_t<
279  std::is_copy_constructible<T>::value, void> * = nullptr>
280  std::vector<BufferT> get_all_data_impl()
281  {
282  std::lock_guard<std::mutex> lock(mutex_);
283  std::vector<BufferT> result_vtr;
284  result_vtr.reserve(size_);
285  for (size_t id = 0; id < size_; ++id) {
286  result_vtr.emplace_back(ring_buffer_[(read_index_ + id) % capacity_]);
287  }
288  return result_vtr;
289  }
290 
291  template<typename T = BufferT, std::enable_if_t<!is_std_unique_ptr<T>::value &&
292  !std::is_copy_constructible<T>::value, void> * = nullptr>
293  std::vector<BufferT> get_all_data_impl()
294  {
295  throw std::logic_error("Underlined type results in invalid get_all_data_impl()");
296  return {};
297  }
298 
299  template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
300  !std::is_copy_constructible<typename is_std_unique_ptr<T>::Ptr_type>::value,
301  void> * = nullptr>
302  std::vector<BufferT> get_all_data_impl()
303  {
304  throw std::logic_error("Underlined type in unique_ptr results in invalid get_all_data_impl()");
305  return {};
306  }
307 
308  const size_t capacity_;
309 
310  std::vector<BufferT> ring_buffer_;
311 
312  size_t write_index_;
313  size_t read_index_;
314  size_t size_;
315 
316  mutable std::mutex mutex_;
317 };
318 
319 } // namespace buffers
320 } // namespace experimental
321 } // namespace rclcpp
322 
323 #endif // RCLCPP__EXPERIMENTAL__BUFFERS__RING_BUFFER_IMPLEMENTATION_HPP_
bool is_full() const
Get if the size of the buffer is equal to its capacity.
bool has_data() const override
Get if the ring buffer has at least one element stored.
size_t available_capacity() const override
Get the remaining capacity to store messages.
std::vector< BufferT > get_all_data() override
Get all the elements from the ring buffer.
void enqueue(BufferT request) override
Add a new element to store in the ring buffer.
size_t next(size_t val)
Get the next index value for the ring buffer.
BufferT dequeue() override
Remove the oldest element from ring buffer.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.