Lunchbox  1.17.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>
44 class Buffer
45 {
46 public:
49  : _data(nullptr)
50  , _size(0)
51  , _maxSize(0)
52  {
53  }
54 
56  explicit Buffer(const uint64_t size)
57  : _data(0)
58  , _size(0)
59  , _maxSize(0)
60  {
61  reset(size);
62  }
63 
65  Buffer(const Buffer& from);
66 
68  Buffer(Buffer&& from);
69 
71  ~Buffer() { clear(); }
73  void clear()
74  {
75  if (_data)
76  free(_data);
77  _data = 0;
78  _size = 0;
79  _maxSize = 0;
80  }
81 
87  T* pack();
88 
90  Buffer& operator=(const Buffer& from);
91 
93  Buffer& operator=(Buffer&& from);
94 
96  T& operator[](const uint64_t position)
97  {
98  LBASSERT(_size > position);
99  return _data[position];
100  }
101 
103  const T& operator[](const uint64_t position) const
104  {
105  LBASSERT(_size > position);
106  return _data[position];
107  }
108 
116  T* resize(const uint64_t newSize);
117 
124  void grow(const uint64_t newSize);
125 
133  T* reserve(const uint64_t newSize);
134 
142  T* reset(const uint64_t newSize);
143 
145  void setZero() { ::lunchbox::setZero(_data, _size); }
147  void append(const T* data, const uint64_t size);
148 
150  void append(const T& element);
151 
153  void replace(const void* data, const uint64_t size);
154 
156  void replace(const Buffer& from) { replace(from._data, from._size); }
158  void swap(Buffer& buffer);
159 
161  T* getData() { return _data; }
163  const T* getData() const { return _data; }
172  bool setSize(const uint64_t size);
173 
175  uint64_t getSize() const { return _size; }
177  uint64_t getNumBytes() const { return _size * sizeof(T); }
179  bool isEmpty() const { return (_size == 0); }
181  uint64_t getMaxSize() const { return _maxSize; }
182 private:
184  T* _data;
185 
187  uint64_t _size;
188 
190  uint64_t _maxSize;
191 };
192 }
193 
194 #include "buffer.ipp" // template implementation
195 
196 #endif // LUNCHBOX_BUFFER_H
bool isEmpty() const
Definition: buffer.h:179
Buffer(const uint64_t size)
Construct a new buffer of the given size.
Definition: buffer.h:56
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:95
Buffer & operator=(const Buffer &from)
Assignment operator, copies data from Buffer.
Definition: buffer.ipp:54
void append(const T *data, const uint64_t size)
Append elements to the buffer, increasing the size.
Definition: buffer.ipp:114
uint64_t getMaxSize() const
Definition: buffer.h:181
bool setSize(const uint64_t size)
Set the size of the buffer without changing its allocation.
Definition: buffer.ipp:158
uint64_t getNumBytes() const
Definition: buffer.h:177
~Buffer()
Destruct the buffer.
Definition: buffer.h:71
const T & operator[](const uint64_t position) const
Direct const access to an element.
Definition: buffer.h:103
void swap(Buffer &buffer)
Swap the buffer contents with another Buffer.
Definition: buffer.ipp:142
const T * getData() const
Definition: buffer.h:163
T * getData()
Definition: buffer.h:161
void clear()
Flush the buffer, deleting all data.
Definition: buffer.h:73
void grow(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:88
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
void replace(const Buffer &from)
Replace the existing data.
Definition: buffer.h:156
T * resize(const uint64_t newSize)
Ensure that the buffer contains at least newSize elements.
Definition: buffer.ipp:73
void replace(const void *data, const uint64_t size)
Replace the existing data with new data.
Definition: buffer.ipp:131
T * reset(const uint64_t newSize)
Set the buffer size and malloc enough memory.
Definition: buffer.ipp:106
Buffer()
Construct a new, empty buffer.
Definition: buffer.h:48
T & operator[](const uint64_t position)
Direct access to the element at the given index.
Definition: buffer.h:96
uint64_t getSize() const
Definition: buffer.h:175
void setZero()
Set the buffer content to 0.
Definition: buffer.h:145
A simple memory buffer with some helper functions.
Definition: buffer.h:44
T * pack()
Tighten the allocated memory to the size of the buffer.
Definition: buffer.ipp:43