34 #ifndef GRT_CIRCULAR_BUFFER_HEADER
35 #define GRT_CIRCULAR_BUFFER_HEADER
38 #include "GRTTypedefs.h"
51 errorLog.setProceedingText(
"[ERROR: CircularBuffer]");
53 numValuesInBuffer = 0;
65 errorLog.setProceedingText(
"[ERROR: CircularBuffer]");
67 this->numValuesInBuffer = 0;
70 this->bufferInit =
false;
73 this->bufferInit = rhs.bufferInit;
74 this->bufferSize = rhs.bufferSize;
75 this->numValuesInBuffer = rhs.numValuesInBuffer;
76 this->buffer.resize( rhs.bufferSize );
77 for(
unsigned int i=0; i<rhs.bufferSize; i++){
78 this->buffer[i] = rhs.buffer[ i ];
80 this->readPtr = rhs.readPtr;
81 this->writePtr = rhs.writePtr;
91 errorLog.setProceedingText(
"[ERROR: CircularBuffer]");
115 if( rhs.bufferInit ){
116 this->bufferInit = rhs.bufferInit;
117 this->bufferSize = rhs.bufferSize;
118 this->numValuesInBuffer = rhs.numValuesInBuffer;
119 this->buffer.resize( rhs.bufferSize );
120 for(
unsigned int i=0; i<rhs.bufferSize; i++){
121 this->buffer[i] = rhs.buffer[ i ];
123 this->readPtr = rhs.readPtr;
124 this->writePtr = rhs.writePtr;
137 return buffer[ (readPtr + index) % bufferSize ];
147 return buffer[ (readPtr + index) % bufferSize ];
157 return buffer[ index ];
167 return buffer[ index ];
176 bool resize(
const unsigned int newBufferSize){
177 return resize(newBufferSize,T());
187 bool resize(
const unsigned int newBufferSize,
const T &defaultValue){
192 if( newBufferSize == 0 )
return false;
195 bufferSize = newBufferSize;
196 buffer.resize(bufferSize,defaultValue);
197 numValuesInBuffer = 0;
216 errorLog <<
"Can't push_back value to circular buffer as the buffer has not been initialized!" << std::endl;
221 buffer[ writePtr ] = value;
225 writePtr = writePtr % bufferSize;
228 if( ++numValuesInBuffer > bufferSize ){
229 numValuesInBuffer = bufferSize;
233 readPtr = readPtr % bufferSize;
250 for(
unsigned int i=0; i<bufferSize; i++){
262 numValuesInBuffer = 0;
272 numValuesInBuffer = 0;
285 GRT_DEPRECATED_MSG(
"Use getData() instead!", std::vector< T > getDataAsVector()
const );
301 for(
unsigned int i=0; i<numValuesInBuffer; i++){
302 data[i] = (*this)[i];
322 bool getBufferFilled()
const {
return bufferInit ? numValuesInBuffer==bufferSize :
false; }
329 unsigned int getSize()
const {
return bufferInit ? bufferSize : 0; }
358 if( !bufferInit )
return T();
359 return buffer[ (readPtr + numValuesInBuffer - 1) % bufferSize ];
364 unsigned int bufferSize;
365 unsigned int numValuesInBuffer;
366 unsigned int readPtr;
367 unsigned int writePtr;
378 #endif //GRT_CIRCULAR_BUFFER_HEADER
bool push_back(const T &value)
const T & operator[](const unsigned int &index) const
bool getBufferFilled() const
CircularBuffer & operator=(const CircularBuffer &rhs)
CircularBuffer(const CircularBuffer &rhs)
GRT_DEPRECATED_MSG("Use getData() instead!", std::vector< T > getDataAsVector() const )
unsigned int getWritePointerPosition() const
virtual ~CircularBuffer()
T & operator[](const unsigned int &index)
unsigned int getNumValuesInBuffer() const
unsigned int getReadPointerPosition() const
CircularBuffer(unsigned int bufferSize)
bool resize(const unsigned int newBufferSize, const T &defaultValue)
const T & operator()(const unsigned int &index) const
bool setAllValues(const T &value)
T & operator()(const unsigned int &index)
Vector< T > getData(const bool rawBuffer=false) const
unsigned int getSize() const
bool resize(const unsigned int newBufferSize)