36 #ifndef CIRCULARBUFFER_H
37 #define CIRCULARBUFFER_H
43 #include "../utils_global.h"
51 #include <QSharedPointer>
72 template<
typename _Tp>
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;
166 template<
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))
181 template<
typename _Tp>
184 delete m_pFreeElements;
185 delete m_pUsedElements;
191 template<
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);
210 template<
typename _Tp>
213 if(m_pFreeElements->tryAcquire(1, m_iTimeout)) {
214 m_pBuffer[mapIndex(m_iCurrentWriteIndex)] = newElement;
215 const QSemaphoreReleaser releaser(m_pUsedElements, 1);
225 template<
typename _Tp>
229 if(m_pUsedElements->tryAcquire(1, m_iTimeout)) {
230 element = m_pBuffer[mapIndex(m_iCurrentReadIndex)];
231 const QSemaphoreReleaser releaser(m_pFreeElements, 1);
242 template<
typename _Tp>
246 return index = ++aux % m_uiMaxNumElements;
251 template<
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;
265 template<
typename _Tp>
273 template<
typename _Tp>
276 return m_pUsedElements->available();
281 template<
typename _Tp>
284 return m_pFreeElements->available();
302 #endif // CIRCULARBUFFER_H