99 lines
3.9 KiB
C++
99 lines
3.9 KiB
C++
|
|
#ifndef DEF_CircularBuffer
|
||
|
|
#define DEF_CircularBuffer
|
||
|
|
|
||
|
|
template<size_t N, typename T>
|
||
|
|
class CircularBuffer
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
/// Creates a circular buffer of size N containing items of type T, with values uninitialized.
|
||
|
|
CircularBuffer()
|
||
|
|
: data(NULL),
|
||
|
|
readPointer(0),
|
||
|
|
writePointer(0)
|
||
|
|
{ data = new T[N]; }
|
||
|
|
|
||
|
|
/// Creates a circular buffer of size N containing items of type T, initialized with the value initValue.
|
||
|
|
CircularBuffer(T const& initValue)
|
||
|
|
{
|
||
|
|
data = new T[N];
|
||
|
|
Fill(initValue);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Destroys the circular buffer.
|
||
|
|
~CircularBuffer() { delete[] data; }
|
||
|
|
|
||
|
|
void Fill(T const& initValue)
|
||
|
|
{
|
||
|
|
for(size_t i = 0 ; i < N ; i++)
|
||
|
|
data[i] = initValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Sets the read pointer's absolute position.
|
||
|
|
void SetReadPointer(int i) { readPointer = BoundIndex(i); }
|
||
|
|
|
||
|
|
/// Gets the read pointer's absolute position.
|
||
|
|
int GetReadPointer() const { return readPointer; }
|
||
|
|
|
||
|
|
/// Sets the write pointer's absolute position.
|
||
|
|
void SetWritePointer(int i) { writePointer = BoundIndex(i); }
|
||
|
|
|
||
|
|
/// Gets the write pointer's absolute position.
|
||
|
|
int GetWritePointer() const { return writePointer; }
|
||
|
|
|
||
|
|
/// Returns a reference to the element i in the buffer. The given index is absolute.
|
||
|
|
T & operator[](int i) { return data[i]; }
|
||
|
|
|
||
|
|
/// Returns a constant reference to the element i in the buffer. The given index is absolute.
|
||
|
|
T const& operator[](int i) const { return data[i]; }
|
||
|
|
|
||
|
|
/// Returns a reference to the element i in the buffer. The given index is relative to the read pointer.
|
||
|
|
T & operator()(int i) { return data[(readPointer + i) % (int)N]; }
|
||
|
|
|
||
|
|
/// Returns a constant reference to the element i in the buffer. The given index is relative to the read pointer.
|
||
|
|
T const& operator()(int i) const { return data[(readPointer + i) % (int)N]; }
|
||
|
|
|
||
|
|
/// Writes the value at the current write pointer's location and moves it forward by 1. If the write pointer catches back with the read pointer, false is returned.
|
||
|
|
bool WriteFwd(T const& v)
|
||
|
|
{
|
||
|
|
data[writePointer] = v;
|
||
|
|
writePointer = BoundIndex(writePointer + 1);
|
||
|
|
return writePointer != readPointer;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Writes the value at the current write pointer's location and moves it backwards by 1. If the write pointer catches back with the read pointer, false is returned.
|
||
|
|
bool WriteBck(T const& v)
|
||
|
|
{
|
||
|
|
data[writePointer] = v;
|
||
|
|
writePointer = BoundIndex(writePointer - 1);
|
||
|
|
return writePointer != readPointer;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Reads the value at the current read pointer's location and moves it forward by 1. If the read pointer is co-located with the the write pointer at the time of the read, false is returned.
|
||
|
|
bool ReadFwd(T & v)
|
||
|
|
{
|
||
|
|
v = data[readPointer];
|
||
|
|
int readPointerWhenRead = readPointer;
|
||
|
|
readPointer = BoundIndex(readPointer + 1);
|
||
|
|
return writePointer != readPointerWhenRead;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Reads the value at the current read pointer's location and moves it backwards by 1. If the read pointer is co-located with the the write pointer at the time of the read, false is returned.
|
||
|
|
bool ReadBck(T & v)
|
||
|
|
{
|
||
|
|
v = data[readPointer];
|
||
|
|
int readPointerWhenRead = readPointer;
|
||
|
|
readPointer = BoundIndex(readPointer - 1);
|
||
|
|
return writePointer != readPointerWhenRead;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Bounds the index between 0 and N;
|
||
|
|
static inline int BoundIndex(int i) { return (i >= 0) ? i % (int)N : ((int)N + (i % (int)N)) % (int)N; }
|
||
|
|
|
||
|
|
protected:
|
||
|
|
T *data;
|
||
|
|
int readPointer; //!< Read pointer location
|
||
|
|
int writePointer; //!< Write pointer location
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif
|