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

Template Class that holds values with given priorities in a ring buffer. More...

#include <RingBuffer.h>

Collaboration diagram for tPriorityRingBuffer< T >:

Classes

struct  tPriorityRef
 Structure handles meta data for each priority. More...
 
struct  tValueSlot
 Structure that holds the actual value and meta data. More...
 

Public Member Functions

 tPriorityRingBuffer (uint16_t _size, uint8_t _maxPriorities=1)
 Construct a new Priority ring buffer object. More...
 
virtual ~tPriorityRingBuffer ()
 Destroy the priority ring buffer object Frees all memory. More...
 
bool add (const T &val, uint8_t _priority=0)
 Add a value to the ring buffer with a given priority. More...
 
void clean ()
 This function does nothing at the moment! More...
 
void clear ()
 Clears the whole ring buffer. More...
 
uint16_t count ()
 Returns the number of values in the ring buffer. More...
 
T * getAddRef (uint8_t _priority=0)
 Get the pointer to new value added to the ring buffer. More...
 
const T * getReadRef (uint8_t *_priority=0)
 Get pointer to highest priority value read out from the the ring buffer. More...
 
const T * getReadRef (uint8_t _priority)
 Get the pointer to value with given priority read out from the ring buffer. More...
 
uint16_t getSize () const
 Get the size of the ring buffer. More...
 
bool isEmpty (uint8_t _priority=0xff) const
 Check is ring buffer empty. More...
 
bool read (T &val)
 Reads highest priority value out from the ring buffer. More...
 

Static Public Member Functions

static uint32_t getMemSize (uint16_t _size, uint8_t _maxPriorities=1)
 Get the size of memory required by ring buffer. More...
 

Protected Attributes

tValueSlotbuffer
 Pointer to the ring puffer in memory. More...
 
uint16_t head
 Index of the first free index in the ring buffer. More...
 
uint8_t maxPriorities
 highest priority possible More...
 
tPriorityRefpriorityReferencies
 Pointer to priority references in buffer. More...
 
uint16_t size
 Number of values that can be stored in the ring buffer. More...
 
uint16_t tail
 Index of the last occupied index in the ring buffer. More...
 

Detailed Description

template<typename T>
class tPriorityRingBuffer< T >

Template Class that holds values with given priorities in a ring buffer.

tPriorityRingBuffer is similar as tRingBuffer, but it extends functionality with value priority. When you add values to the buffer, you can give them priority. When you read them out you can get highest priority or request specific priority value.

The advantage of tPriorityRingBuffer is that it will reserve one continuous block for all priorities instead of reserve one buffer for each priority.

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

Definition at line 255 of file RingBuffer.h.

Constructor & Destructor Documentation

◆ tPriorityRingBuffer()

template<typename T >
tPriorityRingBuffer< T >::tPriorityRingBuffer ( uint16_t  _size,
uint8_t  _maxPriorities = 1 
)

Construct a new Priority ring buffer object.

Allocates all the memory needed, initialize attributes and priorities.

Parameters
_sizeSize of the ring buffer
_maxPrioritieshighest priority possible

◆ ~tPriorityRingBuffer()

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

Destroy the priority ring buffer object Frees all memory.

Member Function Documentation

◆ add()

template<typename T >
bool tPriorityRingBuffer< T >::add ( const T &  val,
uint8_t  _priority = 0 
)

Add a value to the ring buffer with a given priority.

See also
Parameters
valValue to be added
_priorityPriority for the added value (default = 0)
Return values
trueAdd succeeded
falseBuffer is full

◆ clean()

template<typename T >
void tPriorityRingBuffer< T >::clean ( )

This function does nothing at the moment!

◆ clear()

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

Clears the whole ring buffer.

◆ count()

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

Returns the number of values in the ring buffer.

Currently function returns simple buffer count instead of true value count. Priority ring buffer may have values released between other values.

Return values
uint16_tNumber of values in buffer

◆ getAddRef()

template<typename T >
T * tPriorityRingBuffer< T >::getAddRef ( uint8_t  _priority = 0)

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.
Parameters
_priorityPriority for the value (default = 0)
Return values
T *Pointer to added new value
0Buffer is full

◆ getMemSize()

template<typename T >
static uint32_t tPriorityRingBuffer< T >::getMemSize ( uint16_t  _size,
uint8_t  _maxPriorities = 1 
)
static

Get the size of memory required by ring buffer.

This is static function and can be used to test how much ring buffer would require memory with given size and count of priorities. Function can be used e.g., for checking available memory or possible memory "bank" handling.

Returns
uint32_t Memory required by ring buffer

◆ getReadRef() [1/2]

template<typename T >
const T * tPriorityRingBuffer< T >::getReadRef ( uint8_t *  _priority = 0)

Get pointer to highest priority value read out from the 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.
Parameters
*_priorityPointer to priority, which will be set to priority of the value read out.
Return values
const T*Pointer to value read out from the ring buffer
0No values available.

◆ getReadRef() [2/2]

template<typename T >
const T * tPriorityRingBuffer< T >::getReadRef ( uint8_t  _priority)

Get the pointer to value with given priority 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.
Parameters
_priorityPriority for the value read out
Return values
const T*Pointer to value read out from the ring buffer
0No values available with given priority

◆ getSize()

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

Get the size of the ring buffer.

Return values
uint16_tSize of ring buffer

Definition at line 326 of file RingBuffer.h.

◆ isEmpty()

template<typename T >
bool tPriorityRingBuffer< T >::isEmpty ( uint8_t  _priority = 0xff) const

Check is ring buffer empty.

Result depends of _priority parameter. If valid priority within initial _maxPriorities is provided, function returns empty state of given priority. Otherwise it returns empty state of whole buffer.

Parameters
_priorityWhich buffer
Return values
boolBuffer empty state

◆ read()

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

Reads highest priority value out from the ring buffer.

See also
Parameters
valReference to the value read out from 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 >
tValueSlot* tPriorityRingBuffer< T >::buffer
protected

Pointer to the ring puffer in memory.

Definition at line 294 of file RingBuffer.h.

◆ head

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

Index of the first free index in the ring buffer.

Definition at line 298 of file RingBuffer.h.

◆ maxPriorities

template<typename T >
uint8_t tPriorityRingBuffer< T >::maxPriorities
protected

highest priority possible

Definition at line 304 of file RingBuffer.h.

◆ priorityReferencies

template<typename T >
tPriorityRef* tPriorityRingBuffer< T >::priorityReferencies
protected

Pointer to priority references in buffer.

Definition at line 296 of file RingBuffer.h.

◆ size

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

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

Definition at line 302 of file RingBuffer.h.

◆ tail

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

Index of the last occupied index in the ring buffer.

Definition at line 300 of file RingBuffer.h.


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