All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::RingBuffer< TElem > Class Template Reference

Simple ring-buffer template. More...

#include <Radiant/RingBuffer.hpp>

Inheritance diagram for Radiant::RingBuffer< TElem >:
Radiant::RingBufferDelay< TElem >

Public Member Functions

TElem * data ()
 Get a raw pointer to data. More...
 
const TElem * data () const
 Get a raw pointer to data. More...
 
int elemSize () const
 Get the size of a sample in bytes. More...
 
TElem & getIndex (unsigned nIndex)
 Get a sample from the buffer. More...
 
const TElem & getIndex (unsigned nIndex) const
 Get a sample from the buffer. More...
 
const TElem & getIndexConst (unsigned nIndex) const
 Get a sample from the buffer. More...
 
unsigned mask () const
 Report mask used for address translation. More...
 
RingBuffer< TElem > & operator= (const RingBuffer &xrBuffer)
 Deep copy operator. More...
 
TElem & operator[] (unsigned nIndex)
 Get the sample with the given index. More...
 
const TElem & operator[] (unsigned nIndex) const
 Get the sample with the given index. More...
 
bool resize (unsigned nBufSize)
 Resize the buffer. More...
 
 RingBuffer ()
 Create an empty ring buffer.
 
 RingBuffer (unsigned nSize)
 Create a ring-buffer with given minimum size. More...
 
 RingBuffer (const RingBuffer &xrBuffer)
 Create a deep copy. More...
 
void setAll (TElem xVal)
 Set all buffer values. More...
 
void setIndex (unsigned nIndex, const TElem &xVal)
 Set a sample of the buffer. More...
 
unsigned size () const
 Get buffer size. More...
 
unsigned sizeOf () const
 Get buffer size in bytes. More...
 
virtual ~RingBuffer ()
 Destroy the ring-buffer and free memory.
 

Static Public Member Functions

static unsigned targetSize (unsigned nBufSize)
 Calculate the real size of the buffer if the required number of samples is nBufSize. More...
 

Protected Attributes

TElem * m_line
 Data buffer.
 
unsigned m_mask
 Mask for determining index.
 
unsigned m_size
 Real size.
 

Detailed Description

template<class TElem>
class Radiant::RingBuffer< TElem >

Simple ring-buffer template.

The operations are optimized and thus there are few safety checks.

Buffer size must always be a power-of-two.

The elements of this class need to be numeric: float, double, int, long etc.

Author
Tommi Ilmonen, taken from the Mustajuuri project (Copyright, Tommi Ilmonen).

Constructor & Destructor Documentation

template<class TElem>
Radiant::RingBuffer< TElem >::RingBuffer ( unsigned  nSize)
inline

Create a ring-buffer with given minimum size.

The actual buffer might be larger than the given value.

Parameters
nSizeminimum buffer size
template<class TElem>
Radiant::RingBuffer< TElem >::RingBuffer ( const RingBuffer< TElem > &  xrBuffer)
inline

Create a deep copy.

Parameters
xrBufferbuffer to copy

Member Function Documentation

template<class TElem>
TElem* Radiant::RingBuffer< TElem >::data ( )
inline

Get a raw pointer to data.

Returns
raw pointer to ring-buffer data
template<class TElem>
const TElem* Radiant::RingBuffer< TElem >::data ( ) const
inline

Get a raw pointer to data.

Returns
raw pointer to ring-buffer data
template<class TElem>
int Radiant::RingBuffer< TElem >::elemSize ( ) const
inline

Get the size of a sample in bytes.

Returns
sample size in bytes
template<class TElem>
TElem& Radiant::RingBuffer< TElem >::getIndex ( unsigned  nIndex)
inline

Get a sample from the buffer.

Parameters
nIndexindex of the sample
Returns
sample with the given index
template<class TElem>
const TElem& Radiant::RingBuffer< TElem >::getIndex ( unsigned  nIndex) const
inline

Get a sample from the buffer.

Parameters
nIndexindex of the sample
Returns
sample with the given index
template<class TElem>
const TElem& Radiant::RingBuffer< TElem >::getIndexConst ( unsigned  nIndex) const
inline

Get a sample from the buffer.

Parameters
nIndexindex of the sample
Returns
sample with the given index
template<class TElem>
unsigned Radiant::RingBuffer< TElem >::mask ( ) const
inline

Report mask used for address translation.

Returns
buffer mask
template<class TElem>
RingBuffer<TElem>& Radiant::RingBuffer< TElem >::operator= ( const RingBuffer< TElem > &  xrBuffer)
inline

Deep copy operator.

Parameters
xrBufferbuffer to copy
Returns
reference to this
template<class TElem>
TElem& Radiant::RingBuffer< TElem >::operator[] ( unsigned  nIndex)
inline

Get the sample with the given index.

Parameters
nIndexindex to get
Returns
sample with the given index
template<class TElem>
const TElem& Radiant::RingBuffer< TElem >::operator[] ( unsigned  nIndex) const
inline

Get the sample with the given index.

Parameters
nIndexindex to get
Returns
sample with the given index
template<class TElem>
bool Radiant::RingBuffer< TElem >::resize ( unsigned  nBufSize)
inline

Resize the buffer.

Old buffer elements are lost when the buffer is resized. If new size equals the old size the buffer is not reallocated. The new buffer will be able to hold at least nBufSize elements. The new buffer elements are by default initialized to zero.

Parameters
nBufSizenew buffer size
Returns
true on success
template<class TElem>
void Radiant::RingBuffer< TElem >::setAll ( TElem  xVal)
inline

Set all buffer values.

Fills the buffer with the given value.

Parameters
xValvalue to set
template<class TElem>
void Radiant::RingBuffer< TElem >::setIndex ( unsigned  nIndex,
const TElem &  xVal 
)
inline

Set a sample of the buffer.

Parameters
nIndexsample index
xValsample value
template<class TElem>
unsigned Radiant::RingBuffer< TElem >::size ( ) const
inline

Get buffer size.

Returns
buffer size in number of samples
template<class TElem>
unsigned Radiant::RingBuffer< TElem >::sizeOf ( ) const
inline

Get buffer size in bytes.

Returns
size of the buffer in bytes
template<class TElem>
static unsigned Radiant::RingBuffer< TElem >::targetSize ( unsigned  nBufSize)
inlinestatic

Calculate the real size of the buffer if the required number of samples is nBufSize.

Outsiders should not need this method too often.

Parameters
nBufSizetarget buffer size
Returns
required buffer size