RTTI Part 5. Summary.


© Lyapin Ilya
Articles in this Topic    Discussions in this Topic

RTTI is undocumented in Delphi 3,Delphi 4, Delphi 5 as well as in any version of C++ Builder. There are two places where you can find information about RTTI - typinfo.pas source file, which is located in X:\delphi\Source\VCL directory and this article. This article contains a complete listing of RTTI types, functions and procedures.

TypeInfo function.

Function TypeInfo(TypeIdent):pointer;
Retrieves a pointer to the RTTI for given type. This function is built into the compiler and does not have actual declaration, but can be used anywhere.

TObject.ClassInfo

class function ClassInfo: Pointer;
Returns a pointer to the RTTI table for the object type. You should use in instead of TypeInfo when RTTI functions are used on objects.

GetTypeData.

function GetTypeData(TypeInfo: PTypeInfo): PTypeData;
GetTypeData retrieves a detailed type information (See TTypeData record). The only argument is a pointer to the type's RTTI information.

E.g.
pIntTypeData:=GetTypeData(typeInfo(integer))
pObjTypeData:=GetTypeData(obj.ClassInfo)

TTypeKind

TTypeKind enumerated type is defined as foolowing:
TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat, tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString, tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray);
TTypeKinds = set of TTypeKind;

Note that tkInt64 and tkDynArray constants are not included in Delphi 3 There are no corresponding types (Int64 and Dynamic array) in Delphi 3.
TTypeKinds is a set of TTypeKind values.

Enumerated type functions.

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

GetEnumName is used to obtain a string equivalent of enum value, SetEnumValue retrieves ordinal value from the string representation.
The first argument of either function is a pointer to the RTTI for an enumerated type. The second argument is a string or an ordinal correspondingly.
Example:
S:=GetEnumName(TypeInfo(TBitBtnKind),3);
ShowMessage(S); // displays "bkHelp"
I:=GetEnumValue(TypeInfo(TBitBtnKind),s);
ShowMessage(IntToStr(i)); // displays 3

GetPropInfo.

function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string): PPropInfo;
GetPropInfo retrieves a detailed information on the given property. The first argument is a pointer to the RTTI for an object' class. PropName is a string that contains the name of the property.
Delphi 5 also defines an overloaded routine where a kind of the property can be specified:
function GetPropInfo(TypeInfo: PTypeInfo; const PropName: string; TypeKinds: TTypeKinds): PPropInfo;
This function returns nil if the property of given name has other type than specified.
Example:
for i:=0 to ComponentCount-1 do
pPInfo:=GetPropInfo(Components[i].ClassInfo,'Color');
GetPropInfos and GetPropList.
procedure GetPropInfos(TypeInfo: PTypeInfo; PropList: PPropList);
function GetPropList(TypeInfo: PTypeInfo; TypeKinds: TTypeKinds;
PropList: PPropList): Integer;

These routines retrieve the list of an object's propertyes. The first argument is a pointer to the RTTI for an object. PropList is a pointer to an array of PPropInfo records. Application should allocate memory for the array.
I recommend you to write something like following:
Count := GetPropList( Obj.ClassInfo ,tkAll, nil);
PpList := AllocMem( Count * Sizeof ( ppList[0]));

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo