36#ifndef CIRCULARBUFFER_H
37#define CIRCULARBUFFER_H
43#include "../utils_global.h"
51#include <QSharedPointer>
76 typedef QSharedPointer<CircularBuffer>
SPtr;
77 typedef QSharedPointer<const CircularBuffer>
ConstSPtr;
100 inline bool push(
const _Tp* pArray,
unsigned int size);
108 inline bool push(
const _Tp& newElement);
116 inline bool pop(_Tp& element);
128 inline void pause(
bool);
150 inline unsigned int mapIndex(
int& index);
151 unsigned int m_uiMaxNumElements;
153 int m_iCurrentReadIndex;
154 int m_iCurrentWriteIndex;
155 QSemaphore* m_pFreeElements;
156 QSemaphore* m_pUsedElements;
166template<
typename _Tp>
168: m_uiMaxNumElements(uiMaxNumElements)
169, m_pBuffer(new _Tp[m_uiMaxNumElements])
170, m_iCurrentReadIndex(-1)
171, m_iCurrentWriteIndex(-1)
172, m_pFreeElements(new QSemaphore(m_uiMaxNumElements))
173, m_pUsedElements(new QSemaphore(0))
181template<
typename _Tp>
184 delete m_pFreeElements;
185 delete m_pUsedElements;
191template<
typename _Tp>
195 if(m_pFreeElements->tryAcquire(size, m_iTimeout)) {
196 for(
unsigned int i = 0; i < size; ++i) {
197 m_pBuffer[mapIndex(m_iCurrentWriteIndex)] = pArray[i];
199 const QSemaphoreReleaser releaser(m_pUsedElements, size);
210template<
typename _Tp>
213 if(m_pFreeElements->tryAcquire(1, m_iTimeout)) {
214 m_pBuffer[mapIndex(m_iCurrentWriteIndex)] = newElement;
215 const QSemaphoreReleaser releaser(m_pUsedElements, 1);
225template<
typename _Tp>
229 if(m_pUsedElements->tryAcquire(1, m_iTimeout)) {
230 element = m_pBuffer[mapIndex(m_iCurrentReadIndex)];
231 const QSemaphoreReleaser releaser(m_pFreeElements, 1);
242template<
typename _Tp>
246 return index = ++aux % m_uiMaxNumElements;
251template<
typename _Tp>
254 delete m_pFreeElements;
255 m_pFreeElements =
new QSemaphore(m_uiMaxNumElements);
256 delete m_pUsedElements;
257 m_pUsedElements =
new QSemaphore(0);
259 m_iCurrentReadIndex = -1;
260 m_iCurrentWriteIndex = -1;
265template<
typename _Tp>
273template<
typename _Tp>
276 return m_pUsedElements->available();
281template<
typename _Tp>
284 return m_pFreeElements->available();
CircularBuffer< QPair< int, int > > CircularBuffer_pair_int_int
CircularBuffer< Eigen::MatrixXf > CircularBuffer_Matrix_float
CircularBuffer< QPair< double, double > > CircularBuffer_pair_double_double
CircularBuffer< short > CircularBuffer_short
CircularBuffer< Eigen::MatrixXd > CircularBuffer_Matrix_double
CircularBuffer< double > CircularBuffer_double
CircularBuffer< char > CircularBuffer_char
CircularBuffer< int > CircularBuffer_int
The TEMPLATE CIRCULAR BUFFER provides a template for thread safe circular buffers.
QSharedPointer< CircularBuffer > SPtr
int getFreeElementsRead()
QSharedPointer< const CircularBuffer > ConstSPtr
CircularBuffer(unsigned int uiMaxNumElements)
bool push(const _Tp *pArray, unsigned int size)
int getFreeElementsWrite()