-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.h
91 lines (66 loc) · 1.36 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* ringbuffer.h
*
* Created on: 09/06/2014
* Author: Wellington
*/
#ifndef RINGBUFFER_H_
#define RINGBUFFER_H_
template<typename T, int BUFFERSIZE>
class ringbuffer {
public:
ringbuffer();
virtual ~ringbuffer();
bool empty();
void push_back(T);
T pop_front();
void clear_buffer();
private:
int head;
int tail;
T buffer[BUFFERSIZE];
};
template<typename T, int BUFFERSIZE>
ringbuffer<T,BUFFERSIZE>::ringbuffer() {
clear_buffer();
}
template<typename T, int BUFFERSIZE>
ringbuffer<T,BUFFERSIZE>::~ringbuffer() {
}
template<typename T, int BUFFERSIZE>
bool ringbuffer<T,BUFFERSIZE>::empty() {
return head == tail;
}
template<typename T, int BUFFERSIZE>
void ringbuffer<T,BUFFERSIZE>::clear_buffer(){
_disable_interrupts();
head = 0;
tail = 0;
_enable_interrupts();
}
template<typename T, int BUFFERSIZE>
void ringbuffer<T,BUFFERSIZE>::push_back(T c){
int i = (unsigned int)(head + 1);
if(i == BUFFERSIZE){
i=0;
}
if ( i != tail ) {
buffer[head] = c;
head = i;
}
}
template<typename T, int BUFFERSIZE>
T ringbuffer<T,BUFFERSIZE>::pop_front() {
T c;
_disable_interrupts();
if (head != tail) {
c = buffer[tail];
tail = (unsigned int)(tail + 1);
if(tail==BUFFERSIZE){
tail=0;
}
}
_enable_interrupts();
return c;
}
#endif /* RINGBUFFER_H_ */