Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
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"
6#include "Misc/AssertionMacros.h"
7#include "Misc/OptionalFwd.h"
8#include "Templates/EnableIf.h"
9#include "Templates/PointerIsConvertibleFromTo.h"
10
11class FArchive;
12enum class EDefaultConstructNonNullPtr { UnsafeDoNotUse }; // So we can construct TNonNullPtrs
13
14namespace UE::Core::Private::NonNullPtr {
15template <typename...>
16using TVoid = void;
17
18/**
19 * Version of `::TPointerIsConvertibleFromTo` that produces an incomplete type
20 * when either `From` or `To` are incomplete types
21 */
22template <typename, typename, typename = void>
23struct TPointerIsConvertibleFromTo;
24
25/**
26 * Specialization of
27 * `UE::Core::Private::NonNullPtr::TPointerIsConvertibleFromTo` for complete
28 * non-function types
29 */
30template <typename From, typename To>
31struct TPointerIsConvertibleFromTo<From, To, TVoid<decltype(sizeof(From)), decltype(sizeof(To))>>
33{
34};
35
36/**
37 * Specialization of
38 * `UE::Core::Private::NonNullPtr::TPointerIsConvertibleFromTo` for function
39 * types, which are always complete types
40 */
41template <typename Result1, typename... Args1, typename Result2, typename... Args2>
44{
45};
46}
47
48/**
49 * TNonNullPtr is a non-nullable, non-owning, raw/naked/unsafe pointer.
50 */
51template<typename ObjectType>
53{
54public:
55
56 /**
57 * Hack that can be used under extraordinary circumstances
58 */
60 : Object(nullptr)
61 {
62 }
63
64 /**
65 * nullptr constructor - not allowed.
66 */
67 FORCEINLINE TNonNullPtr(TYPE_OF_NULLPTR)
68 {
69 // Essentially static_assert(false), but this way prevents GCC/Clang from crying wolf by merely inspecting the function body
70 static_assert(sizeof(ObjectType) == 0, "Tried to initialize TNonNullPtr with a null pointer!");
71 }
72
73 /**
74 * Constructs a non-null pointer from the provided pointer. Must not be nullptr.
75 */
76 FORCEINLINE TNonNullPtr(ObjectType* InObject)
78 {
79 ensureMsgf(InObject, TEXT("Tried to initialize TNonNullPtr with a null pointer!"));
80 }
81
82 /**
83 * Constructs a non-null pointer from another non-null pointer
84 */
85 template <
86 typename OtherObjectType,
88 >
89 FORCEINLINE TNonNullPtr(const TNonNullPtr<OtherObjectType>& Other)
91 {
92 }
93
94 /**
95 * Assignment operator taking a nullptr - not allowed.
96 */
97 FORCEINLINE TNonNullPtr& operator=(TYPE_OF_NULLPTR)
98 {
99 // Essentially static_assert(false), but this way prevents GCC/Clang from crying wolf by merely inspecting the function body
100 static_assert(sizeof(ObjectType) == 0, "Tried to assign a null pointer to a TNonNullPtr!");
101 }
102
103 /**
104 * Assignment operator taking a pointer
105 */
106 FORCEINLINE TNonNullPtr& operator=(ObjectType* InObject)
107 {
108 ensureMsgf(InObject, TEXT("Tried to assign a null pointer to a TNonNullPtr!"));
110 return *this;
111 }
112
113 /**
114 * Assignment operator taking another TNonNullPtr
115 */
116 template <typename OtherObjectType>
118 {
120 return *this;
121 }
122
123 /**
124 * Returns the internal pointer
125 */
126 FORCEINLINE operator ObjectType*() const
127 {
128 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
129 return Object;
130 }
131
132 /**
133 * Returns the internal pointer
134 */
135 FORCEINLINE ObjectType* Get() const
136 {
137 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
138 return Object;
139 }
140
141 /**
142 * Dereference operator returns a reference to the object this pointer points to
143 */
144 FORCEINLINE ObjectType& operator*() const
145 {
146 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
147 return *Object;
148 }
149
150 /**
151 * Arrow operator returns a pointer to this pointer's object
152 */
153 FORCEINLINE ObjectType* operator->() const
154 {
155 ensureMsgf(Object, TEXT("Tried to access null pointer!"));
156 return Object;
157 }
158
159private:
160
161 /** The object we're holding a reference to. */
162 ObjectType* Object;
163
164};
165
166
167/**
168 * Specialization of TOptional for TNonNullPtr value types
169 */
170template<typename OptionalType>
171struct TOptional<TNonNullPtr<OptionalType>>
172{
173public:
174 /** Construct an OptionaType with a valid value. */
175 TOptional(const TNonNullPtr<OptionalType>& InPointer)
177 {
178 }
179
180 /** Construct an OptionalType with no value; i.e. unset */
182 : Pointer(nullptr)
183 {
184 }
185
186 /** Construct an OptionalType with an invalid value. */
188 : TOptional()
189 {
190 }
191
193 {
195 return *this;
196 }
197
198 TOptional& operator=(OptionalType* InPointer)
199 {
201 return *this;
202 }
203
204 void Reset()
205 {
206 Pointer = nullptr;
207 }
208
209 OptionalType* Emplace(OptionalType* InPointer)
210 {
212 return InPointer;
213 }
214
215 bool operator==(const TOptional& rhs) const
216 {
217 return Pointer == rhs.Pointer;
218 }
219
220 bool operator!=(const TOptional& rhs) const
221 {
222 return Pointer != rhs.Pointer;
223 }
224
225 friend FArchive& operator<<(FArchive& Ar, TOptional& Optional)
226 {
228 return Ar;
229 }
230
231 /** @return true when the value is meaningful; false if calling GetValue() is undefined. */
232 bool IsSet() const { return Pointer != nullptr; }
233 FORCEINLINE explicit operator bool() const { return Pointer != nullptr; }
234
235 /** @return The optional value; undefined when IsSet() returns false. */
236 OptionalType* GetValue() const { checkf(IsSet(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return Pointer; }
237
238 OptionalType* operator->() const { return Pointer; }
239
240 OptionalType& operator*() const { return *Pointer; }
241
242 /** @return The optional value when set; DefaultValue otherwise. */
243 OptionalType* Get(OptionalType* DefaultPointer) const { return IsSet() ? Pointer : DefaultPointer; }
244
245private:
246 OptionalType* Pointer;
247};
#define ensureMsgf( InExpression, InFormat,...)
#define checkf(expr, format,...)
EDefaultConstructNonNullPtr
#define FORCEINLINE
Definition Platform.h:644
FORCEINLINE TNonNullPtr(const TNonNullPtr< OtherObjectType > &Other)
FORCEINLINE TNonNullPtr & operator=(ObjectType *InObject)
FORCEINLINE operator ObjectType *() const
FORCEINLINE ObjectType * operator->() const
FORCEINLINE TNonNullPtr & operator=(TYPE_OF_NULLPTR)
FORCEINLINE ObjectType * Get() const
FORCEINLINE TNonNullPtr(EDefaultConstructNonNullPtr)
FORCEINLINE TNonNullPtr(TYPE_OF_NULLPTR)
ObjectType * Object
FORCEINLINE ObjectType & operator*() const
FORCEINLINE TNonNullPtr(ObjectType *InObject)
FORCEINLINE TEnableIf< UE::Core::Private::NonNullPtr::TPointerIsConvertibleFromTo< OtherObjectType, ObjectType >::Value, TNonNullPtr & >::Type operator=(const TNonNullPtr< OtherObjectType > &Other)
Definition Vector.h:40
TOptional & operator=(const TOptional &Other)
OptionalType * Emplace(OptionalType *InPointer)
bool operator==(const TOptional &rhs) const
TOptional & operator=(OptionalType *InPointer)
bool operator!=(const TOptional &rhs) const
TOptional(const TNonNullPtr< OptionalType > &InPointer)
OptionalType * Get(OptionalType *DefaultPointer) const