UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
NonNullPointer.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"
8#include "Misc/NotNull.h"
9#include "Misc/OptionalFwd.h"
10#include "Templates/Requires.h"
12
13#include <type_traits>
14
15class FArchive;
16enum class EDefaultConstructNonNullPtr { UnsafeDoNotUse }; // So we can construct TNonNullPtrs
17
21template<typename ObjectType>
23{
24public:
25
33
38 {
39 // Essentially static_assert(false), but this way prevents GCC/Clang from crying wolf by merely inspecting the function body
40 static_assert(sizeof(ObjectType) == 0, "Tried to initialize TNonNullPtr with a null pointer!");
41 }
42
46 inline TNonNullPtr(ObjectType* InObject)
47 : Object(InObject)
48 {
49 ensureMsgf(InObject, TEXT("Tried to initialize TNonNullPtr with a null pointer!"));
50 }
51
52#if UE_ENABLE_NOTNULL_WRAPPER
56 template <
57 typename OtherObjectType
58 UE_REQUIRES(std::is_convertible_v<OtherObjectType, ObjectType*>)
59 >
61 : Object(InObject)
62 {
63 }
64#endif
65
69 template <
70 typename OtherObjectType
71 UE_REQUIRES(std::is_convertible_v<OtherObjectType*, ObjectType*>)
72 >
77
82 {
83 // Essentially static_assert(false), but this way prevents GCC/Clang from crying wolf by merely inspecting the function body
84 static_assert(sizeof(ObjectType) == 0, "Tried to assign a null pointer to a TNonNullPtr!");
85 return *this;
86 }
87
91 inline TNonNullPtr& operator=(ObjectType* InObject)
92 {
93 ensureMsgf(InObject, TEXT("Tried to assign a null pointer to a TNonNullPtr!"));
94 Object = InObject;
95 return *this;
96 }
97
98#if UE_ENABLE_NOTNULL_WRAPPER
102 template <
103 typename OtherObjectType
104 UE_REQUIRES(std::is_convertible_v<OtherObjectType, ObjectType*>)
105 >
107 {
108 Object = InObject;
109 return *this;
110 }
111#endif
112
116 template <
117 typename OtherObjectType
118 UE_REQUIRES(std::is_convertible_v<OtherObjectType*, ObjectType*>)
119 >
121 {
122 Object = Other.Get();
123 return *this;
124 }
125
130 {
131 return Object == Other.Object;
132 }
133#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
135 {
136 return Object != Other.Object;
137 }
138#endif
139
143 template <
144 typename OtherObjectType
145 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<ObjectType*>() == std::declval<OtherObjectType*>()))
146 >
148 {
149 return Object == Other;
150 }
151 template <
152 typename OtherObjectType
153 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<OtherObjectType*>() == std::declval<ObjectType*>()))
154 >
156 {
157 return Lhs == Rhs.Object;
158 }
159#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
160 template <
161 typename OtherObjectType
162 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<ObjectType*>() == std::declval<OtherObjectType*>()))
163 >
165 {
166 return Object != Other;
167 }
168 template <
169 typename OtherObjectType
170 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<OtherObjectType*>() == std::declval<ObjectType*>()))
171 >
173 {
174 return Lhs != Rhs.Object;
175 }
176#endif
177
181 inline operator ObjectType*() const
182 {
183 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
184 return Object;
185 }
186
190 inline ObjectType* Get() const
191 {
192 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
193 return Object;
194 }
195
199 inline ObjectType& operator*() const
200 {
201 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
202 return *Object;
203 }
204
208 inline ObjectType* operator->() const
209 {
210 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
211 return Object;
212 }
213
214 /*
215 * WARNING: Hack that can be used under extraordinary circumstances. Pointers here
216 * should always be valid but might be in the EDefaultConstructNonNullPtr state
217 * during initialization.
218 */
220 {
221 return Object != nullptr;
222 }
223
227 explicit operator bool() const = delete;
228
230 // Start - intrusive TOptional<TNonNullPtr> state //
232 constexpr static bool bHasIntrusiveUnsetOptionalState = true;
235 : Object(nullptr)
236 {
237 }
239 {
240 return Object == nullptr;
241 }
243 // End - intrusive TOptional<TNonNullPtr> state //
245
246private:
248 {
249 return PointerHash(InPtr.Object);
250 }
251
253 ObjectType* Object;
254};
255
257template<typename ObjectType>
259{
260 return Optional.IsSet() ? Optional->Get() : nullptr;
261}
262
263#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_4
264#include "Templates/EnableIf.h"
266#endif
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TYPE_OF_NULLPTR TYPE_OF_NULLPTR
The type of the C++ nullptr keyword.
Definition Platform.h:1157
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
T TNotNull
Definition NotNull.h:307
const bool
Definition NetworkReplayStreaming.h:178
EDefaultConstructNonNullPtr
Definition NonNullPointer.h:16
ObjectType * GetRawPointerOrNull(const TOptional< TNonNullPtr< ObjectType > > &Optional)
Definition NonNullPointer.h:258
#define UE_REQUIRES(...)
Definition Requires.h:86
#define UE_REQUIRES_EXPR(...)
Definition Requires.h:89
uint32 PointerHash(const void *Key)
Definition TypeHash.h:91
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
Definition NonNullPointer.h:23
ObjectType * Get() const
Definition NonNullPointer.h:190
friend UE_FORCEINLINE_HINT uint32 GetTypeHash(const TNonNullPtr &InPtr)
Definition NonNullPointer.h:247
TNonNullPtr & operator=(ObjectType *InObject)
Definition NonNullPointer.h:91
UE_FORCEINLINE_HINT TNonNullPtr(EDefaultConstructNonNullPtr)
Definition NonNullPointer.h:29
UE_FORCEINLINE_HINT bool operator==(FIntrusiveUnsetOptionalState) const
Definition NonNullPointer.h:238
UE_FORCEINLINE_HINT TNonNullPtr(FIntrusiveUnsetOptionalState)
Definition NonNullPointer.h:234
UE_FORCEINLINE_HINT bool IsInitialized() const
Definition NonNullPointer.h:219
UE_FORCEINLINE_HINT bool operator!=(OtherObjectType *Other) const
Definition NonNullPointer.h:164
UE_FORCEINLINE_HINT bool operator==(const TNonNullPtr &Other) const
Definition NonNullPointer.h:129
TNonNullPtr & operator=(const TNonNullPtr< OtherObjectType > &Other)
Definition NonNullPointer.h:120
UE_FORCEINLINE_HINT TNonNullPtr(TYPE_OF_NULLPTR)
Definition NonNullPointer.h:37
TNonNullPtr & operator=(TYPE_OF_NULLPTR)
Definition NonNullPointer.h:81
UE_FORCEINLINE_HINT bool operator!=(const TNonNullPtr &Other) const
Definition NonNullPointer.h:134
ObjectType & operator*() const
Definition NonNullPointer.h:199
static constexpr bool bHasIntrusiveUnsetOptionalState
Definition NonNullPointer.h:232
TNonNullPtr(ObjectType *InObject)
Definition NonNullPointer.h:46
UE_FORCEINLINE_HINT friend bool operator!=(OtherObjectType *Lhs, const TNonNullPtr &Rhs)
Definition NonNullPointer.h:172
UE_FORCEINLINE_HINT TNonNullPtr(const TNonNullPtr< OtherObjectType > &Other)
Definition NonNullPointer.h:73
ObjectType * operator->() const
Definition NonNullPointer.h:208
UE_FORCEINLINE_HINT bool operator==(OtherObjectType *Other) const
Definition NonNullPointer.h:147
UE_FORCEINLINE_HINT friend bool operator==(OtherObjectType *Lhs, const TNonNullPtr &Rhs)
Definition NonNullPointer.h:155
Definition IntrusiveUnsetOptionalState.h:71
Definition Optional.h:131