/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/Engine/Foundation/Containers/Implementation/StaticRingBuffer_inl.h
168 строк
4 KB
Jan Krassnigg
Code fixes and C++ 20 preparations (#1122)
06 дек 2023, 14:41
Не верифицирован
06 дек 2023, 14:41
0de5f99
Код
Авторство
О чём код?
#pragma once template <typename T, ezUInt32 C> ezStaticRingBuffer<T, C>::ezStaticRingBuffer() { m_pElements = GetStaticArray(); m_uiFirstElement = 0; m_uiCount = 0; } template <typename T, ezUInt32 C> ezStaticRingBuffer<T, C>::ezStaticRingBuffer(const ezStaticRingBuffer<T, C>& rhs) { m_pElements = GetStaticArray(); m_uiFirstElement = 0; m_uiCount = 0; *this = rhs; } template <typename T, ezUInt32 C> ezStaticRingBuffer<T, C>::~ezStaticRingBuffer() { Clear(); } template <typename T, ezUInt32 C> void ezStaticRingBuffer<T, C>::operator=(const ezStaticRingBuffer<T, C>& rhs) { Clear(); for (ezUInt32 i = 0; i < rhs.GetCount(); ++i) PushBack(rhs[i]); } template <typename T, ezUInt32 C> bool ezStaticRingBuffer<T, C>::operator==(const ezStaticRingBuffer<T, C>& rhs) const { if (GetCount() != rhs.GetCount()) return false; for (ezUInt32 i = 0; i < m_uiCount; ++i) { if ((*this)[i] != rhs[i]) return false; } return true; } template <typename T, ezUInt32 C> void ezStaticRingBuffer<T, C>::PushBack(const T& element) { EZ_ASSERT_DEV(CanAppend(), "The ring-buffer is full, no elements can be appended before removing one."); const ezUInt32 uiLastElement = (m_uiFirstElement + m_uiCount) % C; ezMemoryUtils::CopyConstruct(&m_pElements[uiLastElement], element, 1); ++m_uiCount; } template <typename T, ezUInt32 C> void ezStaticRingBuffer<T, C>::PushBack(T&& element) { EZ_ASSERT_DEV(CanAppend(), "The ring-buffer is full, no elements can be appended before removing one."); const ezUInt32 uiLastElement = (m_uiFirstElement + m_uiCount) % C; ezMemoryUtils::MoveConstruct(&m_pElements[uiLastElement], std::move(element)); ++m_uiCount; } template <typename T, ezUInt32 C> T& ezStaticRingBuffer<T, C>::PeekBack() { EZ_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the last element."); const ezUInt32 uiLastElement = (m_uiFirstElement + m_uiCount - 1) % C; return m_pElements[uiLastElement]; } template <typename T, ezUInt32 C> const T& ezStaticRingBuffer<T, C>::PeekBack() const { EZ_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the last element."); const ezUInt32 uiLastElement = (m_uiFirstElement + m_uiCount - 1) % C; return m_pElements[uiLastElement]; } template <typename T, ezUInt32 C> void ezStaticRingBuffer<T, C>::PopFront(ezUInt32 uiElements) { EZ_ASSERT_DEV(m_uiCount >= uiElements, "The ring-buffer contains {0} elements, cannot remove {1} elements from it.", m_uiCount, uiElements); while (uiElements > 0) { ezMemoryUtils::Destruct(&m_pElements[m_uiFirstElement], 1); ++m_uiFirstElement; m_uiFirstElement %= C; --m_uiCount; --uiElements; } } template <typename T, ezUInt32 C> EZ_FORCE_INLINE const T& ezStaticRingBuffer<T, C>::PeekFront() const { EZ_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the first element."); return m_pElements[m_uiFirstElement]; } template <typename T, ezUInt32 C> EZ_FORCE_INLINE T& ezStaticRingBuffer<T, C>::PeekFront() { EZ_ASSERT_DEV(!IsEmpty(), "The ring-buffer is empty, cannot peek at the first element."); return m_pElements[m_uiFirstElement]; } template <typename T, ezUInt32 C> EZ_FORCE_INLINE const T& ezStaticRingBuffer<T, C>::operator[](ezUInt32 uiIndex) const { EZ_ASSERT_DEBUG(uiIndex < m_uiCount, "The ring-buffer only has {0} elements, cannot access element {1}.", m_uiCount, uiIndex); return m_pElements[(m_uiFirstElement + uiIndex) % C]; } template <typename T, ezUInt32 C> EZ_FORCE_INLINE T& ezStaticRingBuffer<T, C>::operator[](ezUInt32 uiIndex) { EZ_ASSERT_DEBUG(uiIndex < m_uiCount, "The ring-buffer only has {0} elements, cannot access element {1}.", m_uiCount, uiIndex); return m_pElements[(m_uiFirstElement + uiIndex) % C]; } template <typename T, ezUInt32 C> EZ_ALWAYS_INLINE ezUInt32 ezStaticRingBuffer<T, C>::GetCount() const { return m_uiCount; } template <typename T, ezUInt32 C> EZ_ALWAYS_INLINE bool ezStaticRingBuffer<T, C>::IsEmpty() const { return m_uiCount == 0; } template <typename T, ezUInt32 C> EZ_ALWAYS_INLINE bool ezStaticRingBuffer<T, C>::CanAppend(ezUInt32 uiElements) { return (m_uiCount + uiElements) <= C; } template <typename T, ezUInt32 C> void ezStaticRingBuffer<T, C>::Clear() { while (!IsEmpty()) PopFront(); } template <typename T, ezUInt32 C> EZ_ALWAYS_INLINE T* ezStaticRingBuffer<T, C>::GetStaticArray() { return reinterpret_cast<T*>(m_Data); }