UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
CircularQueue.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
7#include "Templates/Atomic.h"
8
9// WARNING: This queue is planned for deprecation in favor of TSpscQueue
10
25template<typename T> class TCircularQueue
26{
27public:
28 using FElementType = T;
29
36 : Buffer(CapacityPlusOne)
37 , Head(0)
38 , Tail(0)
39 { }
40
41public:
42
51 uint32 Count() const
52 {
53 int32 Count = Tail.Load() - Head.Load();
54
55 if (Count < 0)
56 {
57 Count += Buffer.Capacity();
58 }
59
60 return (uint32)Count;
61 }
62
71 {
72 const uint32 CurrentHead = Head.Load();
73
74 if (CurrentHead != Tail.Load())
75 {
77 Head.Store(Buffer.GetNextIndex(CurrentHead));
78
79 return true;
80 }
81
82 return false;
83 }
84
91 bool Dequeue()
92 {
93 const uint32 CurrentHead = Head.Load();
94
95 if (CurrentHead != Tail.Load())
96 {
97 Head.Store(Buffer.GetNextIndex(CurrentHead));
98
99 return true;
100 }
101
102 return false;
103 }
104
111 void Empty()
112 {
113 Head.Store(Tail.Load());
114 }
115
123 bool Enqueue(const FElementType& Element)
124 {
125 const uint32 CurrentTail = Tail.Load();
127
128 if (NewTail != Head.Load())
129 {
130 Buffer[CurrentTail] = Element;
131 Tail.Store(NewTail);
132
133 return true;
134 }
135
136 return false;
137 }
138
146 bool Enqueue(FElementType&& Element)
147 {
148 const uint32 CurrentTail = Tail.Load();
150
151 if (NewTail != Head.Load())
152 {
153 Buffer[CurrentTail] = MoveTemp(Element);
154 Tail.Store(NewTail);
155
156 return true;
157 }
158
159 return false;
160 }
161
172 {
173 return (Head.Load() == Tail.Load());
174 }
175
185 bool IsFull() const
186 {
187 return (Buffer.GetNextIndex(Tail.Load()) == Head.Load());
188 }
189
198 {
199 const uint32 CurrentHead = Head.Load();
200
201 if (CurrentHead != Tail.Load())
202 {
203 OutItem = Buffer[CurrentHead];
204
205 return true;
206 }
207
208 return false;
209 }
210
218 const FElementType* Peek() const
219 {
220 const uint32 CurrentHead = Head.Load();
221
222 if (CurrentHead != Tail.Load())
223 {
224 return &Buffer[CurrentHead];
225 }
226
227 return nullptr;
228 }
229
230private:
231
234
236 TAtomic<uint32> Head;
237
239 TAtomic<uint32> Tail;
240};
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Atomic.h:538
Definition CircularBuffer.h:18
UE_FORCEINLINE_HINT uint32 Capacity() const
Definition CircularBuffer.h:79
UE_FORCEINLINE_HINT uint32 GetNextIndex(uint32 CurrentIndex) const
Definition CircularBuffer.h:90
Definition CircularQueue.h:26
bool IsFull() const
Definition CircularQueue.h:185
bool Peek(FElementType &OutItem) const
Definition CircularQueue.h:197
bool Dequeue(FElementType &OutElement)
Definition CircularQueue.h:70
bool Dequeue()
Definition CircularQueue.h:91
const FElementType * Peek() const
Definition CircularQueue.h:218
void Empty()
Definition CircularQueue.h:111
T FElementType
Definition CircularQueue.h:28
bool Enqueue(FElementType &&Element)
Definition CircularQueue.h:146
uint32 Count() const
Definition CircularQueue.h:51
UE_FORCEINLINE_HINT bool IsEmpty() const
Definition CircularQueue.h:171
bool Enqueue(const FElementType &Element)
Definition CircularQueue.h:123
TCircularQueue(uint32 CapacityPlusOne)
Definition CircularQueue.h:35