Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
CircularDifferentialBuffer< T > Class Template Reference

Differential Buffer implementation. More...

#include <cs_CircularDifferentialBuffer.h>

Public Member Functions

 CircularDifferentialBuffer (uint16_t capacity=32, bool initialize=false)
 Default constructor @capacity the size with which the buffer should be initialized. More...
 
virtual ~CircularDifferentialBuffer ()
 Default destructor. More...
 
bool init ()
 Initializes and allocates memory for the buffer based on the capacity used with the constructor. More...
 
void clear ()
 Clears the buffer. More...
 
uint16_t size () const
 Returns the number of elements stored. More...
 
uint16_t capacity () const
 Returns the capacity of the buffer. More...
 
bool empty () const
 Checks if the buffer is empty. More...
 
bool full () const
 Checks if the buffer is full. More...
 
peek () const
 Peek at the oldest element without removing it. More...
 
bool getFirstElement (T &val)
 Get the first element of the buffer @val the value of the oldest element. More...
 
bool getNextElement (T &val)
 Get the next value of the buffer, after calling <CircularDifferentialBuffer>>getFirstElement()> More...
 
bool getLastElement (T &val)
 Get the first element of the buffer @val the value of the oldest element. More...
 
uint16_t getSerializedLength () const
 Get the serialized length. More...
 
void serialize (uint8_t *buffer)
 Write all elements to a buffer @buffer buffer to which to write the elements. More...
 
pop ()
 Get and remove the oldest element. More...
 
bool push (T value)
 Add an element to the end of the buffer @value the element to be added. More...
 

Private Member Functions

void incTail ()
 Increases the tail. More...
 
void incHead ()
 Increases the head. More...
 

Private Attributes

int8_t * _array
 Pointer to the array storing the difference of elements compared to the previous element. More...
 
uint16_t _capacity
 The capacity of the buffer (maximum number of elements) More...
 
uint16_t _head
 Index of the head (next element to be removed) More...
 
uint16_t _tail
 Index of the tail (where the next element will be inserted) More...
 
uint16_t _contentSize
 Number of elements stored in the buffer. More...
 
_firstVal
 Value of the oldest element in the buffer. More...
 
_lastVal
 Value of the newest element in the buffer. More...
 
uint16_t _readIdx
 Index of the last read value, used in getNextElement() More...
 

Detailed Description

template<typename T>
class CircularDifferentialBuffer< T >

Differential Buffer implementation.

Parameters
Tprimitive type such as uint8_t

Elements are added at the back and removed from the front. If the capacity of the buffer is reached, the oldest element will be overwritten.

Constructor & Destructor Documentation

◆ CircularDifferentialBuffer()

template<typename T >
CircularDifferentialBuffer< T >::CircularDifferentialBuffer ( uint16_t  capacity = 32,
bool  initialize = false 
)
inline

Default constructor @capacity the size with which the buffer should be initialized.

A maximum of capacity elements can be stored in the buffer before the oldest element will be overwritten @initialize if true, the array will be directly initialized, otherwise <CircularDifferentialBuffer::init()> has to be called before accessing the buffer

If <initialize> is set to true, the buffer will be directly initialized. To avoid unnecessary allocation of memory, use initialize = false and call <CircularDifferentialBuffer::init()> only once the buffer is used.

◆ ~CircularDifferentialBuffer()

template<typename T >
virtual CircularDifferentialBuffer< T >::~CircularDifferentialBuffer ( )
inlinevirtual

Default destructor.

Free memory allocated for the buffer

Member Function Documentation

◆ capacity()

template<typename T >
uint16_t CircularDifferentialBuffer< T >::capacity ( ) const
inline

Returns the capacity of the buffer.

The capacity is the maximum number of elements that can be stored in the buffer

Returns
the capacity of the buffer

◆ clear()

template<typename T >
void CircularDifferentialBuffer< T >::clear ( )
inline

Clears the buffer.

The buffer is cleared by setting head and tail to the beginning of the buffer. The array itself doesn't have to be cleared

◆ empty()

template<typename T >
bool CircularDifferentialBuffer< T >::empty ( ) const
inline

Checks if the buffer is empty.

Returns
true if empty, false otherwise

◆ full()

template<typename T >
bool CircularDifferentialBuffer< T >::full ( ) const
inline

Checks if the buffer is full.

Returns
true if full, false otherwise

