RingBuffer< T, N > Class Template Reference

RingBuffer implements a lock-free ring buffer for one writer and N readers, that is to be used to store a sample type T. More...

#include <RingBuffer.h>

Public Member Functions

 RingBuffer (int n)
 Create a ring buffer with room to write n samples. More...
 
virtual ~RingBuffer ()
 
int getSize () const
 Return the total capacity of the ring buffer in samples. More...
 
RingBuffer< T, N > resized (int newSize) const
 Return a new ring buffer of the given size, containing the same data as this one as perceived by reader 0 of this buffer. More...
 
void reset ()
 Reset read and write pointers, thus emptying the buffer. More...
 
int getReadSpace (int R=0) const
 Return the amount of data available for reading by reader R, in samples. More...
 
int getWriteSpace () const
 Return the amount of space available for writing, in samples. More...
 
int read (T *destination, int n, int R=0)
 Read n samples from the buffer, for reader R. More...
 
int readAdding (T *destination, int n, int R=0)
 Read n samples from the buffer, for reader R, adding them to the destination. More...
 
readOne (int R=0)
 Read one sample from the buffer, for reader R. More...
 
int peek (T *destination, int n, int R=0) const
 Read n samples from the buffer, if available, for reader R, without advancing the read pointer – i.e. More...
 
peekOne (int R=0) const
 Read one sample from the buffer, if available, without advancing the read pointer – i.e. More...
 
