Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buffer.h
1 
2 /* Copyright (c) 2007-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2011-2012, Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef LUNCHBOX_BUFFER_H
20 #define LUNCHBOX_BUFFER_H
21 
22 #include <lunchbox/debug.h> // LBASSERT macro
23 #include <lunchbox/os.h> // setZero used inline
24 #include <lunchbox/types.h>
25 
26 #include <cstdlib> // for malloc
27 #include <cstring> // for memcpy
28 
29 namespace lunchbox
30 {
43 template< class T > class Buffer
44 {
45 public:
47  Buffer() : _data(0), _size(0), _maxSize(0) {}
48 
50  explicit Buffer( const uint64_t size ) : _data(0), _size(0), _maxSize(0)
51  { reset( size ); }
52 
54  explicit Buffer( Buffer& from );
55 
57  ~Buffer() { clear(); }
58 
60  void clear() { if( _data ) free( _data ); _data=0; _size=0; _maxSize=0; }
61 
67  T* pack();
68 
70  Buffer& operator = ( const Buffer& from );
71 
73  T& operator [] ( const uint64_t position )
74  { LBASSERT( _size > position ); return _data[ position ]; }
75 
77  const T& operator [] ( const uint64_t position ) const
78  { LBASSERT( _size > position ); return _data[ position ]; }
79 
87  T* resize( const uint64_t newSize );
88 
95  void grow( const uint64_t newSize );
96 
104  T* reserve( const uint64_t newSize );
105 
113  T* reset( const uint64_t newSize );
114 
116  void setZero() { ::lunchbox::setZero( _data, _size ); }
117 
119  void append( const T* data, const uint64_t size );
120 
122  void append( const T& element );
123 
125  void replace( const void* data, const uint64_t size );
126 
128  void replace( const Buffer& from ) { replace( from._data, from._size ); }
129 
131  void swap( Buffer& buffer );
132 
134  T* getData() { return _data; }
135 
137  const T* getData() const { return _data; }
138 
147  bool setSize( const uint64_t size );
148 
150  uint64_t getSize() const { return _size; }
151 
153  uint64_t getNumBytes() const { return _size * sizeof( T ); }
154 
156  bool isEmpty() const { return (_size==0); }
157 
159  uint64_t getMaxSize() const { return _maxSize; }
160 
161 private:
163  T* _data;
164 
166  uint64_t _size;
167 
169  uint64_t _maxSize;
170 };
171 }
172 
173 #include "buffer.ipp" // template implementation
174 
175 #endif //LUNCHBOX_BUFFER_H
bool isEmpty() const
Definition: buffer.h:156
Buffer(const uint64_t size)
Construct a new buffer of the given size.
Definition: buffer.h:50
Basic type definitions not provided by the operating system.
T * reserve(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:64
Buffer & operator=(const Buffer &from)
Assignment operator, copies data from Buffer.
Definition: buffer.ipp:38
void append(const T *data, const uint64_t size)
Append elements to the buffer, increasing the size.
Definition: buffer.ipp:82
uint64_t getMaxSize() const
Definition: buffer.h:159
bool setSize(const uint64_t size)
Set the size of the buffer without changing its allocation.
Definition: buffer.ipp:124
uint64_t getNumBytes() const
Definition: buffer.h:153
~Buffer()
Destruct the buffer.
Definition: buffer.h:57
void swap(Buffer &buffer)
Swap the buffer contents with another Buffer.
Definition: buffer.ipp:109
const T * getData() const
Definition: buffer.h:137
T * getData()
Definition: buffer.h:134
void clear()
Flush the buffer, deleting all data.
Definition: buffer.h:60
void grow(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:58
void replace(const Buffer &from)
Replace the existing data.
Definition: buffer.h:128
T * resize(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:44
void replace(const void *data, const uint64_t size)
Replace the existing data with new data.
Definition: buffer.ipp:99
T * reset(const uint64_t newSize)
Set the buffer size and malloc enough memory.
Definition: buffer.ipp:74
Buffer()
Construct a new, empty buffer.
Definition: buffer.h:47
T & operator[](const uint64_t position)
Direct access to the element at the given index.
Definition: buffer.h:73
uint64_t getSize() const
Definition: buffer.h:150
void setZero()
Set the buffer content to 0.
Definition: buffer.h:116
A simple memory buffer with some helper functions.
Definition: buffer.h:43
T * pack()
Tighten the allocated memory to the size of the buffer.
Definition: buffer.ipp:27