Acoustic Research Tool (ART)  v0.10
mpBuffer< _Type, _Alloc > Class Template Reference

template<typename _Type, typename _Alloc = std::allocator<_Type>>
class mpBuffer< _Type, _Alloc >

A generic buffer which may be either used as a simple vector or ring buffer.

The implementation of this class is based on the STL vector and behaves exactly like it in case it is used as an array. Only the methods referenced by the muParser have been implemented - some missing parts like iterators are not available but can still be implemented.

Public Types

typedef _Type & reference
 Reference type of the data being stored in the vector.
typedef const _Type & const_reference
 Constant reference type of the data being stored in the vector.
typedef _Type * pointer
 Pointer type of the data being stored in the vector.
typedef const _Type * const_pointer
 Constant pointer type of the data being stored in the vector.
typedef int size_type
 The standard size type is int to allow negative indices for access.
typedef _Type value_type
 Data type of the data being stored.
typedef _Alloc allocator_type
 Allocator type for the used data type.

Public Member Functions

 mpBuffer (const allocator_type &a=allocator_type())
 Creates a new buffer object with no elements.
 mpBuffer (size_type n, bool rb=false, const value_type &v=value_type(), const allocator_type &a=allocator_type())
 Creates a new buffer object with the specified number of elements and the specified type.
 mpBuffer (const mpBuffer< value_type, allocator_type > &buf)
 Creates a new buffer by copying all elements of the specified buffer object.
virtual mpBufferoperator= (const mpBuffer< value_type, allocator_type > &buf)
 Assignment operator - deallocates all currently saved object in the buffer and creates a copy of the specified buffer.
virtual ~mpBuffer ()
 The destructor of the mpBuffer class.
virtual size_type size () const
 Returns the current size of the buffer.
virtual size_type max_size () const
 Returns the maximum number of elements which can be saved in a the buffer class.
virtual void resize (size_type sz, value_type c=value_type())
 Resizes the current buffer to the given size.
virtual size_type capacity () const
 Returns the capacity of the current buffer which is equal to the size.
virtual bool empty () const
 Returns true if the current amount of saved elements is zero.
virtual reference at (size_type n)
 Returns the element currently saved at the specified position.
virtual const_reference at (size_type n) const
 Returns the element currently saved at the specified position.
virtual reference operator[] (size_type n)
 Returns the element currently saved at the specified position, but uses a modulo operation in case of a ring buffer.
virtual const_reference operator[] (size_type n) const
 Returns the element currently saved at the specified position, but uses a modulo operation in case of a ring buffer.
virtual reference front ()
 Returns the first element of the current buffer.
virtual const_reference front () const
 Returns the first element of the current buffer.
virtual reference back ()
 Returns the last element of the current buffer.
virtual const_reference back () const
 Returns the last element of the current buffer.
virtual allocator_type get_allocator () const
 Returns the allocator type of the current buffer.
virtual void setCurrentIdx (size_type n)
 Sets the current index of the ring buffer to the given value and adapts internal pointers to the first and last element of the ring buffer.
virtual size_type getFirst () const
 Returns the first element saved in the current buffer.
virtual size_type getLast () const
 Returns the last element saved in the current buffer.
virtual size_type getUsedBufferSize () const
 Returns the number of accessed elements of the ring buffer which is equivalent to the actually used buffer size.
virtual bool isRingBuffer () const
 Returns true if the current buffer behaves like a a ring buffer, false otherwise.
virtual void setRingBuffer ()
 Sets the current buffer to behave like a ring buffer.
virtual void setVector ()
 Sets the current buffer to behave like a vector.

Protected Member Functions

virtual void clean ()
 Releases all elements by calling the destructor of the saved elements and deallocating all needed memory.

Protected Attributes

size_type _size
 Current number of elements in use.
allocator_type _allocator
 Allocator of current element type.
pointer _fields
 Pointer to all elements of buffer.
bool _is_ringbuffer
 Attribute deciding whether the current buffer implements a ringbuffer.
size_type _first
 In case of a ring buffer, this is the index to the first valid element.
size_type _last
 In case of a ring buffer, this is the index to the last valid element.
size_type _used_elements
 Saves the number of currently used elements for a ring buffer.

Member Typedef Documentation

template<typename _Type , typename _Alloc = std::allocator<_Type>>
typedef _Alloc mpBuffer< _Type, _Alloc >::allocator_type

Allocator type for the used data type.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
typedef const _Type* mpBuffer< _Type, _Alloc >::const_pointer

Constant pointer type of the data being stored in the vector.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
typedef const _Type& mpBuffer< _Type, _Alloc >::const_reference

Constant reference type of the data being stored in the vector.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
typedef _Type* mpBuffer< _Type, _Alloc >::pointer

Pointer type of the data being stored in the vector.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
typedef _Type& mpBuffer< _Type, _Alloc >::reference

Reference type of the data being stored in the vector.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
typedef int mpBuffer< _Type, _Alloc >::size_type

The standard size type is int to allow negative indices for access.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
typedef _Type mpBuffer< _Type, _Alloc >::value_type

Data type of the data being stored.

Constructor & Destructor Documentation

