forked from empyreanx/gdnet
-
Notifications
You must be signed in to change notification settings - Fork 25
/
gdnet_queue.h
58 lines (43 loc) · 881 Bytes
/
gdnet_queue.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
/* gdnet_rb.h */
#ifndef GDNET_QUEUE_H
#define GDNET_QUEUE_H
#include "core/os/memory.h"
template<class T, int SIZE = 1024>
class GDNetQueue {
T* items[SIZE];
int read_pos;
int write_pos;
public:
bool is_empty() {
return (read_pos == write_pos);
}
bool is_full() {
return ((write_pos + 1) % SIZE == read_pos);
}
int size() {
if (write_pos > read_pos)
return write_pos - read_pos;
else if (write_pos < read_pos)
return (SIZE - read_pos) + write_pos;
else
return 0;
}
void push(T* item) {
ERR_FAIL_COND(is_full());
items[write_pos] = item;
write_pos = (write_pos + 1) % SIZE;
}
T* pop() {
ERR_FAIL_COND_V(is_empty(), NULL);
T* item = items[read_pos];
read_pos = (read_pos + 1) % SIZE;
return item;
}
void clear() {
while (!is_empty()) {
memdelete(pop());
}
}
GDNetQueue() { read_pos = write_pos = 0; }
};
#endif