renderbug-cpp/lib/Figments/Ringbuf.h

100 lines
2.1 KiB
C
Raw Normal View History

2021-03-28 21:50:31 +00:00
#pragma once
#include <array>
2023-03-03 18:43:51 +00:00
/**
* A simple ring buffer structure
*/
2021-03-28 21:50:31 +00:00
template<typename T, int Size>
struct Ringbuf {
Ringbuf() : m_head(0), m_tail(0) {}
2023-03-03 18:43:51 +00:00
/**
* Clears the buffer's contents
*/
2021-03-28 21:50:31 +00:00
void clear() {
m_head = 0;
m_tail = 0;
}
2023-03-03 18:43:51 +00:00
/**
* Returns the value of the next available item, if available
*/
T peek(int offset) const {
const int nextHead = (m_head + offset) % Size;
return m_items[nextHead];
}
2023-03-03 18:43:51 +00:00
/**
* Removes and returns the next available item, if any.
*
* @return false if no data is available, true otherwise
*/
2021-03-28 21:50:31 +00:00
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;
}
2023-03-03 18:43:51 +00:00
/**
* Inserts an item into the buffer. If the buffer is full, the oldest item
* is overwritten.
*/
2021-03-28 21:50:31 +00:00
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;
}
2023-03-03 18:43:51 +00:00
/**
* Consumes the entire buffer and writes it to the given array.
*/
size_t write(T(&dest)[Size]) {
int i = 0;
size_t ret = 0;
while(take(dest[i])) {
i++;
ret += sizeof(T);
}
return ret;
}
2023-03-03 18:43:51 +00:00
/**
* Consumes the entire buffer and writes it to the given output
*/
size_t write(Print& stream) {
T val;
size_t ret = 0;
while(take(val)) {
stream.write(val);
}
return ret;
}
2023-03-03 18:43:51 +00:00
/**
* Returns how many items are available in the buffer
*/
size_t size() {
if (m_tail > m_head) {
return m_tail - m_head;
} else if (m_tail == m_head) {
return 0;
}
return m_tail + (Size - m_head);
}
2021-03-28 21:50:31 +00:00
private:
int m_head = 0;
int m_tail = 0;
std::array<T, Size> m_items;
};