Plasma Engine  2.0
Loading...
Searching...
No Matches
StaticRingBuffer_inl.h
1#pragma once
2
3template <typename T, plUInt32 C>
5{
6 m_pElements = GetStaticArray();
7 m_uiFirstElement = 0;
8 m_uiCount = 0;
9}
10
11template <typename T, plUInt32 C>
13{
14 m_pElements = GetStaticArray();
15 m_uiFirstElement = 0;
16 m_uiCount = 0;
17
18 *this = rhs;
19}
20
21template <typename T, plUInt32 C>
23{
24 Clear();
25}
26
27template <typename T, plUInt32 C>
29{
30 Clear();
32 for (plUInt32 i = 0; i < rhs.GetCount(); ++i)
33 PushBack(rhs[i]);
35
36template <typename T, plUInt32 C>
38{
39 if (GetCount() != rhs.GetCount())
40 return false;
41
42 for (plUInt32 i = 0; i < m_uiCount; ++i)
43 {
44 if ((*this)[i] != rhs[i])
45 return false;
46 }
47
48 return true;
50
51template <typename T, plUInt32 C>
53{
54 PL_ASSERT_DEV(CanAppend(), "The ring-buffer is full, no elements can be appended before removing one.");
56 const plUInt32 uiLastElement = (m_uiFirstElement + m_uiCount) % C;
57
58 plMemoryUtils::CopyConstruct(&m_pElements[uiLastElement], element, 1);
59 ++m_uiCount;
60}
62template <typename T, plUInt32 C>
65 PL_ASSERT_DEV(CanAppend(), "The ring-buffer is full, no elements can be appended before removing one.");
66
67 const plUInt32 uiLastElement = (m_uiFirstElement + m_uiCount) % C;
68
69 plMemoryUtils::MoveConstruct(&m_pElements[uiLastElement], std::move(element));
70 ++m_uiCount;
71}
72
73template <typename T, plUInt32 C>
75{
76 PL_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the last element.");
77
78 const plUInt32 uiLastElement = (m_uiFirstElement + m_uiCount - 1) % C;
79 return m_pElements[uiLastElement];
80}
81
82template <typename T, plUInt32 C>
84{
85 PL_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the last element.");
86
87 const plUInt32 uiLastElement = (m_uiFirstElement + m_uiCount - 1) % C;
88 return m_pElements[uiLastElement];
89}
90
91template <typename T, plUInt32 C>
92void plStaticRingBuffer<T, C>::PopFront(plUInt32 uiElements)
93{
94 PL_ASSERT_DEV(m_uiCount >= uiElements, "The ring-buffer contains {0} elements, cannot remove {1} elements from it.", m_uiCount, uiElements);
95
96 while (uiElements > 0)
97 {
98 plMemoryUtils::Destruct(&m_pElements[m_uiFirstElement], 1);
99 ++m_uiFirstElement;
100 m_uiFirstElement %= C;
101 --m_uiCount;
102
103 --uiElements;
104 }
105}
106
107template <typename T, plUInt32 C>
108PL_FORCE_INLINE const T& plStaticRingBuffer<T, C>::PeekFront() const
109{
110 PL_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the first element.");
111
112 return m_pElements[m_uiFirstElement];
113}
114
115template <typename T, plUInt32 C>
117{
118 PL_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the first element.");
119
120 return m_pElements[m_uiFirstElement];
121}
122
123template <typename T, plUInt32 C>
124PL_FORCE_INLINE const T& plStaticRingBuffer<T, C>::operator[](plUInt32 uiIndex) const
125{
126 PL_ASSERT_DEBUG(uiIndex < m_uiCount, "The ring-buffer only has {0} elements, cannot access element {1}.", m_uiCount, uiIndex);
127
128 return m_pElements[(m_uiFirstElement + uiIndex) % C];
129}
130
131template <typename T, plUInt32 C>
132PL_FORCE_INLINE T& plStaticRingBuffer<T, C>::operator[](plUInt32 uiIndex)
133{
134 PL_ASSERT_DEBUG(uiIndex < m_uiCount, "The ring-buffer only has {0} elements, cannot access element {1}.", m_uiCount, uiIndex);
135
136 return m_pElements[(m_uiFirstElement + uiIndex) % C];
137}
138
139template <typename T, plUInt32 C>
140PL_ALWAYS_INLINE plUInt32 plStaticRingBuffer<T, C>::GetCount() const
141{
142 return m_uiCount;
143}
144
145template <typename T, plUInt32 C>
146PL_ALWAYS_INLINE bool plStaticRingBuffer<T, C>::IsEmpty() const
147{
148 return m_uiCount == 0;
149}
150
151template <typename T, plUInt32 C>
152PL_ALWAYS_INLINE bool plStaticRingBuffer<T, C>::CanAppend(plUInt32 uiElements)
153{
154 return (m_uiCount + uiElements) <= C;
155}
156
157template <typename T, plUInt32 C>
159{
160 while (!IsEmpty())
161 PopFront();
162}
163
164template <typename T, plUInt32 C>
166{
167 return reinterpret_cast<T*>(m_Data);
168}
static void CopyConstruct(Destination *pDestination, const Source &copy, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination, by creating uiCount copies of c...
static void MoveConstruct(T *pDestination, T &&source)
Constructs an object of type T in a raw buffer at pDestination, by using move construction from sourc...
static void Destruct(T *pDestination, size_t uiCount=1)
Destructs uiCount objects of type T at pDestination.
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
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