CircularBuffer.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_CircularBuffer_hxx
00024 #define __ccxx_CircularBuffer_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/AbstractBuffer.h++>
00028 #include <commonc++/IOException.h++>
00029 #include <commonc++/MemoryBlock.h++>
00030 #include <commonc++/Stream.h++>
00031 
00032 #include <algorithm>
00033 #include <cstdlib>
00034 #include <cstring>
00035 
00036 #ifdef CCXX_OS_POSIX
00037 #include <sys/uio.h>
00038 #endif
00039 
00040 namespace ccxx {
00041 
00054 template <typename T> class CircularBuffer : public AbstractBuffer<T>
00055 {
00056   public:
00057 
00062   CircularBuffer(size_t size);
00063 
00065   ~CircularBuffer() throw();
00066 
00068   void clear() throw();
00069 
00074   virtual void setSize(size_t newSize);
00075 
00082   size_t write(const T *buf, size_t count) throw();
00083 
00091   size_t write(Buffer<T>& buffer, size_t count = 0) throw();
00092 
00100   size_t write(Stream& stream, size_t count = 0) throw(IOException);
00101 
00108   size_t read(T *buf, size_t count) throw();
00109 
00117   size_t read(Buffer<T>& buffer, size_t count = 0) throw();
00118 
00126   size_t read(Stream& stream, size_t count = 0) throw(IOException);
00127 
00141   size_t peek(const T &value, size_t maxlen, bool &found,
00142               bool resetPeek = true)
00143     throw();
00144 
00154   size_t fill(const T &value, size_t count) throw();
00155 
00162   size_t getReadExtent() const throw();
00163 
00170   size_t getWriteExtent() const throw();
00171 
00173   inline size_t getRemaining() const throw()
00174   { return(_avail); }
00175 
00179   inline size_t getPeekRemaining() const throw()
00180   { return(_peekAvail); }
00181 
00183   inline bool isEmpty() const throw()
00184   { return(_avail == 0); }
00185 
00187   inline bool isFull() const throw()
00188   { return(this->_size == _avail); }
00189 
00191   inline size_t getFree() const throw()
00192   { return(this->_size - _avail); }
00193 
00195   inline T *getReadPos() throw()
00196   { return(_readPos); }
00197 
00199   inline T *getWritePos() throw()
00200   { return(_writePos); }
00201 
00203   inline T *getPeekPos() throw()
00204   { return(_peekPos); }
00205 
00213   T *advanceReadPos(size_t count) throw();
00214 
00223   T *advanceWritePos(size_t count) throw();
00224 
00231   inline T *advanceReadPos() throw()
00232   { return(advanceReadPos(getReadExtent())); }
00233 
00240   inline T *advanceWritePos() throw()
00241   { return(advanceWritePos(getWriteExtent())); }
00242 
00251   T *advancePeekPos(size_t count) throw();
00252 
00254   void resetPeekPos() throw();
00255 
00260   bool isPartialWrite() const throw()
00261   { return(_writeShift > 0); }
00262 
00267   bool isPartialRead() const throw()
00268   { return(_readShift > 0); }
00269 
00270   private:
00271 
00272   T *_end;
00273   T *_readPos;
00274   T *_writePos;
00275   T *_peekPos;
00276   size_t _avail;
00277   size_t _peekAvail;
00278   size_t _readShift;
00279   size_t _writeShift;
00280 
00281   CCXX_COPY_DECLS(CircularBuffer);
00282 };
00283 
00284 #include <commonc++/CircularBufferImpl.h++>
00285 
00286 typedef CircularBuffer<byte_t> CircularByteBuffer;
00287 typedef CircularBuffer<char> CircularCharBuffer;
00288 
00289 }; // namespace ccxx
00290 
00291 #endif // __ccxx_CircularBuffer_hxx
00292 
00293 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3