27 #ifndef GRT_CIRCULAR_BUFFER_HEADER 28 #define GRT_CIRCULAR_BUFFER_HEADER 31 #include "GRTTypedefs.h" 52 errorLog.
setKey(
"[ERROR CircularBuffer]");
54 numValuesInBuffer = 0;
66 errorLog.
setKey(
"[ERROR CircularBuffer]");
68 this->numValuesInBuffer = 0;
71 this->bufferInit =
false;
74 this->bufferInit = rhs.bufferInit;
75 this->bufferSize = rhs.bufferSize;
76 this->numValuesInBuffer = rhs.numValuesInBuffer;
77 this->buffer.resize( rhs.bufferSize );
78 for(
unsigned int i=0; i<rhs.bufferSize; i++){
79 this->buffer[i] = rhs.buffer[ i ];
81 this->readPtr = rhs.readPtr;
82 this->writePtr = rhs.writePtr;
92 errorLog.
setKey(
"[ERROR CircularBuffer]");
116 if( rhs.bufferInit ){
117 this->bufferInit = rhs.bufferInit;
118 this->bufferSize = rhs.bufferSize;
119 this->numValuesInBuffer = rhs.numValuesInBuffer;
120 this->buffer.resize( rhs.bufferSize );
121 for(
unsigned int i=0; i<rhs.bufferSize; i++){
122 this->buffer[i] = rhs.buffer[ i ];
124 this->readPtr = rhs.readPtr;
125 this->writePtr = rhs.writePtr;
138 return buffer[ (readPtr + index) % bufferSize ];
148 return buffer[ (readPtr + index) % bufferSize ];
158 return buffer[ index ];
168 return buffer[ index ];
177 bool resize(
const unsigned int newBufferSize){
178 return resize(newBufferSize,T());
188 bool resize(
const unsigned int newBufferSize,
const T &defaultValue){
193 if( newBufferSize == 0 )
return false;
196 bufferSize = newBufferSize;
197 buffer.resize(bufferSize,defaultValue);
198 numValuesInBuffer = 0;
217 errorLog <<
"Can't push_back value to circular buffer as the buffer has not been initialized!" << std::endl;
222 buffer[ writePtr ] = value;
226 writePtr = writePtr % bufferSize;
229 if( ++numValuesInBuffer > bufferSize ){
230 numValuesInBuffer = bufferSize;
234 readPtr = readPtr % bufferSize;
251 for(
unsigned int i=0; i<bufferSize; i++){
263 numValuesInBuffer = 0;
273 numValuesInBuffer = 0;
286 GRT_DEPRECATED_MSG(
"Use getData() instead!", std::vector< T > getDataAsVector()
const );
302 for(
unsigned int i=0; i<numValuesInBuffer; i++){
303 data[i] = (*this)[i];
323 bool getBufferFilled()
const {
return bufferInit ? numValuesInBuffer==bufferSize :
false; }
330 unsigned int getSize()
const {
return bufferInit ? bufferSize : 0; }
359 if( !bufferInit )
return T();
360 return buffer[ (readPtr + numValuesInBuffer - 1) % bufferSize ];
365 unsigned int bufferSize;
366 unsigned int numValuesInBuffer;
367 unsigned int readPtr;
368 unsigned int writePtr;
379 #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)
virtual bool setKey(const std::string &key)
sets the key that gets written at the start of each message, this will be written in the format 'key ...
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)