RTTI相关函数

来源:互联网 发布:手机虚拟光驱软件 编辑:程序博客网 时间:2024/04/29 18:43
From  http://www.cnblogs.com/zhwx/archive/2012/08/30/2663138.html

RTTI相关函数

GetTypeData 函数

GetPropInfo 函数 
FindPropInfo 函数 
GetPropInfos 函数 
SortPropList 函数 

GetPropList 函数

GetObjectPropClass 函数 
PropType / PropIsType 函数 
IsPublishedProp 函数 
IsStoredProp 函数 
FreeAndNilProperties 函数 
SetToString / StringToSet 函数 
GetEnumName / GetEnumValue / GetEnumNameValue 函数 

GetTypeData 函数

GetTypeData 函数根据 TTypeInfo 指针获得 TTypeData 的地址。

function GetTypeData(TypeInfo: PTypeInfo): PTypeData; 
asm 
XOR  EDX,EDX  ; EDX 清零 
MOV  DL,[EAX].TTypeInfo.Name.Byte[0]  ; 获得 Name 字符串长度 
LEA  EAX,[EAX].TTypeInfo.Name[EDX+1]  ; 获得 TTypeData 的地址 
end;

GetPropInfo 函数

GetPropInfo 函数用于获得属性的 RTTI 指针 PPropInfo。它有四种重载形式,后面三种重载的实现都是调用第一种形式。AKinds 参数用于限制属性的类型,如果得到的 PPropInfo 不属于指定的类型,则返回 nil。

function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string): PPropInfo;

function GetPropInfo(Instance: TObject; const PropName: string; 
AKinds: TTypeKinds = []): PPropInfo; 
function GetPropInfo(AClass: TClass; const PropName: string; 
AKinds: TTypeKinds = []): PPropInfo; 
function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string; 
AKinds: TTypeKinds): PPropInfo;

FindPropInfo 函数

FindPropInfo 函数根据属性名称获得属性的 RTTI 指针,它只是在 GetPropInfo 函数的基础上加上了错误检查功能,如果没有属性 RTTI 信息,则触发 EPropertyError 异常。

function FindPropInfo(Instance: TObject; const PropName: string): PPropInfo; 
function FindPropInfo(AClass: TClass; const PropName: string): PPropInfo;

GetPropInfos 函数

GetPropInfos 函数的功能是把一个类(class)所有属性 RTTI 指针 PPropInfo 填充至传入的参数 PPropList 数组中。

注意:这个函数不负责分配该数组的内容,使用前必须根据属性的数量分配足够的空间。该数组结束后必须清除分配的内容。

procedure GetPropInfos(TypeInfo: PTypeInfo; PropList: PPropList);

注:使用 GetPropList 实现相同的功能更方便。

SortPropList 函数

SortPropList 可以对 GetPropInfos 函数填充的属性信息指针数组按属性名称排序。

procedure SortPropList(PropList: PPropList; PropCount: Integer);

在 VCL 中 SortPropList 只被 GetPropList 函数使用。

GetPropList 函数

GetPropList 函数同 GetPropInfos 一样,填充 PPropList 数组。GetPropList 实际上是调用 GetPropInfos 进行填充工作,最后返回已填充的属性的数量。

function GetPropList(TypeInfo: PTypeInfo; TypeKinds: TTypeKinds; 
PropList: PPropList; SortList: Boolean): Integer;

function GetPropList(TypeInfo: PTypeInfo; out PropList: PPropList): Integer; 
function GetPropList(AObject: TObject; out PropList: PPropList): Integer;

注意:GetPropList 的内存分配有点混乱,上面第一个 GetPropList 必须自己分配 PPrpList 数组的内存,后面二个 GetPropList 会自动分配 PPropList 数组的内存。造成这种情况的原因是:第一个 GetPropList 可以设置 TypeKinds 参数限制只返回指定类型的属性,这样就不能直接得到可能返回的属性数量。TypeKinds 参数可以设置为 tkAny,表示返回所有数据类型的属性。

