v2.0.0
Loading...
Searching...
No Matches
circularbuffer.h
Go to the documentation of this file.
1//=============================================================================================================
35
36#ifndef CIRCULARBUFFER_H
37#define CIRCULARBUFFER_H
38
39//=============================================================================================================
40// INCLUDES
41//=============================================================================================================
42
43#include "../utils_global.h"
44
45//=============================================================================================================
46// QT INCLUDES
47//=============================================================================================================
48
49#include <QPair>
50#include <QSemaphore>
51#include <QSharedPointer>
52
53//=============================================================================================================
54// EIGEN INCLUDES
55//=============================================================================================================
56
57#include <Eigen/Core>
58
59//=============================================================================================================
60// DEFINE NAMESPACE UTILSLIB
61//=============================================================================================================
62
63namespace UTILSLIB
64{
65
66//=============================================================================================================
72template<typename _Tp>
74{
75public:
76 typedef QSharedPointer<CircularBuffer> SPtr;
77 typedef QSharedPointer<const CircularBuffer> ConstSPtr;
78
79 //=========================================================================================================
85 explicit CircularBuffer(unsigned int uiMaxNumElements);
86
87 //=========================================================================================================
92
93 //=========================================================================================================
100 inline bool push(const _Tp* pArray, unsigned int size);
101
102 //=========================================================================================================
108 inline bool push(const _Tp& newElement);
109
110 //=========================================================================================================
116 inline bool pop(_Tp& element);
117
118 //=========================================================================================================
122 void clear();
123
124 //=========================================================================================================
128 inline void pause(bool);
129
130 //=========================================================================================================
135
136 //=========================================================================================================
141
142private:
143 //=========================================================================================================
150 inline unsigned int mapIndex(int& index);
151 unsigned int m_uiMaxNumElements;
152 _Tp* m_pBuffer;
153 int m_iCurrentReadIndex;
154 int m_iCurrentWriteIndex;
155 QSemaphore* m_pFreeElements;
156 QSemaphore* m_pUsedElements;
157 int m_iTimeout;
158
159 bool m_bPause;
160};
161
162//=============================================================================================================
163// DEFINE MEMBER METHODS
164//=============================================================================================================
165
166template<typename _Tp>
167CircularBuffer<_Tp>::CircularBuffer(unsigned int uiMaxNumElements)
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))
174, m_iTimeout(1000)
175, m_bPause(false)
176{
177}
178
179//=============================================================================================================
180
181template<typename _Tp>
183{
184 delete m_pFreeElements;
185 delete m_pUsedElements;
186 delete [] m_pBuffer;
187}
188
189//=============================================================================================================
190
191template<typename _Tp>
192inline bool CircularBuffer<_Tp>::push(const _Tp* pArray, unsigned int size)
193{
194 if(m_bPause) {
195 return false;
196 }
197
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];
201 }
202 const QSemaphoreReleaser releaser(m_pUsedElements, size);
203 } else {
204 return false;
205 }
206
207 return true;
208}
209
210//=============================================================================================================
211
212template<typename _Tp>
213inline bool CircularBuffer<_Tp>::push(const _Tp& newElement)
214{
215 if(m_bPause) {
216 return false;
217 }
218
219 if(m_pFreeElements->tryAcquire(1, m_iTimeout)) {
220 m_pBuffer[mapIndex(m_iCurrentWriteIndex)] = newElement;
221 const QSemaphoreReleaser releaser(m_pUsedElements, 1);
222 } else {
223 return false;
224 }
225
226 return true;
227}
228
229//=============================================================================================================
230
231template<typename _Tp>
232inline bool CircularBuffer<_Tp>::pop(_Tp& element)
233{
234 if(m_bPause) {
235 return false;
236 }
237
238 if(m_pUsedElements->tryAcquire(1, m_iTimeout)) {
239 element = m_pBuffer[mapIndex(m_iCurrentReadIndex)];
240 const QSemaphoreReleaser releaser(m_pFreeElements, 1);
241 } else {
242 return false;
243 }
244
245 return true;
246}
247
248//=============================================================================================================
249
250template<typename _Tp>
251inline unsigned int CircularBuffer<_Tp>::mapIndex(int& index)
252{
253 int aux = index;
254 return index = ++aux % m_uiMaxNumElements;
255}
256
257//=============================================================================================================
258
259template<typename _Tp>
261{
262 delete m_pFreeElements;
263 m_pFreeElements = new QSemaphore(m_uiMaxNumElements);
264 delete m_pUsedElements;
265 m_pUsedElements = new QSemaphore(0);
266
267 m_iCurrentReadIndex = -1;
268 m_iCurrentWriteIndex = -1;
269}
270
271//=============================================================================================================
272
273template<typename _Tp>
274inline void CircularBuffer<_Tp>::pause(bool bPause)
275{
276 m_bPause = bPause;
277}
278
279//=============================================================================================================
280
281template<typename _Tp>
283{
284 return m_pUsedElements->available();
285}
286
287//=============================================================================================================
288
289template<typename _Tp>
291{
292 return m_pFreeElements->available();
293}
294
295//=============================================================================================================
296// TYPEDEF
297//=============================================================================================================
298
307
308} // NAMESPACE
309
310#endif // CIRCULARBUFFER_H
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
QSharedPointer< const CircularBuffer > ConstSPtr
CircularBuffer(unsigned int uiMaxNumElements)
bool push(const _Tp *pArray, unsigned int size)
bool pop(_Tp &element)
bool push(const _Tp &newElement)