UE4对象系统_UProperty

UProperty(含其子类)实例描述了类的某个成员变量的反射信息。

  • UProperty类
//
// An UnrealScript variable.
// Property的名字在基类中
//
class COREUOBJECT_API UProperty : public UField
{
    DECLARE_CASTED_CLASS_INTRINSIC_NO_CTOR(UProperty, UField, CLASS_Abstract, TEXT("/Script/CoreUObject"), CASTCLASS_UProperty, NO_API)
    DECLARE_WITHIN(UField)  // 该对象实例只能在UField类对象中(Outer关系约束)。

    // Persistent variables.
    int32       ArrayDim;     // Property维度, 例如: int  a, 维度1;  int  a[20], 维度20.  
    int32       ElementSize;   // 数组元素大小
    uint64      PropertyFlags;   // 标记
    uint16      RepIndex;   // Replication时使用

    FName       RepNotifyFunc;  // 在network replication时,变量发生变化的通知回调函数名称。

private:
    // In memory variables (generated during Link()).
    int32       Offset_Internal;    // 类的成员变量在类内存布局中的偏移量

    ELifetimeCondition BlueprintReplicationCondition;

public:
    /** In memory only: Linked list of properties from most-derived to base **/
    UProperty*  PropertyLinkNext;
    /** In memory only: Linked list of object reference properties from most-derived to base **/
    UProperty*  NextRef;
    /** In memory only: Linked list of properties requiring destruction. Note this does not include things that will be destroyed byt he native destructor **/
    UProperty*  DestructorLinkNext;
    /** In memory only: Linked list of properties requiring post constructor initialization.**/
    UProperty*  PostConstructLinkNext;

  • UBoolProperty
//
// Describes a single bit flag variable residing in a 32-bit unsigned double word.
//
class COREUOBJECT_API UBoolProperty : public UProperty
{
    DECLARE_CASTED_CLASS_INTRINSIC_NO_CTOR(UBoolProperty, UProperty, 0, TEXT("/Script/CoreUObject"), CASTCLASS_UBoolProperty, NO_API)

    // Variables.
private:

    /** Size of the bitfield/bool property. Equal to ElementSize but used to check if the property has been properly initialized (0-8, where 0 means uninitialized). */
    uint8 FieldSize;
    /** Offset from the memeber variable to the byte of the property (0-7). */
    uint8 ByteOffset;
    /** Mask of the byte byte with the property value. */
    uint8 ByteMask;
    /** Mask of the field with the property value. Either equal to ByteMask or 255 in case of 'bool' type. */
    uint8 FieldMask;

public:

+UEnumProperty

class COREUOBJECT_API UEnumProperty : public UProperty
{
    DECLARE_CASTED_CLASS_INTRINSIC(UEnumProperty, UProperty, 0, TEXT("/Script/CoreUObject"), CASTCLASS_UEnumProperty)

public:
    UEnumProperty(const FObjectInitializer& ObjectInitializer, UEnum* InEnum);
    UEnumProperty(const FObjectInitializer& ObjectInitializer, ECppProperty, int32 InOffset, uint64 InFlags, UEnum* InEnum);

    // UObject interface
    virtual void Serialize( FArchive& Ar ) override;
    static void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);
    virtual void GetPreloadDependencies(TArray<UObject*>& OutDeps) override;
    // End of UObject interface

    // UField interface
    virtual void AddCppProperty(UProperty* Property) override;
    // End of UField interface

    // UProperty interface
    virtual FString GetCPPMacroType( FString& ExtendedTypeText ) const  override;
    virtual FString GetCPPType( FString* ExtendedTypeText, uint32 CPPExportFlags ) const override;
    virtual FString GetCPPTypeForwardDeclaration() const override;
    virtual void LinkInternal(FArchive& Ar) override;
    virtual bool Identical( const void* A, const void* B, uint32 PortFlags ) const override;
    virtual void SerializeItem( FArchive& Ar, void* Value, void const* Defaults ) const override;
    virtual bool NetSerializeItem( FArchive& Ar, UPackageMap* Map, void* Data, TArray<uint8> * MetaData = NULL ) const override;
    virtual void ExportTextItem( FString& ValueStr, const void* PropertyValue, const void* DefaultValue, UObject* Parent, int32 PortFlags, UObject* ExportRootScope ) const override;
    virtual const TCHAR* ImportText_Internal( const TCHAR* Buffer, void* Data, int32 PortFlags, UObject* OwnerObject, FOutputDevice* ErrorText ) const override;
    virtual int32 GetMinAlignment() const override;
    virtual bool SameType(const UProperty* Other) const override;
    virtual bool ConvertFromType(const FPropertyTag& Tag, FArchive& Ar, uint8* Data, UStruct* DefaultsStruct, bool& bOutAdvanceProperty) override;
    // End of UProperty interface

    /**
     * Returns a pointer to the UEnum of this property.
     */
    FORCEINLINE UEnum* GetEnum() const
    {
        return Enum;
    }