第一个 GetPropList 函数可以设置 SortList 参数对属性名称进行排序。它实际上是调用第二个 GetPropList 并调用 SortPropList 函数执行排序。

注意:PPropList 不再使用的时候,要记得使用 FreeMem 函数清除数组内存(根据返回值是否大于1)。

GetObjectPropClass 函数

GetObjectPropClass 函数用于返回对象类型的属性所属的类(class)。

function GetObjectPropClass(Instance: TObject; PropInfo: PPropInfo): TClass; 
function GetObjectPropClass(Instance: TObject; const PropName: string): TClass; 
function GetObjectPropClass(PropInfo: PPropInfo): TClass;

这个函数被 SetObjectProp 函数使用,用于参数检验。

PropType / PropIsType 函数

PropType 函数用于获得属性的数据类型。

function PropType(Instance: TObject; const PropName: string): TTypeKind; 
function PropType(AClass: TClass; const PropName: string): TTypeKind;

PropIsType 判断属性是否属于某种数据类型。它调用 PropType 实现功能。

function PropIsType(Instance: TObject; const PropName: string; 
TypeKind: TTypeKind): Boolean; 
function PropIsType(AClass: TClass; const PropName: string; 
TypeKind: TTypeKind): Boolean;

IsPublishedProp 函数

IsPublishedProp 函数用于判断属性是否是 published 属性,它通过检查该属性 RTTI 指针是否等于 nil 来实现功能。

function IsPublishedProp(Instance: TObject; const PropName: string): Boolean; 
function IsPublishedProp(AClass: TClass; const PropName: string): Boolean;

IsPublishedProp 函数没有被 VCL 使用。

IsStoredProp 函数

IsStoredProp 函数使用属性信息中的 TPropInfo.StoredProp 函数指针来调用属性定义时用 stored 关键字定义的函数的结果。

这个函数被用于 Delphi 持续机制,TWriter.WriteProperties 方法调用 IsStoredProp 判断是否需要把该属性的值写入流中。

function IsStoredProp(Instance: TObject; PropInfo: PPropInfo): Boolean; 
function IsStoredProp(Instance: TObject; const PropName: string): Boolean;

FreeAndNilProperties 函数

FreeAndNilProperties 函数用于清除一个对象的所有 published 的对象类型的属性的对象。这个函数调用 GetObjectProp 执行获得对象属性的对象句柄,并调用对象的 Free 方法清除这个对象,然后调用 SetObjectProp 设置该属性为 nil。

procedure FreeAndNilProperties(AObject: TObject);

我不知道这个函数能用在哪里,至少 VCL 中没有使用这个函数。

SetToString / StringToSet 函数

SetToString 和 StringToSet 是两个 RTTI 辅助函数,它们把集合值转换为字符串,或者把字符串转换为集合值。

function SetToString(PropInfo: PPropInfo; Value: Integer; 
Brackets: Boolean = False): string;

function StringToSet(PropInfo: PPropInfo; const Value: string): Integer;

注意:这里的集合值最多只能包含 32 个元素(4 bytes),这是集合 RTTI 的限制。

GetEnumName / GetEnumValue / GetEnumNameValue 函数

GetEnumName 函数根据枚举整数值返回枚举字符串。它可以返回以下三种枚举名称:

Integer:直接返回 IntToStr(Integer) 
Boolean:返回 True/False 
Enum  :返回 TTypeData^.NameList 中存储的枚举名称

function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;

GetEnumValue 函数根据枚举字符串返回枚举整数值。它与 GetEnumName 类似,可以返回三种枚举的整数值,但对于 Enum 类型,它调用了 GetEnumNameValue 函数。

function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;

GetEnumNameValue 函数与 GetEnumValue 函数功能差不多,但它是个汇编函数,只能返回纯枚举类型的值。其工作原理也是匹配 TTypeData^.NameList 值。

function GetEnumNameValue(TypeInfo: PTypeInfo; const Name: string): Integer;

注意:GetEnumNameValue 隐藏在 Implementation 段,不能直接使用,它是为 GetEnumValue 函数服务的。

原创粉丝点击