CircularBuffer< TYPE > Class Template Reference

#include <circular_buffer.hpp>

Data Structures

class  const_iterator
 
class  iterator
 

Public Types

typedef int size_type
 

Public Member Functions

 CircularBuffer (uint32_t capacity)
 Constructor with initial capacity as buffer size. More...
 
 CircularBuffer (const CircularBuffer &copy)
 Copy Constructor. More...
 
CircularBufferoperator= (const CircularBuffer &copy)
 = Operator to copy the buffer More...
 
 ~CircularBuffer ()
 Destructor of the buffer. More...
 
bool push_back (const TYPE data, bool forceWrite=false)
 
TYPE pop_front (void)
 
bool pop_front (TYPE *dataPtr)
 
TYPE peek_front (void)
 
bool peek_front (TYPE *dataPtr)
 
uint32_t size (void) const
 
uint32_t capacity (void) const
 
void clear (void)
 Clears the contents of the buffer. More...
 
void operator+= (TYPE item)
 += Operator which is same as push_back() of an item More...
 
TYPE & operator[] (uint32_t index) const
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 

Friends

class iterator
 Iterator is our friend... More...
 

Detailed Description

template<typename TYPE>
class CircularBuffer< TYPE >

Circular buffer class

Usage:

b.push_back(1);
b.push_back(2);
b.push_back(3);
b.push_back(0); // Will fail since buffer is full
b.push_back(4, true); // Overwrite oldest data
// Read the elements without popping the data, access them using index operator
// Should be "2 3 4" since we previously over-wrote "1" with "4"
printf("\nContents using index operator: ");
for (uint32_t i = 0; i < b.size(); i++) {
printf("%i ", b[i]);
}
// Use the iterator to read data without popping the data off the buffer
// Should be "2 3 4"; same as using the index operator.
printf("\nContents using iterator: ");
for(CircularBuffer<int>::iterator cb = b.begin(); cb != b.end(); ++cb)
{
printf("%i ", *(cb));
}
// Pop the data from the buffer, should pop "2 3 4"
int i = 0;
while (b.pop_front(&i)) {
printf("\nPopped %i", i);
}

Member Typedef Documentation

template<typename TYPE>
typedef int CircularBuffer< TYPE >::size_type

Constructor & Destructor Documentation

template<typename TYPE >
CircularBuffer< TYPE >::CircularBuffer ( uint32_t  capacity)

Constructor with initial capacity as buffer size.

template<typename TYPE >
CircularBuffer< TYPE >::CircularBuffer ( const CircularBuffer< TYPE > &  copy)

Copy Constructor.

template<typename TYPE >
CircularBuffer< TYPE >::~CircularBuffer ( )

Destructor of the buffer.

Member Function Documentation

template<typename TYPE>
iterator CircularBuffer< TYPE >::begin ( )
inline

Get iterators

template<typename TYPE>
const_iterator CircularBuffer< TYPE >::begin ( ) const
inline
template<typename TYPE>
uint32_t CircularBuffer< TYPE >::capacity ( void  ) const
inline
Returns
the capacity of the circular buffer
template<typename TYPE>
void CircularBuffer< TYPE >::clear ( void  )
inline

Clears the contents of the buffer.

template<typename TYPE>
iterator CircularBuffer< TYPE >::end ( )
inline
template<typename TYPE>
const_iterator CircularBuffer< TYPE >::end ( ) const
inline
template<typename TYPE>
void CircularBuffer< TYPE >::operator+= ( TYPE  item)
inline

+= Operator which is same as push_back() of an item

template<typename TYPE >
CircularBuffer< TYPE > & CircularBuffer< TYPE >::operator= ( const CircularBuffer< TYPE > &  copy)

= Operator to copy the buffer

template<typename TYPE>
TYPE& CircularBuffer< TYPE >::operator[] ( uint32_t  index) const
inline

Index operator. This will always return in the FIFO order, so index 0 represents the OLDEST data. The max index value should not go beyond [getCapacity() - 1]

template<typename TYPE >
TYPE CircularBuffer< TYPE >::peek_front ( void  )
Returns
the oldest element, but doesn't remove it from the buffer
template<typename TYPE >
bool CircularBuffer< TYPE >::peek_front ( TYPE *  dataPtr)
Returns
true if an element is available, and is read into dataPtr
template<typename TYPE >
TYPE CircularBuffer< TYPE >::pop_front ( void  )
Returns
the oldest element
template<typename TYPE >
bool CircularBuffer< TYPE >::pop_front ( TYPE *  dataPtr)
Returns
true if an element is available to be read, and is read to dataPtr
template<typename TYPE >
bool CircularBuffer< TYPE >::push_back ( const TYPE  data,
bool  forceWrite = false 
)

Write to end of buffer.

Parameters
dataThe data to write.
forceWriteOptional parameter, if true, and the buffer is full, will force a write, discarding oldest data.
Returns
true if successful or false if no capacity

If we don't have the capacity, then perform a read() to pop the oldest element, and then cap the count.

template<typename TYPE>
uint32_t CircularBuffer< TYPE >::size ( void  ) const
inline
Returns
the number of elements in the array

Friends And Related Function Documentation

template<typename TYPE>
friend class iterator
friend

Iterator is our friend...


The documentation for this class was generated from the following file: