arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
DynamicMultiBuffer< T, BufferType > Class Template Reference

Auto-expanding buffer composed of multiple buffer instances. More...

#include <DynamicMultiBuffer.h>

Inheritance diagram for DynamicMultiBuffer< T, BufferType >:
BaseBuffer< T >

Public Member Functions

 DynamicMultiBuffer (size_t component_size, size_t initial_components=1, size_t max_components=0)
 Constructor with buffer configuration.
 
virtual ~DynamicMultiBuffer ()
 Destructor - releases all buffer components.
 
T * address () override
 Get pointer to current read position.
 
int available () override
 Get number of elements available to read.
 
int availableForWrite () override
 Get space available for writing.
 
void clear ()
 same as reset
 
virtual int clearArray (int len)
 Removes the next len entries.
 
size_t getComponentCount ()
 Get the number of buffer components.
 
size_t getComponentSize ()
 Get the size of each component.
 
bool isEmpty () override
 Check if the buffer is empty.
 
bool isFull () override
 Check if the buffer is full.
 
virtual float levelPercent ()
 Returns the level of the buffer in %.
 
bool peek (T &result) override
 Peek at the next value without removing it.
 
bool read (T &result) override
 Read a single value from the buffer.
 
int readArray (T data[], int len) override
 Optimized bulk read operation.
 
void reset () override
 Reset the buffer to empty state.
 
bool resize (int new_size) override
 Resize the buffer.
 
size_t size () override
 Get total capacity of the buffer.
 
bool write (T data) override
 Write a value to the buffer.
 
int writeArray (const T data[], int len) override
 Optimized bulk write operation.
 
virtual int writeArrayOverwrite (const T data[], int len)
 Fills the buffer data and overwrites the oldest data if the buffer is full.
 

Protected Member Functions

bool addBufferComponent ()
 Add a new buffer component.
 
bool isCurrentBufferFull ()
 Check if the current write buffer is full.
 
size_t totalCapacity ()
 Calculate total capacity of all buffer components.
 

Protected Attributes

Vector< BufferType< T > * > buffer_components
 
size_t component_size_ = 0
 
size_t max_components_ = 0
 
size_t read_pos = 0
 
size_t write_pos = 0
 

Detailed Description

template<typename T, template< typename > class BufferType>
class audio_tools::DynamicMultiBuffer< T, BufferType >

Auto-expanding buffer composed of multiple buffer instances.

DynamicMultiBuffer manages a collection of buffer components, automatically adding new buffers when existing ones become full. This provides dynamically growing storage capacity while maintaining the performance characteristics of the underlying buffer implementation.

Ideal use cases:

Author
Phil Schatzmann
Template Parameters
TData type to be stored in the buffer
BufferTypeThe buffer implementation to use for each component

Constructor & Destructor Documentation

◆ DynamicMultiBuffer()

template<typename T , template< typename > class BufferType>
DynamicMultiBuffer ( size_t  component_size,
size_t  initial_components = 1,
size_t  max_components = 0 
)
inline

Constructor with buffer configuration.

Parameters
component_sizeSize of each individual buffer component (elements)
initial_componentsNumber of buffer components to pre-allocate
max_componentsMaximum number of components (0 for unlimited)

Member Function Documentation

◆ addBufferComponent()

template<typename T , template< typename > class BufferType>
bool addBufferComponent ( )
inlineprotected

Add a new buffer component.

Creates and adds a new buffer component if allowed by max_components.

Returns
true if component was added, false if max reached or allocation failed

◆ address()

template<typename T , template< typename > class BufferType>
T * address ( )
inlineoverridevirtual

Get pointer to current read position.

Note: This method has limitations since data may span multiple buffer components. It returns the address from the current component.

Returns
Pointer to the current read position in memory

Implements BaseBuffer< T >.

◆ available()

template<typename T , template< typename > class BufferType>
int available ( )
inlineoverridevirtual

Get number of elements available to read.

Returns
Number of elements that can be read from the buffer

Implements BaseBuffer< T >.

◆ availableForWrite()

template<typename T , template< typename > class BufferType>
int availableForWrite ( )
inlineoverridevirtual

