-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathringbuf.h
62 lines (58 loc) · 1.05 KB
/
ringbuf.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
#ifndef RINGBUF_H_INC
#define RINGBUF_H_INC
template<class T, long bufSize>
class ringbuffer
{
protected:
T buf[bufSize];
long read, write;
public:
ringbuffer()
{
read=0; write=1;
}
bool operator << (T &obj)
{
buf[write]=obj;
++write;
while(write>=bufSize)write-=bufSize;
if(write==read)
{
--write; //make sure this happens next time
while(write<0)write+=bufSize;
return false;
}
return true;
}
bool operator >> (T &res)
{
++read;
while(read>=bufSize)read-=bufSize;
if(read==write)
{
++write; //make sure this happens next time we call and the buf is still empty
while(write>=bufSize)write-=bufSize;
return false;
}
res=buf[read];
return true;
}
unsigned long dataSize()
{
unsigned long wc=write;
while(wc<read)wc+=bufSize;
return wc-read-1;
}
void flood(const T &value)
{
//loop through all indices, flooding them
//this is basically a reset
read=0;
for(write=0;write<bufSize;write++)
{
buf[write]=value;
}
write=1;
}
};
#endif