| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef RINGBUFFER_H_ |
| 17 |
#define RINGBUFFER_H_ |
| 18 |
|
| 19 |
#include "utils/Macros.h" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
|
| 23 |
namespace android { |
| 24 |
namespace uirenderer { |
| 25 |
|
| 26 |
template <class T, size_t SIZE> |
| 27 |
class RingBuffer { |
| 28 |
PREVENT_COPY_AND_ASSIGN(RingBuffer); |
| 29 |
|
| 30 |
public: |
| 31 |
RingBuffer() {} |
| 32 |
~RingBuffer() {} |
| 33 |
|
| 34 |
constexpr size_t capacity() const { return SIZE; } |
| 35 |
size_t size() const { return mCount; } |
| 36 |
|
| 37 |
T& next() { |
| 38 |
mHead = (mHead + 1) % SIZE; |
| 39 |
if (mCount < SIZE) { |
| 40 |
mCount++; |
| 41 |
} |
| 42 |
return mBuffer[mHead]; |
| 43 |
} |
| 44 |
|
| 45 |
T& front() { return (*this)[0]; } |
| 46 |
|
| 47 |
T& back() { return (*this)[size() - 1]; } |
| 48 |
|
| 49 |
T& operator[](size_t index) { return mBuffer[(mHead + index + 1) % mCount]; } |
| 50 |
|
| 51 |
const T& operator[](size_t index) const { return mBuffer[(mHead + index + 1) % mCount]; } |
| 52 |
|
| 53 |
void clear() { |
| 54 |
mCount = 0; |
| 55 |
mHead = -1; |
| 56 |
} |
| 57 |
|
| 58 |
private: |
| 59 |
T mBuffer[SIZE]; |
| 60 |
int mHead = -1; |
| 61 |
size_t mCount = 0; |
| 62 |
}; |
| 63 |
|
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#endif |