Get space available for writing.

Returns remaining space in existing components plus potential new components up to max_components limit.

Returns
Number of elements that can be written

Implements BaseBuffer< T >.

◆ clearArray()

template<typename T >
virtual int clearArray ( int  len)
inlinevirtualinherited

◆ getComponentCount()

template<typename T , template< typename > class BufferType>
size_t getComponentCount ( )
inline

Get the number of buffer components.

Returns
Current number of buffer components

◆ getComponentSize()

template<typename T , template< typename > class BufferType>
size_t getComponentSize ( )
inline

Get the size of each component.

Returns
Size of each buffer component in elements

◆ isCurrentBufferFull()

template<typename T , template< typename > class BufferType>
bool isCurrentBufferFull ( )
inlineprotected

Check if the current write buffer is full.

Returns
true if current buffer is full or no buffers exist

◆ isEmpty()

template<typename T , template< typename > class BufferType>
bool isEmpty ( )
inlineoverride

Check if the buffer is empty.

Returns
true if no elements are available to read

◆ isFull()

template<typename T , template< typename > class BufferType>
bool isFull ( )
inlineoverridevirtual

Check if the buffer is full.

Buffer is considered full only if we've reached max components and the last component is full.

Returns
true if no more elements can be written

Reimplemented from BaseBuffer< T >.

◆ peek()

template<typename T , template< typename > class BufferType>
bool peek ( T &  result)
inlineoverridevirtual

Peek at the next value without removing it.

Returns the next element that would be read without advancing the read position.

Parameters
resultReference where the peeked value will be stored
Returns
true if peek was successful, false if buffer is empty

Implements BaseBuffer< T >.

◆ read()

template<typename T , template< typename > class BufferType>
bool read ( T &  result)
inlineoverridevirtual

Read a single value from the buffer.

Reads from the current read position, potentially spanning buffer components.

Parameters
resultReference where the read value will be stored
Returns
true if read was successful, false if buffer is empty

Implements BaseBuffer< T >.

◆ readArray()

template<typename T , template< typename > class BufferType>
int readArray ( data[],
int  len 
)
inlineoverridevirtual

Optimized bulk read operation.

Reads multiple elements, potentially spanning across buffer components.

Parameters
dataBuffer where read data will be stored
lenNumber of elements to read
Returns
Number of elements actually read

Reimplemented from BaseBuffer< T >.

◆ reset()

template<typename T , template< typename > class BufferType>
void reset ( )
inlineoverridevirtual

Reset the buffer to empty state.

Clears all buffer components but keeps them allocated.

Implements BaseBuffer< T >.

◆ resize()

template<typename T , template< typename > class BufferType>
bool resize ( int  new_size)
inlineoverridevirtual

Resize the buffer.

Changes the number of buffer components to match the requested size.

Parameters
new_sizeNew size in number of elements
Returns
true if resize was successful

Reimplemented from BaseBuffer< T >.

◆ size()

template<typename T , template< typename > class BufferType>
size_t size ( )
inlineoverridevirtual

Get total capacity of the buffer.

Returns
Size of all components combined, in number of elements

Implements BaseBuffer< T >.

◆ totalCapacity()

template<typename T , template< typename > class BufferType>
size_t totalCapacity ( )
inlineprotected

Calculate total capacity of all buffer components.

Returns
Combined size of all components in elements

◆ write()

template<typename T , template< typename > class BufferType>
bool write ( data)
inlineoverridevirtual

Write a value to the buffer.

Adds an element to the buffer at the current write position, automatically expanding capacity by adding a new buffer component if needed.

Parameters
dataValue to write
Returns
true if write was successful, false if buffer is full (max components reached)

Implements BaseBuffer< T >.

◆ writeArray()

template<typename T , template< typename > class BufferType>
int writeArray ( const T  data[],
int  len 
)
inlineoverridevirtual

Optimized bulk write operation.

Writes multiple elements, automatically expanding capacity by adding new buffer components as needed.

Parameters
dataArray of elements to write
lenNumber of elements to write
Returns
Number of elements actually written

Reimplemented from BaseBuffer< T >.


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