Karma Engine
Loading...
Searching...
No Matches
UObjectBase.h
1
11/*=============================================================================
12 UObjectBase.h: Base class for UObject, defines low level functionality
13=============================================================================*/
14
15#pragma once
16
17#include "Core/UObjectGlobals.h"
18
19namespace Karma
20{
21 class UObject;
22 class UClass;
23
24 class UPackage;
25
27 #define RF_AllFlags (EObjectFlags)0xffffffff
28
33 class KARMA_API UObjectBase
34 {
35 protected:
37
42 FORCEINLINE void SetFlagsTo(EObjectFlags NewFlags)
43 {
44 KR_CORE_ASSERT((NewFlags & ~RF_AllFlags) == 0, "%s flagged as 0x%x but is trying to set flags to RF_AllFlags");
45 m_ObjectFlags = NewFlags;
46 }
47
48 public:
58 UObjectBase(UClass* inClass, EObjectFlags inFlags, EInternalObjectFlags inInternalFlags, UObject* inOuter, const std::string& inName);
59
65 UPackage* GetPackage() const;
66
68 UPackage* GetExternalPackage() const;
69
71 // Caution: use with vigilance
72 bool IsUnreachable() const;
73
75 FORCEINLINE void SetInternalIndex(uint32_t StoreIndex) { m_InternalIndex = StoreIndex; }
76
77 FORCEINLINE uint32_t GetInterIndex() const { return m_InternalIndex; }
78
79 private:
80
83 EObjectFlags m_ObjectFlags;
84
89 uint32_t m_InternalIndex;
90
92 UObject* m_OuterPrivate;
93
95 UClass* m_ClassPrivate;
96
98 std::string m_NamePrivate;
99
101 static bool m_bPendingKillDisabled;
102
103 private:
110 void AddObject(const std::string& name, EInternalObjectFlags inSetInternalFlags);
111
112 public:
116 void MarkAsGarbage();
117
121 void ClearGarbage();
122
124 FORCEINLINE const std::string& GetName() const
125 {
126 return m_NamePrivate;
127 }
128
129 FORCEINLINE void SetObjectName(const std::string& aName)
130 {
131 m_NamePrivate = aName;
132 }
133
135 FORCEINLINE UObject* GetOuter() const
136 {
137 return m_OuterPrivate;
138 }
139
146 UObject* GetTypedOuter(UClass* Target) const;
147
153 template<typename T>
154 T* GetTypedOuter() const
155 {
156 return (T*)GetTypedOuter(T::StaticClass());
157 }
158
163 bool IsValidLowLevel() const;
164
166 FORCEINLINE static EInternalObjectFlags FixGarbageOrPendingKillInternalObjectFlags(const EInternalObjectFlags InFlags)
167 {
168 //PRAGMA_DISABLE_DEPRECATION_WARNINGS
169 if (!(int32_t(InFlags) & (int32_t(EInternalObjectFlags::Garbage) | int32_t(EInternalObjectFlags::PendingKill))))
170 {
171 // Pass through
172 return InFlags;
173 }
174 else
175 {
176 return m_bPendingKillDisabled ?
177 EInternalObjectFlags(((int32_t(InFlags) & ~int32_t(EInternalObjectFlags::PendingKill)) | int32_t(EInternalObjectFlags::Garbage))) : // Replace PK with Garbage
178 EInternalObjectFlags(((int32_t(InFlags) & ~int32_t(EInternalObjectFlags::Garbage)) | int32_t(EInternalObjectFlags::PendingKill))); // Replace Garbage with PK
179 }
180 //PRAGMA_ENABLE_DEPRECATION_WARNINGS
181 }
182
183
184 public:
186 template <typename OtherClassType>
187 FORCEINLINE bool IsA(OtherClassType SomeBase) const
188 {
189 // We have a cyclic dependency between UObjectBase and UClass,
190 // so we use a template to allow inlining of something we haven't yet seen, because it delays compilation until the function is called.
191
192 // 'static_assert' that this thing is actually a UClass pointer or convertible to it.
193 const UClass* SomeBaseClass = SomeBase;
194 (void)SomeBaseClass;
195 KR_CORE_ASSERT(SomeBaseClass, "IsA(NULL) cannot yield meaningful results");
196
197 const UClass* ThisClass = GetClass();
198
199 // Stop the compiler doing some unnecessary branching for nullptr checks
200 // Would be interesting to write the analog of this
201 //UE_ASSUME(SomeBaseClass);
202 //UE_ASSUME(ThisClass);
203
204 KR_CORE_ASSERT(ThisClass, "Comparing with null is useless");
205
206 return IsChildOfWorkaround(ThisClass, SomeBaseClass);
207 }
208
210 FORCEINLINE UClass* GetClass() const
211 {
212 return m_ClassPrivate;
213 }
214
215 /*-------------------
216 Flags
217 -------------------*/
218
224 FORCEINLINE EObjectFlags GetFlags() const
225 {
226 KR_CORE_ASSERT((m_ObjectFlags & ~RF_AllFlags) == 0, "{0} flagged as RF_ALLFlags", GetName());
227 return m_ObjectFlags;
228 }
229
233 FORCEINLINE void SetFlags(EObjectFlags NewFlags)
234 {
235 KR_CORE_ASSERT(!(NewFlags & (RF_MarkAsNative | RF_MarkAsRootSet | RF_PendingKill | RF_Garbage)), "These flags can't be used outside of constructors / internal code");
236 KR_CORE_ASSERT(!(NewFlags & (EObjectFlags)(RF_PendingKill | RF_Garbage)) || (GetFlags() & (NewFlags & (EObjectFlags)(RF_PendingKill | RF_Garbage))) == (NewFlags & (EObjectFlags)(RF_PendingKill | RF_Garbage)), "RF_PendingKill and RF_garbage can not be set through SetFlags function. Use MarkAsGarbage() instead");
237
238 SetFlagsTo(EObjectFlags (GetFlags() | NewFlags));
239 }
240
242 FORCEINLINE void ClearFlags(EObjectFlags NewFlags)
243 {
244 KR_CORE_ASSERT(!(NewFlags & (RF_MarkAsNative | RF_MarkAsRootSet | RF_PendingKill | RF_Garbage)) || NewFlags == RF_AllFlags, "These flags can't be used outside of constructors / internal code");
245 KR_CORE_ASSERT(!(NewFlags & (EObjectFlags)(RF_PendingKill | RF_Garbage)) || (GetFlags() & (NewFlags & (EObjectFlags)(RF_PendingKill | RF_Garbage))) == RF_NoFlags, "RF_PendingKill and RF_garbage can not be cleared through ClearFlags function. Use ClearGarbage() instead");
246
247 SetFlagsTo(EObjectFlags (GetFlags() & ~NewFlags));
248 }
249
256 FORCEINLINE bool HasAnyFlags(EObjectFlags FlagsToCheck) const
257 {
258 KR_CORE_ASSERT(!(FlagsToCheck & (RF_MarkAsNative | RF_MarkAsRootSet)) || FlagsToCheck == RF_AllFlags, "Illegal flags being used"); // These flags can't be used outside of constructors / internal code
259 return (GetFlags() & FlagsToCheck) != 0;
260 }
261
268 FORCEINLINE bool HasAnyInternalFlags(EInternalObjectFlags FlagsToCheck) const
269 {
270 return GUObjectStore.IndexToObject(m_InternalIndex)->HasAnyFlags(FlagsToCheck);
271 }
272
278 FORCEINLINE void SetInternalFlags(EInternalObjectFlags FlagsToSet) const
279 {
280 FUObjectItem* ObjectItem = GUObjectStore.IndexToObject(m_InternalIndex);
281 KR_CORE_ASSERT(!(int32_t(FlagsToSet) & (int32_t(EInternalObjectFlags::PendingKill) | int32_t(EInternalObjectFlags::Garbage))) || (int32_t(FlagsToSet) & (int32_t(EInternalObjectFlags::PendingKill) | int32_t(EInternalObjectFlags::Garbage))) == (int32_t(ObjectItem->GetFlags()) & (int32_t(EInternalObjectFlags::PendingKill) | int32_t(EInternalObjectFlags::Garbage))), "");
282
283 ObjectItem->SetFlags(FlagsToSet);
284 }
285
290 FORCEINLINE void AddToRoot()
291 {
292 // Need to understand the logic of GC first
293 //GUObjectStore.IndexToObject(InternalIndex)->SetRootSet();
294 }
295
296 /*-------------------
297 Class
298 -------------------*/
299
300 private:
301
305 static bool IsChildOfWorkaround(const UClass* ObjClass, const UClass* TestClass);
306
307 //static FORCEINLINE bool IsChildOfWorkaround(const ClassType* ObjClass, const ClassType* TestCls);
308 //{
309 // return ObjClass->IsChildOf(TestCls);
310 //}
311 };
312}
FORCEINLINE BuildingBlock & IndexToObject(int32_t Index)
Definition KarmaTypes.h:308
Definition Class.h:99
Low level implementation of UObject, should not be used directly in game code Taken from UE's UObject...
Definition UObjectBase.h:34
FORCEINLINE void SetFlagsTo(EObjectFlags NewFlags)
Definition UObjectBase.h:42
FORCEINLINE const std::string & GetName() const
Definition UObjectBase.h:124
static FORCEINLINE EInternalObjectFlags FixGarbageOrPendingKillInternalObjectFlags(const EInternalObjectFlags InFlags)
Definition UObjectBase.h:166
FORCEINLINE void ClearFlags(EObjectFlags NewFlags)
Definition UObjectBase.h:242
FORCEINLINE UObject * GetOuter() const
Definition UObjectBase.h:135
T * GetTypedOuter() const
Definition UObjectBase.h:154
FORCEINLINE UClass * GetClass() const
Definition UObjectBase.h:210
FORCEINLINE bool HasAnyFlags(EObjectFlags FlagsToCheck) const
Definition UObjectBase.h:256
FORCEINLINE void SetInternalIndex(uint32_t StoreIndex)
Definition UObjectBase.h:75
FORCEINLINE void SetFlags(EObjectFlags NewFlags)
Definition UObjectBase.h:233
FORCEINLINE void SetInternalFlags(EInternalObjectFlags FlagsToSet) const
Definition UObjectBase.h:278
FORCEINLINE EObjectFlags GetFlags() const
Definition UObjectBase.h:224
FORCEINLINE bool IsA(OtherClassType SomeBase) const
Definition UObjectBase.h:187
FORCEINLINE void AddToRoot()
Definition UObjectBase.h:290
FORCEINLINE bool HasAnyInternalFlags(EInternalObjectFlags FlagsToCheck) const
Definition UObjectBase.h:268
The base class of all the game code relevant objects.
Definition Object.h:106
Definition Package.h:9
Definition UObjectGlobals.h:248