PSCF v1.1
Public Member Functions | List of all members
Util::RingBuffer< Data > Class Template Reference

Class for storing history of previous values in an array. More...

#include <RingBuffer.h>

Public Member Functions

 RingBuffer ()
 Contructor. More...
 
 RingBuffer (RingBuffer< Data > const &other)
 Copy contructor. More...
 
RingBufferoperator= (RingBuffer< Data > const &other)
 Assignment. More...
 
virtual ~RingBuffer ()
 Destructor. More...
 
void allocate (int capacity)
 Allocate a new empty buffer. More...
 
void clear ()
 Set previously allocated buffer to empty state. More...
 
void append (Data const &value)
 Add a new value to the buffer. More...
 
void advance ()
 Advances the pointer to an added element without assigning a value. More...
 
int size () const
 Return number of values currently in the buffer. More...
 
int capacity () const
 Return the capacity of the buffer. More...
 
bool isAllocated () const
 Return true if the RingBuffer has been allocated, false otherwise. More...
 
bool isFull () const
 Return true if full (if size == capacity), false otherwise. More...
 
Data const & operator[] (int offset) const
 Retrieve a const value, a specified number of time steps ago. More...
 
Data & operator[] (int offset)
 Retrieve a value, a specified number of time steps ago. More...
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 Serialize a RingBuffer to/from an Archive. More...
 

Detailed Description

template<class Data>
class Util::RingBuffer< Data >

Class for storing history of previous values in an array.

Data is stored in a circular buffer, in which, once the array is full, the newest data value overwrites the oldest.

Definition at line 26 of file RingBuffer.h.

Constructor & Destructor Documentation

◆ RingBuffer() [1/2]

template<class Data >
Util::RingBuffer< Data >::RingBuffer

Contructor.

Definition at line 169 of file RingBuffer.h.

◆ RingBuffer() [2/2]

template<class Data >
Util::RingBuffer< Data >::RingBuffer ( RingBuffer< Data > const &  other)

Copy contructor.

Parameters
otherobject to be copied.

Definition at line 184 of file RingBuffer.h.

◆ ~RingBuffer()

template<class Data >
Util::RingBuffer< Data >::~RingBuffer
virtual

Destructor.

Definition at line 248 of file RingBuffer.h.

Member Function Documentation

◆ operator=()

template<class Data >
RingBuffer< Data > & Util::RingBuffer< Data >::operator= ( RingBuffer< Data > const &  other)

Assignment.

Parameters
otherobject to be assigned.

Definition at line 211 of file RingBuffer.h.

References Util::RingBuffer< Data >::isAllocated(), and UTIL_THROW.

◆ allocate()

template<class Data >
void Util::RingBuffer< Data >::allocate ( int  capacity)

Allocate a new empty buffer.

Allocate a new array containing capacity elements.

Throw an Exception if this RingBuffer has already been allocated - a RingBuffer can only be allocated once.

Parameters
capacitynumber of elements to allocate.

Definition at line 257 of file RingBuffer.h.

References UTIL_THROW.

◆ clear()

template<class Data >
void Util::RingBuffer< Data >::clear
inline

Set previously allocated buffer to empty state.

Definition at line 274 of file RingBuffer.h.

◆ append()

template<class Data >
void Util::RingBuffer< Data >::append ( Data const &  value)
inline

Add a new value to the buffer.

This function updates the pointer to the most recent element and then uses an assignment (=) operator to assign argument "value" to this element of the underlying array.

Parameters
valuenew value to be added

Definition at line 285 of file RingBuffer.h.

◆ advance()

template<class Data >
void Util::RingBuffer< Data >::advance
inline

Advances the pointer to an added element without assigning a value.

This function advances the pointer that identifies the the newest element but does not assign a value to that element. Immediately after applying this function to a RingBuffer<T> named buffer, the value of the most recently added element, buffer[0], is undefined. A call to RingBuffer<T>::advance must be thus always be followed by code that assigns this value. The simplest usage for a Ringbuffer<T> named buffer would thus be:

buffer.advance();
buffer[0] = value;

where "value" is an object of type T. This combination is equivalent to buffer.append(value).

Rationale: This is designed as an alternative to the append memmber function for cases in which type T is a large object. This avoids the need to construct and assign a value to a T object outside the buffer which must then be re-assigned to an array element inside the RingBuffer<T>::append function.

Definition at line 306 of file RingBuffer.h.

◆ size()

template<class Data >
int Util::RingBuffer< Data >::size
inline

Return number of values currently in the buffer.

Definition at line 325 of file RingBuffer.h.

Referenced by pscfpp.file.File::__eq__(), pscfpp.text.Record::__init__(), pscfpp.file.File::__init__(), pscfpp.file.File::__repr__(), and pscfpp.text.Record::__str__().

◆ capacity()

template<class Data >
int Util::RingBuffer< Data >::capacity
inline

Return the capacity of the buffer.

Definition at line 333 of file RingBuffer.h.

◆ isAllocated()

template<class Data >
bool Util::RingBuffer< Data >::isAllocated
inline

Return true if the RingBuffer has been allocated, false otherwise.

Definition at line 341 of file RingBuffer.h.

Referenced by Util::RingBuffer< Data >::operator=().

◆ isFull()

template<class Data >
bool Util::RingBuffer< Data >::isFull
inline

Return true if full (if size == capacity), false otherwise.

Definition at line 349 of file RingBuffer.h.

◆ operator[]() [1/2]

template<class Data >
Data const & Util::RingBuffer< Data >::operator[] ( int  offset) const
inline

Retrieve a const value, a specified number of time steps ago.

Parameters
offsetnumber of steps back in time (offset=0 is current value).

Definition at line 357 of file RingBuffer.h.

References UTIL_THROW.

◆ operator[]() [2/2]

template<class Data >
Data & Util::RingBuffer< Data >::operator[] ( int  offset)
inline

Retrieve a value, a specified number of time steps ago.

Parameters
offsetnumber of steps back in time (offset=0 is current value).

Definition at line 375 of file RingBuffer.h.

References UTIL_THROW.

◆ serialize()

template<class Data >
template<class Archive >
void Util::RingBuffer< Data >::serialize ( Archive &  ar,
const unsigned int  version 
)

Serialize a RingBuffer to/from an Archive.

Parameters
ararchive
versionarchive version id

Definition at line 393 of file RingBuffer.h.

References UTIL_THROW.


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