template<typename _Type , typename _Alloc = std::allocator<_Type>>
mpBuffer< _Type, _Alloc >::mpBuffer ( const allocator_type a = allocator_type())
inlineexplicit

Creates a new buffer object with no elements.

The default buffer type is array.

Parameters
[in]aDefines the allocator type for the class which shall be saved in the buffer. Defaults to the standard allocator.
template<typename _Type , typename _Alloc = std::allocator<_Type>>
mpBuffer< _Type, _Alloc >::mpBuffer ( size_type  n,
bool  rb = false,
const value_type v = value_type(),
const allocator_type a = allocator_type() 
)
inlineexplicit

Creates a new buffer object with the specified number of elements and the specified type.

Parameters
[in]nInitial size of the array.
[in]rbDefines the type of the array: If set to true, the buffer is a ring buffer, otherwise, it will be an array.
[in]vDefines the class type which shall be saved in the buffer.
[in]aDefines the allocator type for the class which shall be saved in the buffer. Defaults to the standard allocator.
template<typename _Type , typename _Alloc = std::allocator<_Type>>
mpBuffer< _Type, _Alloc >::mpBuffer ( const mpBuffer< value_type, allocator_type > &  buf)
inline

Creates a new buffer by copying all elements of the specified buffer object.

Parameters
[in]bufThe buffer to copy.
Warning
Take care when copying a buffer containing pointers to elements - only the pointers will be copied, but not the elements themselves.

Member Function Documentation

template<typename _Type , typename _Alloc = std::allocator<_Type>>
virtual reference mpBuffer< _Type, _Alloc >::at ( size_type  n)
inlinevirtual

Returns the element currently saved at the specified position.

This function behaves the same for the array and ring buffer implementation and does not change the _used_elements field.

Parameters
[in]nThe position of the element saved in the buffer.
Exceptions
std::out_of_rangeIf the specified position is not within 0 <= n < size.
template<typename _Type , typename _Alloc = std::allocator<_Type>>
virtual const_reference mpBuffer< _Type, _Alloc >::at ( size_type  n) const
inlinevirtual

Returns the element currently saved at the specified position.

This function behaves the same for the array and ring buffer implementation and does not change the _used_elements field.

Parameters
[in]nThe index of the element saved in the buffer.
Exceptions
std::out_of_rangeIf the specified position is not within 0 <= n < size.
template<typename _Type , typename _Alloc = std::allocator<_Type>>
virtual mpBuffer& mpBuffer< _Type, _Alloc >::operator= ( const mpBuffer< value_type, allocator_type > &  buf)
inlinevirtual

Assignment operator - deallocates all currently saved object in the buffer and creates a copy of the specified buffer.

Parameters
[in]bufThe buffer to copy.
Warning
Take care when copying a buffer containing pointers to elements - only the pointers will be copied, but not the elements themselves.
template<typename _Type , typename _Alloc = std::allocator<_Type>>
virtual reference mpBuffer< _Type, _Alloc >::operator[] ( size_type  n)
inlinevirtual

Returns the element currently saved at the specified position, but uses a modulo operation in case of a ring buffer.

Parameters
[in]nThe index of the element saved in the buffer.
Exceptions
std::out_of_rangeIf the current buffer is an array and the index is not within 0 <= n < size or the number of used elements in the ring buffer is greater than the current size.
template<typename _Type , typename _Alloc = std::allocator<_Type>>
virtual const_reference mpBuffer< _Type, _Alloc >::operator[] ( size_type  n) const
inlinevirtual

Returns the element currently saved at the specified position, but uses a modulo operation in case of a ring buffer.

Parameters
[in]nThe index of the element saved in the buffer.
Exceptions
std::out_of_rangeIf the current buffer is an array and the index is not within 0 <= n < size or the number of used elements in the ring buffer is greater than the current size.
template<typename _Type , typename _Alloc = std::allocator<_Type>>
virtual void mpBuffer< _Type, _Alloc >::resize ( size_type  sz,
value_type  c = value_type() 
)
inlinevirtual

Resizes the current buffer to the given size.

Parameters
[in]szThe size of the new buffer.
[in]cThe class type of the elements to save in the buffer.
Exceptions
std::length_errorIf the new size of a ring buffer will be less than the current number of elements in use.

Member Data Documentation

template<typename _Type , typename _Alloc = std::allocator<_Type>>
allocator_type mpBuffer< _Type, _Alloc >::_allocator
protected

Allocator of current element type.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
pointer mpBuffer< _Type, _Alloc >::_fields
protected

Pointer to all elements of buffer.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
size_type mpBuffer< _Type, _Alloc >::_first
protected

In case of a ring buffer, this is the index to the first valid element.

Initial value is -1.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
bool mpBuffer< _Type, _Alloc >::_is_ringbuffer
protected

Attribute deciding whether the current buffer implements a ringbuffer.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
size_type mpBuffer< _Type, _Alloc >::_last
mutableprotected

In case of a ring buffer, this is the index to the last valid element.

Initial value is -1.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
size_type mpBuffer< _Type, _Alloc >::_size
protected

Current number of elements in use.

template<typename _Type , typename _Alloc = std::allocator<_Type>>
size_type mpBuffer< _Type, _Alloc >::_used_elements
mutableprotected

Saves the number of currently used elements for a ring buffer.