1
0
mirror of https://github.com/peterantypas/maiana.git synced 2025-05-15 23:10:11 -07:00
maiana/legacy/application/src/CircularQueue.hpp
2020-10-02 18:40:04 -07:00

76 lines
1.3 KiB
C++

/*
* CircularQueue.hpp
*
* Created on: Mar 5, 2016
* Author: peter
*/
#ifndef CIRCULARQUEUE_HPP_
#define CIRCULARQUEUE_HPP_
#include "_assert.h"
//#define NUMQELEMENTS 100
template<typename T> class CircularQueue
{
public:
CircularQueue(size_t size)
{
mReadPosition = 0;
mWritePosition = 0;
mSize = size;
mBuffer = new T[mSize];
}
bool empty()
{
return mReadPosition == mWritePosition;
}
bool push(T &Element)
{
int nextElement = (mWritePosition + 1) % mSize;
if ( isSafeToWrite(nextElement) ) {
mBuffer[mWritePosition] = Element;
mWritePosition = nextElement;
return true;
}
else
return false;
}
bool pop(T &Element)
{
if ( empty() )
return false;
int nextElement = (mReadPosition + 1) % mSize;
Element = mBuffer[mReadPosition];
mReadPosition = nextElement;
return true;
}
T top()
{
ASSERT(!empty());
return mBuffer[mReadPosition];
}
private:
bool isSafeToWrite(int nextElement)
{
return nextElement != mReadPosition;
}
private:
volatile int mReadPosition;
volatile int mWritePosition;
size_t mSize;
T* mBuffer;
};
#endif /* CIRCULARQUEUE_HPP_ */