UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
EnumRange.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"
6#include <type_traits>
7
25#define ENUM_RANGE_BY_COUNT(EnumType, Count) ENUM_RANGE_BY_FIRST_AND_LAST(EnumType, 0, (std::underlying_type_t<EnumType>)(Count) - 1)
26
27
47#define ENUM_RANGE_BY_FIRST_AND_LAST(EnumType, First, Last) \
48 template <> \
49 struct NEnumRangePrivate::TEnumRangeTraits<EnumType> \
50 { \
51 enum { RangeType = 0 }; \
52 static constexpr std::underlying_type_t<EnumType> Begin = (std::underlying_type_t<EnumType>)(First); \
53 static constexpr std::underlying_type_t<EnumType> End = (std::underlying_type_t<EnumType>)(Last) + 1; \
54 };
55
56
74#define ENUM_RANGE_BY_VALUES(EnumType, ...) \
75 template <> \
76 struct NEnumRangePrivate::TEnumRangeTraits<EnumType> \
77 { \
78 enum { RangeType = 1 }; \
79 template <typename Dummy> \
80 static const EnumType* GetPointer(bool bLast) \
81 { \
82 static constexpr EnumType Values[] = { __VA_ARGS__ }; \
83 return bLast ? Values + sizeof(Values) / sizeof(EnumType) : Values; \
84 } \
85 };
86
87
89{
90 template <typename EnumType>
92 {
93 enum { RangeType = -1 };
94 };
95
96 template <typename EnumType, int32 RangeType>
98 {
99 static_assert(sizeof(EnumType) == 0, "Unknown enum type - use one of the ENUM_RANGE macros to define iteration semantics for your enum type.");
100 };
101
102 template <typename EnumType>
104 {
105 using IntType = std::underlying_type_t<EnumType>;
106
111
113 {
114 ++Value;
115 return *this;
116 }
117
119 {
120 return (EnumType)Value;
121 }
122
123 private:
124 IntType Value;
125
127 {
128 return Lhs.Value != Rhs.Value;
129 }
130 };
131
132 template <typename EnumType>
134 {
136 : Ptr(InPtr)
137 {
138 }
139
141 {
142 ++Ptr;
143 return *this;
144 }
145
147 {
148 return *Ptr;
149 }
150
151 private:
152 const EnumType* Ptr;
153
155 {
156 return Lhs.Ptr != Rhs.Ptr;
157 }
158 };
159
160 template <typename EnumType>
161 struct TEnumRange_Impl<EnumType, 0>
162 {
164
165 inline explicit TEnumRange_Impl()
166 : BeginValue(TEnumRangeTraits<EnumType>::Begin)
167 , EndValue(TEnumRangeTraits<EnumType>::End)
168 {
169 }
170
175 inline explicit TEnumRange_Impl(const EnumType InFirstValue, const EnumType InLastValue)
176 : BeginValue(static_cast<IntType>(InFirstValue))
177 , EndValue(static_cast<IntType>(InLastValue) + 1)
178 {
179 checkf(
180 BeginValue < EndValue
181 && BeginValue >= TEnumRangeTraits<EnumType>::Begin
182 && EndValue <= TEnumRangeTraits<EnumType>::End,
183 TEXT("The provided first and last enum values must be within the available range specified in the ENUM_RANGE macro."));
184 }
185
188
189 private:
190 const IntType BeginValue;
191 const IntType EndValue;
192 };
193
194 template <typename EnumType>
204}
205
207{
208 template <typename EnumType>
210 {
211 using IntType = std::underlying_type_t<EnumType>;
212
214 : Flags(IntType(InFlags))
215 {
216 }
217
219 {
220 const IntType PoppedBit = Flags & (~Flags + 1);
221 Flags ^= PoppedBit;
222 return *this;
223 }
224
225 inline EnumType operator*() const
226 {
227 const IntType Result = Flags & (~Flags + 1);
228 return (EnumType)Result;
229 }
230
231 private:
232 IntType Flags;
233
234 UE_FORCEINLINE_HINT friend bool operator!=(const TIterator& Lhs, const TIterator& Rhs)
235 {
236 return Lhs.Flags != Rhs.Flags;
237 }
238 };
239
240 template <typename EnumType>
241 struct TRange
242 {
243 explicit TRange(EnumType InFlags) : Flags(InFlags) {}
244
247
248 private:
249 EnumType Flags;
250 };
251} // namespace UE::EnumFlags::Private
252
264template <typename EnumType>
265struct TEnumRange : NEnumRangePrivate::TEnumRange_Impl<EnumType, NEnumRangePrivate::TEnumRangeTraits<EnumType>::RangeType>
266{
268
269 using Super::Super;
270};
271
284template <typename EnumType>
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
#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::EnumFlags::Private::TRange< EnumType > MakeFlagsRange(EnumType Flags)
Definition EnumRange.h:285
Definition EnumRange.h:89
Definition EnumRange.h:207
std::underlying_type_t< EnumType > IntType
Definition EnumRange.h:105
UE_FORCEINLINE_HINT TEnumContiguousIterator(IntType InValue)
Definition EnumRange.h:107
TEnumContiguousIterator & operator++()
Definition EnumRange.h:112
UE_FORCEINLINE_HINT friend bool operator!=(const TEnumContiguousIterator &Lhs, const TEnumContiguousIterator &Rhs)
Definition EnumRange.h:126
UE_FORCEINLINE_HINT EnumType operator*() const
Definition EnumRange.h:118
Definition EnumRange.h:92
@ RangeType
Definition EnumRange.h:93
TEnumRange_Impl(const EnumType InFirstValue, const EnumType InLastValue)
Definition EnumRange.h:175
TEnumContiguousIterator< EnumType > begin() const
Definition EnumRange.h:186
typename TEnumContiguousIterator< EnumType >::IntType IntType
Definition EnumRange.h:163
TEnumContiguousIterator< EnumType > end() const
Definition EnumRange.h:187
UE_FORCEINLINE_HINT TEnumRange_Impl(const EnumType InFirstValue, const EnumType InLastValue)=delete
TEnumValueArrayIterator< EnumType > begin() const
Definition EnumRange.h:201
TEnumValueArrayIterator< EnumType > end() const
Definition EnumRange.h:202
UE_FORCEINLINE_HINT TEnumRange_Impl()=default
Definition EnumRange.h:98
TEnumValueArrayIterator & operator++()
Definition EnumRange.h:140
UE_FORCEINLINE_HINT friend bool operator!=(const TEnumValueArrayIterator &Lhs, const TEnumValueArrayIterator &Rhs)
Definition EnumRange.h:154
UE_FORCEINLINE_HINT EnumType operator*() const
Definition EnumRange.h:146
UE_FORCEINLINE_HINT TEnumValueArrayIterator(const EnumType *InPtr)
Definition EnumRange.h:135
Definition EnumRange.h:266
Definition EnumRange.h:210
EnumType operator*() const
Definition EnumRange.h:225
TIterator & operator++()
Definition EnumRange.h:218
std::underlying_type_t< EnumType > IntType
Definition EnumRange.h:211
UE_FORCEINLINE_HINT friend bool operator!=(const TIterator &Lhs, const TIterator &Rhs)
Definition EnumRange.h:234
UE_FORCEINLINE_HINT TIterator(EnumType InFlags)
Definition EnumRange.h:213
Definition EnumRange.h:242
TRange(EnumType InFlags)
Definition EnumRange.h:243
Private::TIterator< EnumType > begin() const
Definition EnumRange.h:245
Private::TIterator< EnumType > end() const
Definition EnumRange.h:246