|
| 1 | +/* Circular Buffer |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/Arduino-Source |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef PokemonAutomation_CircularBuffer_H |
| 8 | +#define PokemonAutomation_CircularBuffer_H |
| 9 | + |
| 10 | +#include <new> |
| 11 | +#include <utility> |
| 12 | +#include "Common/Cpp/Exceptions.h" |
| 13 | +#include "Common/Cpp/Containers/AlignedMalloc.h" |
| 14 | + |
| 15 | +namespace PokemonAutomation{ |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +template <typename Object> |
| 20 | +class CircularBuffer{ |
| 21 | +public: |
| 22 | + ~CircularBuffer(); |
| 23 | + CircularBuffer(CircularBuffer&& x) noexcept; |
| 24 | + void operator=(CircularBuffer&& x) noexcept; |
| 25 | + CircularBuffer(const CircularBuffer& x); |
| 26 | + void operator=(const CircularBuffer& x); |
| 27 | + |
| 28 | +public: |
| 29 | + CircularBuffer(); |
| 30 | + CircularBuffer(size_t capacity); |
| 31 | + |
| 32 | + void clear() noexcept; |
| 33 | + |
| 34 | + size_t empty() const{ return m_front == m_back; } |
| 35 | + size_t size() const{ return m_back - m_front; } |
| 36 | + |
| 37 | + const Object& front() const; |
| 38 | + Object& front(); |
| 39 | + const Object& operator[](size_t index) const; |
| 40 | + Object& operator[](size_t index); |
| 41 | + |
| 42 | + |
| 43 | + template <class... Args> |
| 44 | + Object& push_back(Args&&... args); |
| 45 | + void pop_front(); |
| 46 | + |
| 47 | +private: |
| 48 | + size_t m_capacity; |
| 49 | + size_t m_front; |
| 50 | + size_t m_back; |
| 51 | + Object* m_ptr; |
| 52 | +}; |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +template <typename Object> |
| 57 | +CircularBuffer<Object>::~CircularBuffer(){ |
| 58 | + clear(); |
| 59 | + aligned_free(m_ptr); |
| 60 | +} |
| 61 | +template <typename Object> |
| 62 | +CircularBuffer<Object>::CircularBuffer(CircularBuffer&& x) noexcept |
| 63 | + : m_capacity(x.m_capacity) |
| 64 | + , m_front(x.m_front) |
| 65 | + , m_back(x.m_back) |
| 66 | + , m_ptr(x.m_ptr) |
| 67 | +{ |
| 68 | + x.m_capacity = 0; |
| 69 | + x.m_front = 0; |
| 70 | + x.m_back = 0; |
| 71 | + x.m_ptr = nullptr; |
| 72 | +} |
| 73 | +template <typename Object> |
| 74 | +void CircularBuffer<Object>::operator=(CircularBuffer&& x) noexcept{ |
| 75 | + if (this == &x){ |
| 76 | + return; |
| 77 | + } |
| 78 | + clear(); |
| 79 | + aligned_free(m_ptr); |
| 80 | + m_capacity = x.m_capacity; |
| 81 | + m_front = x.m_front; |
| 82 | + m_back = x.m_back; |
| 83 | + m_ptr = x.m_ptr; |
| 84 | + x.m_capacity = 0; |
| 85 | + x.m_front = 0; |
| 86 | + x.m_back = 0; |
| 87 | + x.m_ptr = nullptr; |
| 88 | +} |
| 89 | +template <typename Object> |
| 90 | +CircularBuffer<Object>::CircularBuffer(const CircularBuffer& x) |
| 91 | + : CircularBuffer(x.m_capacity) |
| 92 | +{ |
| 93 | + size_t size = x.size(); |
| 94 | + for (size_t c = 0; c < size; c++){ |
| 95 | + push_back(x[c]); |
| 96 | + } |
| 97 | +} |
| 98 | +template <typename Object> |
| 99 | +void CircularBuffer<Object>::operator=(const CircularBuffer& x){ |
| 100 | + if (this == &x){ |
| 101 | + return; |
| 102 | + } |
| 103 | + CircularBuffer tmp(x); |
| 104 | + *this = std::move(tmp); |
| 105 | +} |
| 106 | + |
| 107 | +template <typename Object> |
| 108 | +CircularBuffer<Object>::CircularBuffer() |
| 109 | + : m_capacity(0) |
| 110 | + , m_front(0) |
| 111 | + , m_back(0) |
| 112 | + , m_ptr(nullptr) |
| 113 | +{} |
| 114 | +template <typename Object> |
| 115 | +CircularBuffer<Object>::CircularBuffer(size_t capacity) |
| 116 | + : m_capacity(capacity) |
| 117 | + , m_front(0) |
| 118 | + , m_back(0) |
| 119 | + , m_ptr(nullptr) |
| 120 | +{ |
| 121 | + if (capacity == 0){ |
| 122 | + return; |
| 123 | + } |
| 124 | + m_ptr = (Object*)aligned_malloc(capacity * sizeof(Object), alignof(Object)); |
| 125 | + if (m_ptr == nullptr){ |
| 126 | + throw std::bad_alloc(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +template <typename Object> |
| 131 | +void CircularBuffer<Object>::clear() noexcept{ |
| 132 | + while (m_back > m_front){ |
| 133 | + pop_front(); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +template <typename Object> |
| 139 | +const Object& CircularBuffer<Object>::front() const{ |
| 140 | + return m_ptr[m_front]; |
| 141 | +} |
| 142 | +template <typename Object> |
| 143 | +Object& CircularBuffer<Object>::front(){ |
| 144 | + return m_ptr[m_front]; |
| 145 | +} |
| 146 | +template <typename Object> |
| 147 | +const Object& CircularBuffer<Object>::operator[](size_t index) const{ |
| 148 | + index += m_front; |
| 149 | + if (index >= m_capacity){ |
| 150 | + index -= m_capacity; |
| 151 | + } |
| 152 | + return m_ptr[index]; |
| 153 | +} |
| 154 | +template <typename Object> |
| 155 | +Object& CircularBuffer<Object>::operator[](size_t index){ |
| 156 | + index += m_front; |
| 157 | + if (index >= m_capacity){ |
| 158 | + index -= m_capacity; |
| 159 | + } |
| 160 | + return m_ptr[index]; |
| 161 | +} |
| 162 | + |
| 163 | +template <typename Object> |
| 164 | +template <class... Args> |
| 165 | +Object& CircularBuffer<Object>::push_back(Args&&... args){ |
| 166 | + if (m_back - m_front >= m_capacity){ |
| 167 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "CircularBuffer: Attempted to push while full."); |
| 168 | + } |
| 169 | + size_t back = m_back; |
| 170 | + if (back >= m_capacity){ |
| 171 | + back -= m_capacity; |
| 172 | + } |
| 173 | + new (m_ptr + back) Object(std::forward<Args>(args)...); |
| 174 | + m_back++; |
| 175 | + return m_ptr[back]; |
| 176 | +} |
| 177 | + |
| 178 | +template <typename Object> |
| 179 | +void CircularBuffer<Object>::pop_front(){ |
| 180 | + if (m_front == m_back){ |
| 181 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "CircularBuffer: Attempted to pop while empty."); |
| 182 | + } |
| 183 | + size_t front = m_front; |
| 184 | + m_ptr[front].~Object(); |
| 185 | + front++; |
| 186 | + size_t diff = front >= m_capacity ? m_capacity : 0; |
| 187 | + m_front = front - diff; |
| 188 | + m_back -= diff; |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +} |
| 198 | +#endif |
0 commit comments