Plasma Engine  2.0
Loading...
Searching...
No Matches
StaticRingBuffer.h
1#pragma once
2
3#include <Foundation/Containers/StaticArray.h>
4
8template <typename T, plUInt32 Capacity>
10{
11public:
12 static_assert(Capacity > 1, "ORLY?");
13
15 plStaticRingBuffer(); // [tested]
16
19
21 ~plStaticRingBuffer(); // [tested]
22
24 void operator=(const plStaticRingBuffer<T, Capacity>& rhs); // [tested]
25
27 bool operator==(const plStaticRingBuffer<T, Capacity>& rhs) const; // [tested]
28 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plStaticRingBuffer<T, Capacity>&);
29
31 void PushBack(const T& element); // [tested]
32
34 void PushBack(T&& element); // [tested]
35
37 T& PeekBack(); // [tested]
38
40 const T& PeekBack() const; // [tested]
41
43 void PopFront(plUInt32 uiElements = 1); // [tested]
44
46 const T& PeekFront() const; // [tested]
47
49 T& PeekFront(); // [tested]
50
52 const T& operator[](plUInt32 uiIndex) const; // [tested]
53
55 T& operator[](plUInt32 uiIndex); // [tested]
56
58 plUInt32 GetCount() const; // [tested]
59
61 bool IsEmpty() const; // [tested]
62
64 bool CanAppend(plUInt32 uiElements = 1); // [tested]
65
67 void Clear(); // [tested]
68
69 const T* GetElements() const { return m_pElements; }
70
71private:
72 T* GetStaticArray();
73
75 struct alignas(PL_ALIGNMENT_OF(T))
76 {
77 plUInt8 m_Data[Capacity * sizeof(T)];
78 };
79
80 T* m_pElements;
81 plUInt32 m_uiCount;
82 plUInt32 m_uiFirstElement;
83};
84
85#include <Foundation/Containers/Implementation/StaticRingBuffer_inl.h>
A ring-buffer container that will use a static array of a given capacity to cycle through elements.
Definition StaticRingBuffer.h:10
void Clear()
Destructs all elements in the ring-buffer.
Definition StaticRingBuffer_inl.h:158
plUInt32 GetCount() const
Returns the number of elements that are currently in the ring-buffer.
Definition StaticRingBuffer_inl.h:140
void PushBack(const T &element)
Appends an element at the end of the ring-buffer. Asserts that CanAppend() is true.
Definition StaticRingBuffer_inl.h:52
const T & PeekFront() const
Accesses the oldest element in the ring-buffer.
Definition StaticRingBuffer_inl.h:108
bool CanAppend(plUInt32 uiElements=1)
Returns true, if the ring-buffer can store at least uiElements additional elements.
Definition StaticRingBuffer_inl.h:152
void PopFront(plUInt32 uiElements=1)
Removes the oldest element from the ring-buffer.
Definition StaticRingBuffer_inl.h:92
void operator=(const plStaticRingBuffer< T, Capacity > &rhs)
Copies the content from rhs into this ring-buffer.
Definition StaticRingBuffer_inl.h:28
T & PeekBack()
Accesses the latest element in the ring-buffer.
Definition StaticRingBuffer_inl.h:74
const T & operator[](plUInt32 uiIndex) const
Accesses the n-th element in the ring-buffer.
Definition StaticRingBuffer_inl.h:124
bool IsEmpty() const
Returns true if the ring-buffer currently contains no elements.
Definition StaticRingBuffer_inl.h:146
plStaticRingBuffer(const plStaticRingBuffer< T, Capacity > &rhs)
Copies the content from rhs into this ring-buffer.
bool operator==(const plStaticRingBuffer< T, Capacity > &rhs) const
Compares two ring-buffers for equality.
Definition StaticRingBuffer_inl.h:37
plStaticRingBuffer()
Constructs an empty ring-buffer.
Definition StaticRingBuffer_inl.h:4
~plStaticRingBuffer()
Destructs all remaining elements.
Definition StaticRingBuffer_inl.h:22