AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Public Member Functions | List of all members
RingBufferT< T, N, SIZE > Class Template Reference

a template-based ring buffer class that can store different kinds of objects in buffers of whatever size is needed. More...

#include <RingBufferT.h>

Public Member Functions

 RingBufferT ()
 Construct a ring buffer to store elements of type T indexed by integer type N, with size SIZE. All of these are passed as template parameters. *.
 
pull ()
 Extract the next (first) element from the ring buffer. More...
 
peek (N index=0)
 Examine an element in the ring buffer. More...
 
bool push (T element)
 Push an element into the ring buffer. The element is appended to the back of the buffer. More...
 
bool isEmpty ()
 Determine if the buffer is empty . More...
 
bool isNotEmpty ()
 Determine if the buffer is not empty. More...
 
bool isFull ()
 Determine if the buffer is full and cannot accept more bytes. More...
 
bool isNotFull ()
 Determine if the buffer is not full and can accept more bytes. More...
 
void discardFromFront (N nbrElements)
 discard a number of elements from the front of the ring buffer. More...
 
void clear ()
 Clear the ring buffer, leaving it empty.
 

Detailed Description

template<typename T, typename N, unsigned int SIZE>
class RingBufferT< T, N, SIZE >

a template-based ring buffer class that can store different kinds of objects in buffers of whatever size is needed.

The implementation of RingBufferT is interrupt safe: the key operations are atomic, allowing for RingBuffer objects to be shared between interrupt functions and ordinary code.

The template-based RingBufferT class provides a very flexible ring buffer implementation; however different instantiations of RingBufferT (e.g., RingBufferT< char, int, 32 > and RingBufferT< char, int, 16 >) result in replicated code for each instantiation, even when they could logically share code. For a more efficient ring buffer that avoids such code bloat but can only store bytes, use RingBuffer.

Template Parameters
Tis the type of object that will be stored in the RingBufferT instantiation.
Nis the integer type that will be used to index the RingBufferT elements.
SIZEis an integer indicating the size of the RingBufferT instantiation.

Member Function Documentation

§ discardFromFront()

template<typename T , typename N , unsigned int SIZE>
void RingBufferT< T, N, SIZE >::discardFromFront ( nbrElements)
inline

discard a number of elements from the front of the ring buffer.

  • nbrElements the number of elements to discard.

§ isEmpty()

template<typename T , typename N , unsigned int SIZE>
bool RingBufferT< T, N, SIZE >::isEmpty ( )
inline

Determine if the buffer is empty .

Returns
true if the buffer is empty; false if not.

§ isFull()

template<typename T , typename N , unsigned int SIZE>
bool RingBufferT< T, N, SIZE >::isFull ( )
inline

Determine if the buffer is full and cannot accept more bytes.

Returns
true if the buffer is full; false if not.

§ isNotEmpty()

template<typename T , typename N , unsigned int SIZE>
bool RingBufferT< T, N, SIZE >::isNotEmpty ( )
inline

Determine if the buffer is not empty.

Returns
true if the buffer is not empty; false if it is empty.

§ isNotFull()

template<typename T , typename N , unsigned int SIZE>
bool RingBufferT< T, N, SIZE >::isNotFull ( )
inline

Determine if the buffer is not full and can accept more bytes.

Returns
true if the buffer is not full; false if it is full.

§ peek()

template<typename T , typename N , unsigned int SIZE>
T RingBufferT< T, N, SIZE >::peek ( index = 0)
inline

Examine an element in the ring buffer.

  • index the element to examine; 0 means the first (= next) element in the buffer. The default if the argument is omitted is to return the first element.
Note
There is no general purpose safe value to return to indicate an empty element, so before calling peek() be sure the element exists.
Returns
the next element.

§ pull()

template<typename T , typename N , unsigned int SIZE>
T RingBufferT< T, N, SIZE >::pull ( )
inline

Extract the next (first) element from the ring buffer.

Note
There is no general purpose safe value to return to indicate an empty buffer, so before calling pull() be sure to check the ring buffer is not empty.
Returns
the next element.

§ push()

template<typename T , typename N , unsigned int SIZE>
bool RingBufferT< T, N, SIZE >::push ( element)
inline

Push an element into the ring buffer. The element is appended to the back of the buffer.

  • element is the item to append to the ring buffer.
Returns
0 (false) if it succeeds; 1 (true) if it fails because the buffer is full.

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