Zen 0.3.0
Loading...
Searching...
No Matches
ZEN_EventBuffer.cpp
1#include <zen/events/ZEN_Event.h>
2#include <zen/events/ZEN_EventBuffer.h>
3
4namespace Zen {
5
6 void EventBuffer::enqueue(ZenEvent event) {
7 m_buffer[m_head] = event;
8
9 if (m_full) {
10 m_tail = (m_tail + 1) % m_maxCapacity;
11 }
12
13 m_head = (m_head + 1) % m_maxCapacity;
14 m_full = m_head == m_tail;
15 }
16
17 ZenEvent EventBuffer::dequeue() {
18 if (isEmpty()) {
19 return {};
20 }
21
22 ZenEvent event = m_buffer[m_tail];
23
24 m_full = false;
25 m_tail = (m_tail + 1) % m_maxCapacity;
26
27 return event;
28 }
29
30 void EventBuffer::flush() {
31 m_head = m_tail;
32 m_full = false;
33 }
34
35 bool EventBuffer::isEmpty() const { return (!m_full && (m_head == m_tail)); }
36 bool EventBuffer::isFull() const { return m_full; }
37
38 size_t EventBuffer::capacity() const { return m_maxCapacity; }
39 size_t EventBuffer::size() const {
40 size_t size = m_maxCapacity;
41
42 if (!m_full) {
43 if (m_head >= m_tail) {
44 size = m_head - m_tail;
45 } else {
46 size = m_maxCapacity + m_head - m_tail;
47 }
48 }
49
50 return size;
51 }
52
53} // namespace Zen