    /**
     * Returns the numeric property which represents the integral type of the enum.
     */
    FORCEINLINE UNumericProperty* GetUnderlyingProperty() const
    {
        return UnderlyingProp;
    }

private:
    virtual uint32 GetValueTypeHashInternal(const void* Src) const override;

    friend struct UE4EnumProperty_Private::FEnumPropertyFriend;
    
#if HACK_HEADER_GENERATOR
public:
#endif
    UNumericProperty* UnderlyingProp; // The property which represents the underlying type of the enum
    UEnum* Enum; // The enum represented by this property  所属的Enum类型
};
  • UNumericProperty
    数字属性类, 其子类有 int32, int64, uint32, uint64, float, double等其它具体类型数字的属性类。
class COREUOBJECT_API UNumericProperty : public UProperty
{
    DECLARE_CASTED_CLASS_INTRINSIC(UNumericProperty, UProperty, CLASS_Abstract, TEXT("/Script/CoreUObject"), CASTCLASS_UNumericProperty)

    UNumericProperty(ECppProperty, int32 InOffset, uint64 InFlags)
        : UProperty(FObjectInitializer::Get(), EC_CppProperty, InOffset, InFlags)
    {}

    UNumericProperty( const FObjectInitializer& ObjectInitializer, ECppProperty, int32 InOffset, uint64 InFlags )
        :   UProperty( ObjectInitializer, EC_CppProperty, InOffset, InFlags )
    {}

    // UProperty interface.
    virtual const TCHAR* ImportText_Internal( const TCHAR* Buffer, void* Data, int32 PortFlags, UObject* Parent, FOutputDevice* ErrorText ) const override;

    virtual void ExportTextItem( FString& ValueStr, const void* PropertyValue, const void* DefaultValue, UObject* Parent, int32 PortFlags, UObject* ExportRootScope ) const override;
    // End of UProperty interface

    // UNumericProperty interface.

    /** Return true if this property is for a floating point number **/
    virtual bool IsFloatingPoint() const;

    /** Return true if this property is for a integral or enum type **/
    virtual bool IsInteger() const;
};

  • UObjectProperty
    对象引用属性类。
//
// Describes a reference variable to another object which may be nil.
//
class COREUOBJECT_API UObjectProperty : public TUObjectPropertyBase<UObject*>
{
    DECLARE_CASTED_CLASS_INTRINSIC(UObjectProperty, TUObjectPropertyBase<UObject*>, 0, TEXT("/Script/CoreUObject"), CASTCLASS_UObjectProperty)

    UObjectProperty(ECppProperty, int32 InOffset, uint64 InFlags, UClass* InClass)
        : TUObjectPropertyBase(FObjectInitializer::Get(), EC_CppProperty, InOffset, InFlags, InClass)
    {
    }

    UObjectProperty( const FObjectInitializer& ObjectInitializer, ECppProperty, int32 InOffset, uint64 InFlags, UClass* InClass )
        : TUObjectPropertyBase(ObjectInitializer, EC_CppProperty, InOffset, InFlags, InClass)
    {
    }

    // UHT interface
    virtual FString GetCPPMacroType( FString& ExtendedTypeText ) const  override;
    virtual FString GetCPPTypeForwardDeclaration() const override;
    // End of UHT interface

    // UProperty interface
    virtual void SerializeItem( FArchive& Ar, void* Value, void const* Defaults ) const override;
    virtual void EmitReferenceInfo(UClass& OwnerClass, int32 BaseOffset, TArray<const UStructProperty*>& EncounteredStructProps) override;
    virtual const TCHAR* ImportText_Internal(const TCHAR* Buffer, void* Data, int32 PortFlags, UObject* OwnerObject, FOutputDevice* ErrorText) const override;
    virtual bool ConvertFromType(const FPropertyTag& Tag, FArchive& Ar, uint8* Data, UStruct* DefaultsStruct, bool& bOutAdvanceProperty) override;

private:
    virtual uint32 GetValueTypeHashInternal(const void* Src) const override;
public:
    // End of UProperty interface

    // UObjectPropertyBase interface
    virtual UObject* GetObjectPropertyValue(const void* PropertyValueAddress) const override;
    virtual void SetObjectPropertyValue(void* PropertyValueAddress, UObject* Value) const override;
    virtual FString GetCPPTypeCustom(FString* ExtendedTypeText, uint32 CPPExportFlags, const FString& InnerNativeTypeName)  const override;
    // End of UObjectPropertyBase interface
};
  • UArrayProperty
    动态数组成员变量属性, 对应C++中TArray<XXX>
//
// Describes a dynamic array.
//

// need to break this out a different type so that the DECLARE_CASTED_CLASS_INTRINSIC macro can digest the comma
typedef TProperty<FScriptArray, UProperty> UArrayProperty_Super;

class COREUOBJECT_API UArrayProperty : public UArrayProperty_Super
{
    DECLARE_CASTED_CLASS_INTRINSIC(UArrayProperty, UArrayProperty_Super, 0, TEXT("/Script/CoreUObject"), CASTCLASS_UArrayProperty)

