A Hopf, Skip and a Jump
Public Member Functions | Protected Attributes | List of all members
slidingbuffer::SlidingBuffer< T, S, P > Class Template Reference

SlidingBuffer is class template which can be used when reading a data stream to provide access to the stream's immediate history. More...

#include <slidingbuffer.h>

Public Member Functions

 SlidingBuffer (P &sp, const std::size_t max_segs, const std::size_t seg_size)
 Create a SlidingBuffer. More...
 
virtual ~SlidingBuffer ()
 Destructor.
 
T & operator[] (long int idx)
 Access an element of the SlidingBuffer. More...
 

Protected Attributes

std::size_t origin
 Lowest index in buffer.
 
const std::size_t max_segs
 The largest number of segments in the buffer A history of at least \(seg_size\cdot(max_segs-1)\) entities will be made available subject to sufficient data having been read from the data stream.
 
const std::size_t seg_size
 The number of entities in each segment.
 
std::deque< S *, UniqueAllocator< S * > > segs
 Segments held by the buffer.
 
P & sp
 The producer to call to fill a segment.
 

Detailed Description

template<typename T, typename S, typename P>
class slidingbuffer::SlidingBuffer< T, S, P >

SlidingBuffer is class template which can be used when reading a data stream to provide access to the stream's immediate history.

Structure of a SlidingBuffer

A SlidingBuffer is composed of a number of Segments . It provides a method of accessing the segments through the [] operator so that the reading and discarding of segments is automatic and transparent. This means the data stream can be accessed as if it it were a conventional array (or other container supporting the operator[] method), except that as the greatest index to date increases, older elements are forgotten.

Segments are created and filled on demand. As the number of Segments specified at instantiation is exceeded, the oldest Segments are discarded. This means that the recent history of the buffered data is always available, "recent" being defined by the size of the SlidingBuffer.

SlidingBuffers may only slide forward, which is to say that the accessed index must never be less than the length of the buffer prior to the greatest index ever accessed. Failing to comply with this constraint causes an exception to be thrown.

For example, suppose that a buffer comprising four segments each of three integers is created.

TODO The following isn't implemented yet... SegmentProducer::generate() is called asynchronously to fill new Segments. A separate thread is created to calculate the datasamples used to populate the segment. In the event that a SlidingBuffer Segment is accessed before the SegmentProducer has finished filling it, the access blocks until such time as tne data becomes available. END TODO

Mindful that the act of filling a SlidingBuffer may be (and usually is) stateful, although the accessed index can be advanced arbitrarily towards the end of the SlidingBuffer, the SegmentProducer will be called to generate data up until the point it is required, attempting to access data before the beginning of the SlidingBuffer will cause an exception to be thrown, even though the index may be well above 0.

Template Parameters
TThe type of the primitive data held in the buffer
SThe types of the segments comprising the buffer
PThe type of the producer
See also
Sliding Buffer: Internal Operation

Definition at line 245 of file slidingbuffer.h.

Constructor & Destructor Documentation

◆ SlidingBuffer()

template<typename T, typename S, typename P>
slidingbuffer::SlidingBuffer< T, S, P >::SlidingBuffer ( P &  sp,
const std::size_t  max_segs,
const std::size_t  seg_size 
)
inline

Create a SlidingBuffer.

Starting at any point in the buffer, a history of at least seg_size*(max_segs-1) entities is available.

Parameters
spThe producer whose generate() method is invoked to populate a new segment
max_segsThe maxiumum number of segments stored in the buffer
seg_sizeThe number of entities in each segment

Definition at line 268 of file slidingbuffer.h.

Member Function Documentation

◆ operator[]()

template<typename T, typename S, typename P>
T& slidingbuffer::SlidingBuffer< T, S, P >::operator[] ( long int  idx)
inline

Access an element of the SlidingBuffer.

Return a reference to the element of the SlidingBuffer or throw an exception if it is not available

Parameters
idxIndex to access.
Exceptions
NegativeIndexExceptionThe index is less than 0.
ExpiredIndexExceptionThe indexed element is no longer available.
IndexOutOfRangeExceptionThe indexed element is not yet in the SlidingBuffer and can not be produced by the Producer.

Definition at line 295 of file slidingbuffer.h.


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