Switchboard SDK
Loading...
Searching...
No Matches
switchboard::RingBuffer< T > Class Template Reference

Class providing ring buffer functionality. Supports single-threaded and multi-threaded (single producer, single consumer) use cases. More...

#include <RingBuffer.hpp>

Public Member Functions

 RingBuffer (uint numberOfItems)
 Creates a RingBuffer instance. Allocates the underlying buffer, not suitable for use in the real-time thread.
 
 RingBuffer (uint numberOfItems, T *buffer)
 Creates a RingBuffer instance without allocating. Suitable for resizing a ring buffer in the real-time thread.
 
 ~RingBuffer ()
 RingBuffer destructor.
 
void clear ()
 Clears all data in the ring buffer.
 
uint getAvailableItemsToProduce () const
 Returns the maximum number of items that can be added to the ring buffer.
 
void produce (const T *sourceBuffer, uint numberOfItems)
 Adds items to the ring buffer.
 
uint getAvailableItemsToConsume () const
 Returns the maximum number of items that can be read from the ring buffer.
 
void consume (T *destBuffer, uint numberOfItems)
 Reads items from the ring buffer and updates the read pointer.
 
void read (T *destBuffer, uint numberOfItems)
 Reads items from the ring buffer without updating the read pointer.
 
void discard (uint numberOfItems)
 Discards items from the ring buffer. Steps the read pointer without reading the items.
 

Detailed Description

template<typename T>
class switchboard::RingBuffer< T >

Class providing ring buffer functionality. Supports single-threaded and multi-threaded (single producer, single consumer) use cases.

RingBuffer class.

Constructor & Destructor Documentation

◆ RingBuffer() [1/2]

template<typename T >
switchboard::RingBuffer< T >::RingBuffer ( uint numberOfItems)

Creates a RingBuffer instance. Allocates the underlying buffer, not suitable for use in the real-time thread.

Parameters
numberOfItemsThe number of items that the ring buffer can hold.

◆ RingBuffer() [2/2]

template<typename T >
switchboard::RingBuffer< T >::RingBuffer ( uint numberOfItems,
T * buffer )

Creates a RingBuffer instance without allocating. Suitable for resizing a ring buffer in the real-time thread.

Does not delete the buffer on destruction. Make sure to handle deletion.

Parameters
numberOfItemsThe number of items that the ring buffer can hold.
bufferThe underlying buffer the ring buffer is operating on.

Member Function Documentation

◆ consume()

template<typename T >
void switchboard::RingBuffer< T >::consume ( T * destBuffer,
uint numberOfItems )

Reads items from the ring buffer and updates the read pointer.

Parameters
destBuffer[inout] The buffer where the read items will be copied to.
numberOfItemsThe number of items that should be consumed.

◆ discard()

template<typename T >
void switchboard::RingBuffer< T >::discard ( uint numberOfItems)

Discards items from the ring buffer. Steps the read pointer without reading the items.

Parameters
numberOfItemsThe number of items that should be discarded.

◆ getAvailableItemsToConsume()

template<typename T >
uint switchboard::RingBuffer< T >::getAvailableItemsToConsume ( ) const

Returns the maximum number of items that can be read from the ring buffer.

Returns
The number items that can be consumed.

◆ getAvailableItemsToProduce()

template<typename T >
uint switchboard::RingBuffer< T >::getAvailableItemsToProduce ( ) const

Returns the maximum number of items that can be added to the ring buffer.

Returns
The number of items that can be produced.

◆ produce()

template<typename T >
void switchboard::RingBuffer< T >::produce ( const T * sourceBuffer,
uint numberOfItems )

Adds items to the ring buffer.

Parameters
sourceBufferThe buffer where the items are copied from.
numberOfItemsThe number of items that should be added to the ring buffer.

◆ read()

template<typename T >
void switchboard::RingBuffer< T >::read ( T * destBuffer,
uint numberOfItems )

Reads items from the ring buffer without updating the read pointer.

Parameters
destBuffer[inout] The buffer where the read items will be copied to.
numberOfItemsThe number of items that should be consumed.