◆ getFirstElement()

template<typename T >
bool CircularDifferentialBuffer< T >::getFirstElement ( T &  val)
inline

Get the first element of the buffer @val the value of the oldest element.

Returns
false when the buffer was empty

◆ getLastElement()

template<typename T >
bool CircularDifferentialBuffer< T >::getLastElement ( T &  val)
inline

Get the first element of the buffer @val the value of the oldest element.

Returns
false when the buffer was empty

◆ getNextElement()

template<typename T >
bool CircularDifferentialBuffer< T >::getNextElement ( T &  val)
inline

Get the next value of the buffer, after calling <CircularDifferentialBuffer>>getFirstElement()>

@val the previous value, will be set to the value of the next element

Returns
false when the last element has been reached (this read is invalid).

◆ getSerializedLength()

template<typename T >
uint16_t CircularDifferentialBuffer< T >::getSerializedLength ( ) const
inline

Get the serialized length.

Returns
the size to fit all elements in a uint8_t buffer

◆ incHead()

template<typename T >
void CircularDifferentialBuffer< T >::incHead ( )
inlineprivate

Increases the head.

Decreases the contentsSize and increases the index of the head. It also wraps around the head if the end of the array is reached.

◆ incTail()

template<typename T >
void CircularDifferentialBuffer< T >::incTail ( )
inlineprivate

Increases the tail.

Increases the contentsSize and the index of the tail. It also wraps the tail around if the end of the array is reached.

◆ init()

template<typename T >
bool CircularDifferentialBuffer< T >::init ( )
inline

Initializes and allocates memory for the buffer based on the capacity used with the constructor.

If the constructor was called with initialize = false, then this function has to be called before the buffer can be accessed!

Returns
true if memory allocation was successful, false otherwise

◆ peek()

template<typename T >
T CircularDifferentialBuffer< T >::peek ( ) const
inline

Peek at the oldest element without removing it.

This returns the value of the oldest element without removing the element from the buffer. Use <CircularBuffer>>pop()> to get the value and remove it at the same time

Returns
the value of the oldest element

◆ pop()

template<typename T >
T CircularDifferentialBuffer< T >::pop ( )
inline

Get and remove the oldest element.

This returns the value of the oldest element and removes it from the buffer

Returns
the value of the oldest element

◆ push()

template<typename T >
bool CircularDifferentialBuffer< T >::push ( value)
inline

Add an element to the end of the buffer @value the element to be added.

Elements are added at the end of the buffer and removed from the beginning. If the buffer is full the oldest element will be overwritten.

Returns
false when the difference was too large, and thus when the buffer was cleared

◆ serialize()

template<typename T >
void CircularDifferentialBuffer< T >::serialize ( uint8_t *  buffer)

Write all elements to a buffer @buffer buffer to which to write the elements.

The buffer has to be of the correct size, use <CircularDifferentialBuffer>>getSerializedLength()> First sizeof(T) bytes is the first value, the bytes afterwards are increments (int8_t).

◆ size()

template<typename T >
uint16_t CircularDifferentialBuffer< T >::size ( ) const
inline

Returns the number of elements stored.

Returns
the number of elements stored in the buffer

Member Data Documentation

◆ _array

template<typename T >
int8_t* CircularDifferentialBuffer< T >::_array
private

Pointer to the array storing the difference of elements compared to the previous element.

◆ _capacity

template<typename T >
uint16_t CircularDifferentialBuffer< T >::_capacity
private

The capacity of the buffer (maximum number of elements)

◆ _contentSize

template<typename T >
uint16_t CircularDifferentialBuffer< T >::_contentSize
private

Number of elements stored in the buffer.

◆ _firstVal

template<typename T >
T CircularDifferentialBuffer< T >::_firstVal
private

Value of the oldest element in the buffer.

◆ _head

template<typename T >
uint16_t CircularDifferentialBuffer< T >::_head
private

Index of the head (next element to be removed)

◆ _lastVal

template<typename T >
T CircularDifferentialBuffer< T >::_lastVal
private

Value of the newest element in the buffer.

◆ _readIdx

template<typename T >
uint16_t CircularDifferentialBuffer< T >::_readIdx
private

Index of the last read value, used in getNextElement()

◆ _tail

template<typename T >
uint16_t CircularDifferentialBuffer< T >::_tail
private

Index of the tail (where the next element will be inserted)


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