    // Variables.
    UProperty* Inner;  // 描述数组元素的类型

public:
    typedef UArrayProperty_Super::TTypeFundamentals TTypeFundamentals;
    typedef TTypeFundamentals::TCppType TCppType;

    UArrayProperty(ECppProperty, int32 InOffset, uint64 InFlags)
        : UArrayProperty_Super(FObjectInitializer::Get(), EC_CppProperty, InOffset, InFlags)
    {
    }

    UArrayProperty( const FObjectInitializer& ObjectInitializer, ECppProperty, int32 InOffset, uint64 InFlags )
    :   UArrayProperty_Super( ObjectInitializer, EC_CppProperty, InOffset, InFlags)
    {
    }

    // UObject interface
    virtual void Serialize( FArchive& Ar ) override;
    static void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);
    virtual void GetPreloadDependencies(TArray<UObject*>& OutDeps) override;
    // End of UObject interface

    // UField interface
    virtual void AddCppProperty( UProperty* Property ) override;
    // End of UField interface

    // UProperty interface
    virtual FString GetCPPMacroType( FString& ExtendedTypeText ) const  override;
    virtual FString GetCPPType( FString* ExtendedTypeText, uint32 CPPExportFlags ) const override;
    virtual FString GetCPPTypeForwardDeclaration() const override;
    virtual void LinkInternal(FArchive& Ar) override;
    virtual bool Identical( const void* A, const void* B, uint32 PortFlags ) const override;
    virtual void SerializeItem( FArchive& Ar, void* Value, void const* Defaults ) const override;
    virtual bool NetSerializeItem( FArchive& Ar, UPackageMap* Map, void* Data, TArray<uint8> * MetaData = NULL ) const override;
    virtual void ExportTextItem( FString& ValueStr, const void* PropertyValue, const void* DefaultValue, UObject* Parent, int32 PortFlags, UObject* ExportRootScope ) const override;
    virtual const TCHAR* ImportText_Internal( const TCHAR* Buffer, void* Data, int32 PortFlags, UObject* OwnerObject, FOutputDevice* ErrorText ) const override;
    virtual void CopyValuesInternal( void* Dest, void const* Src, int32 Count  ) const override;
    virtual void ClearValueInternal( void* Data ) const override;
    virtual void DestroyValueInternal( void* Dest ) const override;
    virtual bool PassCPPArgsByRef() const override;
    virtual void InstanceSubobjects( void* Data, void const* DefaultData, UObject* Owner, struct FObjectInstancingGraph* InstanceGraph ) override;
    virtual bool ContainsObjectReference(TArray<const UStructProperty*>& EncounteredStructProps) const override;
    virtual bool ContainsWeakObjectReference() const override;
    virtual void EmitReferenceInfo(UClass& OwnerClass, int32 BaseOffset, TArray<const UStructProperty*>& EncounteredStructProps) override;
    virtual bool SameType(const UProperty* Other) const override;
    virtual bool ConvertFromType(const FPropertyTag& Tag, FArchive& Ar, uint8* Data, UStruct* DefaultsStruct, bool& bOutAdvanceProperty) override;
    // End of UProperty interface

    FString GetCPPTypeCustom(FString* ExtendedTypeText, uint32 CPPExportFlags, const FString& InnerTypeText, const FString& InInnerExtendedTypeText) const;
};

  • UStructProperty
    描述结构体成员变量的属性。
//
// Describes a structure variable embedded in (as opposed to referenced by) 
// an object.
//
class COREUOBJECT_API UStructProperty : public UProperty
{
    DECLARE_CASTED_CLASS_INTRINSIC(UStructProperty, UProperty, 0, TEXT("/Script/CoreUObject"), CASTCLASS_UStructProperty)

    // Variables.
    class UScriptStruct* Struct;   // 该结构体类型的反射数据(元数据)
public:
    UStructProperty(ECppProperty, int32 InOffset, uint64 InFlags, UScriptStruct* InStruct);
    UStructProperty( const FObjectInitializer& ObjectInitializer, ECppProperty, int32 InOffset, uint64 InFlags, UScriptStruct* InStruct );

    // UObject interface
    virtual void Serialize( FArchive& Ar ) override;
    static void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);
    virtual void GetPreloadDependencies(TArray<UObject*>& OutDeps) override;
    // End of UObject interface
};

还有其它Property类,用于描述C++或蓝图的数据类型, TMap, TSet,Delegate等等。阅读源码的时候需要弄清各个字段的含义。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,558评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,002评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,024评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,144评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,255评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,295评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,068评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,478评论 1 305
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,789评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,965评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,649评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,267评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,982评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,800评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,847评论 2 351

推荐阅读更多精彩内容