1 #ifndef _SLIDINGBUFFER_H_ 2 #define _SLIDINGBUFFER_H_ 9 #include "uniqueallocator.h" 21 :
public std::runtime_error {
28 : std::runtime_error(what)
47 "SlidingBuffer: Attempt to access negative index",
65 "SlidingBuffer: Indexed item no longer available (underflow)",
82 "SlidingBuffer: Attempt to access past end of data",
110 virtual std::size_t generate(T seg[],
111 const std::size_t idx,
112 const std::size_t size) = 0;
120 virtual bool more(
void) = 0;
137 template <
typename T,
typename P>
class Segment {
153 Segment(
const std::size_t size,
const std::size_t origin, P& sp)
154 : seg {
new T [size] }
156 , size { sp.generate(seg, origin, size) }
255 std::deque<S*, UniqueAllocator<S*>>
segs;
269 const std::size_t max_segs,
270 const std::size_t seg_size
273 , max_segs { max_segs }
274 , seg_size { seg_size }
300 if (static_cast<size_t>(idx) < origin)
303 std::size_t segment { (
static_cast<std::size_t
>(idx) - origin) / seg_size };
305 while (segment >= segs.size()) {
309 segs.push_back(
new S(seg_size,
310 origin + segs.size() * seg_size,
314 if (segs.size() > max_segs) {
323 S*
const s { segs[segment] };
324 std::size_t
const i { idx%seg_size };
326 if (s->get_size() <= i)
335 #endif // _SLIDINGBUFFER_H_ NegativeIndexException(const long int idx)
Construct an Exception object.
A Segment is an aggregation of base data with a SlidingBuffer.
std::size_t size
Number of entities held.
P & sp
The producer to call to fill a segment.
P & sp
The producer invoked to fill this Segment.
std::size_t origin
Lowest index in buffer.
virtual ~SegmentProducer()
Destructor: to be provided by derived class.
std::deque< S *, UniqueAllocator< S * > > segs
Segments held by the buffer.
const long int index
The exception was caused by attempting to access this index.
Exception thown when the entity at position 'index' is not yet available and the SegmentProducer for ...
T & operator[](std::size_t idx)
Access an entity, waiting for it to be constructed if deferred construction is still taking place...
virtual ~SlidingBuffer()
Destructor.
const std::size_t max_segs
The largest number of segments in the buffer A history of at least entities will be made available s...
std::size_t get_size(void) const
Get the Segment's size.
Segment(const std::size_t size, const std::size_t origin, P &sp)
Construct a Segment.
SlidingBufferException(const char *what, const long int idx)
Construct an Exception object.
const std::size_t seg_size
The number of entities in each segment.
SlidingBuffer(P &sp, const std::size_t max_segs, const std::size_t seg_size)
Create a SlidingBuffer.
Exception thrown when an attempt was made to access a negative index in the SlidingBuffer.
virtual ~Segment()
Destroy this object and all the entities it contains.
T * seg
Dynamic array of entities in storage.
ExpiredIndexException(const long int idx)
Construct an Exception object.
IndexOutOfRangeException(const long int idx)
Construct an Exception object.
The base class for all exceptions thrown by a SlidingBuffer.
Exception thrown when an apparently valid index (positive, within the extent of the data read so far)...
const std::size_t origin
Index of the first entity in this Segment from the point of view of the SlidingBuffer.
Template of a pure virtual class which populates a Segment with entities.
bool valid
Indicates entities have been constructed (for deferred initialisation: not yet implemented).
SlidingBuffer is class template which can be used when reading a data stream to provide access to the...
T & operator[](long int idx)
Access an element of the SlidingBuffer.