UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ValueOrError.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 "Misc/Optional.h"
7#include "Misc/TVariant.h"
8#include "Templates/Tuple.h"
10#include <type_traits>
11
12template <typename... ArgTypes>
14{
16 : Args(Forward<ArgTypes>(InArgs)...)
17 {
18 }
19
21};
22
23template <typename... ArgTypes>
25{
27 : Args(Forward<ArgTypes>(InArgs)...)
28 {
29 }
30
32};
33
34template <typename... ArgTypes>
39
40template <typename... ArgTypes>
45
56template <typename ValueType, typename ErrorType>
58{
59 static_assert(!std::is_reference_v<ValueType> && !std::is_reference_v<ErrorType>, "ValueType and ErrorType cannot be references");
60
62 struct FWrapErrorType
63 {
64 template <typename... ArgTypes>
65 FWrapErrorType(ArgTypes&&... Args) : Error(Forward<ArgTypes>(Args)...) {}
66 ErrorType Error;
67 };
68
70 struct FEmptyType
71 {
72 };
73
74 template <typename... ArgTypes, uint32... ArgIndices>
76 : Variant(TInPlaceType<ValueType>(), MoveTemp(Proxy.Args).template Get<ArgIndices>()...)
77 {
78 }
79
80 template <typename... ArgTypes, uint32... ArgIndices>
82 : Variant(TInPlaceType<FWrapErrorType>(), MoveTemp(Proxy.Args).template Get<ArgIndices>()...)
83 {
84 }
85
86public:
88 template <typename... ArgTypes>
93
95 template <typename... ArgTypes>
100
102 [[nodiscard]] UE_REWRITE bool IsValid() const
103 {
104 return Variant.template IsType<ValueType>();
105 }
106
109 {
110 return Variant.template IsType<FWrapErrorType>();
111 }
112
114 [[nodiscard]] inline ErrorType& GetError() & UE_LIFETIMEBOUND
115 {
116 return Variant.template Get<FWrapErrorType>().Error;
117 }
118 [[nodiscard]] UE_REWRITE const ErrorType& GetError() const & UE_LIFETIMEBOUND
119 {
120 return const_cast<TValueOrError*>(this)->GetError();
121 }
123 {
124 return MoveTempIfPossible(this->GetError());
125 }
126
128 [[nodiscard]] inline ErrorType* TryGetError() UE_LIFETIMEBOUND
129 {
130 if (FWrapErrorType* Wrap = Variant.template TryGet<FWrapErrorType>())
131 {
132 return &Wrap->Error;
133 }
134 return nullptr;
135 }
137 {
138 return const_cast<TValueOrError*>(this)->TryGetError();
139 }
140
142 [[nodiscard]] inline ErrorType StealError()
143 {
144 ErrorType Temp = MoveTempIfPossible(GetError());
145 Variant.template Emplace<FEmptyType>();
146 return Temp;
147 }
148
151 {
152 return Variant.template IsType<ValueType>();
153 }
154
156 [[nodiscard]] inline ValueType& GetValue() & UE_LIFETIMEBOUND
157 {
158 return Variant.template Get<ValueType>();
159 }
160 [[nodiscard]] UE_REWRITE const ValueType& GetValue() const & UE_LIFETIMEBOUND
161 {
162 return const_cast<TValueOrError*>(this)->GetValue();
163 }
165 {
166 return MoveTempIfPossible(this->GetValue());
167 }
168
171 {
172 return Variant.template TryGet<ValueType>();
173 }
175 {
176 return const_cast<TValueOrError*>(this)->TryGetValue();
177 }
178
180 [[nodiscard]] inline ValueType StealValue()
181 {
182 ValueType Temp = MoveTempIfPossible(GetValue());
183 Variant.template Emplace<FEmptyType>();
184 return Temp;
185 }
186
187private:
189};
190
191template <typename ValueType>
192class TValueOrError<ValueType, void>
193{
194 static_assert(!std::is_reference_v<ValueType>, "ValueType cannot be a reference");
195
196 template <typename... ArgTypes, uint32... ArgIndices>
198 : Value(InPlace, MoveTemp(Proxy.Args).template Get<ArgIndices>()...)
199 {
200 }
201
202public:
204 template <typename... ArgTypes>
209
212 {
213 }
214
217 {
218 return !Value.IsSet();
219 }
220
223 {
224 return Value.IsSet();
225 }
226
228 [[nodiscard]] inline ValueType& GetValue() & UE_LIFETIMEBOUND
229 {
230 return Value.GetValue();
231 }
232 [[nodiscard]] UE_REWRITE const ValueType& GetValue() const & UE_LIFETIMEBOUND
233 {
234 return const_cast<TValueOrError*>(this)->GetValue();
235 }
237 {
238 return MoveTempIfPossible(this->GetValue());
239 }
240
242 [[nodiscard]] inline ValueType* TryGetValue() UE_LIFETIMEBOUND
243 {
244 return Value.GetPtrOrNull();
245 }
247 {
248 return const_cast<TValueOrError*>(this)->TryGetValue();
249 }
250
252 [[nodiscard]] inline ValueType StealValue()
253 {
254 ValueType Temp = MoveTempIfPossible(GetValue());
255 Value.Reset();
256 return Temp;
257 }
258
259private:
261};
262
263template <typename ErrorType>
264class TValueOrError<void, ErrorType>
265{
266 static_assert(!std::is_reference_v<ErrorType>, "ErrorType cannot be a reference");
267
268 template <typename... ArgTypes, uint32... ArgIndices>
270 : Error(InPlace, MoveTemp(Proxy.Args).template Get<ArgIndices>()...)
271 {
272 }
273
274public:
277 {
278 }
279
281 template <typename... ArgTypes>
286
289 {
290 return !Error.IsSet();
291 }
292
295 {
296 return Error.IsSet();
297 }
298
300 [[nodiscard]] inline ErrorType& GetError() & UE_LIFETIMEBOUND
301 {
302 return Error.GetValue();
303 }
304 [[nodiscard]] UE_REWRITE const ErrorType& GetError() const & UE_LIFETIMEBOUND
305 {
306 return const_cast<TValueOrError*>(this)->GetError();
307 }
309 {
310 return MoveTempIfPossible(this->GetError());
311 }
312
314 [[nodiscard]] inline ErrorType* TryGetError() UE_LIFETIMEBOUND
315 {
316 return Error.GetPtrOrNull();
317 }
319 {
320 return const_cast<TValueOrError*>(this)->TryGetError();
321 }
322
324 [[nodiscard]] inline ErrorType StealError()
325 {
326 ErrorType Temp = MoveTempIfPossible(GetError());
327 Error.Reset();
328 return Temp;
329 }
330
331private:
333};
334
335template <>
337{
338public:
341 : bValue(true)
342 {
343 }
344
347 : bValue(false)
348 {
349 }
350
353 {
354 return !bValue;
355 }
356
359 {
360 return bValue;
361 }
362
363private:
364 bool bValue;
365};
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
@ InPlace
Definition CoreMiscDefines.h:162
#define UE_LIFETIMEBOUND
Definition Platform.h:812
#define UE_REWRITE
Definition Platform.h:747
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
typename UE4IntegerSequence_Private::TMakeIntegerSequenceImpl< T, N >::Type TMakeIntegerSequence
Definition IntegerSequence.h:31
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTempIfPossible(T &&Obj) noexcept
Definition UnrealTemplate.h:538
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
UE_REWRITE TValueOrError_ErrorProxy< ArgTypes... > MakeError(ArgTypes &&... Args UE_LIFETIMEBOUND)
Definition ValueOrError.h:41
UE_REWRITE TValueOrError_ValueProxy< ArgTypes... > MakeValue(ArgTypes &&... Args UE_LIFETIMEBOUND)
Definition ValueOrError.h:35
uint32_t uint32
Definition binka_ue_file_header.h:6
ValueType & GetValue() &UE_LIFETIMEBOUND
Definition ValueOrError.h:228
UE_REWRITE const ValueType * TryGetValue() const UE_LIFETIMEBOUND
Definition ValueOrError.h:246
UE_REWRITE bool HasError() const
Definition ValueOrError.h:216
TValueOrError(TValueOrError_ErrorProxy<> &&Proxy)
Definition ValueOrError.h:211
UE_REWRITE const ValueType & GetValue() const &UE_LIFETIMEBOUND
Definition ValueOrError.h:232
ValueType * TryGetValue() UE_LIFETIMEBOUND
Definition ValueOrError.h:242
ValueType StealValue()
Definition ValueOrError.h:252
UE_REWRITE ValueType && GetValue() &&UE_LIFETIMEBOUND
Definition ValueOrError.h:236
TValueOrError(TValueOrError_ValueProxy< ArgTypes... > &&Proxy)
Definition ValueOrError.h:205
UE_REWRITE bool HasValue() const
Definition ValueOrError.h:222
TValueOrError(TValueOrError_ErrorProxy< ArgTypes... > &&Proxy)
Definition ValueOrError.h:282
ErrorType & GetError() &UE_LIFETIMEBOUND
Definition ValueOrError.h:300
UE_REWRITE bool HasError() const
Definition ValueOrError.h:294
UE_REWRITE const ErrorType & GetError() const &UE_LIFETIMEBOUND
Definition ValueOrError.h:304
TValueOrError(TValueOrError_ValueProxy<> &&Proxy)
Definition ValueOrError.h:276
ErrorType * TryGetError() UE_LIFETIMEBOUND
Definition ValueOrError.h:314
UE_REWRITE ErrorType && GetError() &&UE_LIFETIMEBOUND
Definition ValueOrError.h:308
UE_REWRITE bool HasValue() const
Definition ValueOrError.h:288
ErrorType StealError()
Definition ValueOrError.h:324
UE_REWRITE const ErrorType * TryGetError() const UE_LIFETIMEBOUND
Definition ValueOrError.h:318
TValueOrError(TValueOrError_ValueProxy<> &&Proxy)
Definition ValueOrError.h:340
UE_REWRITE bool HasError() const
Definition ValueOrError.h:352
TValueOrError(TValueOrError_ErrorProxy<> &&Proxy)
Definition ValueOrError.h:346
UE_REWRITE bool HasValue() const
Definition ValueOrError.h:358
Definition ValueOrError.h:58
ErrorType StealError()
Definition ValueOrError.h:142
ErrorType & GetError() &UE_LIFETIMEBOUND
Definition ValueOrError.h:114
TValueOrError(TValueOrError_ValueProxy< ArgTypes... > &&Proxy)
Definition ValueOrError.h:89
UE_REWRITE bool HasValue() const
Definition ValueOrError.h:150
ErrorType * TryGetError() UE_LIFETIMEBOUND
Definition ValueOrError.h:128
UE_REWRITE bool HasError() const
Definition ValueOrError.h:108
UE_REWRITE const ValueType * TryGetValue() const UE_LIFETIMEBOUND
Definition ValueOrError.h:174
UE_REWRITE ValueType * TryGetValue() UE_LIFETIMEBOUND
Definition ValueOrError.h:170
UE_REWRITE const ErrorType & GetError() const &UE_LIFETIMEBOUND
Definition ValueOrError.h:118
TValueOrError(TValueOrError_ErrorProxy< ArgTypes... > &&Proxy)
Definition ValueOrError.h:96
ValueType StealValue()
Definition ValueOrError.h:180
UE_REWRITE const ErrorType * TryGetError() const UE_LIFETIMEBOUND
Definition ValueOrError.h:136
UE_REWRITE const ValueType & GetValue() const &UE_LIFETIMEBOUND
Definition ValueOrError.h:160
UE_REWRITE ValueType && GetValue() &&UE_LIFETIMEBOUND
Definition ValueOrError.h:164
ValueType & GetValue() &UE_LIFETIMEBOUND
Definition ValueOrError.h:156
UE_REWRITE ErrorType && GetError() &&UE_LIFETIMEBOUND
Definition ValueOrError.h:122
UE_REWRITE bool IsValid() const
Definition ValueOrError.h:102
Definition TVariant.h:48
@ false
Definition radaudio_common.h:23
Definition TVariant.h:13
Definition IntegerSequence.h:9
Definition Optional.h:131
Definition Tuple.h:652
Definition ValueOrError.h:25
TValueOrError_ErrorProxy(ArgTypes &&... InArgs UE_LIFETIMEBOUND)
Definition ValueOrError.h:26
TTuple< ArgTypes &&... > Args
Definition ValueOrError.h:31
Definition ValueOrError.h:14
TTuple< ArgTypes &&... > Args
Definition ValueOrError.h:20
TValueOrError_ValueProxy(ArgTypes &&... InArgs UE_LIFETIMEBOUND)
Definition ValueOrError.h:15