template<typename T,int N>
class RingBuf
{
public:
RingBuf() : capSize(N){
first = last = 0;
size = 0;
}
~RingBuf() {
}
bool get(T& a) {
if (isEmpty()) {
return false;
}
a = buf[first++];
first %= capSize;
size -= 1;
return true;
}
bool put(T a) {
buf[last] = a;
last += 1;
last %= capSize;
if (isFull()) {
first += 1;
first %= capSize;
}else {
size += 1;
}
return true;
}
bool isFull() {
return size == capSize;
}
bool isEmpty() {
return size==0;
}
void msg() {
for (int i = 0; i < size; ++i) {
int ix = (first + i) % capSize;
std::cout << buf[ix] << " ";
if (i == size - 1) {
std::cout << "\n";
}
}
if (size == 0) {
std::cout << " \n";
}
}
private:
T buf[N];
int first;
int last;
int size;
const int capSize;
};