libcommonc++  0.7
CircularBuffer.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_CircularBuffer_hxx
24 #define __ccxx_CircularBuffer_hxx
25 
26 #include <commonc++/Common.h++>
30 #include <commonc++/Stream.h++>
31 
32 #include <algorithm>
33 #include <cstdlib>
34 #include <cstring>
35 
36 #ifdef CCXX_OS_POSIX
37 #include <sys/uio.h>
38 #endif
39 
40 namespace ccxx {
41 
54 template <typename T> class CircularBuffer : public AbstractBuffer<T>
55 {
56  public:
57 
63  CircularBuffer(uint_t size);
64 
67 
69  void clear();
70 
76  void setSize(uint_t newSize);
77 
85  uint_t write(const T* buf, uint_t count);
86 
95  uint_t write(Buffer<T>& buffer, uint_t count = 0);
96 
105  uint_t write(Stream& stream, uint_t count = 0);
106 
114  uint_t read(T* buf, uint_t count);
115 
124  uint_t read(Buffer<T>& buffer, uint_t count = 0);
125 
134  uint_t read(Stream& stream, uint_t count = 0);
135 
150  uint_t peek(const T& value, uint_t maxlen, bool& found,
151  bool resetPeek = true);
152 
163  uint_t fill(const T& value, uint_t count);
164 
171  uint_t getReadExtent() const;
172 
180  uint_t getWriteExtent() const;
181 
183  inline uint_t getRemaining() const
184  { return(_avail); }
185 
190  inline uint_t getPeekRemaining() const
191  { return(_peekAvail); }
192 
194  inline bool isEmpty() const
195  { return(_avail == 0); }
196 
198  inline bool isFull() const
199  { return(this->_size == _avail); }
200 
202  inline uint_t getFree() const
203  { return(this->_size - _avail); }
204 
206  inline T* getReadPos()
207  { return(_readPos); }
208 
210  inline T* getWritePos()
211  { return(_writePos); }
212 
214  inline T* getPeekPos()
215  { return(_peekPos); }
216 
225  T* advanceReadPos(uint_t count);
226 
236  T* advanceWritePos(uint_t count);
237 
245  inline T* advanceReadPos()
246  { return(advanceReadPos(getReadExtent())); }
247 
255  inline T* advanceWritePos()
256  { return(advanceWritePos(getWriteExtent())); }
257 
267  T* advancePeekPos(uint_t count);
268 
270  void resetPeekPos();
271 
277  bool isPartialWrite() const
278  { return(_writeShift > 0); }
279 
285  bool isPartialRead() const
286  { return(_readShift > 0); }
287 
293  void markReadPos();
294 
299  void rewindReadPos();
300 
306  void markWritePos();
307 
312  void rewindWritePos();
313 
322  bool setReadPosFromMark(uint_t count);
323 
332  bool setWritePosFromMark(uint_t count);
333 
334  private:
335 
336  T* _end;
337  T* _readPos;
338  T* _writePos;
339  T* _peekPos;
340  T* _readMark;
341  T* _writeMark;
342  uint_t _avail;
343  uint_t _peekAvail;
344  uint_t _readShift;
345  uint_t _writeShift;
346 
347  CCXX_COPY_DECLS(CircularBuffer);
348 };
349 
350 #include <commonc++/CircularBufferImpl.h++>
351 
354 
355 } // namespace ccxx
356 
357 #endif // __ccxx_CircularBuffer_hxx
T * advanceReadPos()
Advance the read position by the number of elements in the read extent, wrapping to the beginning of ...
Definition: CircularBuffer.h++:245
uint_t getPeekRemaining() const
Get the number of elements available to be peeked, that is, the number of elements between the peek p...
Definition: CircularBuffer.h++:190
uint_t getFree() const
Get the number of elements available to be written to the buffer.
Definition: CircularBuffer.h++:202
void rewindWritePos()
Rewinds the write position to the write mark, and clears the write mark.
bool setWritePosFromMark(uint_t count)
Moves the write position relative to the write mark.
An unbuffered I/O stream.
Definition: Stream.h++:60
bool isPartialRead() const
Determine if a partially-read element is in the buffer.
Definition: CircularBuffer.h++:285
void markWritePos()
Marks the current write position.
T * getWritePos()
Get the current write position.
Definition: CircularBuffer.h++:210
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
void clear()
Clear the buffer.
uint_t read(T *buf, uint_t count)
Read data from the buffer into an array.
bool setReadPosFromMark(uint_t count)
Moves the read position relative to the read mark.
T * getReadPos()
Get the current read position.
Definition: CircularBuffer.h++:206
uint_t getReadExtent() const
Get the read extent.
T * advanceWritePos()
Advance the write position by the number of elements in the write extent, wrapping to the beginning o...
Definition: CircularBuffer.h++:255
~CircularBuffer()
Destructor.
uint_t getWriteExtent() const
Get the write extent.
T * advancePeekPos(uint_t count)
Advance the peek position by the given number of elements, wrapping to the beginning of the buffer if...
void markReadPos()
Marks the current read position.
uint_t write(const T *buf, uint_t count)
Write data from an array into the buffer.
bool isFull() const
Determine if the buffer is full.
Definition: CircularBuffer.h++:198
bool isPartialWrite() const
Determine if a partially-written element is in the buffer.
Definition: CircularBuffer.h++:277
void setSize(uint_t newSize)
Resize the buffer.
A buffer for storing a contiguous sequence of elements.
Definition: Buffer.h++:44
uint_t fill(const T &value, uint_t count)
Fill the buffer with the given value.
A circular buffer that can be used to efficiently transfer data between buffers and/or streams...
Definition: CircularBuffer.h++:54
uint_t _size
The size of the buffer.
Definition: AbstractBuffer.h++:98
bool isEmpty() const
Determine if the buffer is empty.
Definition: CircularBuffer.h++:194
An abstract base class for buffers.
Definition: AbstractBuffer.h++:38
uint_t getRemaining() const
Get the number of elements available to be read from the buffer.
Definition: CircularBuffer.h++:183
CircularBuffer(uint_t size)
Construct a new CircularBuffer with the given size.
void rewindReadPos()
Rewinds the read position to the read mark, and clears the read mark.
Definition: AllocationMap.c++:25
void resetPeekPos()
Reset the peek position to the read position.
uint_t peek(const T &value, uint_t maxlen, bool &found, bool resetPeek=true)
Scan forward from the current peek position for an element equal to the given value.
T * getPeekPos()
Get the current peek position.
Definition: CircularBuffer.h++:214
CircularBuffer< byte_t > CircularByteBuffer
Definition: CircularBuffer.h++:352
CircularBuffer< char > CircularCharBuffer
Definition: CircularBuffer.h++:353