UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Optional.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
8
9namespace uLang
10{
11
15template <typename T> struct TIsOptional;
16
21template<typename OptionalType>
23{
24public:
27 {
28 new(&_Value.Get()) OptionalType(Value);
29 _Result = EResult::OK;
30 }
32 {
34 _Result = EResult::OK;
35 }
36
39 : _Result(Result)
40 {
41 ULANG_ASSERTF(Result != EResult::OK, "Must not initialize TOptional with EResult::OK without also providing a value.");
42 }
43
45 {
46 Reset();
47 }
48
51 : _Result(Value._Result)
52 {
53 if (Value._Result == EResult::OK)
54 {
55 new(&_Value.Get()) OptionalType(Value._Value.Get());
56 }
57 }
59 : _Result(Value._Result)
60 {
61 if (Value._Result == EResult::OK)
62 {
63 new(&_Value.Get()) OptionalType(uLang::MoveIfPossible(Value._Value.Get()));
64 }
65 }
66
68 {
69 if (&Value != this)
70 {
71 Reset();
72 _Result = Value._Result;
73 if (Value._Result == EResult::OK)
74 {
75 new(&_Value.Get()) OptionalType(Value._Value.Get());
76 }
77 }
78 return *this;
79 }
81 {
82 if (&Value != this)
83 {
84 Reset();
85 _Result = Value._Result;
86 if (Value._Result == EResult::OK)
87 {
88 new(&_Value.Get()) OptionalType(uLang::MoveIfPossible(Value._Value.Get()));
89 }
90 }
91 return *this;
92 }
93
95 {
96 if (&Value != &_Value.Get())
97 {
98 Reset();
99 new(&_Value.Get()) OptionalType(Value);
100 _Result = EResult::OK;
101 }
102 return *this;
103 }
105 {
106 if (&Value != &_Value.Get())
107 {
108 Reset();
110 _Result = EResult::OK;
111 }
112 return *this;
113 }
114
115 void Reset()
116 {
117 if (_Result == EResult::OK)
118 {
119 // We need a typedef here because VC won't compile the destructor call below if OptionalType itself has a member called OptionalType
121 _Value.Get().OptionalDestructOptionalType::~OptionalDestructOptionalType();
122 _Result = EResult::Unspecified;
123 }
124 }
125
126 template <typename... ArgsType>
127 void Emplace(ArgsType&&... Args)
128 {
129 Reset();
130 new(&_Value.Get()) OptionalType(ForwardArg<ArgsType>(Args)...);
131 _Result = EResult::OK;
132 }
133
134 friend bool operator==(const TOptional& lhs, const TOptional& rhs)
135 {
136 if (lhs._Result != rhs._Result)
137 {
138 return false;
139 }
140 if (lhs._Result != EResult::OK) // both unset
141 {
142 return true;
143 }
144 return lhs._Value.Get() == rhs._Value.Get();
145 }
146
147 friend bool operator!=(const TOptional& lhs, const TOptional& rhs)
148 {
149 if (lhs._Result != rhs._Result)
150 {
151 return true;
152 }
153 if (lhs._Result != EResult::OK) // both unset
154 {
155 return false;
156 }
157 return lhs._Value.Get() != rhs._Value.Get();
158 }
159
161 ULANG_FORCEINLINE bool IsSet() const { return _Result == EResult::OK; }
162 ULANG_FORCEINLINE EResult GetResult() const { return _Result; }
163 ULANG_FORCEINLINE explicit operator bool() const { return _Result == EResult::OK; }
164
165 ULANG_FORCEINLINE operator OptionalType*() { return IsSet() ? &_Value.Get() : nullptr; }
166 ULANG_FORCEINLINE operator const OptionalType*() const { return IsSet() ? &_Value.Get() : nullptr; }
167
169 const OptionalType& GetValue() const { ULANG_ASSERTF(IsSet(), "It is an error to call GetValue() on an unset TOptional. Please either assert IsSet() or use Get(DefaultValue) instead."); return _Value.Get(); }
170 OptionalType& GetValue() { ULANG_ASSERTF(IsSet(), "It is an error to call GetValue() on an unset TOptional. Please either assert IsSet() or use Get(DefaultValue) instead."); return _Value.Get(); }
171
173 const OptionalType& operator*() const { return GetValue(); }
175
176 const OptionalType* operator->() const { return &GetValue(); }
178
180 const OptionalType& Get(const OptionalType& DefaultValue) const { return IsSet() ? _Value.Get() : DefaultValue; }
181
182private:
184 EResult _Result;
185};
186
187template <typename T> struct TIsOptional { enum { Value = false }; };
188template <typename T> struct TIsOptional<TOptional<T>> { enum { Value = true }; };
189
190}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
#define ULANG_FORCEINLINE
Definition Common.h:188
#define ULANG_ASSERTF(expr, format,...)
Definition Common.h:290
Definition VVMEngineEnvironment.h:23
ULANG_FORCEINLINE TRemoveReference< T >::Type && MoveIfPossible(T &&Obj)
Definition References.h:104
EResult
Generic error codes.
Definition Common.h:352
@ Unspecified
Not sure if success or failure.
@ OK
Success.
Definition Optional.h:187
@ Value
Definition Optional.h:187
Definition Optional.h:23
OptionalType & GetValue()
Definition Optional.h:170
TOptional(OptionalType &&Value)
Definition Optional.h:31
TOptional & operator=(OptionalType &&Value)
Definition Optional.h:104
ULANG_FORCEINLINE EResult GetResult() const
Definition Optional.h:162
const OptionalType & Get(const OptionalType &DefaultValue) const
Definition Optional.h:180
TOptional & operator=(const TOptional &Value)
Definition Optional.h:67
TOptional(const TOptional &Value)
Definition Optional.h:50
void Emplace(ArgsType &&... Args)
Definition Optional.h:127
~TOptional()
Definition Optional.h:44
ULANG_FORCEINLINE bool IsSet() const
Definition Optional.h:161
TOptional(const OptionalType &Value)
Definition Optional.h:26
const OptionalType & GetValue() const
Definition Optional.h:169
TOptional(EResult Result=EResult::Unspecified)
Definition Optional.h:38
const OptionalType & operator*() const
Definition Optional.h:173
TOptional & operator=(TOptional &&Value)
Definition Optional.h:80
friend bool operator!=(const TOptional &lhs, const TOptional &rhs)
Definition Optional.h:147
friend bool operator==(const TOptional &lhs, const TOptional &rhs)
Definition Optional.h:134
TOptional & operator=(const OptionalType &Value)
Definition Optional.h:94
OptionalType * operator->()
Definition Optional.h:177
void Reset()
Definition Optional.h:115
OptionalType & operator*()
Definition Optional.h:174
TOptional(TOptional &&Value)
Definition Optional.h:58
const OptionalType * operator->() const
Definition Optional.h:176
Definition Storage.h:106
ElementType & Get()
Definition Storage.h:107