ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
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  ring_buffer_.clear();
235  size_ = 0;
236  read_index_ = 0;
237  write_index_ = capacity_ - 1;
238  }
239 
241  template<typename ...>
242  struct is_std_unique_ptr final : std::false_type {};
243  template<class T, typename ... Args>
244  struct is_std_unique_ptr<std::unique_ptr<T, Args...>> final : std::true_type
245  {
246  typedef T Ptr_type;
247  };
248 
250 
257  template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
258  std::is_copy_constructible<
259  typename is_std_unique_ptr<T>::Ptr_type
260  >::value,
261  void> * = nullptr>
262  std::vector<BufferT> get_all_data_impl()
263  {
264  std::lock_guard<std::mutex> lock(mutex_);
265  std::vector<BufferT> result_vtr;
266  result_vtr.reserve(size_);
267  for (size_t id = 0; id < size_; ++id) {
268  const auto & elem(ring_buffer_[(read_index_ + id) % capacity_]);
269  if (elem != nullptr) {
270  result_vtr.emplace_back(new typename is_std_unique_ptr<T>::Ptr_type(
271  *elem));
272  } else {
273  result_vtr.emplace_back(nullptr);
274  }
275  }
276  return result_vtr;
277  }
278 
279  template<typename T = BufferT, std::enable_if_t<
280  std::is_copy_constructible<T>::value, void> * = nullptr>
281  std::vector<BufferT> get_all_data_impl()
282  {
283  std::lock_guard<std::mutex> lock(mutex_);
284  std::vector<BufferT> result_vtr;
285  result_vtr.reserve(size_);
286  for (size_t id = 0; id < size_; ++id) {
287  result_vtr.emplace_back(ring_buffer_[(read_index_ + id) % capacity_]);
288  }
289  return result_vtr;
290  }
291 
292  template<typename T = BufferT, std::enable_if_t<!is_std_unique_ptr<T>::value &&
293  !std::is_copy_constructible<T>::value, void> * = nullptr>
294  std::vector<BufferT> get_all_data_impl()
295  {
296  throw std::logic_error("Underlined type results in invalid get_all_data_impl()");
297  return {};
298  }
299 
300  template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
301  !std::is_copy_constructible<typename is_std_unique_ptr<T>::Ptr_type>::value,
302  void> * = nullptr>
303  std::vector<BufferT> get_all_data_impl()
304  {
305  throw std::logic_error("Underlined type in unique_ptr results in invalid get_all_data_impl()");
306  return {};
307  }
308 
309  const size_t capacity_;
310 
311  std::vector<BufferT> ring_buffer_;
312 
313  size_t write_index_;
314  size_t read_index_;
315  size_t size_;
316 
317  mutable std::mutex mutex_;
318 };
319 
320 } // namespace buffers
321 } // namespace experimental
322 } // namespace rclcpp
323 
324 #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.