mn_ringbuffer.hpp
Go to the documentation of this file.
1 
20 #ifndef MINLIB_ESP32_RINGBUFFER_
21 #define MINLIB_ESP32_RINGBUFFER_
22 
23 #include "mn_config.hpp"
24 
25 #include <freertos/FreeRTOS.h>
26 #include <freertos/task.h>
27 
28 #include "mn_autolock.hpp"
29 #include "mn_allocator.hpp"
30 
31 
32 namespace mn {
33 
34  namespace container {
35 
43  template<typename T, typename E = typename T::value_type>
45  {
46  public:
47  using value_type = T;
48  using size_type = size_t;
50  using pointer = T*;
51  using const_pointer = const T*;
52  using reference = T&;
53  using const_reference = const T&;
55 
56  using ringbuffer_iterator = typename T::iterator;
57  using const_ringbuffer_iterator = typename T::const_iterator;
58 
59  basic_ring_buffer_iterator(pointer b, size_t start_pos)
60  : m_buf(b), m_pos(start_pos) { }
62  : m_buf(it.m_buf), m_pos(it.m_pos) { }
63 
65  m_buf = it.m_buf; m_pos = it.m_pos; return *this;
66  }
67 
68  E &operator*() {
69  return (*m_buf)[m_pos];
70  }
71  E *operator->() {
72  return &(operator*());
73  }
75  ++m_pos;
76  return *this;
77  }
79  --m_pos;
80  return *this;
81  }
83  self_type tmp(*this);
84  ++(*this);
85  return tmp;
86  }
88  self_type tmp(*this);
89  --(*this);
90  return tmp;
91  }
93  self_type tmp(*this);
94  tmp.m_pos += n;
95  return tmp;
96  }
98  self_type tmp(*this);
99  tmp.m_pos -= n;
100  return tmp;
101  }
103  m_pos += n;
104  return *this;
105  }
107  m_pos -= n;
108  return *this;
109  }
110  bool operator == (const self_type &other) const {
111  return (this->m_pos == other.m_pos);
112  }
113  bool operator != (const self_type &other) const {
114  return (this->m_pos != other.m_pos);
115  }
117  return *(*this + n);
118  }
119  bool operator < (const self_type& it) const {
120  return m_pos < it.m_pos;
121  }
122  bool operator > (const self_type& it) const {
123  return it < *this;
124  }
125  bool operator <= (const self_type& it) const {
126  return !(it < *this);
127  }
128  bool operator >= (const self_type& it) const {
129  return !(*this < it);
130  }
131  private:
133  size_t m_pos;
134  };
135 
148  template <class T, size_t TCAPACITY = 100, typename TLOCK = LockType_t >
150  public:
151  using value_type = T;
152  using pointer = T*;
153  using const_pointer = const T*;
154  using reference = T&;
155  using const_reference = const T&;
156  using size_type = size_t;
158  using lock_type = TLOCK;
161 
164 
166 
170  void clear() {
172  m_Head = m_Tail = m_ContentsSize = 0;
173  memset(m_Array, 0, TCAPACITY);
174  }
178  void reset() {
180  m_Head = m_Tail = 0;
181  }
182 
187  void push_back(const value_type &value) {
189 
190  inc_tail();
191  if (m_ContentsSize == TCAPACITY)
192  inc_head();
193 
194  m_Array[m_Tail] = value;
195  }
204 
205  inc_head();
206  return m_Array[m_Head];
207  }
216 
217  inc_head();
218  return m_Array[m_Head];
219  }
220 
223  return iterator(this, TCAPACITY == 0 ? 0 : m_Array[m_Head] );
224  }
227  return iterator(this, TCAPACITY);
228  }
231  return const_iterator(this, 0);
232  }
235  return const_iterator(this, TCAPACITY);
236  }
239  return m_Array[m_Head];
240  }
243  return m_Array[m_Head];
244  }
247  return m_Array[m_Tail];
248  }
251  return m_Array[m_Tail];
252  }
253 
259  size_type size() const {
261  return TCAPACITY;
262  }
268  size_type capacity() const {
270  return m_ContentsSize;
271  }
278  bool empty() const {
280  return TCAPACITY == 0;
281  }
288  bool full() const {
290  return TCAPACITY == m_ContentsSize;
291  }
292  size_type max() const {
294  return size_type(-1) / sizeof(value_type);
295  }
296 
301  size_t get_head() {
303  return m_Head;
304  }
309  size_t get_tail() {
311  return m_Tail;
312  }
313 
314 
315  private:
316  void inc_tail() {
317  ++m_Tail;
318  ++m_ContentsSize;
319 
320  if (m_Tail == TCAPACITY) m_Tail = 0;
321  }
322  void inc_head() {
323  ++m_Head;
324  --m_ContentsSize;
325 
326  if (m_Head == TCAPACITY) m_Head = 0;
327  }
328  private:
329  T m_Array[TCAPACITY];
330  size_t m_Head;
331  size_t m_Tail;
334  };
335 
336  template <class T, size_t TCAPACITY = 100, typename TLOCK = LockType_t >
338 
339 #ifdef __EXPERT
340  template<class TRingBuffer, class TARRAY >
341  inline int write(TRingBuffer& rng, TARRAY& _array) {
342  using size_type = typename TRingBuffer::size_type;
343  size_type t = 0;
344 
345  for(; t < _array.size(); t++) {
346  rng.push_back(_array[t]);
347  }
348 
349  return t;
350  }
351 
352  template<class TRingBuffer,
353  typename value_type = typename TRingBuffer::value_type,
354  typename size_type = typename TRingBuffer::size_type>
355  inline int write(TRingBuffer& rng, const value_type* tarray, const size_type size ) {
356  size_type t = 0;
357 
358  for(; t < size; t++) {
359  rng.push_back(_array[t]);
360  }
361  return t;
362  }
363 #endif
364  }
365 
366 }
367 
368 #endif // MINLIB_ESP32_RINGBUFFER_
Definition: mn_lock.hpp:122
ring_buffer_iterator
Definition: mn_ringbuffer.hpp:45
value_type & operator[](difference_type n) const
Definition: mn_ringbuffer.hpp:116
typename T::const_iterator const_ringbuffer_iterator
Definition: mn_ringbuffer.hpp:57
bool operator>=(const self_type &it) const
Definition: mn_ringbuffer.hpp:128
self_type & operator+=(difference_type n)
Definition: mn_ringbuffer.hpp:102
bool operator<(const self_type &it) const
Definition: mn_ringbuffer.hpp:119
E & operator*()
Definition: mn_ringbuffer.hpp:68
value_type * m_buf
Definition: mn_ringbuffer.hpp:132
bool operator!=(const self_type &other) const
Definition: mn_ringbuffer.hpp:113
bool operator>(const self_type &it) const
Definition: mn_ringbuffer.hpp:122
const T & const_reference
Definition: mn_ringbuffer.hpp:53
size_t m_pos
Definition: mn_ringbuffer.hpp:133
bool operator<=(const self_type &it) const
Definition: mn_ringbuffer.hpp:125
self_type operator-(difference_type n)
Definition: mn_ringbuffer.hpp:97
T * pointer
Definition: mn_ringbuffer.hpp:50
self_type & operator++()
Definition: mn_ringbuffer.hpp:74
T value_type
Definition: mn_ringbuffer.hpp:47
T & reference
Definition: mn_ringbuffer.hpp:52
basic_ring_buffer_iterator(const basic_ring_buffer_iterator &it)
Definition: mn_ringbuffer.hpp:61
E * operator->()
Definition: mn_ringbuffer.hpp:71
const T * const_pointer
Definition: mn_ringbuffer.hpp:51
size_t size_type
Definition: mn_ringbuffer.hpp:48
bool operator==(const self_type &other) const
Definition: mn_ringbuffer.hpp:110
ptrdiff_t difference_type
Definition: mn_ringbuffer.hpp:49
basic_ring_buffer_iterator(pointer b, size_t start_pos)
Definition: mn_ringbuffer.hpp:59
basic_ring_buffer_iterator & operator=(const basic_ring_buffer_iterator &it)
Definition: mn_ringbuffer.hpp:64
self_type operator+(difference_type n)
Definition: mn_ringbuffer.hpp:92
self_type & operator-=(difference_type n)
Definition: mn_ringbuffer.hpp:106
self_type & operator--()
Definition: mn_ringbuffer.hpp:78
typename T::iterator ringbuffer_iterator
Definition: mn_ringbuffer.hpp:56
A basic ring buffer with iterator.
Definition: mn_ringbuffer.hpp:149
T value_type
Definition: mn_ringbuffer.hpp:151
size_type size() const
Get the size of the ringbuffer.
Definition: mn_ringbuffer.hpp:259
ptrdiff_t difference_type
Definition: mn_ringbuffer.hpp:157
reference pop_front()
pop (read) the element from the front and remove it from buffer
Definition: mn_ringbuffer.hpp:202
size_type capacity() const
Get the number of stored elements in the buffer.
Definition: mn_ringbuffer.hpp:268
T m_Array[TCAPACITY]
Definition: mn_ringbuffer.hpp:329
T * pointer
Definition: mn_ringbuffer.hpp:152
size_type max() const
Definition: mn_ringbuffer.hpp:292
void inc_head()
Definition: mn_ringbuffer.hpp:322
const_reference pop_front() const
pop (read) the element from the front and remove it from buffer
Definition: mn_ringbuffer.hpp:214
basic_ring_buffer_iterator< self_type, value_type > iterator
Definition: mn_ringbuffer.hpp:162
const_reference crfront() const
Definition: mn_ringbuffer.hpp:241
reference rfront()
Definition: mn_ringbuffer.hpp:237
void clear()
Clear the ringbuffer and set read/write positoin to 0.
Definition: mn_ringbuffer.hpp:170
reference rback()
Definition: mn_ringbuffer.hpp:249
void inc_tail()
Definition: mn_ringbuffer.hpp:316
void reset()
Set read/write positoin to 0, for clear use function clear()
Definition: mn_ringbuffer.hpp:178
const T & const_reference
Definition: mn_ringbuffer.hpp:155
iterator begin()
Definition: mn_ringbuffer.hpp:221
bool empty() const
Is the buffer empty?
Definition: mn_ringbuffer.hpp:278
const_iterator cend()
Definition: mn_ringbuffer.hpp:233
size_t get_head()
Definition: mn_ringbuffer.hpp:301
size_t get_tail()
Definition: mn_ringbuffer.hpp:309
basic_ring_buffer()
Definition: mn_ringbuffer.hpp:165
size_t m_Tail
Definition: mn_ringbuffer.hpp:331
size_t m_Head
Definition: mn_ringbuffer.hpp:330
TLOCK lock_type
Definition: mn_ringbuffer.hpp:158
T & reference
Definition: mn_ringbuffer.hpp:154
iterator end()
Definition: mn_ringbuffer.hpp:225
const T * const_pointer
Definition: mn_ringbuffer.hpp:153
size_t size_type
Definition: mn_ringbuffer.hpp:156
lock_type m_lockObject
Definition: mn_ringbuffer.hpp:333
basic_ring_buffer_iterator< self_type, const value_type > const_iterator
Definition: mn_ringbuffer.hpp:163
bool full() const
Is the buffer full?
Definition: mn_ringbuffer.hpp:288
size_t m_ContentsSize
Definition: mn_ringbuffer.hpp:332
const_reference crback() const
Definition: mn_ringbuffer.hpp:245
void push_back(const value_type &value)
push a value to the end of the buffer
Definition: mn_ringbuffer.hpp:187
const_iterator cbegin()
Definition: mn_ringbuffer.hpp:229
This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
int lock(TLOCK &m1, unsigned int timeout)
Definition: mn_autolock.hpp:70
long ptrdiff_t
Definition: mn_def.hpp:49
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48