2021-03-28 21:50:31 +00:00
|
|
|
#pragma once
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
template<typename T, int Size>
|
|
|
|
struct Ringbuf {
|
|
|
|
Ringbuf() : m_head(0), m_tail(0) {}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
m_head = 0;
|
|
|
|
m_tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool take(T& dest) {
|
|
|
|
if (m_head == m_tail) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const int cur = m_head;
|
|
|
|
const int nextHead = (m_head + 1) % Size;
|
|
|
|
m_head = nextHead;
|
|
|
|
dest = m_items[cur];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(const T& src) {
|
|
|
|
const int cur = m_tail;
|
|
|
|
const int nextTail = (m_tail + 1) % Size;
|
|
|
|
if (nextTail == m_head) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
m_tail = nextTail;
|
|
|
|
}
|
|
|
|
m_items[cur] = src;
|
|
|
|
}
|
2021-03-31 18:50:00 +00:00
|
|
|
|
|
|
|
size_t write(T(&dest)[Size]) {
|
|
|
|
int i = 0;
|
|
|
|
size_t ret = 0;
|
|
|
|
while(take(dest[i])) {
|
|
|
|
i++;
|
|
|
|
ret += sizeof(T);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2021-03-28 21:50:31 +00:00
|
|
|
private:
|
|
|
|
int m_head = 0;
|
|
|
|
int m_tail = 0;
|
|
|
|
std::array<T, Size> m_items;
|
|
|
|
};
|
|
|
|
|