int skip (int n, int R=0)
 Pretend to read n samples from the buffer, for reader R, without actually returning them (i.e. More...
 
int write (const T *source, int n)
 Write n samples to the buffer. More...
 
int zero (int n)
 Write n zero-value samples to the buffer. More...
 
 RingBuffer (const RingBuffer &)=default
 
RingBufferoperator= (const RingBuffer &)=default
 

Protected Attributes

std::vector< T, breakfastquay::StlAllocator< T > > m_buffer
 
int m_writer
 
std::vector< int > m_readers
 
int m_size
 

Detailed Description

template<typename T, int N = 1>
class RingBuffer< T, N >

RingBuffer implements a lock-free ring buffer for one writer and N readers, that is to be used to store a sample type T.

Definition at line 45 of file RingBuffer.h.

Constructor & Destructor Documentation

template<typename T , int N>
RingBuffer< T, N >::RingBuffer ( int  n)

Create a ring buffer with room to write n samples.

Note that the internal storage size will actually be n+1 samples, as one element is unavailable for administrative reasons. Since the ring buffer performs best if its size is a power of two, this means n should ideally be some power of two minus one.

Definition at line 168 of file RingBuffer.h.

template<typename T , int N>
RingBuffer< T, N >::~RingBuffer ( )
virtual

Definition at line 180 of file RingBuffer.h.

template<typename T, int N = 1>
RingBuffer< T, N >::RingBuffer ( const RingBuffer< T, N > &  )
default

Member Function Documentation

template<typename T , int N>
int RingBuffer< T, N >::getSize ( ) const

Return the total capacity of the ring buffer in samples.

(This is the argument n passed to the constructor.)

Definition at line 189 of file RingBuffer.h.

References RingBuffer< T, N >::m_size.

Referenced by MIDIInput::postEvent(), and OSCQueue::postMessage().

template<typename T , int N>
RingBuffer< T, N > RingBuffer< T, N >::resized ( int  newSize) const

Return a new ring buffer of the given size, containing the same data as this one as perceived by reader 0 of this buffer.

If another thread reads from or writes to this buffer during the call, the contents of the new buffer may be incomplete or inconsistent. If this buffer's data will not fit in the new size, the contents are undetermined.

Definition at line 200 of file RingBuffer.h.

References RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_readers, RingBuffer< T, N >::m_size, RingBuffer< T, N >::m_writer, and RingBuffer< T, N >::write().

template<typename T , int N>
void RingBuffer< T, N >::reset ( )

Reset read and write pointers, thus emptying the buffer.

Should be called from the write thread.

Definition at line 222 of file RingBuffer.h.

References RingBuffer< T, N >::m_readers, and RingBuffer< T, N >::m_writer.

Referenced by DSSIPluginInstance::clearEvents(), and DSSIPluginInstance::discardEvents().

template<typename T , int N>
int RingBuffer< T, N >::getWriteSpace ( ) const
template<typename T, int N>
int RingBuffer< T, N >::read ( T *  destination,
int  n,
int  R = 0 
)

Read n samples from the buffer, for reader R.

If fewer than n are available, the remainder will be zeroed out. Returns the number of samples actually read.

Definition at line 279 of file RingBuffer.h.

References RingBuffer< T, N >::getReadSpace(), RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_readers, and RingBuffer< T, N >::m_size.

template<typename T, int N>
int RingBuffer< T, N >::readAdding ( T *  destination,
int  n,
int  R = 0 
)

Read n samples from the buffer, for reader R, adding them to the destination.

If fewer than n are available, the remainder will be left alone. Returns the number of samples actually read.

Definition at line 316 of file RingBuffer.h.

References RingBuffer< T, N >::getReadSpace(), RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_readers, and RingBuffer< T, N >::m_size.

template<typename T , int N>
T RingBuffer< T, N >::readOne ( int  R = 0)

Read one sample from the buffer, for reader R.

If no sample is available, this will silently return zero. Calling this repeatedly is obviously slower than calling read once, but it may be good enough if you don't want to allocate a buffer to read into.

Definition at line 354 of file RingBuffer.h.

References RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_readers, RingBuffer< T, N >::m_size, and RingBuffer< T, N >::m_writer.

Referenced by MIDIInput::readEvent(), OSCQueue::readMessage(), and OSCQueue::~OSCQueue().

template<typename T, int N>
int RingBuffer< T, N >::peek ( T *  destination,
int  n,
int  R = 0 
) const

Read n samples from the buffer, if available, for reader R, without advancing the read pointer – i.e.

a subsequent read() or skip() will be necessary to empty the buffer. If fewer than n are available, the remainder will be zeroed out. Returns the number of samples actually read.

Definition at line 375 of file RingBuffer.h.

References RingBuffer< T, N >::getReadSpace(), RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_readers, and RingBuffer< T, N >::m_size.

template<typename T , int N>
T RingBuffer< T, N >::peekOne ( int  R = 0) const

Read one sample from the buffer, if available, without advancing the read pointer – i.e.

a subsequent read() or skip() will be necessary to empty the buffer. Returns zero if no sample was available.

Definition at line 409 of file RingBuffer.h.

References RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_readers, and RingBuffer< T, N >::m_writer.

Referenced by DSSIPluginInstance::run(), and DSSIPluginInstance::runGrouped().

template<typename T , int N>
int RingBuffer< T, N >::skip ( int  n,
int  R = 0 
)

Pretend to read n samples from the buffer, for reader R, without actually returning them (i.e.

discard the next n samples). Returns the number of samples actually available for discarding.

Definition at line 428 of file RingBuffer.h.

References RingBuffer< T, N >::getReadSpace(), RingBuffer< T, N >::m_readers, and RingBuffer< T, N >::m_size.

Referenced by DSSIPluginInstance::run(), and DSSIPluginInstance::runGrouped().

template<typename T, int N>
int RingBuffer< T, N >::write ( const T *  source,
int  n 
)

Write n samples to the buffer.

If insufficient space is available, not all samples may actually be written. Returns the number of samples actually written.

Definition at line 449 of file RingBuffer.h.

References RingBuffer< T, N >::getWriteSpace(), RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_size, and RingBuffer< T, N >::m_writer.

Referenced by MIDIInput::postEvent(), OSCQueue::postMessage(), RingBuffer< T, N >::resized(), and DSSIPluginInstance::sendEvent().

template<typename T , int N>
int RingBuffer< T, N >::zero ( int  n)

Write n zero-value samples to the buffer.

If insufficient space is available, not all zeros may actually be written. Returns the number of zeroes actually written.

Definition at line 485 of file RingBuffer.h.

References RingBuffer< T, N >::getWriteSpace(), RingBuffer< T, N >::m_buffer, RingBuffer< T, N >::m_size, and RingBuffer< T, N >::m_writer.

template<typename T, int N = 1>
RingBuffer& RingBuffer< T, N >::operator= ( const RingBuffer< T, N > &  )
default

Member Data Documentation

template<typename T, int N = 1>
std::vector<T, breakfastquay::StlAllocator<T> > RingBuffer< T, N >::m_buffer
protected

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