Karma Engine
Loading...
Searching...
No Matches
UObjectGlobals.h
1#pragma once
2
3#include "krpch.h"
4
5namespace Karma
6{
7 class UObject;
8 class AActor;
9 class UClass;
10 class UPackage;
11 //enum EObjectFlags;
12 //enum class EInternalObjectFlags;
13
14 #define INVALID_OBJECT (UObject*)-1
15
16 // 32-bit signed integer <- find or write appropriate class for such type
17 typedef signed int int32;
18
19 enum { INDEX_NONE = -1 };
20
21 // Got from EnumClassFlags.h
22 template<typename Enum>
23 constexpr bool EnumHasAnyFlags(Enum Flags, Enum Contains)
24 {
25 return(((__underlying_type(Enum))Flags) & (__underlying_type(Enum))Contains) != 0;
26 }
27
34 enum EClassFlags
35 {
37 CLASS_None = 0x00000000u,
39 CLASS_Abstract = 0x00000001u,
41 CLASS_DefaultConfig = 0x00000002u,
43 CLASS_Config = 0x00000004u,
45 CLASS_Transient = 0x00000008u,
47 CLASS_Optional = 0x00000010u,
49 CLASS_MatchedSerializers = 0x00000020u,
51 CLASS_ProjectUserConfig = 0x00000040u,
53 CLASS_Native = 0x00000080u,
55 //CLASS_NoExport UE_DEPRECATED(5.1, "CLASS_NoExport should no longer be used. It is no longer being set by engine code.") = 0x00000100u,
57 CLASS_NotPlaceable = 0x00000200u,
59 CLASS_PerObjectConfig = 0x00000400u,
60
62 CLASS_ReplicationDataIsSetUp = 0x00000800u,
63
65 CLASS_EditInlineNew = 0x00001000u,
67 CLASS_CollapseCategories = 0x00002000u,
69 CLASS_Interface = 0x00004000u,
71 //CLASS_CustomConstructor UE_DEPRECATED(5.1, "CLASS_CustomConstructor should no longer be used. It is no longer being set by engine code.") = 0x00008000u,
73 CLASS_Const = 0x00010000u,
74
76 CLASS_NeedsDeferredDependencyLoading = 0x00020000u,
77
79 CLASS_CompiledFromBlueprint = 0x00040000u,
80
82 CLASS_MinimalAPI = 0x00080000u,
83
85 CLASS_RequiredAPI = 0x00100000u,
86
88 CLASS_DefaultToInstanced = 0x00200000u,
89
91 CLASS_TokenStreamAssembled = 0x00400000u,
93 CLASS_HasInstancedReference = 0x00800000u,
95 CLASS_Hidden = 0x01000000u,
97 CLASS_Deprecated = 0x02000000u,
99 CLASS_HideDropDown = 0x04000000u,
101 CLASS_GlobalUserConfig = 0x08000000u,
103 CLASS_Intrinsic = 0x10000000u,
105 CLASS_Constructed = 0x20000000u,
107 CLASS_ConfigDoNotCheckDefaults = 0x40000000u,
109 CLASS_NewerVersionExists = 0x80000000u,
110 };
111
118 enum EPackageFlags
119 {
120 PKG_None = 0x00000000,
121 PKG_NewlyCreated = 0x00000001,
122 PKG_ClientOptional = 0x00000002,
123 PKG_ServerSideOnly = 0x00000004,
124 PKG_CompiledIn = 0x00000010,
125 PKG_ForDiffing = 0x00000020,
126 PKG_EditorOnly = 0x00000040,
127 PKG_Developer = 0x00000080,
128 PKG_UncookedOnly = 0x00000100,
129 PKG_Cooked = 0x00000200,
130 PKG_ContainsNoAsset = 0x00000400,
131 PKG_NotExternallyReferenceable = 0x00000800,
132 // PKG_Unused = 0x00001000,
133 PKG_UnversionedProperties = 0x00002000,
134 PKG_ContainsMapData = 0x00004000,
135 PKG_IsSaving = 0x00008000,
136 PKG_Compiling = 0x00010000,
137 PKG_ContainsMap = 0x00020000,
138 PKG_RequiresLocalizationGather = 0x00040000,
139 // PKG_Unused = 0x00080000,
140 PKG_PlayInEditor = 0x00100000,
141 PKG_ContainsScript = 0x00200000,
142 PKG_DisallowExport = 0x00400000,
143 // PKG_Unused = 0x00800000,
144 // PKG_Unused = 0x01000000,
145 // PKG_Unused = 0x02000000,
146 // PKG_Unused = 0x04000000,
147 PKG_CookGenerated = 0x08000000,
148 PKG_DynamicImports = 0x10000000,
149 PKG_RuntimeGenerated = 0x20000000,
150 PKG_ReloadingForCooker = 0x40000000,
151 PKG_FilterEditorOnly = 0x80000000,
152
153 // Transient Flags are cleared when serializing to or from PackageFileSummary
154 PKG_TransientFlags = PKG_NewlyCreated | PKG_IsSaving | PKG_ReloadingForCooker,
155 };
156
160 enum EObjectFlags
161 {
162 // Do not add new flags unless they truly belong here. There are alternatives.
163 // if you change any the bit of any of the RF_Load flags, then you will need legacy serialization
164 RF_NoFlags = 0x00000000,
165
166 // This first group of flags mostly has to do with what kind of object it is. Other than transient, these are the persistent object flags.
167 // The garbage collector also tends to look at these.
168 RF_Public = 0x00000001,
169 RF_Standalone = 0x00000002,
170 RF_MarkAsNative = 0x00000004,
171 RF_Transactional = 0x00000008,
172 RF_ClassDefaultObject = 0x00000010,
173 RF_ArchetypeObject = 0x00000020,
174 RF_Transient = 0x00000040,
175
176 // This group of flags is primarily concerned with garbage collection.
177 RF_MarkAsRootSet = 0x00000080,
178 RF_TagGarbageTemp = 0x00000100,
179
180 // The group of flags tracks the stages of the lifetime of a uobject
181 RF_NeedInitialization = 0x00000200,
182 RF_NeedLoad = 0x00000400,
183 RF_KeepForCooker = 0x00000800,
184 RF_NeedPostLoad = 0x00001000,
185 RF_NeedPostLoadSubobjects = 0x00002000,
186 RF_NewerVersionExists = 0x00004000,
187 RF_BeginDestroyed = 0x00008000,
188 RF_FinishDestroyed = 0x00010000,
189
190 // Misc. Flags
191 RF_BeingRegenerated = 0x00020000,
192 RF_DefaultSubObject = 0x00040000,
193 RF_WasLoaded = 0x00080000,
194 RF_TextExportTransient = 0x00100000,
195 RF_LoadCompleted = 0x00200000,
196 RF_InheritableComponentTemplate = 0x00400000,
197 RF_DuplicateTransient = 0x00800000,
198 RF_StrongRefOnFrame = 0x01000000,
199 RF_NonPIEDuplicateTransient = 0x02000000,
200 RF_Dynamic /*UE_DEPRECATED(5.0, "RF_Dynamic should no longer be used. It is no longer being set by engine code.")*/ = 0x04000000,
201 RF_WillBeLoaded = 0x08000000,
202 RF_HasExternalPackage = 0x10000000,
203
204 // RF_Garbage and RF_PendingKill are mirrored in EInternalObjectFlags because checking the internal flags is much faster for the Garbage Collector
205 // while checking the object flags is much faster outside of it where the Object pointer is already available and most likely cached.
206 // RF_PendingKill is mirrored in EInternalObjectFlags because checking the internal flags is much faster for the Garbage Collector
207 // while checking the object flags is much faster outside of it where the Object pointer is already available and most likely cached.
208
209 RF_PendingKill /*UE_DEPRECATED(5.0, "RF_PendingKill should not be used directly. Make sure references to objects are released using one of the existing engine callbacks or use weak object pointers.")*/ = 0x20000000,
210 RF_Garbage /*UE_DEPRECATED(5.0, "RF_Garbage should not be used directly. Use MarkAsGarbage and ClearGarbage instead.")*/ = 0x40000000,
211 RF_AllocatedInSharedPage = 0x80000000
212 };
213
220 enum class EInternalObjectFlags : int32
221 {
222 None = 0,
223
224 LoaderImport = 1 << 20,
225 Garbage = 1 << 21,
226 PersistentGarbage = 1 << 22,
227 ReachableInCluster = 1 << 23,
228 ClusterRoot = 1 << 24,
229 Native = 1 << 25,
230 Async = 1 << 26,
231 AsyncLoading = 1 << 27,
232 Unreachable = 1 << 28,
233 PendingKill /*UE_DEPRECATED(5.0, "PendingKill flag should no longer be used. Use Garbage flag instead.")*/ = 1 << 29,
234 RootSet = 1 << 30,
235 PendingConstruction = 1 << 31,
236
237 GarbageCollectionKeepFlags = Native | Async | AsyncLoading | LoaderImport,
238 MirroredFlags = Garbage | PendingKill,
239
240 //~ Make sure this is up to date!
241 AllFlags = LoaderImport | Garbage | PersistentGarbage | ReachableInCluster | ClusterRoot | Native | Async | AsyncLoading | Unreachable | PendingKill | RootSet | PendingConstruction
242 };
243
248 {
249 // Pointer to the allocated object
250 class UObjectBase* m_Object;
251
252 // Internal Flags
253 int32_t m_InternalFlags;
254
255 FUObjectItem() : m_Object(nullptr), m_InternalFlags(0)
256 {
257 }
258
259 // Non-copyable
260 FUObjectItem(FUObjectItem&&) = delete;
261 FUObjectItem(const FUObjectItem&) = delete;
262 FUObjectItem& operator=(FUObjectItem&&) = delete;
263 FUObjectItem& operator=(const FUObjectItem&) = delete;
264
265 // digression from ue, from threadatomicallysetflag
266 FORCEINLINE void SetFlags(EInternalObjectFlags FlagsToSet)
267 {
268 KR_CORE_ASSERT((int32_t(FlagsToSet) & ~int32_t(EInternalObjectFlags::AllFlags)) == 0, "");
269
270 m_InternalFlags = int32_t(FlagsToSet);
271 }
272
273 FORCEINLINE EInternalObjectFlags GetFlags() const
274 {
275 //return FPlatformAtomics::AtomicRead_Relaxed((int32*)&Flags);
276 return EInternalObjectFlags(m_InternalFlags);
277 }
278
279 FORCEINLINE bool HasAnyFlags(EInternalObjectFlags InFlags) const
280 {
281 return !!(m_InternalFlags & int32_t(InFlags));
282 }
283 };
284
294 class KARMA_API FUObjectArray : public KarmaVector<FUObjectItem*>
295 {
296 public:
301 {
302 public:
303 enum EEndTagType
304 {
305 EndTag
306 };
307
314 TIterator(const FUObjectArray& InArray, bool bOnlyGCedObjects = false) :
315 m_Array(InArray),
316 m_Index(-1),
317 m_CurrentObject(nullptr)
318 {
319 if (bOnlyGCedObjects)
320 {
321 //m_Index = m_Array.ObjLastNonGCIndex;
322 }
323 Advance();
324 }
325
332 TIterator(EEndTagType, const TIterator& InIter) :
333 m_Array(InIter.m_Array)
334 {
335 m_Index = m_Array.Num();
336 }
337
341 FORCEINLINE void operator++()
342 {
343 Advance();
344 }
345
346 bool operator==(const TIterator& Rhs) const { return m_Index == Rhs.m_Index; }
347 bool operator!=(const TIterator& Rhs) const { return m_Index != Rhs.m_Index; }
348
350 FORCEINLINE explicit operator bool() const
351 {
352 return !!m_CurrentObject;
353 }
355 FORCEINLINE bool operator !() const
356 {
357 return !(bool)*this;
358 }
359
360 FORCEINLINE int32 GetIndex() const
361 {
362 return m_Index;
363 }
364
365 protected:
366
372 FORCEINLINE FUObjectItem* GetObject() const
373 {
374 return m_CurrentObject;
375 }
376
381 FORCEINLINE bool Advance()
382 {
383 //@todo UE check this for LHS on Index on consoles
384 FUObjectItem* NextObject = nullptr;
385 m_CurrentObject = nullptr;
386 while(++m_Index < m_Array.GetObjectsList().size())
387 {
388 NextObject = m_Array.GetObjectsList()[m_Index];
389 if (NextObject->m_Object)
390 {
391 m_CurrentObject = NextObject;
392 return true;
393 }
394 }
395 return false;
396 }
397
400 {
401 return m_Array;
402 }
403
404 private:
406 const FUObjectArray& m_Array;
408 int32_t m_Index;
409
411 mutable FUObjectItem* m_CurrentObject;
412 };
413
414 public:
416 // Maybe use pool allocations instead of new delete operators for optimization
417 void AddUObject(UObject* Object);
418
420 const std::vector<FUObjectItem*>& GetObjectsList() const { return m_Elements; }
421 };
422
423 class KarmaClassObjectMap : public KarmaMap<UClass*, KarmaVector<UObject*>*>
424 {
425 public:
427 {}
428
430
440
449 };
450
457 extern KARMA_API FUObjectArray GUObjectStore;
458
469 extern KARMA_API KarmaClassObjectMap m_ClassToObjectVectorMap;
470
476 {
479
482
484 std::string m_Name;
485
487 EObjectFlags m_SetFlags = EObjectFlags::RF_NoFlags;
488
490 EInternalObjectFlags m_InternalSetFlags = EInternalObjectFlags::None;
491
494
497
502 UObject* m_Template = nullptr;
503
505 //FObjectInstancingGraph* InstanceGraph = nullptr;
506
508 //UPackage* ExternalPackage = nullptr;
509
511 //TFunction<void()> PropertyInitCallback;
512
514 //FObjectInstancingGraph* InstanceGraph = nullptr;
515
516 private:
517 //FObjectInitializer::FOverrides* SubobjectOverrides = nullptr;
518
519 public:
520
521 //COREUOBJECT_API FStaticConstructObjectParameters(const UClass* InClass);
522
523 //friend FObjectInitializer;
524 };
525
527namespace ECastCheckedType
528{
536};
537
545template <class T, class U>
546FORCEINLINE T* CastChecked(const U& Src, ECastCheckedType::Type CheckType = ECastCheckedType::NullChecked)
547{
548 if (Src)
549 {
550 T* Result = static_cast<T*>(Src);// this line may need some generalization, I will come back later
551 if (!Result)
552 {
553 KR_CORE_ERROR("Casting failed");
554 }
555
556 return Result;
557 }
558
559 if (CheckType == ECastCheckedType::NullChecked)
560 {
561 KR_CORE_ASSERT(false, "Attempting to cast nullptr");
562 }
563
564 return nullptr;
565}
566
576KARMA_API UObject* StaticConstructObject_Internal(const FStaticConstructObjectParameters& Params);
577
594KARMA_API UObject* StaticAllocateObject(const UClass* Class, UObject* InOuter, const std::string& name, EObjectFlags SetFlags = EObjectFlags::RF_NoFlags, EInternalObjectFlags InternalSetFlags = EInternalObjectFlags::None);
595
596
611template< class T >
612FUNCTION_NON_NULL_RETURN_START
613T* NewObject(UObject* Outer, const UClass* Class, std::string name = "No_Name", EObjectFlags Flags = RF_NoFlags, UObject* Template = nullptr, bool bCopyTransientsFromClassDefaults = false/*, FObjectInstancingGraph* InInstanceGraph = nullptr, UPackage* ExternalPackage = nullptr*/)
614FUNCTION_NON_NULL_RETURN_END
615{
616 if (name == "")
617 {
618 KR_CORE_ASSERT(false, "NewObject with empty name can't be used to create default subobjects");
619 //FObjectInitializer::AssertIfInConstructor(Outer, TEXT("NewObject with empty name can't be used to create default subobjects (inside of UObject derived class constructor) as it produces inconsistent object names. Use ObjectInitializer.CreateDefaultSubobject<> instead."));
620 }
621
622 FStaticConstructObjectParameters Params;
623 Params.m_Class = Class;
624 Params.m_Outer = Outer;
625 Params.m_Name = name;
626 Params.m_SetFlags = Flags;
627 Params.m_Class = Class;
628 //Params.m_Template = Template;
629 Params.m_bCopyTransientsFromClassDefaults = bCopyTransientsFromClassDefaults;
630 //Params.InstanceGraph = InInstanceGraph;
631 //Params.ExternalPackage = ExternalPackage;
632
633 return static_cast<T*>(StaticConstructObject_Internal(Params));
634}
635/*
636template< class T >
637FUNCTION_NON_NULL_RETURN_START*/
638//T* NewObject(UObject* Outer, const std::string& Name, EObjectFlags Flags = RF_NoFlags, UObject* Template = nullptr, bool bCopyTransientsFromClassDefaults = false/*, FObjectInstancingGraph* InInstanceGraph = nullptr*/)
639/*FUNCTION_NON_NULL_RETURN_END
640{
641 if (Name == "")
642 {
643 KR_CORE_ASSERT(false, "NewObject with empty name can't be used to create default subobjects");
644 //FObjectInitializer::AssertIfInConstructor(Outer, TEXT("NewObject with empty name can't be used to create default subobjects (inside of UObject derived class constructor) as it produces inconsistent object names. Use ObjectInitializer.CreateDefaultSubobject<> instead."));
645 }
646
647 FStaticConstructObjectParameters Params;// (T::StaticClass());
648 Params.m_Outer = Outer;
649 Params.m_Name = Name;
650 Params.m_SetFlags = Flags;
651 //Params.Template = Template;
652 Params.m_bCopyTransientsFromClassDefaults = bCopyTransientsFromClassDefaults;
653 Params.m_Class = T::StaticClass<T>();
654 //Params.InstanceGraph = InInstanceGraph;
655
656 return static_cast<T*>(StaticConstructObject_Internal(Params));
657}*/
658
675KARMA_API UObject* StaticFindObjectFastInternal(const UClass* ObjectClass, const UObject* ObjectPackage, const std::string& ObjectName, bool bExactClass = false, EObjectFlags ExcludeFlags = RF_NoFlags, EInternalObjectFlags ExclusiveInternalFlags = EInternalObjectFlags::None);
676
682KARMA_API UPackage* CreatePackage(const std::string& PackageName);
683
695KARMA_API void GetObjectsOfClass(const UClass* ClassToLookFor, KarmaVector<UObject *>& Results, bool bIncludeDerivedClasses = true, EObjectFlags ExcludeFlags = RF_ClassDefaultObject, EInternalObjectFlags ExclusionInternalFlags = EInternalObjectFlags::None);
696
706KARMA_API void ForEachObjectOfClass(const UClass* ClassToLookFor, std::function<void(UObject*)> Operation, bool bIncludeDerivedClasses = true, EObjectFlags ExcludeFlags = RF_ClassDefaultObject, EInternalObjectFlags ExclusionInternalFlags = EInternalObjectFlags::None);
707
711typedef void (*FUObjectAllocatorCallback)(void* InObject, const std::string& InName, size_t InSize, size_t InAlignment, class UClass* InClass);
712
719KARMA_API extern void RegisterUObjectsStatisticsCallback(FUObjectAllocatorCallback dumpCallback);
720
721
728KARMA_API void CacheObject(class UObject* Object);
729
730// Global Internal functions
731extern void StaticUObjectInit();
732extern UPackage* GetTransientPackage();
733
737 class KARMA_API FObjectInitializer
738 {
739 public:
744
753 FObjectInitializer(UObject* InObj, UObject* InObjectArchetype, bool bInCopyTransientsFromClassDefaults, bool bInShouldInitializeProps, struct FObjectInstancingGraph* InInstanceGraph = nullptr);
754
756
760 FORCEINLINE UObject* GetObj() const
761 {
762 return m_Object;
763 }
764
765 private:
766 friend class UObject;
767
768 template<class T>
769 friend void InternalConstructor(const class FObjectInitializer& X);
770
775 void PostConstructInit();
776
785 static void InitProperties(UObject* Object, UClass* DefaultsClass, UObject* DefaultData, bool bCopyTransientsFromClassDefaults);
786
787 private:
789 UObject* m_Object;
790
792 UObject* m_ObjectArchetype;
793
795 bool m_bCopyTransientsFromClassDefaults;
796
798 bool m_bShouldInitializePropsFromArchetype;
799
801 bool m_bSubobjectClassInitializationAllowed;
802
804 UObject* m_LastConstructedObject;
805 };
806}
void InternalConstructor(const FObjectInitializer &X)
Helper template to call the default constructor for a class.
Definition Object.h:33
Karma's std::map wrapper.
Definition KarmaTypes.h:63
Karma's std::vector wrapper.
Definition KarmaTypes.h:128
Definition UObjectGlobals.h:738
FORCEINLINE UObject * GetObj() const
Definition UObjectGlobals.h:760
Definition UObjectGlobals.h:301
FORCEINLINE FUObjectItem * GetObject() const
Definition UObjectGlobals.h:372
FORCEINLINE void operator++()
Definition UObjectGlobals.h:341
TIterator(EEndTagType, const TIterator &InIter)
Definition UObjectGlobals.h:332
const FUObjectArray & GetIteratedArray() const
Definition UObjectGlobals.h:399
TIterator(const FUObjectArray &InArray, bool bOnlyGCedObjects=false)
Definition UObjectGlobals.h:314
FORCEINLINE bool Advance()
Definition UObjectGlobals.h:381
Definition UObjectGlobals.h:295
const std::vector< FUObjectItem * > & GetObjectsList() const
Definition UObjectGlobals.h:420
Definition UObjectGlobals.h:424
KarmaVector< UObject * > * FindClassObjects(const UClass *Key)
Definition UObjectGlobals.cpp:594
KarmaVector< UObject * > * FindOrAddClass(const UClass *Key)
Definition UObjectGlobals.cpp:607
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
The base class of all the game code relevant objects.
Definition Object.h:106
@ None
Definition KarmaTypes.h:33
Type
Definition UObjectGlobals.h:530
@ NullAllowed
Definition UObjectGlobals.h:532
@ NullChecked
Definition UObjectGlobals.h:534
Definition UObjectGlobals.h:476
bool m_bAssumeTemplateIsArchetype
Definition UObjectGlobals.h:496
EObjectFlags m_SetFlags
Definition UObjectGlobals.h:487
EInternalObjectFlags m_InternalSetFlags
Definition UObjectGlobals.h:490
std::string m_Name
Definition UObjectGlobals.h:484
bool m_bCopyTransientsFromClassDefaults
Definition UObjectGlobals.h:493
UObject * m_Template
Definition UObjectGlobals.h:502
UObject * m_Outer
Definition UObjectGlobals.h:481
const UClass * m_Class
Definition UObjectGlobals.h:478
Definition UObjectGlobals.h:248