NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
tRingBuffer< T > Class Template Reference

Template Class that holds values in a ring buffer. More...

#include <RingBuffer.h>

Public Member Functions

 tRingBuffer (uint16_t _size)
 Construct a new ring buffer object. More...
 
virtual ~tRingBuffer ()
 Destroy the ring buffer object. More...
 
bool add (const T &val)
 Adds a new value to the ring buffer. More...
 
void clear ()
 Clears the whole ring buffer. More...
 
uint16_t count ()
 Returns the number of values in the ring buffer. More...
 
T * getAddRef ()
 Get the pointer to new value added to the ring buffer. More...
 
const T * getReadRef ()
 Get the pointer to value read out from the ring buffer. More...
 
uint16_t getSize () const
 Get the size of the ring buffer. More...
 
bool isEmpty () const
 Checks if the ring buffer is empty. More...
 
T * peek ()
 Get pointer to next value to be read out from the ring buffer. Function does not read value out from the ring buffer. More...
 
bool read (T &val)
 Reads a value out from the ring buffer. More...
 

Protected Attributes

T * buffer
 Pointer to the ring buffer of values in memory. More...
 
uint16_t head
 Index of the first value in the ring buffer. More...
 
uint16_t size
 Number of values that can be stored in the ring buffer. More...
 
uint16_t tail
 Index of the last value in the ring buffer. More...
 

Detailed Description

template<typename T>
class tRingBuffer< T >

Template Class that holds values in a ring buffer.

With tRingBuffer one can save values to a ring buffer. Value can be simple value or data structure.

Note
You have to take care of data locking from other threads or interrupts around calls using buffer handling routines.
See also
Template Parameters
TTemplate used for the class

Definition at line 113 of file RingBuffer.h.

Constructor & Destructor Documentation

◆ tRingBuffer()

template<typename T >
tRingBuffer< T >::tRingBuffer ( uint16_t  _size)

Construct a new ring buffer object.

A buffer with the corresponding size of values will be allocated to the memory.

Parameters
_sizeSize of the ring buffer

◆ ~tRingBuffer()

template<typename T >
virtual tRingBuffer< T >::~tRingBuffer ( )
virtual

Destroy the ring buffer object.

All memory will be freed

Member Function Documentation

◆ add()

template<typename T >
bool tRingBuffer< T >::add ( const T &  val)

Adds a new value to the ring buffer.

See also
Parameters
valValue to be added
Return values
trueAdd succeeded
falseBuffer is full

◆ clear()

template<typename T >
void tRingBuffer< T >::clear ( )

Clears the whole ring buffer.

◆ count()

template<typename T >
uint16_t tRingBuffer< T >::count ( )

Returns the number of values in the ring buffer.

Return values
uint16_tNumber of values in the ring buffer

◆ getAddRef()

template<typename T >
T * tRingBuffer< T >::getAddRef ( )

Get the pointer to new value added to the ring buffer.

Using getAddRef instead of add saves data copying between values. By using add, one must first setup value, which will be then copied to buffer within add. By using getAddRef one can request pointer to next buffer value and copy data directly to it.

Note
In multitasking or with interrupts one must take care that buffer will no be used before data handling has been finished. This differs from add, where it is enough to use locking only around calling add.
Return values
T *Pointer to added new value
0Buffer is full

◆ getReadRef()

template<typename T >
const T * tRingBuffer< T >::getReadRef ( )

Get the pointer to value read out from the ring buffer.

Using getReadRef instead of read saves data copying between values. By using read data will be copied to value, which one then may forward elsewhere. By using getReadRef one can request pointer to value and forward data directly from it.

Note
In multitasking or with interrupts one must take care that buffer will no be used before data handling has been finished. This differs from read, where it is enough to use locking only around calling read.
Return values
const T*Pointer to value read out from the ring buffer
0No values available

◆ getSize()

template<typename T >
uint16_t tRingBuffer< T >::getSize ( ) const
inline

Get the size of the ring buffer.

Return values
uint16_tsize of ring buffer

Definition at line 144 of file RingBuffer.h.

◆ isEmpty()

template<typename T >
bool tRingBuffer< T >::isEmpty ( ) const

Checks if the ring buffer is empty.

Return values
trueBuffer is empty
falseBuffer has data

◆ peek()

template<typename T >
T * tRingBuffer< T >::peek ( )

Get pointer to next value to be read out from the ring buffer. Function does not read value out from the ring buffer.

Return values
const T*Pointer to value, which would be next read out from the ring buffer
0No values available, buffer is empty.

◆ read()

template<typename T >
bool tRingBuffer< T >::read ( T &  val)

Reads a value out from the ring buffer.

See also
Parameters
valReference to the value read out from the ring buffer
Return values
trueValue read out from buffer. Value has been copied to parameter val
falseThere is no data available inside the buffer. Parameter val will be unchanged.

Member Data Documentation

◆ buffer

template<typename T >
T* tRingBuffer< T >::buffer
protected

Pointer to the ring buffer of values in memory.

Definition at line 117 of file RingBuffer.h.

◆ head

template<typename T >
uint16_t tRingBuffer< T >::head
protected

Index of the first value in the ring buffer.

Definition at line 119 of file RingBuffer.h.

◆ size

template<typename T >
uint16_t tRingBuffer< T >::size
protected

Number of values that can be stored in the ring buffer.

Definition at line 123 of file RingBuffer.h.

◆ tail

template<typename T >
uint16_t tRingBuffer< T >::tail
protected

Index of the last value in the ring buffer.

Definition at line 121 of file RingBuffer.h.


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