UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UObjectBase.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 UObjectBase.h: Base class for UObject, defines low level functionality
5=============================================================================*/
6
7#pragma once
8
9#include "Containers/Map.h"
12#include "HAL/PlatformAtomics.h"
13#include "HAL/PlatformMath.h"
15#include "Stats/Stats.h"
16#include "UObject/NameTypes.h"
20#include "UObject/UnrealNames.h"
21#include "UObject/ObjectPtr.h"
22#include "AutoRTFM.h"
23
24class UClass;
25class UEnum;
26class UObject;
27class UPackage;
28class UScriptStruct;
29
31{
32 struct FObjectHandleUtils;
33}
34
35namespace UE::CoreUObject
36{
37#if UE_WITH_CONSTINIT_UOBJECT
38 // Finish constructing compiled-in objects with operations that can only be done at runtime
39 // (i.e. creating FNames) but do not create any class default objects and minimize callbacks
40 // to other systems (i.e. FCoreUObjectDelegates::CompiledInUObjectsRegisteredDelegate)
41 // Expected to be called from Launch before any systems that want basic access to compiled-in
42 // objects such as classes and structs for reflection (i.e. reading config via reflection).
44#endif
45}
46
47// If FName is 4 bytes than we can use padding after it to store internal object list index and use array instead of a hash map for lookup in UObjectHash.cpp
48// This might change the each UClass' object list iteration order
49#if !defined(UE_STORE_OBJECT_LIST_INTERNAL_INDEX)
50# define UE_STORE_OBJECT_LIST_INTERNAL_INDEX 0
51#endif
52
54
59{
60 friend class UObjectBaseUtility;
62 friend class FUObjectArray; // for access to InternalIndex without revealing it to anyone else
63 friend class FUObjectAllocator; // for access to destructor without revealing it to anyone else
64 friend struct FInternalUObjectBaseUtilityIsValidFlagsChecker; // for access to InternalIndex
65 friend struct UE::CoreUObject::Private::FObjectHandleUtils; // for access to OuterPrivate
68 class UClass* (*TClass_StaticClassFn)(),
72 const TCHAR* PackageName,
73 const TCHAR* Name
74 );
75 friend COREUOBJECT_API UObject* StaticAllocateObject( // for access to ObjectListInternalIndex
76 const UClass*,
77 UObject*,
78 FName,
81 bool,
82 bool*,
83 UPackage*,
84 int32,
87
88protected:
90 ClassPrivate(NoInit),
91 NamePrivate(NoInit), // screwy, but the name was already set and we don't want to set it again
92 OuterPrivate(NoInit)
93 {
94 }
95
96#if UE_WITH_CONSTINIT_UOBJECT
97public:
98 explicit consteval UObjectBase(UE::CodeGen::ConstInit::FObjectParams InParams)
101 , ClassPrivate(ConstEval, InParams.Class)
103 , OuterPrivate(InParams.Outer)
104 {
105 }
106protected:
107#endif
108
114public:
115
130 FName InName,
134
139
140protected:
148
149#if !UE_WITH_CONSTINIT_UOBJECT
151 virtual void RegisterDependencies() {}
152
154 COREUOBJECT_API void Register(class UClass* (*StaticClassFn)(), const TCHAR* PackageName, const TCHAR* Name);
155 UE_DEPRECATED(5.6, "Use the new method above and specify UClass::StaticClass as the first argument")
156 COREUOBJECT_API void Register(const TCHAR* PackageName, const TCHAR* Name);
157#endif // !UE_WITH_CONSTINIT_UOBJECT
158
164 COREUOBJECT_API virtual void DeferredRegister(UClass* UClassStaticClass, const TCHAR* PackageName, const TCHAR* Name
167#endif
168 );
169
170private:
180
185 COREUOBJECT_API bool IsValidLowLevelForDestruction() const;
186
187#if UE_WITH_REMOTE_OBJECT_HANDLE
189#endif
190
191public:
196 COREUOBJECT_API bool IsValidLowLevel() const;
197
205 COREUOBJECT_API bool IsValidLowLevelFast(bool bRecursive = true) const;
206
212 {
213 return (uint32)InternalIndex;
214 }
215
218 {
219 return ClassPrivate;
220 }
221
224 {
225#if UE_WITH_REMOTE_OBJECT_HANDLE
227#else
228 return OuterPrivate;
229#endif
230 }
231
234 {
235#if UE_WITH_CONSTINIT_UOBJECT
236 checkfSlow(!EnumHasAllFlags((EObjectFlags)GetFlagsInternal(), RF_NeedInitialization|RF_MarkAsNative), TEXT("UObjectBase::GetFName called on a native object which is not yet initialized."));
237#endif // UE_WITH_CONSTINIT_UOBJECT
238 return NamePrivate.Name;
239 }
240
243
245 static COREUOBJECT_API FString RemoveClassPrefix(const TCHAR* ClassName);
246
249
252
255
259 COREUOBJECT_API void MarkAsReachable() const;
260
262 COREUOBJECT_API void AddRef() const;
263
265 COREUOBJECT_API void ReleaseRef() const;
266
267protected:
272 UE_DEPRECATED(5.3, "This function is not thread-safe. Use AtomicallySetFlags or AtomicallyClearFlags instead.")
274 {
275 checkfSlow((NewFlags & ~RF_AllFlags) == 0, TEXT("%s flagged as 0x%x but is trying to set flags to RF_AllFlags"), *GetFName().ToString(), (int)ObjectFlags);
276 ObjectFlags = NewFlags;
277 }
278
279public:
286 {
287 EObjectFlags Flags = (EObjectFlags)GetFlagsInternal();
288 checkfSlow((Flags & ~RF_AllFlags) == 0, TEXT("%s flagged as RF_AllFlags"), *GetFName().ToString());
289 return Flags;
290 }
291
296 {
297 int32 OldFlags = GetFlagsInternal();
298 int32 NewFlags = OldFlags | FlagsToAdd;
299
300 // Fast path without atomics if already set
301 if (NewFlags == OldFlags)
302 {
303 return;
304 }
305
307 {
308 FPlatformAtomics::InterlockedOr((int32*)&ObjectFlags, FlagsToAdd);
309 };
310
311 // If we abort we undo setting the flags we just set.
312 AutoRTFM::OnAbort([this, OldFlags, FlagsToAdd]
313 {
314 int32 MaskFlags = OldFlags;
315
316 // Now just extract out the old flags that mattered (the ones we were setting).
318 // And unmask the flags we didn't mention.
320
321 FPlatformAtomics::InterlockedAnd((int32*)&ObjectFlags, MaskFlags);
322 });
323 }
324
329 {
330 int32 OldFlags = GetFlagsInternal();
331 int32 NewFlags = OldFlags & ~FlagsToClear;
332
333 // Fast path without atomics if already cleared
334 if (NewFlags == OldFlags)
335 {
336 return;
337 }
338
340 {
341 FPlatformAtomics::InterlockedAnd((int32*)&ObjectFlags, ~FlagsToClear);
342 };
343
344 // If we abort we undo clearing the flags we just unset.
345 AutoRTFM::OnAbort([this, OldFlags, FlagsToClear]
346 {
347 int32 MaskFlags = OldFlags;
348
349 // Now just extract out the old flags that mattered (the ones we were setting).
351
352 FPlatformAtomics::InterlockedOr((int32*)&ObjectFlags, MaskFlags);
353 });
354 }
355
358
359private:
360 FORCEINLINE int32 GetFlagsInternal() const
361 {
362 static_assert(sizeof(int32) == sizeof(ObjectFlags), "Flags must be 32-bit for atomics.");
363
364 int32 Result = 0;
365
367 {
368 Result = FPlatformAtomics::AtomicRead_Relaxed((int32*)&ObjectFlags);
369 };
370
371 return Result;
372 }
373
376 EObjectFlags ObjectFlags;
377
380
383
384 // Merged data for constinit UObjects to hold their name before the FName system is initialized
385 // The object flags RF_MarkAsNative | RF_NeedInitialization are used to assert correct use of this union.
386 struct FNameAndObjectHashIndex
387 {
388 FName Name;
389#if UE_STORE_OBJECT_LIST_INTERNAL_INDEX
395#endif
396 constexpr FNameAndObjectHashIndex()
397 : Name()
400#endif
401 {
402 }
403 FNameAndObjectHashIndex(ENoInit)
404 : Name(NoInit)
405 {}
406 FNameAndObjectHashIndex(FName InName)
407 : Name(InName)
410#endif
411 {
412 }
413 };
414 union
415 {
416 FNameAndObjectHashIndex NamePrivate = {};
417#if UE_WITH_CONSTINIT_UOBJECT
419#endif
420 };
421#if UE_WITH_CONSTINIT_UOBJECT
422 static_assert(sizeof(UninitializedNameUTF8) <= sizeof(FNameAndObjectHashIndex),
423 "Wasted space in UObject? If FName is small in this configuration, enable UE_STORE_OBJECT_LIST_INTERNAL_INDEX to speed up the UObject Hash.");
424#endif
425
426#if UE_WITH_CONSTINIT_UOBJECT
429 {
431 {
433 return NewName;
434 }
435 checkfSlow(false, TEXT("Compiled-in object unexpectedly already had its name initialized? %s"), *NamePrivate.Name.ToString());
436 return FName();
437 }
438#endif
439
442
446 friend class FDeferredRegistry;
450
451#if UE_WITH_CONSTINIT_UOBJECT
452 void AddConstInitObject();
453#endif
454
455#if UE_STORE_OBJECT_LIST_INTERNAL_INDEX
456 // Internal accessor for internal index to hide gory union/struct details
457 inline int32& GetObjectHashInternalIndex() { return NamePrivate.ObjectListInternalIndex; }
458#endif
459
460#if WITH_EDITOR
463#endif
464};
465
473
478
479#if !UE_WITH_CONSTINIT_UOBJECT
483template <typename T, typename V>
485{
486 using TType = T;
487 using TVersion = V;
488
492};
493
498{
499#if WITH_RELOAD
500 SIZE_T Size = 0;
501 uint32 Hash = 0;
502#endif
503};
504
509
514{
515 class UClass* (*OuterRegister)();
516 class UClass* (*InnerRegister)();
517 const TCHAR* Name;
520};
521
526
531{
532#if WITH_RELOAD
533 SIZE_T Size = 0;
534 uint32 Hash = 0;
535#endif
536};
537
542
547{
548 class UScriptStruct* (*OuterRegister)();
549 void* (*CreateCppStructOps)();
550 const TCHAR* Name;
553};
554
559
564
569{
570#if WITH_RELOAD
571 uint32 Hash = 0;
572#endif
573};
574
579
590
595
599COREUOBJECT_API class UEnum* GetStaticEnum(class UEnum* (*InRegister)(), UObject* EnumOuter, const TCHAR* EnumName);
600
605{
606#if WITH_RELOAD
607 uint32 BodyHash = 0;
609#endif
610};
611
616
621
626
627#endif // !UE_WITH_CONSTINIT_UOBJECT
628
629
632
634COREUOBJECT_API void ProcessNewlyLoadedUObjects(FName InModuleName = NAME_None, bool bCanProcessNewlyLoadedObjects = true);
635
636#if !UE_WITH_CONSTINIT_UOBJECT
641{
642 template <typename... ArgTypes>
647};
648#endif // !UE_WITH_CONSTINIT_UOBJECT
649
653void UObjectBaseInit();
654
#define NULL
Definition oodle2base.h:134
#define FORCENOINLINE
Definition AndroidPlatform.h:142
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define checkfSlow(expr, format,...)
Definition AssertionMacros.h:333
ENoInit
Definition CoreMiscDefines.h:158
@ NoInit
Definition CoreMiscDefines.h:158
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define UE_WITH_REMOTE_OBJECT_HANDLE
Definition CoreMiscDefines.h:620
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
@ ConstEval
Definition CoreMiscDefines.h:161
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::UTF8CHAR UTF8CHAR
An 8-bit character containing a UTF8 (Unicode, 8-bit, variable-width) code unit.
Definition Platform.h:1137
#define DECLARE_DWORD_COUNTER_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:682
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
constexpr bool EnumHasAllFlags(Enum Flags, Enum Contains)
Definition EnumClassFlags.h:28
EInternalObjectFlags
Definition ObjectMacros.h:631
EObjectFlags
Definition ObjectMacros.h:552
@ RF_MarkAsRootSet
Object will be marked as root set on construction and not be garbage collected, even if unreferenced ...
Definition ObjectMacros.h:568
@ RF_NeedInitialization
This object has not completed its initialization process. Cleared when ~FObjectInitializer completes.
Definition ObjectMacros.h:572
@ RF_MarkAsNative
Object (UField) will be marked as native on construction (DO NOT USE THIS FLAG in HasAnyFlags() etc)
Definition ObjectMacros.h:561
#define RF_AllFlags
All flags, used mainly for error checking.
Definition ObjectMacros.h:612
COREUOBJECT_API void UObjectForceRegistration(UObjectBase *Object, bool bCheckForModuleRelease=true)
Definition UObjectBase.cpp:636
COREUOBJECT_API void RegisterCompiledInInfo(class UClass *(*InOuterRegister)(), class UClass *(*InInnerRegister)(), const TCHAR *InPackageName, const TCHAR *InName, FClassRegistrationInfo &InInfo, const FClassReloadVersionInfo &InVersionInfo)
Definition UObjectBase.cpp:718
COREUOBJECT_API void ProcessNewlyLoadedUObjects(FName InModuleName=NAME_None, bool bCanProcessNewlyLoadedObjects=true)
Definition UObjectBase.cpp:1027
COREUOBJECT_API void RegisterModularObjectsProcessing()
Definition UObjectBase.cpp:1011
COREUOBJECT_API class UEnum * GetStaticEnum(class UEnum *(*InRegister)(), UObject *EnumOuter, const TCHAR *EnumName)
Definition UObjectBase.cpp:691
void UObjectBaseShutdown()
Definition UObjectBase.cpp:1344
COREUOBJECT_API bool UObjectInitialized()
Definition UObjectBase.cpp:59
void UObjectBaseInit()
Definition UObjectBase.cpp:1271
COREUOBJECT_API class UScriptStruct * GetStaticStruct(class UScriptStruct *(*InRegister)(), UObject *StructOuter, const TCHAR *StructName)
Definition UObjectBase.cpp:675
#define UE_STORE_OBJECT_LIST_INTERNAL_INDEX
Definition UObjectBase.h:50
int32 InternalIndex
Definition VulkanMemory.cpp:4036
uint32 Size
Definition VulkanMemory.cpp:4034
if(Failed) console_printf("Failed.\n")
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition UObjectGlobals.cpp:3494
Definition NameTypes.h:617
Definition UObjectAllocator.h:16
Definition UObjectArray.h:940
Definition UObjectHash.cpp:669
TObjectPtr< T > & GetAccessTrackedObjectPtr()
Definition ObjectPtr.h:922
Definition Class.h:3793
Definition Class.h:2791
Definition UObjectBaseUtility.h:45
Definition UObjectBase.h:59
friend class FCompiledInObjectRegistry
Definition UObjectBase.h:447
COREUOBJECT_API UPackage * GetExternalPackage() const
Definition UObjectBase.cpp:312
FORCEINLINE void SetFlagsTo(EObjectFlags NewFlags)
Definition UObjectBase.h:273
friend void RemoveFromClassMap(class FUObjectHashTables &ThreadHash, UObjectBase *Object)
Definition UObjectHash.cpp:1413
COREUOBJECT_API void AddRef() const
Definition GarbageCollection.cpp:6488
friend class FDeferredRegistry
Definition UObjectBase.h:446
COREUOBJECT_API void SetExternalPackage(UPackage *InPackage)
Definition UObjectBase.cpp:337
friend COREUOBJECT_API void InitializePrivateStaticClass(class UClass *(*TClass_StaticClassFn)(), class UClass *TClass_Super_StaticClass, class UClass *TClass_PrivateStaticClass, class UClass *TClass_WithinClass_StaticClass, const TCHAR *PackageName, const TCHAR *Name)
Definition Class.cpp:127
FORCEINLINE UObject * GetOuter() const
Definition UObjectBase.h:223
virtual COREUOBJECT_API void DeferredRegister(UClass *UClassStaticClass, const TCHAR *PackageName, const TCHAR *Name)
Definition UObjectBase.cpp:198
friend COREUOBJECT_API void UObjectForceRegistration(UObjectBase *Object, bool bCheckForModuleRelease)
Definition UObjectBase.cpp:636
FORCEINLINE FName GetFName() const
Definition UObjectBase.h:233
static void PrefetchOuter(UObject *Object)
Definition UObjectBase.h:357
static void PrefetchClass(UObject *Object)
Definition UObjectBase.h:356
COREUOBJECT_API UPackage * GetExternalPackageInternal() const
Definition UObjectBase.cpp:327
friend class FBlueprintCompileReinstancer
Definition UObjectBase.h:443
FORCEINLINE uint32 GetUniqueID() const
Definition UObjectBase.h:211
virtual COREUOBJECT_API ~UObjectBase()
Definition UObjectBase.cpp:174
friend void AddToClassMap(class FUObjectHashTables &ThreadHash, UObjectBase *Object)
Definition UObjectHash.cpp:1335
COREUOBJECT_API void ReleaseRef() const
Definition GarbageCollection.cpp:6494
COREUOBJECT_API void MarkAsReachable() const
Definition GarbageCollection.cpp:6481
friend class FVerseObjectClassReplacer
Definition UObjectBase.h:444
FORCEINLINE EObjectFlags GetFlags() const
Definition UObjectBase.h:285
virtual void RegisterDependencies()
Definition UObjectBase.h:151
FNameAndObjectHashIndex NamePrivate
Definition UObjectBase.h:416
UObjectBase()
Definition UObjectBase.h:89
COREUOBJECT_API bool IsValidLowLevelFast(bool bRecursive=true) const
Definition UObjectBase.cpp:399
friend class FContextObjectManager
Definition UObjectBase.h:445
friend struct Z_Construct_UClass_UObject_Statics
Definition UObjectBase.h:61
virtual COREUOBJECT_API FName GetFNameForStatID() const
Definition UObjectBase.cpp:699
friend COREUOBJECT_API UObject * StaticAllocateObject(const UClass *, UObject *, FName, EObjectFlags, EInternalObjectFlags, bool, bool *, UPackage *, int32, FRemoteObjectId, class FGCReconstructionGuard *)
Definition UObjectGlobals.cpp:3576
COREUOBJECT_API void LowLevelRename(FName NewName, UObject *NewOuter=NULL)
Definition UObjectBase.cpp:274
FORCENOINLINE void AtomicallySetFlags(EObjectFlags FlagsToAdd)
Definition UObjectBase.h:295
FORCENOINLINE void AtomicallyClearFlags(EObjectFlags FlagsToClear)
Definition UObjectBase.h:328
FORCEINLINE UClass * GetClass() const
Definition UObjectBase.h:217
COREUOBJECT_API bool IsValidLowLevel() const
Definition UObjectBase.cpp:394
static COREUOBJECT_API FString RemoveClassPrefix(const TCHAR *ClassName)
Definition UObjectBase.cpp:705
Definition Object.h:95
Definition Package.h:216
Definition Class.h:1720
const TCHAR * Name
Definition OodleDataCompression.cpp:30
Definition CoreGlobals.cpp:268
Definition CoreGlobals.cpp:268
UE_STRING_CLASS Result(Forward< LhsType >(Lhs), RhsLen)
Definition String.cpp.inl:732
Definition UObjectBase.h:514
FClassReloadVersionInfo VersionInfo
Definition UObjectBase.h:519
const TCHAR * Name
Definition UObjectBase.h:517
FClassRegistrationInfo * Info
Definition UObjectBase.h:518
Definition UObjectBase.h:498
Definition UObjectBase.h:584
FEnumRegistrationInfo * Info
Definition UObjectBase.h:587
FEnumReloadVersionInfo VersionInfo
Definition UObjectBase.h:588
const TCHAR * Name
Definition UObjectBase.h:586
Definition UObjectBase.h:569
static FORCEINLINE void Prefetch(const void *Ptr)
Definition GenericPlatformMisc.h:1443
Definition UObjectBase.h:605
Definition UObjectBase.h:641
FRegisterCompiledInInfo(ArgTypes &&... Args)
Definition UObjectBase.h:643
Definition RemoteObjectTypes.h:212
Definition UObjectBase.h:547
const TCHAR * Name
Definition UObjectBase.h:550
FStructRegistrationInfo * Info
Definition UObjectBase.h:551
FStructReloadVersionInfo VersionInfo
Definition UObjectBase.h:552
Definition UObjectBase.h:531
static FORCEINLINE T * NoAccessTrackingGetNoResolve(const TObjectPtr< T > &Ptr)
Definition ObjectPtr.h:873
Definition UObjectBase.h:485
TType * OuterSingleton
Definition UObjectBase.h:490
TType * InnerSingleton
Definition UObjectBase.h:489
V TVersion
Definition UObjectBase.h:487
T TType
Definition UObjectBase.h:486
TVersion ReloadVersionInfo
Definition UObjectBase.h:491
Definition ObjectHandlePrivate.h:10