| Fling Engine 0.00.1 Fling Engine is a game engine written in Vulkan

CircularBuffer.hpp
Go to the documentation of this file.
1 #pragma once
2
3 #include "FlingTypes.h"
4
5 namespace Fling
6 {
7
15 template<typename T, size_t t_Size>
16 class CircularBuffer
17 {
18 public:
19
20 CircularBuffer();
21
22 ~CircularBuffer() = default;
23
24 T* GetItem();
25
26 private:
27
28 const UINT32 m_BufferSize;
29
30 T m_Buffer [t_Size];
31
32 UINT32 m_AllocatedIndex = 0;
33 };
34
35 template<typename T, size_t t_Size>
36 inline CircularBuffer<T, t_Size>::CircularBuffer()
37 : m_BufferSize(t_Size)
38 , m_AllocatedIndex(0)
39 , m_Buffer{}
40 {
41 static_assert((t_Size != 0 && (t_Size & (t_Size-1)) == 0), "CircularBuffer::t_Size must be a power of 2!");
42 }
43
44 template<typename T, size_t t_Size>
45 inline T* CircularBuffer<T, t_Size>::GetItem()
46 {
47 const uint32_t index = m_AllocatedIndex++;
48 // This bit shifting is the same as the % operator
49 // @see https://blog.molecular-matters.com/2015/09/08/job-system-2-0-lock-free-work-stealing-part-2-a-specialized-allocator/
50 // for more about that
51 return &(m_Buffer[(index - 1u) & (m_BufferSize - 1u)]);
52 }
53 } // namespace Fling
Fling::CircularBuffer::GetItem
T * GetItem()
Definition: CircularBuffer.hpp:45
Fling::CircularBuffer
A simple circular buffer that will allow you get the next element in a buffer It does not ensure that...
Definition: CircularBuffer.hpp:16
Fling::CircularBuffer::~CircularBuffer
~CircularBuffer()=default
Fling::CircularBuffer::m_Buffer
T m_Buffer[t_Size]
Definition: CircularBuffer.hpp:30
Fling::CircularBuffer::m_BufferSize
const UINT32 m_BufferSize
Definition: CircularBuffer.hpp:28
Fling::CircularBuffer::CircularBuffer
CircularBuffer()
Definition: CircularBuffer.hpp:36
Fling::CircularBuffer::m_AllocatedIndex
UINT32 m_AllocatedIndex
Definition: CircularBuffer.hpp:32
UINT32
uint32_t UINT32
Definition: FlingTypes.h:13
Fling
Definition: Engine.h:29
FlingTypes.h