libcommonc++  0.7
Buffer.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_Buffer_hxx
24 #define __ccxx_Buffer_hxx
25 
26 #include <commonc++/Common.h++>
29 
30 #include <cstring>
31 #include <algorithm>
32 
33 namespace ccxx {
34 
44 template <typename T> class Buffer : public AbstractBuffer<T>
45 {
46  public:
47 
53  Buffer(uint_t size);
54 
62  Buffer(T* data, size_t size, bool takeOwnership = true);
63 
65  virtual ~Buffer();
66 
71  virtual void clear();
72 
77  void flip();
78 
80  void rewind();
81 
83  void setLimit(uint_t limit);
84 
86  void setPosition(uint_t pos);
87 
95  uint_t bump(uint_t delta);
96 
105  uint_t skip(uint_t delta);
106 
115  bool put(const T& item);
116 
125  bool get(T * item);
126 
136  bool put(const T* items, uint_t count);
137 
146  void fill(const T& item, uint_t count = 0);
147 
157  bool get(T* items, uint_t count);
158 
167  int peek(const T& item) const;
168 
170  inline T* getPointer()
171  { return(this->_data + this->_pos); }
172 
174  inline const T* getPointer() const
175  { return(this->_data + this->_pos); }
176 
178  inline uint_t getLimit() const
179  { return(_limit); }
180 
186  inline uint_t getRemaining() const
187  { return(_limit - _pos); }
188 
195  inline bool hasRemaining() const
196  { return(_limit > _pos); }
197 
199  inline uint_t getPosition() const
200  { return(_pos); }
201 
208  T& operator[](int index);
209 
216  T operator[](int index) const;
217 
218  protected:
219 
224 
225  private:
226 
227  CCXX_COPY_DECLS(Buffer);
228 };
229 
230 #include <commonc++/BufferImpl.h++>
231 
234 
235 } // namespace ccxx
236 
237 #endif // __ccxx_Buffer_hxx
void rewind()
Rewind the buffer.
T * getPointer()
Get a pointer to the element at the current position.
Definition: Buffer.h++:170
Buffer< byte_t > ByteBuffer
Definition: Buffer.h++:232
Buffer< char > CharBuffer
Definition: Buffer.h++:233
void fill(const T &item, uint_t count=0)
Fill the buffer with a given item.
void setLimit(uint_t limit)
Set the limit.
uint_t _pos
The position.
Definition: Buffer.h++:223
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
void flip()
Flip the buffer.
T & operator[](int index)
Get a reference to the element at the specified index.
uint_t getRemaining() const
Get the number of elements available to be read or written.
Definition: Buffer.h++:186
bool hasRemaining() const
Test if there are any elements available to be read or written.
Definition: Buffer.h++:195
const T * getPointer() const
Get a pointer to the next element to be read or written.
Definition: Buffer.h++:174
uint_t bump(uint_t delta)
Bump (advance) the position by the given number of elements, or to the limit, whichever occurs first...
bool put(const T &item)
Copy an item into the buffer at the current position and bump the position by 1.
uint_t getLimit() const
Get the limit.
Definition: Buffer.h++:178
void setPosition(uint_t pos)
Set the position.
T * _data
A pointer to the raw buffer.
Definition: AbstractBuffer.h++:96
A buffer for storing a contiguous sequence of elements.
Definition: Buffer.h++:44
virtual ~Buffer()
Destructor.
int peek(const T &item) const
Scan forward from the current position for an element equal to the given value.
virtual void clear()
Clear the buffer.
Buffer(uint_t size)
Construct a new Buffer with the given size.
uint_t skip(uint_t delta)
Advance the position by the given number of elements, or to the limit, whichever occurs first...
An abstract base class for buffers.
Definition: AbstractBuffer.h++:38
uint_t _limit
The limit.
Definition: Buffer.h++:221
Definition: AllocationMap.c++:25
uint_t getPosition() const
Get the position of the next element to be read or written.
Definition: Buffer.h++:199