UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Storage.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2// Templates for memory, storage, containers and alignment
3
4#pragma once
5
12
13#include <initializer_list>
14#include <type_traits>
15#include <new>
16
17#include <string.h>
18
19namespace uLang
20{
21
22//------------------------------------------------------------------
23// From UnrealTemplate.h
24
30auto ULangGetData(T&& Container) -> decltype(Container.GetData())
31{
32 return Container.GetData();
33}
34
35template <typename T, size_t N>
36constexpr T* ULangGetData(T (&Container)[N])
37{
38 return Container;
39}
40
41template <typename T>
42constexpr T* ULangGetData(std::initializer_list<T> List)
43{
44 return List.begin();
45}
46
47template <typename FirstIterator, typename LastIterator>
49{
50 return View.begin();
51}
52
59{
60 return (size_t)Container.Num();
61}
62
63template <typename T, size_t N>
64constexpr size_t ULangGetNum(T (&Container)[N])
65{
66 return N;
67}
68
69template <typename T>
70constexpr size_t ULangGetNum(std::initializer_list<T> List)
71{
72 return List.size();
73}
74
75template <typename FirstIterator, typename LastIterator>
77{
78 return View.Num();
79}
80
81//------------------------------------------------------------------
82// From AlignmentTemplates.h
83
92template <typename T>
93ULANG_FORCEINLINE constexpr T AlignUp(T Val, uint64_t Alignment)
94{
95 static_assert(TIsIntegral<T>::Value || TIsPointer<T>::Value, "Align expects an integer or pointer type");
96
97 return (T)(((uint64_t)Val + Alignment - 1) & ~(Alignment - 1));
98}
99
100//------------------------------------------------------------------
101// From TypeCompatibleBytes.h
102
104template<typename ElementType>
106{
107 ElementType& Get()
108 {
109 return *std::launder(reinterpret_cast<ElementType*>(_Bytes));
110 }
111 const ElementType& Get() const
112 {
113 return *std::launder(reinterpret_cast<const ElementType*>(_Bytes));
114 }
115
116private:
117 alignas(ElementType) uint8_t _Bytes[sizeof(ElementType)];
118};
119
120//------------------------------------------------------------------
121// From UnrealTemplate.h
122
126template <typename T>
128{
129 // We don't use bitwise swapping for 'register' types because this will force them into memory and be slower.
131};
132
133
137template <typename T>
138inline typename TEnableIf<TUseBitwiseSwap<T>::Value>::Type Swap(T& A, T& B)
139{
140 if (ULANG_LIKELY(&A != &B))
141 {
144 memcpy((void*)&Temp, &A, sizeof(T));
145 memcpy((void*)&A, &B, sizeof(T));
146 memcpy((void*)&B, &Temp, sizeof(T));
148 }
149}
150
151template <typename T>
152inline typename TEnableIf<!TUseBitwiseSwap<T>::Value>::Type Swap(T& A, T& B)
153{
154 T Temp = uLang::Move(A);
155 A = uLang::Move(B);
156 B = uLang::Move(Temp);
157}
158
164{
165protected:
166 // ensure the class cannot be constructed directly
167 CNoncopyable() = default;
168 // the class should not be used polymorphically
169 ~CNoncopyable() = default;
170
171private:
172 CNoncopyable(const CNoncopyable&) = delete;
173 CNoncopyable(CNoncopyable&&) = delete;
174 CNoncopyable& operator=(const CNoncopyable&) = delete;
175 CNoncopyable& operator=(CNoncopyable&&) = delete;
176};
177
185template <typename RefType, typename AssignedType = RefType>
186struct TGuardValue : private CNoncopyable
187{
188 TGuardValue(RefType& ReferenceValue, const AssignedType& NewValue)
189 : RefValue(ReferenceValue), OldValue(Move(ReferenceValue))
190 {
191 RefValue = NewValue;
192 }
193
194 explicit TGuardValue(RefType& ReferenceValue)
195 : RefValue(ReferenceValue), OldValue(ReferenceValue)
196 {
197 }
198
200 {
201 RefValue = Move(OldValue);
202 }
203
211 {
212 return OldValue;
213 }
214
215private:
216 RefType& RefValue;
217 AssignedType OldValue;
218};
219
220template <typename Function>
221struct TGuard : private CNoncopyable
222{
223 template <typename... TArgs, typename = TEnableIfT<std::is_constructible_v<Function, TArgs&&...>>>
224 explicit TGuard(TArgs&&... Args)
225 : _Function(uLang::ForwardArg<TArgs>(Args)...)
226 {
227 }
228
230 {
231 uLang::Invoke(_Function);
232 }
233
234private:
235 Function _Function;
236};
237
238template <typename Function>
240}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define ULANG_LIKELY(x)
Definition Common.h:216
#define ULANG_IGNORE_CLASS_MEMACCESS_WARNING_START
Definition Common.h:128
#define ULANG_FORCEINLINE
Definition Common.h:188
#define ULANG_IGNORE_CLASS_MEMACCESS_WARNING_END
Definition Common.h:129
float Val(const FString &Value)
Definition UnrealMath.cpp:3163
memcpy(InputBufferBase, BinkBlocksData, BinkBlocksSize)
Definition Storage.h:164
CNoncopyable()=default
~CNoncopyable()=default
Definition Conditionals.h:95
Definition VVMEngineEnvironment.h:23
typename TEnableIf< Predicate, Result >::Type TEnableIfT
Definition Conditionals.h:109
ULANG_FORCEINLINE constexpr T AlignUp(T Val, uint64_t Alignment)
Definition Storage.h:93
ULANG_FORCEINLINE auto Invoke(FuncType &&Func, ArgTypes &&... Args) -> decltype(uLang::ForwardArg< FuncType >(Func)(uLang::ForwardArg< ArgTypes >(Args)...))
Definition Invoke.h:47
ULANG_FORCEINLINE T && ForwardArg(typename TRemoveReference< T >::Type &Obj)
Definition References.h:115
auto ULangGetData(T &&Container) -> decltype(Container.GetData())
Definition Storage.h:30
ULANG_FORCEINLINE TRemoveReference< T >::Type && Move(T &&Obj)
Definition References.h:86
size_t ULangGetNum(T &&Container)
Definition Storage.h:58
Definition Storage.h:187
TGuardValue(RefType &ReferenceValue)
Definition Storage.h:194
TGuardValue(RefType &ReferenceValue, const AssignedType &NewValue)
Definition Storage.h:188
ULANG_FORCEINLINE const AssignedType & operator*() const
Definition Storage.h:210
~TGuardValue()
Definition Storage.h:199
Definition Storage.h:222
~TGuard()
Definition Storage.h:229
TGuard(TArgs &&... Args)
Definition Storage.h:224
Definition TypeTraits.h:63
Definition TypeTraits.h:94
Definition TypeTraits.h:126
Definition Conditionals.h:49
Definition RangeView.h:9
TFirst begin() const
Definition RangeView.h:28
int32_t Num() const
Definition RangeView.h:43
Definition Storage.h:106
ElementType & Get()
Definition Storage.h:107
const ElementType & Get() const
Definition Storage.h:111
Definition Storage.h:128