9 #include <boost/thread/mutex.hpp>
15 template <
typename T,
typename U>
27 int GetNumBuffers ()
const {
return (
int)_buffers.size(); }
28 int GetBufferSize ()
const {
return _bufferSize; }
29 int GetCurrentBuffer ()
const {
return _currentBuffer; }
30 int GetFirstBufferIndex ()
const {
return _firstBufferIndex; }
31 int GetNumBuffersInRing ()
const {
return max(_buffers.size() - _firstBufferIndex, 0); }
46 bool Subtract (
int sourceIndex,
int destIndex);
54 std::vector<std::vector<T>> _buffers;
64 int _firstBufferIndex;
69 void AddOrDeleteBuffers (
int numTotalBuffers);
75 bool UsingSameType()
const {
return (std::is_same<T, U>::value); }
85 template <
typename T,
typename U>
95 template <
typename T,
typename U>
99 _bufferSize = sizeOfBuffer;
108 template <
typename T,
typename U>
112 CreateBuffers(
int bufferSize,
int numBuffers);
118 template <
typename T,
typename U>
124 _firstBufferIndex = 0;
130 template <
typename T,
typename U>
135 _firstBufferIndex = 0;
142 template <
typename T,
typename U>
145 if (value < _buffers.size())
147 _currentBuffer = value;
154 template <
typename T,
typename U>
157 for (
int i = (
int)_buffers.size() - 1; i >= 0; i--)
170 template <
typename T,
typename U>
176 boost::mutex::scoped_lock lock(_mutex);
177 if (bufferSize == _bufferSize && numBuffers == _buffers.size())
180 if (bufferSize <= 0 || numBuffers <= 0)
184 else if (bufferSize == _bufferSize)
186 AddOrDeleteBuffers(numBuffers);
191 _bufferSize = bufferSize;
193 for (
int i = 0; i < numBuffers; i++)
195 _buffers.push_back(vector<T>(_bufferSize));
209 template <
typename T,
typename U>
212 boost::mutex::scoped_lock lock(_mutex);
220 template <
typename T,
typename U>
223 boost::mutex::scoped_lock lock(_mutex);
224 _currentBuffer = _firstBufferIndex;
225 return _firstBufferIndex;
232 template <
typename T,
typename U>
235 int currentBufferCount = (int)_buffers.size();
237 if (numTotalBuffers > currentBufferCount)
239 for (
int i = 0; i < numTotalBuffers - currentBufferCount; i++)
241 _buffers.push_back(vector<T>(_bufferSize));
248 for (
int i = currentBufferCount - 1; i >= numTotalBuffers; i--)
251 _buffers.erase(_buffers.begin() + i);
261 template <
typename T,
typename U>
264 boost::mutex::scoped_lock lock(_mutex);
265 if (index < _buffers.size())
267 return _buffers[index].data();
276 template <
typename T,
typename U>
279 boost::mutex::scoped_lock lock(_mutex);
282 if (_currentBuffer >= _buffers.size())
285 _currentBuffer = _firstBufferIndex;
287 return _currentBuffer;
296 template <
typename T,
typename U>
300 boost::mutex::scoped_lock lock(_mutex);
302 if (index < _buffers.size())
306 memcpy(_buffers[index].data(), buffer, _bufferSize *
sizeof(T));
310 for (
int i = 0; i < _bufferSize; i++)
312 _buffers[index][i] =
static_cast<T
>(*(buffer + i));
327 template <
typename T,
typename U>
331 boost::mutex::scoped_lock lock(_mutex);
333 if (index < _buffers.size())
337 for (
int i = 0; i < _bufferSize; i++)
339 _buffers[index][i] = *(buffer + i) /
static_cast<T
>(divisor);
344 for (
int i = 0; i < _bufferSize; i++)
346 _buffers[index][i] =
static_cast<T
>(*(buffer + i) / divisor);
361 template <
typename T,
typename U>
365 boost::mutex::scoped_lock lock(_mutex);
366 if (index < _buffers.size())
370 memcpy(buffer, _buffers[index].data(), _bufferSize *
sizeof(T));
374 for (
int i = 0; i < _bufferSize; i++)
376 *(buffer + i) =
static_cast<U
>(_buffers[index][i]);
391 template <
typename T,
typename U>
395 boost::mutex::scoped_lock lock(_mutex);
397 if (index < _buffers.size())
401 std::fill_n(_buffers[index].begin(), _bufferSize, value);
405 std::fill_n(_buffers[index].begin(), _bufferSize,
static_cast<T
>(value));
419 template <
typename T,
typename U>
423 boost::mutex::scoped_lock lock(_mutex);
425 if (sourceIndex < (
int)_buffers.size() && destIndex < (
int)_buffers.size())
427 for (
int i = 0; i < _bufferSize; i++)
429 _buffers[destIndex][i] += _buffers[sourceIndex][i];
442 template <
typename T,
typename U>
446 boost::mutex::scoped_lock lock(_mutex);
448 if (sourceIndex < _buffers.size() && destIndex < _buffers.size())
450 for (
int i = 0; i < _bufferSize; i++)
452 _buffers[destIndex][i] -= _buffers[sourceIndex][i];
463 template <
typename T,
typename U>
466 boost::mutex::scoped_lock lock(_mutex);
467 _firstBufferIndex = value;
468 if (_firstBufferIndex > _currentBuffer + 1)
470 _currentBuffer = _firstBufferIndex - 1;
A general purpose templated class that holds a programmable number of buffers, each the same size.
Definition: SameSizeBuffers.h:17
bool CopyBufferIn(U *buffer, int index)
Copies the buffer supplied to the buffer specified by the index.
Definition: SameSizeBuffers.h:297
void Clear()
Deletes all buffers and the pointers to them.
Definition: SameSizeBuffers.h:210
bool SetBufferToValue(int index, U value)
Sets each element in the buffer to value supplied.
Definition: SameSizeBuffers.h:392
void SetCurrentBuffer(int value)
Sets the value of the current buffer.
Definition: SameSizeBuffers.h:143
bool AddTogether(int sourceIndex, int destIndex)
Adds the source and destination buffers together and places the result in the destination.
Definition: SameSizeBuffers.h:420
bool CreateBuffers(int bufferSize, int numBuffers)
Creates the buffers specified by the supplied parameters.
Definition: SameSizeBuffers.h:171
virtual ~SameSizeBuffers()
Finalizes an instance of the SameSizeBuffers<T, U> class.
Definition: SameSizeBuffers.h:119
bool CopyBufferOut(U *buffer, int index)
Copies the buffer specified by the index out to the buffer supplied.
Definition: SameSizeBuffers.h:362
SameSizeBuffers(int sizeOfBuffer)
Initializes a new instance of the SameSizeBuffers<T, U> class.
Definition: SameSizeBuffers.h:96
bool AddBufferToAverage(U *buffer, int index, int divisor)
Copies the buffer supplied to the buffer specified by the index.
Definition: SameSizeBuffers.h:328
bool Subtract(int sourceIndex, int destIndex)
Subtracts the source buffer from the destination buffer and places the result in the destination.
Definition: SameSizeBuffers.h:443
T * operator[](int index)
Returns a pointer to the buffer specified by the index.
Definition: SameSizeBuffers.h:262
int GetNextBufferIndex()
Gets the index of the next buffer in the circular queue.
Definition: SameSizeBuffers.h:277
int ResetCurrentBuffer()
Resets the current buffer to the index of the first buffer in the circular queue.
Definition: SameSizeBuffers.h:221
SameSizeBuffers()
Initializes a new instance of the SameSizeBuffers<T, U> class.
Definition: SameSizeBuffers.h:86
SameSizeBuffers(int sizeOfBuffer, int numBuffers)
Initializes a new instance of the SameSizeBuffers<T, U> class and creates the buffers specified.
Definition: SameSizeBuffers.h:109
void SetFirstBufferIndex(int value)
Sets the first index of the circular buffer.
Definition: SameSizeBuffers.h:464