/tags/rel-0-1-0/FreeSpeech/data-flow/include/RotatingBuffer.h
# · C++ Header · 140 lines · 87 code · 22 blank · 31 comment · 10 complexity · 36bac89eb36f5bc49d739f7006319bbb MD5 · raw file
- // Copyright (C) 1999 Jean-Marc Valin
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2, or (at your option)
- // any later version.
- //
- // This program is distributed in the hope that it will be useful, but
- // WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this file. If not, write to the Free Software Foundation,
- // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- #ifndef ROTATING_BUFFER_H
- #define ROTATING_BUFFER_H
- #include "Object.h"
- #include "ObjectRef.h"
- #include "Exception.h"
- #include <typeinfo>
- #include "Buffer.h"
- #include <vector>
- /**A rotating buffer implementation.
- This buffer keeps the last N lines (frames) it has*/
- class RotatingBuffer : public Buffer {
- protected:
- ///Pointer to raw float
- mutable vector<ObjectRef> data ;
- ///The number N of lines kept
- int bufferLength ;
- public:
- ///Constructor, requires the vector length (vLength) and the buffer length (bLength)
- RotatingBuffer(int bLength)
- : data(bLength)
- , bufferLength (bLength)
- {
- for (int i=0;i<bLength;i++)
- data[i]=ObjectRef(new Object(Object::nil));
- }
- ///Copy constructor (not implemented, do we need one?)
- RotatingBuffer(const RotatingBuffer&);
- ///Indexing operator, also sets the indexed frame as being the current frame
- ObjectRef operator[] (int ind) const;
-
- ///Indexing operator, also sets the indexed frame as being the current frame
- ObjectRef & operator[] (int ind) ;
-
- ///Print
- virtual void printOn(ostream &out = cout) const;
- };
- class RotatingBufferException : public BaseException {
- public:
- ///The constructor with the parameters
- RotatingBufferException(const RotatingBuffer *_buffer, string _message, int _element)
- : buffer (_buffer)
- , message(_message)
- , element(_element)
- {}
- ///The print method
- virtual void print(ostream &out = cerr)
- {
- out<< typeid(RotatingBuffer).name() << " error: "<< message << ".\nElement " << element << endl;
- out << "Buffer is: \n";
- out << *buffer;
- }
- protected:
- ///the buffer that generated the error
- const RotatingBuffer *buffer;
- ///the error message
- string message;
- //The element for which the error occured
- int element;
- };
- inline ObjectRef & RotatingBuffer::operator[] (int ind)
- {
- if (ind > currentPos)
- {
- for (int i=currentPos+1;i<=ind;i++)
- data[ind % bufferLength] = Object::nilObject;
- currentPos = ind;
- }
- if (ind < 0 || ind <= currentPos-bufferLength)
- {
- //cerr << "ind = " << ind << endl;
- throw new RotatingBufferException (this, "trying to access non-existing element",ind);
- }
- return data[ind % bufferLength];
- }
- inline ObjectRef RotatingBuffer::operator[] (int ind) const
- {
- if (ind > currentPos)
- {
- for (int i=currentPos+1;i<=ind;i++)
- data[ind % bufferLength] = Object::nilObject;
- currentPos = ind;
- }
- if (ind < 0 || ind <= currentPos-bufferLength)
- {
- throw new RotatingBufferException (this, "trying to access non-existing element",ind);
- }
- return data[ind % bufferLength];
- }
- inline void RotatingBuffer::printOn(ostream &out) const
- {
- int i;
- //cerr << "printing... currentPos = " << currentPos << " bufferLength = " << bufferLength << endl;
- out << "<RotatingBuffer" << endl;
- for (i=max(0,currentPos-bufferLength+1);i<=currentPos;i++)
- {
- out << "< " << i << " < ";
- out << *((*this)[i]);
- out << " > > ";
- out << endl;
- }
- out << " > " << endl;
- }
- inline RotatingBuffer::RotatingBuffer(const RotatingBuffer&)
- {throw new RotatingBufferException(NULL,"use an ObjectRef instead",0);}
- #endif