36#ifndef CIRCULARBUFFER_H
37#define CIRCULARBUFFER_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);
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>
198 if(m_pFreeElements->tryAcquire(size, m_iTimeout)) {
199 for(
unsigned int i = 0; i < size; ++i) {
200 m_pBuffer[mapIndex(m_iCurrentWriteIndex)] = pArray[i];
202 const QSemaphoreReleaser releaser(m_pUsedElements, size);
212template<
typename _Tp>
219 if(m_pFreeElements->tryAcquire(1, m_iTimeout)) {
220 m_pBuffer[mapIndex(m_iCurrentWriteIndex)] = newElement;
221 const QSemaphoreReleaser releaser(m_pUsedElements, 1);
231template<
typename _Tp>
238 if(m_pUsedElements->tryAcquire(1, m_iTimeout)) {
239 element = m_pBuffer[mapIndex(m_iCurrentReadIndex)];
240 const QSemaphoreReleaser releaser(m_pFreeElements, 1);
250template<
typename _Tp>
251inline unsigned int CircularBuffer<_Tp>::mapIndex(
int& index)
254 return index = ++aux % m_uiMaxNumElements;
259template<
typename _Tp>
262 delete m_pFreeElements;
263 m_pFreeElements =
new QSemaphore(m_uiMaxNumElements);
264 delete m_pUsedElements;
265 m_pUsedElements =
new QSemaphore(0);
267 m_iCurrentReadIndex = -1;
268 m_iCurrentWriteIndex = -1;
273template<
typename _Tp>
281template<
typename _Tp>
284 return m_pUsedElements->available();
289template<
typename _Tp>
292 return m_pFreeElements->available();
utils library export/import macros.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
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
Thread-safe lock-free circular (ring) buffer for producer-consumer data exchange between threads.
QSharedPointer< CircularBuffer > SPtr
int getFreeElementsRead()
QSharedPointer< const CircularBuffer > ConstSPtr
CircularBuffer(unsigned int uiMaxNumElements)
bool push(const _Tp *pArray, unsigned int size)
int getFreeElementsWrite()
bool push(const _Tp &newElement)