Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
RingBuffer< T > Class Template Reference

A ring buffer implementation for storing elements of type T. More...

#include <src/utils/ring_buffer.h>

Public Member Functions

 RingBuffer (size_t size)
 RingBuffer (const RingBuffer &other)
bool write (const T &src)
void force_write (const T &src)
bool write_multiple (const T *src, size_t count)
void force_write_multiple (const T *src, size_t count)
bool skip (size_t num_elements)
bool read (T &dst)
bool peek (T &dst) const
 Peek a single element from the ring buffer without moving the read head.
size_t peek_multiple (T *dst, size_t count) const
 Peek multiple elements from the ring buffer without moving the read head.
void reset ()
size_t capacity () const
size_t write_space () const
size_t read_space () const
bool can_read_multiple (size_t count) const
bool read_multiple (T *dst, size_t count)

Detailed Description

template<typename T>
class RingBuffer< T >

A ring buffer implementation for storing elements of type T.

The RingBuffer class provides a thread-safe, fixed-size buffer that allows for efficient reading and writing of elements. It uses atomic operations to ensure thread safety.

The buffer has a fixed capacity, which is one more than the size specified in the constructor. This extra slot is used to differentiate between a full and an empty buffer.

The write and read functions return true if the operation was successful, and false otherwise (e.g., if the buffer is full or empty, respectively).

The reset function can be used to reset the read and write heads to the beginning of the buffer.

The capacity, write_space, and read_space functions can be used to query the current state of the buffer.

Definition at line 38 of file ring_buffer.h.

Constructor & Destructor Documentation

◆ RingBuffer() [1/2]

template<typename T>
RingBuffer< T >::RingBuffer ( size_t size)
inlineexplicit

Definition at line 41 of file ring_buffer.h.

◆ RingBuffer() [2/2]

template<typename T>
RingBuffer< T >::RingBuffer ( const RingBuffer< T > & other)
inline

Definition at line 47 of file ring_buffer.h.

Member Function Documentation

◆ can_read_multiple()

template<typename T>
bool RingBuffer< T >::can_read_multiple ( size_t count) const
inline

Definition at line 287 of file ring_buffer.h.

◆ capacity()

template<typename T>
size_t RingBuffer< T >::capacity ( ) const
inline

Definition at line 257 of file ring_buffer.h.

◆ force_write()

template<typename T>
void RingBuffer< T >::force_write ( const T & src)
inline

Definition at line 110 of file ring_buffer.h.

◆ force_write_multiple()

template<typename T>
void RingBuffer< T >::force_write_multiple ( const T * src,
size_t count )
inline

Definition at line 147 of file ring_buffer.h.

◆ peek()

template<typename T>
bool RingBuffer< T >::peek ( T & dst) const
inline

Peek a single element from the ring buffer without moving the read head.

This function attempts to peek the next element from the current read position without modifying the state of the buffer.

Parameters
[out]dstReference to the variable where the peeked element will be stored.
Returns
true if an element was successfully peeked, false if the buffer is empty.

Definition at line 207 of file ring_buffer.h.

◆ peek_multiple()

template<typename T>
size_t RingBuffer< T >::peek_multiple ( T * dst,
size_t count ) const
inline

Peek multiple elements from the ring buffer without moving the read head.

This function attempts to peek up to the requested number of elements from the ring buffer, starting from the current read position. It does not modify the state of the buffer.

Parameters
[out]dstPointer to the destination array where peeked elements will be stored.
[in]countMaximum number of elements to peek.
Returns
The number of elements actually peeked, which may be less than or equal to count.

Definition at line 235 of file ring_buffer.h.

◆ read()

template<typename T>
bool RingBuffer< T >::read ( T & dst)
inline

Definition at line 183 of file ring_buffer.h.

◆ read_multiple()

template<typename T>
bool RingBuffer< T >::read_multiple ( T * dst,
size_t count )
inline

Definition at line 289 of file ring_buffer.h.

◆ read_space()

template<typename T>
size_t RingBuffer< T >::read_space ( ) const
inline

Definition at line 273 of file ring_buffer.h.

◆ reset()

template<typename T>
void RingBuffer< T >::reset ( )
inline

Definition at line 251 of file ring_buffer.h.

◆ skip()

template<typename T>
bool RingBuffer< T >::skip ( size_t num_elements)
inline

Definition at line 167 of file ring_buffer.h.

◆ write()

template<typename T>
bool RingBuffer< T >::write ( const T & src)
inline

Definition at line 97 of file ring_buffer.h.

◆ write_multiple()

template<typename T>
bool RingBuffer< T >::write_multiple ( const T * src,
size_t count )
inline

Definition at line 127 of file ring_buffer.h.

◆ write_space()

template<typename T>
size_t RingBuffer< T >::write_space ( ) const
inline

Definition at line 259 of file ring_buffer.h.


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