Lunchbox  1.14.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
buffer.h
1 
2 /* Copyright (c) 2007-2016, Stefan Eilemann <eile@equalizergraphics.com>
3  * 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( nullptr ), _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( const Buffer& from );
55 
57  explicit Buffer( Buffer&& from );
58 
60  ~Buffer() { clear(); }
61 
63  void clear() { if( _data ) free( _data ); _data=0; _size=0; _maxSize=0; }
64 
70  T* pack();
71 
73  Buffer& operator = ( const Buffer& from );
74 
76  Buffer& operator = ( Buffer&& from );
77 
79  T& operator [] ( const uint64_t position )
80  { LBASSERT( _size > position ); return _data[ position ]; }
81 
83  const T& operator [] ( const uint64_t position ) const
84  { LBASSERT( _size > position ); return _data[ position ]; }
85 
93  T* resize( const uint64_t newSize );
94 
101  void grow( const uint64_t newSize );
102 
110  T* reserve( const uint64_t newSize );
111 
119  T* reset( const uint64_t newSize );
120 
122  void setZero() { ::lunchbox::setZero( _data, _size ); }
123 
125  void append( const T* data, const uint64_t size );
126 
128  void append( const T& element );
129 
131  void replace( const void* data, const uint64_t size );
132 
134  void replace( const Buffer& from ) { replace( from._data, from._size ); }
135 
137  void swap( Buffer& buffer );
138 
140  T* getData() { return _data; }
141 
143  const T* getData() const { return _data; }
144 
153  bool setSize( const uint64_t size );
154 
156  uint64_t getSize() const { return _size; }
157 
159  uint64_t getNumBytes() const { return _size * sizeof( T ); }
160 
162  bool isEmpty() const { return (_size==0); }
163 
165  uint64_t getMaxSize() const { return _maxSize; }
166 
167 private:
169  T* _data;
170 
172  uint64_t _size;
173 
175  uint64_t _maxSize;
176 };
177 }
178 
179 #include "buffer.ipp" // template implementation
180 
181 #endif //LUNCHBOX_BUFFER_H
bool isEmpty() const
Definition: buffer.h:162
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:88
Buffer & operator=(const Buffer &from)
Assignment operator, copies data from Buffer.
Definition: buffer.ipp:50
void append(const T *data, const uint64_t size)
Append elements to the buffer, increasing the size.
Definition: buffer.ipp:106
uint64_t getMaxSize() const
Definition: buffer.h:165
bool setSize(const uint64_t size)
Set the size of the buffer without changing its allocation.
Definition: buffer.ipp:147
uint64_t getNumBytes() const
Definition: buffer.h:159
~Buffer()
Destruct the buffer.
Definition: buffer.h:60
void swap(Buffer &buffer)
Swap the buffer contents with another Buffer.
Definition: buffer.ipp:132
const T * getData() const
Definition: buffer.h:143
T * getData()
Definition: buffer.h:140
void clear()
Flush the buffer, deleting all data.
Definition: buffer.h:63
void grow(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:82
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
void replace(const Buffer &from)
Replace the existing data.
Definition: buffer.h:134
T * resize(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:68
void replace(const void *data, const uint64_t size)
Replace the existing data with new data.
Definition: buffer.ipp:122
T * reset(const uint64_t newSize)
Set the buffer size and malloc enough memory.
Definition: buffer.ipp:98
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:79
uint64_t getSize() const
Definition: buffer.h:156
void setZero()
Set the buffer content to 0.
Definition: buffer.h:122
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:39