26 #ifndef CIRCULAR_BUFFER_HPP__ 27 #define CIRCULAR_BUFFER_HPP__ 70 template <
typename TYPE>
86 bool push_back(
const TYPE data,
bool forceWrite =
false);
94 uint32_t
size(
void)
const {
return mCount; }
97 uint32_t
capacity(
void)
const {
return mCapacity; }
100 void clear(
void) { mCount = mWriteIndex = mReadIndex = 0; }
110 TYPE&
operator [] (uint32_t index)
const {
return mpArray[ (index + mReadIndex) % mCapacity]; }
131 self_type
operator++() { self_type i = *
this; mIndex++;
return i; }
134 self_type
operator++(
int unused) { mIndex++; self_type i = *
this;
return i; }
137 reference
operator*() {
return mpCb->operator [] (mIndex); }
140 pointer
operator->() {
return &( mpCb->operator [] (mIndex)); }
143 bool operator!=(
const self_type& rhs) {
return (mpCb != rhs.mpCb) ?
false : (mIndex < mpCb->size()); }
146 bool operator==(
const self_type& rhs) {
return (mpCb != rhs.mpCb) ?
false : (mIndex >= mpCb->size()); }
166 self_type
operator++() { self_type i = *
this; mIndex++;
return i; }
169 self_type
operator++(
int unused) { mIndex++; self_type i = *
this;
return i; }
172 reference
operator*() {
return mpCb->operator [] (mIndex); }
175 pointer
operator->() {
return &( mpCb->operator [] (mIndex)); }
178 bool operator!=(
const self_type& rhs) {
return (mpCb != rhs.mpCb) ?
false : (mIndex < mpCb->size()); }
181 bool operator==(
const self_type& rhs) {
return (mpCb != rhs.mpCb) ?
false : (mIndex >= mpCb->size()); }
203 CircularBuffer() : mCapacity(0), mWriteIndex(0), mReadIndex(0), mCount(0), mpArray(0) { }
205 const uint32_t mCapacity;
206 uint32_t mWriteIndex;
222 mpArray =
new TYPE[mCapacity];
241 template <
typename TYPE>
246 template <
typename TYPE>
252 template <
typename TYPE>
260 for(
unsigned int i = 0; i < copy.
size(); i++)
267 template <
typename TYPE>
273 template <
typename TYPE>
276 bool success =
false;
278 if (mCount < mCapacity || forceWrite)
284 if (++mCount > mCapacity) {
289 mpArray[mWriteIndex] = data;
290 if (++mWriteIndex >= mCapacity) {
298 template <
typename TYPE>
305 data = mpArray[mReadIndex];
306 if (++mReadIndex >= mCapacity) {
314 template <
typename TYPE>
317 bool success = (mCount > 0);
322 template <
typename TYPE>
327 data = mpArray[mReadIndex];
332 template <
typename TYPE>
335 bool success = (mCount > 0);
344 static inline void test_CircularBuffer(
void)
349 assert(0 == b.
size());
355 assert(3 == b.
size());
362 assert(3 == b.
size());
388 assert(*cb == 1); cb++;
389 assert(*cb == 2); cb++;
390 assert(*cb == 3); cb++;
395 assert(*(cb.operator->()) == 1); ++cb;
396 assert(*(cb.operator->()) == 2); ++cb;
397 assert(*(cb.operator->()) == 3); ++cb;
411 assert(3 == b2.
size());
423 assert(2 == b2.
size());
425 assert(1 == *cb); cb++;
426 assert(2 == *cb); cb++;
427 assert(1 != *cb); cb++;
431 assert(1 == *cb); cb++;
432 assert(2 == *cb); cb++;
433 assert(3 == *cb); cb++;
437 assert(1 == *cb); cb++;
438 assert(2 == *cb); cb++;
439 assert(3 == *cb); cb++;
443 assert(3 == b2.
size());
445 assert(2 == *cb); cb++;
446 assert(3 == *cb); cb++;
447 assert(4 == *cb); cb++;
456 assert(3 == b.
size());
459 printf(
"\nContents using index operator: ");
461 for (uint32_t i = 0; i < b.
size(); i++) {
467 printf(
"\nContents using iterator: ");
479 puts(
"\nCircular Buffer Tests Successful!");
const_iterator self_type
Definition: circular_buffer.hpp:156
printf("Recorder started: %s\n", uiTraceStart()?"OK":"ERROR")
self_type operator++()
Preincrement operator ie: ++iterator.
Definition: circular_buffer.hpp:131
TYPE & reference
Definition: circular_buffer.hpp:123
int size_type
Definition: circular_buffer.hpp:116
self_type operator++(int unused)
Postincrement operator ie: iterator++.
Definition: circular_buffer.hpp:134
reference operator*()
Definition: circular_buffer.hpp:137
bool operator!=(const self_type &rhs)
!= operator between iterators
Definition: circular_buffer.hpp:178
bool operator==(const self_type &rhs)
== operator between iterators
Definition: circular_buffer.hpp:181
int difference_type
Definition: circular_buffer.hpp:126
TYPE value_type
Definition: circular_buffer.hpp:122
uint32_t capacity(void) const
Definition: circular_buffer.hpp:97
iterator end()
Definition: circular_buffer.hpp:194
bool push_back(const TYPE data, bool forceWrite=false)
Definition: circular_buffer.hpp:274
TYPE * pointer
Definition: circular_buffer.hpp:124
TYPE value_type
Definition: circular_buffer.hpp:157
Definition: circular_buffer.hpp:153
pointer operator->()
pointer operator to get the address ie: *(iterator.operator->())
Definition: circular_buffer.hpp:175
TYPE & reference
Definition: circular_buffer.hpp:158
~CircularBuffer()
Destructor of the buffer.
Definition: circular_buffer.hpp:268
self_type operator++(int unused)
Postincrement operator ie: iterator++.
Definition: circular_buffer.hpp:169
bool operator==(const self_type &rhs)
== operator between iterators
Definition: circular_buffer.hpp:146
Definition: circular_buffer.hpp:71
self_type operator++()
Preincrement operator ie: ++iterator.
Definition: circular_buffer.hpp:166
TYPE pop_front(void)
Definition: circular_buffer.hpp:299
iterator(CircularBuffer< TYPE > *p)
Definition: circular_buffer.hpp:128
bool operator!=(const self_type &rhs)
!= operator between iterators
Definition: circular_buffer.hpp:143
TYPE peek_front(void)
Definition: circular_buffer.hpp:323
uint32_t size(void) const
Definition: circular_buffer.hpp:94
iterator begin()
Definition: circular_buffer.hpp:193
const_iterator(CircularBuffer< TYPE > *p)
Definition: circular_buffer.hpp:163
TYPE & operator[](uint32_t index) const
Definition: circular_buffer.hpp:110
uint32_t b2
Definition: bit_manip.h:54
std::forward_iterator_tag iterator_category
Definition: circular_buffer.hpp:160
const_iterator begin() const
Definition: circular_buffer.hpp:195
reference operator*()
Definition: circular_buffer.hpp:172
void operator+=(TYPE item)
+= Operator which is same as push_back() of an item
Definition: circular_buffer.hpp:103
const_iterator end() const
Definition: circular_buffer.hpp:196
void clear(void)
Clears the contents of the buffer.
Definition: circular_buffer.hpp:100
std::forward_iterator_tag iterator_category
Definition: circular_buffer.hpp:125
int difference_type
Definition: circular_buffer.hpp:161
iterator self_type
Definition: circular_buffer.hpp:121
pointer operator->()
pointer operator to get the address ie: *(iterator.operator->())
Definition: circular_buffer.hpp:140
Definition: circular_buffer.hpp:118
TYPE * pointer
Definition: circular_buffer.hpp:159
CircularBuffer & operator=(const CircularBuffer ©)
= Operator to copy the buffer
Definition: circular_buffer.hpp:253