UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
InlineValue.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 "HAL/UnrealMemory.h"
10#include "Templates/Decay.h"
13
20template<typename BaseType, uint16 DesiredMaxInlineSize=64, uint8 DefaultAlignment=8>
22{
23public:
28 : bIsValid(false), bInline(true)
29 {
30 }
31
35 template<
36 typename T,
38 >
40 : bIsValid(false)
41 {
43 }
44
48 template<typename... ArgTypes>
50 : bIsValid(false)
51 {
53 }
54
59 {
60 Reset();
61 }
62
67 : bIsValid(false), bInline(true)
68 {
69 *this = MoveTemp(In);
70 }
72 {
73 Reset(MoveTemp(In));
74 return *this;
75 }
76
80 TInlineValue(const TInlineValue& In) = delete;
81 TInlineValue& operator=(const TInlineValue& In) = delete;
82
86 template<typename T>
88 {
89 *this = TInlineValue(Forward<T>(In));
90 return *this;
91 }
92
97 {
98 Reset();
99
100 if (In.bIsValid)
101 {
102 // Steal the object contained within 'In'
103 bIsValid = true;
104 In.bIsValid = false;
105
106 bInline = In.bInline;
107 Data = In.Data;
108 }
109 }
110
114 void Reset()
115 {
116 if (bIsValid)
117 {
118 BaseType& Value = GetValue();
119 // Set bIsValid immediately to avoid double-deletion on potential re-entry
120 bIsValid = false;
121 Value.~BaseType();
122 ConditionallyDestroyAllocation();
123 }
124 }
125
129 template <typename T, typename... ArgsType>
130 inline void Emplace(ArgsType&&... Args)
131 {
132 static_assert(TPointerIsConvertibleFromTo<T, BaseType>::Value, "T must derive from BaseType.");
133 Reset();
135 }
136
141 {
142 return bIsValid;
143 }
144
149 inline BaseType& GetValue() const
150 {
151 checkf(bIsValid, TEXT("It is an error to call GetValue() on an invalid TInlineValue. Please either check IsValid() or use Get(DefaultValue) instead."));
152 return bInline ? (BaseType&)Data : **((BaseType**)&Data);
153 }
154
159 UE_FORCEINLINE_HINT const BaseType& Get(const BaseType& Default) const
160 {
161 return bIsValid ? GetValue() : Default;
162 }
163
168 UE_FORCEINLINE_HINT BaseType* GetPtr(BaseType* Default = nullptr) const
169 {
170 return bIsValid ? &GetValue() : Default;
171 }
172
173 UE_FORCEINLINE_HINT BaseType& operator*() const { return GetValue(); }
174 UE_FORCEINLINE_HINT BaseType* operator->() const { return &GetValue(); }
175
181 {
182 Reset();
183
185
186 ConditionallyAllocateObject(InSize, InAlignment);
187 bIsValid = true;
188
189 return &GetValue();
190 }
191
192private:
193
194 template<typename T, typename... ArgsType>
195 void InitializeFrom(ArgsType&&... Args)
196 {
197 bInline = sizeof(T) <= MaxInlineSize && alignof(T) <= DefaultAlignment;
198
199 // Allocate the object
200 ConditionallyAllocateObject(sizeof(T), alignof(T));
201 bIsValid = true;
202
203 // Placement new our value into the structure
204 ::new((void*)&GetValue()) T(Forward<ArgsType>(Args)...);
205
206 checkf((void*)&GetValue() == (BaseType*)((T*)&GetValue()), TEXT("TInlineValue cannot operate with multiple inheritance objects."));
207 }
208
209 void ConditionallyAllocateObject(uint32 Size, uint32 Alignment)
210 {
211 if (!bInline)
212 {
213 // We store a *ptr* to the data in the aligned bytes, when we allocate on the heap
214 BaseType* Allocation = (BaseType*)FMemory::Malloc(Size, Alignment);
215 *((BaseType**)&Data) = Allocation;
216 }
217 }
218
219 void ConditionallyDestroyAllocation()
220 {
221 if (!bInline)
222 {
223 FMemory::Free(*((void**)&Data));
224 }
225 }
226
227private:
229 enum { MaxInlineSize = DesiredMaxInlineSize < sizeof(void*) ? sizeof(void*) : DesiredMaxInlineSize };
230
233
235 bool bIsValid : 1;
236
238 bool bInline : 1;
239};
240
244template<typename BaseType, typename UserType, uint8 MaxInlineSize = 64, uint8 DefaultAlignment = 8, typename... ArgsType>
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
EInPlace
Definition CoreMiscDefines.h:162
#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
return true
Definition ExternalRpcRegistry.cpp:601
TInlineValue< BaseType, MaxInlineSize, DefaultAlignment > MakeInlineValue(ArgsType... Args)
Definition InlineValue.h:245
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32 Size
Definition VulkanMemory.cpp:4034
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition EnableIf.h:20
Definition InlineValue.h:22
void Reset()
Definition InlineValue.h:114
TInlineValue(T &&In)
Definition InlineValue.h:39
UE_FORCEINLINE_HINT BaseType * operator->() const
Definition InlineValue.h:174
UE_FORCEINLINE_HINT BaseType & operator*() const
Definition InlineValue.h:173
TInlineValue & operator=(const TInlineValue &In)=delete
TInlineValue()
Definition InlineValue.h:27
TInlineValue(const TInlineValue &In)=delete
void * Reserve(uint32 InSize, uint32 InAlignment)
Definition InlineValue.h:180
UE_FORCEINLINE_HINT bool IsValid() const
Definition InlineValue.h:140
UE_FORCEINLINE_HINT const BaseType & Get(const BaseType &Default) const
Definition InlineValue.h:159
void Reset(TInlineValue &&In)
Definition InlineValue.h:96
TInlineValue(TInlineValue &&In)
Definition InlineValue.h:66
void Emplace(ArgsType &&... Args)
Definition InlineValue.h:130
TEnableIf< TPointerIsConvertibleFromTo< typenameTDecay< T >::Type, BaseType >::Value, TInlineValue & >::Type operator=(T &&In)
Definition InlineValue.h:87
UE_FORCEINLINE_HINT BaseType * GetPtr(BaseType *Default=nullptr) const
Definition InlineValue.h:168
~TInlineValue()
Definition InlineValue.h:58
BaseType & GetValue() const
Definition InlineValue.h:149
TInlineValue(EInPlace, ArgTypes &&... Args)
Definition InlineValue.h:49
TInlineValue & operator=(TInlineValue &&In)
Definition InlineValue.h:71
@ false
Definition radaudio_common.h:23
static FORCENOINLINE CORE_API void Free(void *Original)
Definition UnrealMemory.cpp:685
Definition TypeCompatibleBytes.h:17
Definition PointerIsConvertibleFromTo.h:60