-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingBuffer.h
62 lines (49 loc) · 1.69 KB
/
RingBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
This file is based on RingBuffer.h in Smoothie (http://smoothieware.org/).
With chucks taken from http://en.wikipedia.org/wiki/Circular_buffer, see licence there also
*/
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
template<class kind, char length> class RingBuffer {
public:
RingBuffer();
char size();
char capacity();
void pushBack(kind object);
void popFront(kind &object);
bool isFull();
bool isEmpty();
void clear();
private:
kind buffer[length];
volatile char tail;
volatile char head;
};
template<class kind, char length> char RingBuffer<kind, length>::capacity(){
return length-1;
}
template<class kind, char length> char RingBuffer<kind, length>::size(){
char i = head - tail + ((tail > head)?length:0);
return i;
}
template<class kind, char length> RingBuffer<kind, length>::RingBuffer(){
this->tail = this->head = 0;
}
template<class kind, char length> void RingBuffer<kind, length>::clear(){
this->tail = this->head = 0;
}
template<class kind, char length> bool RingBuffer<kind, length>::isFull(){
return ((head+1)&(length-1)) == tail;
}
template<class kind, char length> bool RingBuffer<kind, length>::isEmpty(){
return head == tail;
}
template<class kind, char length> void RingBuffer<kind, length>::pushBack(kind object){
this->buffer[this->head] = object;
this->head = (head+1)&(length-1);
}
template<class kind, char length> void RingBuffer<kind, length>::popFront(kind &object){
object = this->buffer[this->tail];
this->tail = (this->tail+1)&(length-1);
}
#endif