【UE4】通过T::StaticClass()获得对应UObject的UClass的完整代码调用流程

每个继承自UObject的对象都有对应的XXX.generated.h和XXXX.gen.cpp文件.
以MySingleton.generate.h和MySingleton.gen.cpp 为例.

在MySingleton.gen.cpp 中, 使用了StaticClass

    IMPLEMENT_CLASS(UMySingleton, 1249388217);
    template<> UE_DESIGNMODE_API UClass* StaticClass()
    {
        return UMySingleton::StaticClass();
    }

在MySingleton.generated.h文件中:

public: \
    DECLARE_CLASS(UMySingleton, UObject, COMPILED_IN_FLAGS(0), CASTCLASS_None, TEXT("/Script/UE_DesignMode"), NO_API) \
    DECLARE_SERIALIZER(UMySingleton)

在DECLARE_CLASS定义了StaticClass

#define DECLARE_CLASS( TClass, TSuperClass, TStaticFlags, TStaticCastFlags, TPackage, TRequiredAPI  ) \
private: \
    TClass& operator=(TClass&&);   \
    TClass& operator=(const TClass&);   \
    TRequiredAPI static UClass* GetPrivateStaticClass(); \
public: \
    /** Bitwise union of #EClassFlags pertaining to this class.*/ \
    enum {StaticClassFlags=TStaticFlags}; \
    /** Typedef for the base class ({{ typedef-type }}) */ \
    typedef TSuperClass Super;\
    /** Typedef for {{ typedef-type }}. */ \
    typedef TClass ThisClass;\
    /** Returns a UClass object representing this class at runtime */ \
    inline static UClass* StaticClass() \
    { \
        return GetPrivateStaticClass(); \
    } \
    /** Returns the package this class belongs in */ \
    inline static const TCHAR* StaticPackage() \
    { \
        return TPackage; \
    } \
    /** Returns the static cast flags for this class */ \
    inline static EClassCastFlags StaticClassCastFlags() \
    { \
        return TStaticCastFlags; \
    } \
    /** For internal use only; use StaticConstructObject() to create new objects. */ \
    inline void* operator new(const size_t InSize, EInternal InInternalOnly, UObject* InOuter = (UObject*)GetTransientPackage(), FName InName = NAME_None, EObjectFlags InSetFlags = RF_NoFlags) \
    { \
        return StaticAllocateObject(StaticClass(), InOuter, InName, InSetFlags); \
    } \
    /** For internal use only; use StaticConstructObject() to create new objects. */ \
    inline void* operator new( const size_t InSize, EInternal* InMem ) \
    { \
        return (void*)InMem; \
    }

GetPrivateStaticClass定义在IMPLEMENT_CLASS中:

// Register a class at startup time.
#define IMPLEMENT_CLASS(TClass, TClassCrc) \
    static TClassCompiledInDefer AutoInitialize##TClass(TEXT(#TClass), sizeof(TClass), TClassCrc); \
    UClass* TClass::GetPrivateStaticClass() \
    { \
        static UClass* PrivateStaticClass = NULL; \
        if (!PrivateStaticClass) \
        { \
            /* this could be handled with templates, but we want it external to avoid code bloat */ \
            GetPrivateStaticClassBody( \
                StaticPackage(), \
                (TCHAR*)TEXT(#TClass) + 1 + ((StaticClassFlags & CLASS_Deprecated) ? 11 : 0), \
                PrivateStaticClass, \
                StaticRegisterNatives##TClass, \
                sizeof(TClass), \
                (EClassFlags)TClass::StaticClassFlags, \
                TClass::StaticClassCastFlags(), \
                TClass::StaticConfigName(), \
                (UClass::ClassConstructorType)InternalConstructor, \
                (UClass::ClassVTableHelperCtorCallerType)InternalVTableHelperCtorCaller, \
                &TClass::AddReferencedObjects, \
                &TClass::Super::StaticClass, \
                &TClass::WithinClass::StaticClass \
            ); \
        } \
        return PrivateStaticClass; \
    }

而IMPLEMENT_CLASS是在MySingleton.gen.cpp 中

    IMPLEMENT_CLASS(UMySingleton, 1249388217);
    template<> UE_DESIGNMODE_API UClass* StaticClass()
    {
        return UMySingleton::StaticClass();
    }

这就是我们通过T::StaticClass()获得对应UObject的UClass的完整代码调用流程。

你可能感兴趣的:(Unreal,StaticClass,UObject,UClass)