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

Circular Buffer implementation. More...

#include <cs_CircularBuffer.h>

Public Member Functions

 CircularBuffer (uint16_t capacity)
 Default constructor. More...
 
virtual ~CircularBuffer ()
 Default destructor. More...
 
uint16_t getMaxByteSize (uint16_t capacity)
 
uint16_t getMaxByteSize ()
 
uint16_t getMaxSize (uint16_t byteSize)
 
bool init ()
 Initializes and allocates memory for the buffer based on the capacity. More...
 
bool deinit ()
 
bool assign (buffer_ptr_t buffer, uint16_t bufferSize)
 Assign the buffer used to store the data, instead of allocating it via init(). More...
 
bool release ()
 Release the buffer that was assigned. More...
 
bool isInitialized ()
 Returns true when the buffer has been allocated, either via init() or via assign(). More...
 
T * getBuffer ()
 
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...
 
void push (const T &value)
 Add an element to the end of the buffer. More...
 
bool pushUnique (const T &value)
 Add an element to the end of the buffer, but only when it's not already in the buffer. More...
 
const T & pop ()
 Get the oldest element. More...
 
T & peek () const
 Peek at the oldest element without removing it. More...
 
T & operator[] (uint16_t idx) const
 Returns the Nth value, starting from oldest element. More...
 
uint16_t find (const T &value) const
 Find a value in the buffer. More...
 

Private Member Functions

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

Private Attributes

T * _array
 Pointer to the array storing the elements. 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 _contentsSize
 Number of elements stored in the buffer. More...
 
bool _allocatedSelf
 Whether the array was allocated by init() or not. More...
 

Detailed Description

template<class T>
class CircularBuffer< T >

Circular Buffer implementation.

Parameters
TElement type of elements within the buffer.

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

◆ CircularBuffer()

template<class T >
CircularBuffer< T >::CircularBuffer ( uint16_t  capacity)
inline

Default constructor.

◆ ~CircularBuffer()

template<class T >
virtual CircularBuffer< T >::~CircularBuffer ( )
inlinevirtual

Default destructor.

Member Function Documentation

◆ assign()

template<class T >
bool CircularBuffer< T >::assign ( buffer_ptr_t  buffer,
uint16_t  bufferSize 
)
inline

Assign the buffer used to store the data, instead of allocating it via init().

@buffer The buffer to be used. @bufferSize Size of the buffer.

Returns
true on success.

◆ capacity()

template<class T >
uint16_t CircularBuffer< 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<class T >
void CircularBuffer< 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

◆ deinit()

template<class T >
bool CircularBuffer< T >::deinit ( )
inline

◆ empty()

template<class T >
bool CircularBuffer< T >::empty ( ) const
inline

Checks if the buffer is empty.

Returns
true if empty, false otherwise

◆ find()

template<class T >
uint16_t CircularBuffer< T >::find ( const T &  value) const
inline

Find a value in the buffer.

Returns
The first index at which the value was found, or CS_CIRCULAR_BUFFER_INDEX_NOT_FOUND when not found.

◆ full()

template<class T >
bool CircularBuffer< T >::full ( ) const
inline

Checks if the buffer is full.

Returns
true if full, false otherwise

◆ getBuffer()

template<class T >
T * CircularBuffer< T >::getBuffer ( )
inline

◆ getMaxByteSize() [1/2]

template<class T >
uint16_t CircularBuffer< T >::getMaxByteSize ( )
inline

◆ getMaxByteSize() [2/2]

template<class T >
uint16_t CircularBuffer< T >::getMaxByteSize ( uint16_t  capacity)
inline

◆ getMaxSize()

template<class T >
uint16_t CircularBuffer< T >::getMaxSize ( uint16_t  byteSize)
inline

◆ incHead()

template<class T >
void CircularBuffer< 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<class T >
void CircularBuffer< 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<class T >
bool CircularBuffer< T >::init ( )
inline

Initializes and allocates memory for the buffer based on the capacity.

@capacity the number of elements that should be stored in this buffer, before overwriting the oldest element.

Returns
true if memory allocation was successful, false otherwise

◆ isInitialized()

template<class T >
bool CircularBuffer< T >::isInitialized ( )
inline

Returns true when the buffer has been allocated, either via init() or via assign().

◆ operator[]()

template<class T >
T & CircularBuffer< T >::operator[] ( uint16_t  idx) const
inline

Returns the Nth value, starting from oldest element.

Does NOT check if you reached the end, make sure you read no more than size().

◆ peek()

template<class T >
T & CircularBuffer< 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<class T >
const T & CircularBuffer< T >::pop ( )
inline

Get 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<class T >
void CircularBuffer< T >::push ( const T &  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.

◆ pushUnique()

template<class T >
bool CircularBuffer< T >::pushUnique ( const T &  value)
inline

Add an element to the end of the buffer, but only when it's not already in the buffer.

Returns
True when the element was added.

◆ release()

template<class T >
bool CircularBuffer< T >::release ( )
inline

Release the buffer that was assigned.

Returns
true on success.

◆ size()

template<class T >
uint16_t CircularBuffer< T >::size ( ) const
inline

Returns the number of elements stored.

Returns
the number of elements stored in the buffer

Member Data Documentation

◆ _allocatedSelf

template<class T >
bool CircularBuffer< T >::_allocatedSelf
private

Whether the array was allocated by init() or not.

◆ _array

template<class T >
T* CircularBuffer< T >::_array
private

Pointer to the array storing the elements.

◆ _capacity

template<class T >
uint16_t CircularBuffer< T >::_capacity
private

The capacity of the buffer (maximum number of elements)

◆ _contentsSize

template<class T >
uint16_t CircularBuffer< T >::_contentsSize
private

Number of elements stored in the buffer.

◆ _head

template<class T >
uint16_t CircularBuffer< T >::_head
private

Index of the head (next element to be removed)

◆ _tail

template<class T >
uint16_t CircularBuffer< 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: