eDSP  0.0.1
A cross-platform DSP library written in C++.
fixed_ring_buffer.hpp
Go to the documentation of this file.
1 /*
2 * eDSP, A cross-platform Digital Signal Processing library written in modern C++.
3 * Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi, All rights reserved.
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation, either version 3 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along width
16 * this program. If not, see <http://www.gnu.org/licenses/>
17 *
18 * File: fixed_fixed_ring_buffer.hpp
19 * Author: Mohammed Boujemaoui
20 * Date: 07/10/18
21 */
22 
23 #ifndef EDSP_FIXED_RING_BUFFER_HPP
24 #define EDSP_FIXED_RING_BUFFER_HPP
25 
26 #include <edsp/meta/unused.hpp>
27 #include <edsp/types/ring_span.hpp>
28 #include <array>
29 
30 namespace edsp { inline namespace types {
31 
45  template <typename T, std::size_t MaxSize>
47  public:
48  typedef typename ring_span<T>::value_type value_type;
49  typedef typename ring_span<T>::pointer pointer;
50  typedef typename std::add_const_t<pointer> const_pointer;
51  typedef typename ring_span<value_type>::reference reference;
52  typedef typename ring_span<value_type>::const_reference const_reference;
53  typedef typename ring_span<value_type>::iterator iterator;
54  typedef typename ring_span<value_type>::const_iterator const_iterator;
55  typedef typename ring_span<value_type>::reverse_iterator reverse_iterator;
56  typedef typename ring_span<value_type>::const_reverse_iterator const_reverse_iterator;
57  typedef typename ring_span<value_type>::size_type size_type;
58  typedef std::ptrdiff_t difference_type;
59  typedef std::array<T, MaxSize> container_type;
60 
61  fixed_ring_buffer() = default;
62  fixed_ring_buffer(const fixed_ring_buffer&) = default;
63  fixed_ring_buffer(fixed_ring_buffer&&) noexcept = default;
64  fixed_ring_buffer& operator=(const fixed_ring_buffer&) = default;
65  fixed_ring_buffer& operator=(fixed_ring_buffer&&) noexcept = default;
66 
74  ~fixed_ring_buffer() = default;
75 
82  void clear() {
83  ring_ = edsp::ring_span<T>(std::begin(buffer_), std::end(buffer_));
84  }
85 
93  iterator begin() noexcept {
94  return ring_.begin();
95  }
96 
104  const_iterator begin() const noexcept {
105  return ring_.begin();
106  }
107 
115  iterator end() noexcept {
116  return ring_.end();
117  }
118 
126  const_iterator end() const noexcept {
127  return ring_.end();
128  }
129 
130  /*
131  * @brief Returns a read/write iterator that points to the first
132  *
133  * Iteration is done in reverse element order.
134  * @returns Iterator pointing to the first element.
135  */
136  reverse_iterator rbegin() noexcept {
137  return ring_.rbegin();
138  }
139 
147  const_reverse_iterator rbegin() const noexcept {
148  return ring_.rbegin();
149  }
150 
158  reverse_iterator rend() noexcept {
159  return ring_.rend();
160  }
161 
162  /*
163  * @brief Returns a read-only iterator that points to the last
164  * element in the %fixed_ring_buffer.
165  *
166  * Iteration is done in reverse element order.
167  * @returns Iterator pointing to the last element.
168  */
169  const_reverse_iterator rend() const noexcept {
170  return ring_.rend();
171  }
172 
180  const_iterator cbegin() const noexcept {
181  return ring_.cbegin();
182  }
183 
191  const_iterator cend() const noexcept {
192  return ring_.cend();
193  }
194 
202  const_reverse_iterator crbegin() const noexcept {
203  return ring_.crbegin();
204  }
205 
213  const_reverse_iterator crend() const noexcept {
214  return ring_.crend();
215  }
216 
221  size_type size() const {
222  return ring_.size();
223  }
224 
229  size_type max_size() const {
230  return MaxSize;
231  }
232 
237  bool empty() const {
238  return ring_.empty();
239  }
240 
245  bool full() const {
246  return ring_.full();
247  }
248 
253  size_type capacity() const {
254  return ring_.capacity();
255  }
256 
268  reference operator[](std::size_t i) {
269  return ring_[i];
270  }
271 
283  const_reference operator[](std::size_t i) const {
284  return ring_[i];
285  }
286 
298  reference at(std::size_t i) {
299  return ring_.at(i);
300  }
301 
313  const_reference at(std::size_t i) const {
314  return ring_.at(i);
315  }
316 
322  reference front() {
323  return ring_.front();
324  }
325 
331  const_reference front() const {
332  return ring_.front();
333  }
334 
340  reference back() {
341  return ring_.back();
342  }
343 
349  const_reference back() const {
350  return ring_.back();
351  }
352 
361  template <typename... Args>
362  void emplace_front(Args... arg) {
363  ring_.emplace_front(arg...);
364  }
365 
374  template <typename... Args>
375  void emplace_back(Args... arg) {
376  ring_.emplace_back(arg...);
377  }
378 
387  void push_back(const value_type& item) {
388  ring_.push_back(item);
389  }
390 
400  void pop_front() {
401  ring_.pop_front();
402  }
403 
413  void pop_back() {
414  ring_.pop_back();
415  }
416 
421  const container_type& buffer() const {
422  return buffer_;
423  }
424 
429  const_pointer* data() const {
430  return buffer_.data();
431  }
432 
433  private:
434  container_type buffer_{};
435  edsp::ring_span<T> ring_{std::begin(buffer_), std::end(buffer_)};
436  };
437 
438 }} // namespace edsp::types
439 
440 #endif //EDSP_FIXED_RING_BUFFER_HPP
This class implements a ring buffer, also called circular buffer.
Definition: fixed_ring_buffer.hpp:46
const_iterator cbegin() const noexcept
Returns a read-only iterator that points to the first element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:180
ring_span< value_type >::const_reference const_reference
Definition: fixed_ring_buffer.hpp:52
const_reverse_iterator rbegin() const noexcept
Returns a read-only iterator that points to the first element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:147
const_reference at(std::size_t i) const
Provides access to the data contained in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:313
size_type size() const
Returns the number of elements in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:221
bool full() const
Definition: fixed_ring_buffer.hpp:245
const_reverse_iterator crbegin() const noexcept
Returns a read-only iterator that points to the first element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:202
reverse_iterator rbegin() noexcept
Definition: fixed_ring_buffer.hpp:136
ring_span< value_type >::reverse_iterator reverse_iterator
Definition: fixed_ring_buffer.hpp:55
void clear()
Definition: fixed_ring_buffer.hpp:82
bool empty() const
Definition: fixed_ring_buffer.hpp:237
const_iterator cend() const noexcept
Returns a read-only te iterator that points to the last element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:191
size_type capacity() const
Definition: fixed_ring_buffer.hpp:253
const_iterator begin() const noexcept
Returns a read-only iterator that points to the first element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:104
reference front()
Returns a read/write reference to the data at the first element of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:322
size_type max_size() const
Definition: fixed_ring_buffer.hpp:229
iterator begin() noexcept
Returns a read/write iterator that points to the first element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:93
const_reference operator[](std::size_t i) const
Subscript access to the data contained in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:283
void emplace_back(Args... arg)
Inserts an object at the end of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:375
void pop_front()
Removes the first element of the ring.
Definition: fixed_ring_buffer.hpp:400
const container_type & buffer() const
Returns a const reference to the internal buffer.
Definition: fixed_ring_buffer.hpp:421
reverse_iterator rend() noexcept
Returns a read/write iterator that points to the last element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:158
iterator end() noexcept
Returns a read/write iterator that points to the last element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:115
reference operator[](std::size_t i)
Subscript access to the data contained in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:268
void emplace_front(Args... arg)
Inserts an object at the front of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:362
const_reverse_iterator rend() const noexcept
Definition: fixed_ring_buffer.hpp:169
reference at(std::size_t i)
Provides access to the data contained in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:298
std::ptrdiff_t difference_type
Definition: fixed_ring_buffer.hpp:58
ring_span< value_type >::size_type size_type
Definition: fixed_ring_buffer.hpp:57
ring_span< T >::pointer pointer
Definition: fixed_ring_buffer.hpp:49
void push_back(const value_type &item)
Add data to the end of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:387
const_pointer * data() const
Returns a const pointer to the underlying data of the internal buffer.
Definition: fixed_ring_buffer.hpp:429
ring_span< value_type >::const_reverse_iterator const_reverse_iterator
Definition: fixed_ring_buffer.hpp:56
const_reference back() const
Returns a read-only reference to the data at the last element of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:349
void pop_back()
Removes the last element of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:413
const_reverse_iterator crend() const noexcept
Returns a read-only iterator that points to the last element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:213
ring_span< value_type >::iterator iterator
Definition: fixed_ring_buffer.hpp:53
ring_span< T >::value_type value_type
Definition: fixed_ring_buffer.hpp:48
reference back()
Returns a read/write reference to the data at the last element of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:340
const_reference front() const
Returns a read-only reference to the data at the first element of the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:331
ring_span< value_type >::reference reference
Definition: fixed_ring_buffer.hpp:51
ring_span< value_type >::const_iterator const_iterator
Definition: fixed_ring_buffer.hpp:54
std::array< T, MaxSize > container_type
Definition: fixed_ring_buffer.hpp:59
Definition: amplifier.hpp:29
const_iterator end() const noexcept
Returns a read-only iterator that points to the last element in the fixed_ring_buffer.
Definition: fixed_ring_buffer.hpp:126
std::add_const_t< pointer > const_pointer
Definition: fixed_ring_buffer.hpp:50