ROS 2 rclcpp + rcl - kilted  kilted
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  }
182 
183 private:
185 
191  inline size_t next_(size_t val)
192  {
193  return (val + 1) % capacity_;
194  }
195 
197 
202  inline bool has_data_() const
203  {
204  return size_ != 0;
205  }
206 
208 
214  inline bool is_full_() const
215  {
216  return size_ == capacity_;
217  }
218 
220 
225  inline size_t available_capacity_() const
226  {
227  return capacity_ - size_;
228  }
229 
231  template<typename ...>
232  struct is_std_unique_ptr final : std::false_type {};
233  template<class T, typename ... Args>
234  struct is_std_unique_ptr<std::unique_ptr<T, Args...>> final : std::true_type
235  {
236  typedef T Ptr_type;
237  };
238 
240 
247  template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
248  std::is_copy_constructible<
249  typename is_std_unique_ptr<T>::Ptr_type
250  >::value,
251  void> * = nullptr>
252  std::vector<BufferT> get_all_data_impl()
253  {
254  std::lock_guard<std::mutex> lock(mutex_);
255  std::vector<BufferT> result_vtr;
256  result_vtr.reserve(size_);
257  for (size_t id = 0; id < size_; ++id) {
258  const auto & elem(ring_buffer_[(read_index_ + id) % capacity_]);
259  if (elem != nullptr) {
260  result_vtr.emplace_back(new typename is_std_unique_ptr<T>::Ptr_type(
261  *elem));
262  } else {
263  result_vtr.emplace_back(nullptr);
264  }
265  }
266  return result_vtr;
267  }
268 
269  template<typename T = BufferT, std::enable_if_t<
270  std::is_copy_constructible<T>::value, void> * = nullptr>
271  std::vector<BufferT> get_all_data_impl()
272  {
273  std::lock_guard<std::mutex> lock(mutex_);
274  std::vector<BufferT> result_vtr;
275  result_vtr.reserve(size_);
276  for (size_t id = 0; id < size_; ++id) {
277  result_vtr.emplace_back(ring_buffer_[(read_index_ + id) % capacity_]);
278  }
279  return result_vtr;
280  }
281 
282  template<typename T = BufferT, std::enable_if_t<!is_std_unique_ptr<T>::value &&
283  !std::is_copy_constructible<T>::value, void> * = nullptr>
284  std::vector<BufferT> get_all_data_impl()
285  {
286  throw std::logic_error("Underlined type results in invalid get_all_data_impl()");
287  return {};
288  }
289 
290  template<typename T = BufferT, std::enable_if_t<is_std_unique_ptr<T>::value &&
291  !std::is_copy_constructible<typename is_std_unique_ptr<T>::Ptr_type>::value,
292  void> * = nullptr>
293  std::vector<BufferT> get_all_data_impl()
294  {
295  throw std::logic_error("Underlined type in unique_ptr results in invalid get_all_data_impl()");
296  return {};
297  }
298 
299  size_t capacity_;
300 
301  std::vector<BufferT> ring_buffer_;
302 
303  size_t write_index_;
304  size_t read_index_;
305  size_t size_;
306 
307  mutable std::mutex mutex_;
308 };
309 
310 } // namespace buffers
311 } // namespace experimental
312 } // namespace rclcpp
313 
314 #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.