Subsections
typinfex
The TypeInfo unit contains many routines which can be used for
the querying of the Run-Time Type Information (RTTI) which is generated
by the compiler for classes that are compiled under the {M+}
switch. This information can be used to retrieve or set property values
for published properties for totally unknown classes. In particular, it
can be used to stream classes. The TPersistent class in the
Classes unit is compiled in the {M+} state and serves
as the base class for all classes that need to be streamed.
The unit should be compatible to the Delphi 5 unit with the same name.
The only calls that are still missing are the Variant calls, since Free Pascal
does not support the variant type yet.
The examples in this chapter use a rttiobj file, which contains
an object that has a published property of all supported types. It also
contains some auxiliary routines and definitions.
The following constants are used in the implementation section of the unit.
BooleanIdents: array[Boolean] of String = ('False', 'True');
DotSep: String = '.';
The following constants determine the access method for the Stored
identifier of a property as used in the PropProcs field of the
TPropInfo record:
ptField = 0;
ptStatic = 1;
ptVirtual = 2;
ptConst = 3;
The following typed constants are used for easy selection of property types.
tkAny = [Low(TTypeKind)..High(TTypeKind)];
tkMethods = [tkMethod];
tkProperties = tkAny-tkMethods-[tkUnknown];
The following pointer types are defined:
PShortString =^ShortString;
PByte =^Byte;
PWord =^Word;
PLongint =^Longint;
PBoolean =^Boolean;
PSingle =^Single;
PDouble =^Double;
PExtended =^Extended;
PComp =^Comp;
PFixed16 =^Fixed16;
Variant = Pointer;
The TTypeKind determines the type of a property:
TTypeKind = (tkUnknown,tkInteger,tkChar,tkEnumeration,
tkFloat,tkSet,tkMethod,tkSString,tkLString,tkAString,
tkWString,tkVariant,tkArray,tkRecord,tkInterface,
tkClass,tkObject,tkWChar,tkBool,tkInt64,tkQWord,
tkDynArray,tkInterfaceRaw);
tkString = tkSString;
tkString is an alias that is introduced for Delphi compatibility.
If the property is and ordinal type, then TTOrdType determines the
size and sign of the ordinal type:
TTOrdType = (otSByte,otUByte,otSWord,otUWord,otSLong,otULong);
The size of a float type is determined by TFloatType:
TFloatType = (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,
ftFixed16,ftFixed32);
A method property (e.g. an event) can have one of several types:
TMethodKind = (mkProcedure,mkFunction,mkConstructor,mkDestructor,
mkClassProcedure, mkClassFunction);
The kind of parameter to a method is determined by TParamFlags:
TParamFlags = set of (pfVar,pfConst,pfArray,pfAddress,pfReference,pfOut);
Interfaces are described further with TntfFlags:
TIntfFlags = set of (ifHasGuid,ifDispInterface,ifDispatch);
The following defines a set of TTypeKind:
TTypeKinds = set of TTypeKind;
The TypeInfo function returns a pointer to a TTypeInfo record:
TTypeInfo = record
Kind : TTypeKind;
Name : ShortString;
end;
PTypeInfo = ^TTypeInfo;
PPTypeInfo = ^PTypeInfo;
Note that the Name is stored with as much bytes as needed to store the name,
it is not padded to 255 characters.
The type data immediatly follows the TTypeInfo record as a TTypeData record:
PTypeData = ^TTypeData;
TTypeData = packed record
case TTypeKind of
tkUnKnown,tkLString,tkWString,tkAString,tkVariant:
();
tkInteger,tkChar,tkEnumeration,tkWChar:
(OrdType : TTOrdType;
case TTypeKind of
tkInteger,tkChar,tkEnumeration,tkBool,tkWChar : (
MinValue,MaxValue : Longint;
case TTypeKind of
tkEnumeration: (
BaseType : PTypeInfo;
NameList : ShortString
)
);
tkSet: (
CompType : PTypeInfo
)
);
tkFloat: (
FloatType : TFloatType
);
tkSString:
(MaxLength : Byte);
tkClass:
(ClassType : TClass;
ParentInfo : PTypeInfo;
PropCount : SmallInt;
UnitName : ShortString
);
tkMethod:
(MethodKind : TMethodKind;
ParamCount : Byte;
ParamList : array[0..1023] of Char
{in reality ParamList is a array[1..ParamCount] of:
record
Flags : TParamFlags;
ParamName : ShortString;
TypeName : ShortString;
end;
followed by
ResultType : ShortString}
);
tkInt64:
(MinInt64Value, MaxInt64Value: Int64);
tkQWord:
(MinQWordValue, MaxQWordValue: QWord);
tkInterface:
();
end;
If the typeinfo kind is tkClass, then the property
information follows the UnitName string, as an array of TPropInfo records.
The TPropData record is not used, but is provided for completeness and
compatibility with Delphi.
TPropData = packed record
PropCount : Word;
PropList : record end;
end;
The TPropInfo record describes one published property of a class:
PPropInfo = ^TPropInfo;
TPropInfo = packed record
PropType : PTypeInfo;
GetProc : Pointer;
SetProc : Pointer;
StoredProc : Pointer;
Index : Integer;
Default : Longint;
NameIndex : SmallInt;
PropProcs : Byte;
Name : ShortString;
end;
The Name field is stored not with 255 characters, but with just as many characters
as required to store the name.
TProcInfoProc = procedure(PropInfo : PPropInfo) of object;
The following pointer and array types are used for typecasts:
PPropList = ^TPropList;
TPropList = array[0..65535] of PPropInfo;
What follows is a listing of the available functions, grouped by category.
For each function there is a reference to the page where the function
can be found.
Functions for retrieving or examining property information
FindPropInfo Getting property type information, With error checking.]
- GetPropInfo Getting property type information, No error checking.
- GetPropInfos Find property information of a certain kind
- GetObjectPropClass Return the declared class of an object property
- GetPropList Get a list of all published properties
- IsPublishedProp Is a property published
- IsStoredProp Is a property stored
- PropIsType Is a property of a certain kind
- PropType Return the type of a property
-
Functions to set or set a property's value.
GetEnumProp Return the value of an enumerated type property]
- GetFloatProp Return the value of a float property
- GetInt64Prop Return the value of an Int64 property
- GetMethodProp Return the value of a procedural type property
- GetObjectProp Return the value of an object property
- GetOrdProp Return the value of an ordinal type property
- GetPropValue Return the value of a property as a variant
- GetSetProp Return the value of a set property
- GetStrProp Return the value of a string property
- GetVariantProp Return the value of a variant property
- SetEnumProp Set the value of an enumerated type property
- SetFloatProp Set the value of a float property
- SetInt64Prop Set the value of an Int64 property
- SetMethodProp Set the value of a procedural type property
- SetObjectProp Set the value of an object property
- SetOrdProp Set the value of an ordinal type property
- SetPropValue Set the value of a property trhough a variant
- SetSetProp Set the value of a set property
- SetStrProp Set the value of a string property
- SetVariantProp Set the value of a variant property
-
GetEnumName Get an enumerated type element name]
- GetEnumValue Get ordinal number of an enumerated tye, based on the
name.
- GetTypeData Skip type name and return a pointer to the type data
- SetToString Convert a set to its string representation
- StringToSet Convert a string representation of a set to a set
-
1 FindPropInfo
-
Declaration
- Function FindPropInfo(AClass:TClass;const PropName: string): PPropInfo;
Function FindPropInfo(Instance: TObject; const PropName: string): PPropInfo;
-
Description
- FindPropInfo
examines the published property information of a class and
returns a pointer to the property information for property PropName.
The class to be examined can be specified in one of two ways:
- AClass
- a class pointer.
- Instance
- an instance of the class to be investigated.
If the property does not exist, a EPropertyError exception will be
raised. The GetPropInfo function has the same function as the
FindPropInfo function, but returns Nil if the property does not
exist.
-
Errors
- Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetPropInfo, GetPropList, GetPropInfos
-
Example
Program example13;
{ This program demonstrates the FindPropInfo function }
{$mode objfpc}
uses
rttiobj,typinfo,sysutils;
Var
O : TMyTestObject;
PT : PTypeData;
PI : PPropInfo;
I,J : Longint;
PP : PPropList;
prI : PPropInfo;
begin
O:=TMyTestObject.Create;
PI:=FindPropInfo(O,'BooleanField');
Writeln('FindPropInfo(Instance,BooleanField) : ',PI^.Name);
PI:=FindPropInfo(O.ClassType,'ByteField');
Writeln('FindPropInfo(Class,ByteField) : ',PI^.Name);
Write ('FindPropInfo(Class,NonExistingProp) : ');
Try
PI:=FindPropInfo(O,'NonExistingProp');
except
On E: Exception do
Writeln('Caught exception "',E.ClassName,'" with message : ',E.Message);
end;
O.Free;
end.
2 GetEnumName
-
Declaration
- Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
-
Description
- GetEnumName
scans the type information for the enumeration type
described by TypeInfo and returns the name of the enumeration
constant for the element with ordinal value equal to Value.
If Value is out of range, the first element of the enumeration type
is returned. The result is lowercased, but this may change in the future.
This can be used in combination with GetOrdProp to stream a property
of an enumerated type.
-
Errors
- No check is done to determine whether TypeInfo really points to the
type information for an enumerated type.
-
See also
- GetOrdProp, GetEnumValue
-
Example
program example9;
{ This program demonstrates the GetEnumName, GetEnumValue functions }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
TI : PTypeInfo;
begin
O:=TMyTestObject.Create;
TI:=GetPropInfo(O,'MyEnumField')^.PropType;
Writeln('GetEnumName : ',GetEnumName(TI,Ord(O.MyEnumField)));
Writeln('GetEnumValue(mefirst) : ',GetEnumName(TI,GetEnumValue(TI,'mefirst')));
O.Free;
end.
3 GetEnumProp
-
Declaration
- Function GetEnumProp(Instance: TObject;const PropInfo: PPropInfo): string;
Function GetEnumProp(Instance: TObject;const PropName: string): string;
-
Description
- GetEnumProp
returns the value of an property of an enumerated type
and returns the name of the enumerated value for the objetc Instance.
The property whose value must be returned can be specified by its property
info in PropInfo or by its name in PropName
-
Errors
- No check is done to determine whether PropInfo really points to the
property information for an enumerated type.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- SetEnumProp GetOrdProp, GetStrProp,
GetInt64Prop,GetMethodProp, GetSetProp,
GetObjectProp, GetEnumProp
-
Example
program example2;
{ This program demonstrates the GetEnumProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
TI : PTypeInfo;
begin
O:=TMyTestObject.Create;
PI:=GetPropInfo(O,'MyEnumField');
TI:=PI^.PropType;
Writeln('Enum property : ');
Writeln('Value : ',GetEnumName(TI,Ord(O.MyEnumField)));
Writeln('Get (name) : ',GetEnumProp(O,'MyEnumField'));
Writeln('Get (propinfo) : ',GetEnumProp(O,PI));
SetEnumProp(O,'MyEnumField','meFirst');
Writeln('Set (name,meFirst) : ',GetEnumName(TI,Ord(O.MyEnumField)));
SetEnumProp(O,PI,'meSecond');
Writeln('Set (propinfo,meSecond) : ',GetEnumName(TI,Ord(O.MyEnumField)));
O.Free;
end.
4 GetEnumValue
-
Declaration
- Function GetEnumValue(TypeInfo : PTypeInfo;const Name : string) : Integer;
-
Description
- GetEnumValue
scans the type information for the enumeration type
described by TypeInfor and returns the ordinal value for the element
in the enumerated type that has identifier Name. The identifier is
searched in a case-insensitive manner.
This can be used to set the value of enumerated properties from a stream.
-
Errors
- If Name is not found in the list of enumerated values, then -1 is
returned. No check is done whether TypeInfo points to the type information
for an enumerated type.
-
See also
- GetEnumName, SetOrdProp
For an example, see GetEnumName.
5 GetFloatProp
-
Declaration
- Function GetFloatProp(Instance : TObject;PropInfo : PPropInfo) : Extended;
Procedure SetFloatProp(Instance: TObject; const PropName: string; Value: Extended);
-
Description
- GetFloatProp
returns the value of the float property described by
PropInfo or with name Propname for the object Instance.
All float types are converted
to extended.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid float property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- SetFloatProp, GetOrdProp, GetStrProp,
GetInt64Prop,GetMethodProp, GetSetProp,
GetObjectProp, GetEnumProp
-
Example
program example4;
{ This program demonstrates the GetFloatProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
begin
O:=TMyTestObject.Create;
Writeln('Real property : ');
PI:=GetPropInfo(O,'RealField');
Writeln('Value : ',O.RealField);
Writeln('Get (name) : ',GetFloatProp(O,'RealField'));
Writeln('Get (propinfo) : ',GetFloatProp(O,PI));
SetFloatProp(O,'RealField',system.Pi);
Writeln('Set (name,pi) : ',O.RealField);
SetFloatProp(O,PI,exp(1));
Writeln('Set (propinfo,e) : ',O.RealField);
Writeln('Extended property : ');
PI:=GetPropInfo(O,'ExtendedField');
Writeln('Value : ',O.ExtendedField);
Writeln('Get (name) : ',GetFloatProp(O,'ExtendedField'));
Writeln('Get (propinfo) : ',GetFloatProp(O,PI));
SetFloatProp(O,'ExtendedField',system.Pi);
Writeln('Set (name,pi) : ',O.ExtendedField);
SetFloatProp(O,PI,exp(1));
Writeln('Set (propinfo,e) : ',O.ExtendedField);
O.Free;
end.
6 GetInt64Prop
-
Declaration
- Function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64;
Function GetInt64Prop(Instance: TObject; const PropName: string): Int64;
-
Description
Publishing of Int64 properties is not yet supported by Free Pascal. This
function is provided for Delphi compatibility only at the moment.
GetInt64Prop returns the value of the property of type
Int64 that is described by PropInfo or with name Propname
for the object Instance.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid Int64 property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception
-
See also
- SetInt64Prop, GetOrdProp, GetStrProp,
GetFloatProp, GetMethodProp, GetSetProp,
GetObjectProp, GetEnumProp
-
Example
program example15;
{ This program demonstrates the GetInt64Prop function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
begin
O:=TMyTestObject.Create;
Writeln('Int64 property : ');
PI:=GetPropInfo(O,'Int64Field');
Writeln('Value : ',O.Int64Field);
Writeln('Get (name) : ',GetInt64Prop(O,'Int64Field'));
Writeln('Get (propinfo) : ',GetInt64Prop(O,PI));
SetInt64Prop(O,'Int64Field',12345);
Writeln('Set (name,12345) : ',O.Int64Field);
SetInt64Prop(O,PI,54321);
Writeln('Set (propinfo,54321) : ',O.Int64Field);
O.Free;
end.
7 GetMethodProp
-
Declaration
- Function GetMethodProp(Instance : TObject;PropInfo : PPropInfo) : TMethod;
Function GetMethodProp(Instance: TObject; const PropName: string): TMethod;
-
Description
- GetMethodProp
returns the method the property described by
PropInfo or with name Propname for object Instance.
The return type TMethod is defined in the SysUtils unit as:
TMethod = packed record
Code, Data: Pointer;
end;
Data points to the instance of the class with the method Code.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid method property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception
-
See also
- SetMethodProp, GetOrdProp, GetStrProp,
GetFloatProp, GetInt64Prop, GetSetProp,
GetObjectProp, GetEnumProp
-
Example
program example6;
{ This program demonstrates the GetMethodProp function }
{$mode objfpc}
uses rttiobj,typinfo,sysutils;
Type
TNotifyObject = Class(TObject)
Procedure Notification1(Sender : TObject);
Procedure Notification2(Sender : TObject);
end;
Procedure TNotifyObject.Notification1(Sender : TObject);
begin
Write('Received notification 1 of object with class: ');
Writeln(Sender.ClassName);
end;
Procedure TNotifyObject.Notification2(Sender : TObject);
begin
Write('Received notification 2 of object with class: ');
Writeln(Sender.ClassName);
end;
Var
O : TMyTestObject;
PI : PPropInfo;
NO : TNotifyObject;
M : TMethod;
Procedure PrintMethod (Const M : TMethod);
begin
If (M.Data=Pointer(NO)) Then
If (M.Code=Pointer(@TNotifyObject.Notification1)) then
Writeln('Notification1')
else If (M.Code=Pointer(@TNotifyObject.Notification2)) then
Writeln('Notification2')
else
begin
Write('Unknown method adress (data:');
Write(hexStr(Longint(M.data),8));
Writeln(',code:',hexstr(Longint(M.Code),8),')');
end;
end;
begin
O:=TMyTestObject.Create;
NO:=TNotifyObject.Create;
O.NotifyEvent:=@NO.Notification1;
PI:=GetPropInfo(O,'NotifyEvent');
Writeln('Method property : ');
Write('Notifying : ');
O.Notify;
Write('Get (name) : ');
M:=GetMethodProp(O,'NotifyEvent');
PrintMethod(M);
Write('Notifying : ');
O.Notify;
Write('Get (propinfo) : ');
M:=GetMethodProp(O,PI);
PrintMethod(M);
M.Data:=No;
M.Code:=Pointer(@NO.Notification2);
SetMethodProp(O,'NotifyEvent',M);
Write('Set (name,Notification2) : ');
M:=GetMethodProp(O,PI);
PrintMethod(M);
Write('Notifying : ');
O.Notify;
Write('Set (propinfo,Notification1) : ');
M.Data:=No;
M.Code:=Pointer(@NO.Notification1);
SetMethodProp(O,PI,M);
M:=GetMethodProp(O,PI);
PrintMethod(M);
Write('Notifying : ');
O.Notify;
O.Free;
end.
8 GetObjectProp
-
Declaration
- Function GetObjectProp(Instance: TObject; const PropName: string): TObject;
Function GetObjectProp(Instance: TObject; const PropName: string; MinClass:TClass): TObject;
Function GetObjectProp(Instance: TObject; PropInfo: PPropInfo; MinClass: TClass):
TObject;
-
Description
- GetObjectProp
returns the object which the property descroibed by
PropInfo with name Propname points to for object Instance.
If MinClass is specified, then if the object is not descendent of
class MinClass, then Nil is returned.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid method property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- SetMethodProp, GetOrdProp, GetStrProp,
GetFloatProp, GetInt64Prop, GetSetProp,
GetObjectProp, GetEnumProp
-
Example
program example5;
{ This program demonstrates the GetObjectProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
NO1,NO2 : TNamedObject;
begin
O:=TMyTestObject.Create;
NO1:=TNamedObject.Create;
NO1.ObjectName:='First named object';
NO2:=TNamedObject.Create;
NO2.ObjectName:='Second named object';
O.ObjField:=NO1;
Writeln('Object property : ');
PI:=GetPropInfo(O,'ObjField');
Write('Property class : ');
Writeln(GetObjectPropClass(O,'ObjField').ClassName);
Write('Value : ');
Writeln((O.ObjField as TNamedObject).ObjectName);
Write('Get (name) : ');
Writeln((GetObjectProp(O,'ObjField') As TNamedObject).ObjectName);
Write('Get (propinfo) : ');
Writeln((GetObjectProp(O,PI,TObject) as TNamedObject).ObjectName);
SetObjectProp(O,'ObjField',NO2);
Write('Set (name,NO2) : ');
Writeln((O.ObjField as TNamedObject).ObjectName);
SetObjectProp(O,PI,NO1);
Write('Set (propinfo,NO1) : ');
Writeln((O.ObjField as TNamedObject).ObjectName);
O.Free;
end.
9 GetObjectPropClass
-
Declaration
- Function GetObjectPropClass(Instance: TObject; const PropName: string): TClass;
-
Description
- GetObjectPropClass
returns the declared class of the property with name
PropName. This may not be the actual class of the property value.
-
Errors
- No checking is done whether Instance is non-nil.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- SetMethodProp, GetOrdProp, GetStrProp,
GetFloatProp, GetInt64Prop
For an example, see GetObjectProp.
10 GetOrdProp
-
Declaration
- Function GetOrdProp(Instance : TObject;PropInfo : PPropInfo) : Longint;
Function GetOrdProp(Instance: TObject;const PropName: string): Longint;
-
Description
- GetOrdProp
returns the value of the ordinal property described by
PropInfo or with name PropName for the object Instance.
The value is returned as a longint, which should be typecasted to the
needed type.
Ordinal properties that can be retrieved include:
- Integers and subranges of integers
- The value of the integer will be
returned.
- Enumerated types and subranges of enumerated types
- The ordinal value
of the enumerated type will be returned.
- Sets
- If the base type of the set has less than 31 possible values.
If a bit is set in the return value, then the corresponding element of the
base ordinal class of the set type must be included in the set.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid ordinal property of Instance
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- SetOrdProp, GetStrProp, GetFloatProp,
GetInt64Prop,GetMethodProp, GetSetProp,
GetObjectProp, GetEnumProp
-
Example
program example1;
{ This program demonstrates the GetOrdProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
begin
O:=TMyTestObject.Create;
Writeln('Boolean property : ');
Writeln('Value : ',O.BooleanField);
Writeln('Ord(Value) : ',Ord(O.BooleanField));
Writeln('Get (name) : ',GetOrdProp(O,'BooleanField'));
PI:=GetPropInfo(O,'BooleanField');
Writeln('Get (propinfo) : ',GetOrdProp(O,PI));
SetOrdProp(O,'BooleanField',Ord(False));
Writeln('Set (name,false) : ',O.BooleanField);
SetOrdProp(O,PI,Ord(True));
Writeln('Set (propinfo,true) : ',O.BooleanField);
O.Free;
end.
11 GetPropInfo
-
Declaration
- Function GetPropInfo(AClass: TClass; const PropName: string; AKinds: TTypeKinds) : PPropInfo;
Function GetPropInfo(AClass: TClass; const PropName: string): PPropInfo;
Function GetPropInfo(Instance: TObject; const PropName: string): PPropInfo;
Function GetPropInfo(Instance: TObject; const PropName: string; AKinds: TTypeKinds) : PPropInfo;
Function GetPropInfo(TypeInfo: PTypeInfo;const PropName: string) : PPropInfo;
Function GetPropInfo(TypeInfo: PTypeInfo;const PropName: string; AKinds : TTypeKinds) : PPropInfo;
-
Description
- GetPropInfo
returns a pointer to the TPropInfo record for a the
PropName property of a class. The class to examine can be specified
in one of three ways:
- Instance
- An instance of the class.
- AClass
- A class pointer to the class.
- TypeInfo
- A pointer to the type information of the class.
In each of these three ways, if AKinds is specified, if the property
has TypeKind which is not included in Akinds, Nil will be
returned.
-
Errors
- If the property PropName does not exist, Nil is returned.
-
See also
- GetPropInfos,GetPropList
For an example, see most of the other functions.
12 GetPropInfos
-
Declaration
- Procedure GetPropInfos(TypeInfo: PTypeInfo;PropList: PPropList);
-
Description
- GetPropInfos
stores pointers to the property information of all published
properties of a class with class info TypeInfo in the list pointed to by
Proplist. The PropList pointer must point to a memory location that
contains enough space to hold all properties of the class and its parent classes.
-
Errors
- No checks are done to see whether PropList points to a memory area that
is big enough to hold all pointers.
-
See also
- GetPropInfo,GetPropList
-
Example
Program example12;
{ This program demonstrates the GetPropInfos function }
uses
rttiobj,typinfo;
Var
O : TMyTestObject;
PT : PTypeData;
PI : PTypeInfo;
I,J : Longint;
PP : PPropList;
prI : PPropInfo;
begin
O:=TMyTestObject.Create;
PI:=O.ClassInfo;
PT:=GetTypeData(PI);
Writeln('Property Count : ',PT^.PropCount);
GetMem (PP,PT^.PropCount*SizeOf(Pointer));
GetPropInfos(PI,PP);
For I:=0 to PT^.PropCount-1 do
begin
With PP^[i]^ do
begin
Write('Property ',i+1:3,': ',name:30);
writeln(' Type: ',TypeNames[typinfo.PropType(O,Name)]);
end;
end;
FreeMem(PP);
O.Free;
end.
13 GetPropList
-
Declaration
- Function GetPropList(TypeInfo : PTypeInfo;
TypeKinds : TTypeKinds;
PropList : PPropList) : Integer;
-
Description
- GetPropList
stores pointers to property information of the class with class
info TypeInfo for properties of kind TypeKinds in the list pointed to
by Proplist. PropList must contain enough space to hold all properties.
The function returns the number of pointers that matched the criteria and were stored
in PropList.
-
Errors
- No checks are done to see whether PropList points to a memory area that is big enough
to hold all pointers.
-
See also
- GetPropInfos, GetPropInfo
-
Example
Program example13;
{ This program demonstrates the GetPropList function }
uses
rttiobj,typinfo;
Var
O : TMyTestObject;
PT : PTypeData;
PI : PTypeInfo;
I,J : Longint;
PP : PPropList;
prI : PPropInfo;
begin
O:=TMyTestObject.Create;
PI:=O.ClassInfo;
PT:=GetTypeData(PI);
Writeln('Total property Count : ',PT^.PropCount);
GetMem (PP,PT^.PropCount*SizeOf(Pointer));
J:=GetPropList(PI,OrdinalTypes,PP);
Writeln('Ordinal property Count : ',J);
For I:=0 to J-1 do
begin
With PP^[i]^ do
begin
Write('Property ',i+1:3,': ',name:30);
writeln(' Type: ',TypeNames[typinfo.PropType(O,Name)]);
end;
end;
FreeMem(PP);
O.Free;
end.
14 GetPropValue
-
Declaration
- Function GetPropValue(Instance: TObject; const PropName: string): Variant;
Function GetPropValue(Instance: TObject; const PropName: string; PreferStrings: Boolean): Variant;
-
Description
Due to missing Variant support, GetPropValue is not yet implemented.
The declaration is provided for compatibility with Delphi.
-
Errors
-
See also
15 GetSetProp
-
Declaration
- Function GetSetProp(Instance: TObject; const PropInfo: PPropInfo; Brackets: Boolean):
string;
Function GetSetProp(Instance: TObject; const PropName: string): string;
Function GetSetProp(Instance: TObject; const PropName: string; Brackets: Boolean): string;
-
Description
- GetSetProp
returns the contents of a set property as a string.
The property to be returned can be specified by it's name in PropName
or by its property information in PropInfo.
The returned set is a string representation of the elements in the set as
returned by SetToString. The Brackets option can be used to
enclose the string representation in square brackets.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid ordinal property of Instance
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- SetSetProp, GetStrProp, GetFloatProp,
GetInt64Prop,GetMethodProp
-
Example
program example7;
{ This program demonstrates the GetSetProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
Function SetAsString (ASet : TMyEnums) : String;
Var
i : TmyEnum;
begin
result:='';
For i:=mefirst to methird do
If i in ASet then
begin
If (Result<>'') then
Result:=Result+',';
Result:=Result+MyEnumNames[i];
end;
end;
Var
S : TMyEnums;
begin
O:=TMyTestObject.Create;
O.SetField:=[mefirst,meSecond,meThird];
Writeln('Set property : ');
Writeln('Value : ',SetAsString(O.SetField));
Writeln('Ord(Value) : ',Longint(O.SetField));
Writeln('Get (name) : ',GetSetProp(O,'SetField'));
PI:=GetPropInfo(O,'SetField');
Writeln('Get (propinfo) : ',GetSetProp(O,PI,false));
S:=[meFirst,meThird];
SetOrdProp(O,'SetField',Integer(S));
Write('Set (name,[mefirst,methird]) : ');
Writeln(SetAsString(O.SetField));
S:=[meSecond];
SetOrdProp(O,PI,Integer(S));
Write('Set (propinfo,[meSecond]) : ');
Writeln(SetAsString(O.SetField));
O.Free;
end.
16 GetStrProp
-
Declaration
- Function GetStrProp(Instance : TObject;
PropInfo : PPropInfo) : Ansistring;
Function GetStrProp(Instance: TObject;
const PropName: string): string;
-
Description
- GetStrProp
returns the value of the string property described by
PropInfo or with name PropName for object Instance.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid string property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- SetStrProp, GetOrdProp, GetFloatProp,
GetInt64Prop,GetMethodProp
-
Example
program example3;
{ This program demonstrates the GetStrProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
begin
O:=TMyTestObject.Create;
PI:=GetPropInfo(O,'AnsiStringField');
Writeln('String property : ');
Writeln('Value : ',O.AnsiStringField);
Writeln('Get (name) : ',GetStrProp(O,'AnsiStringField'));
Writeln('Get (propinfo) : ',GetStrProp(O,PI));
SetStrProp(O,'AnsiStringField','First');
Writeln('Set (name,''First'') : ',O.AnsiStringField);
SetStrProp(O,PI,'Second');
Writeln('Set (propinfo,''Second'') : ',O.AnsiStringField);
O.Free;
end.
17 GetTypeData
-
Declaration
- Function GetTypeData(TypeInfo : PTypeInfo) : PTypeData;
-
Description
- GetTypeData
returns a pointer to the TTypeData record that
follows after the TTypeInfo record pointed to by TypeInfo.
It essentially skips the Kind and Name fields in the
TTypeInfo record.
-
Errors
- None.
-
See also
18 GetVariantProp
-
Declaration
- Function GetVariantProp(Instance : TObject;PropInfo : PPropInfo): Variant;
-
Description
Due to mising Variant support, the GetVariantProp function is not
yet implemented. Provided for Delphi compatibility only.
-
Errors
-
See also
- SetVariantProp
19 IsPublishedProp
-
Declaration
- Function IsPublishedProp(AClass: TClass; const PropName: string): Boolean;
Function IsPublishedProp(Instance: TObject; const PropName: string): Boolean;
-
Description
- IsPublishedProp
returns true if a class has a published property with
name PropName. The class can be specfied in one of two ways:
- AClass
- A class pointer to the class.
- Instance
- An instance of the class.
-
Errors
- No checks are done to ensure Instance or AClass are valid
pointers. Specifying an invalid property name in PropName will result
in an EPropertyError exception.
-
See also
- IsStoredProp, PropIsType
-
Example
program example10;
{ This program demonstrates the IsPublishedProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
begin
O:=TMyTestObject.Create;
Writeln('Property tests : ');
Write('IsPublishedProp(O,BooleanField) : ');
Writeln(IsPublishedProp(O,'BooleanField'));
Write('IsPublishedProp(Class,BooleanField) : ');
Writeln(IsPublishedProp(O.ClassType,'BooleanField'));
Write('IsPublishedProp(O,SomeField) : ');
Writeln(IsPublishedProp(O,'SomeField'));
Write('IsPublishedProp(Class,SomeField) : ');
Writeln(IsPublishedProp(O.ClassType,'SomeField'));
O.Free;
end.
20 IsStoredProp
-
Declaration
- Function IsStoredProp(Instance : TObject;PropInfo : PPropInfo) : Boolean;
Function IsStoredProp(Instance: TObject; const PropName: string): Boolean;
-
Description
- IsStoredProp
returns True if the Stored modifier evaluates
to True for the property described by PropInfo or with name
PropName for object Instance.
It returns False otherwise. If the function returns
True, this indicates that the property should be written when
streaming the object Instance.
If there was no stored modifier in the declaration of the property,
True will be returned.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- IsPublishedProp, PropIsType
-
Example
program example11;
{ This program demonstrates the IsStoredProp function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
begin
O:=TMyTestObject.Create;
Writeln('Stored tests : ');
Write('IsStoredProp(O,StoredIntegerConstFalse) : ');
Writeln(IsStoredProp(O,'StoredIntegerConstFalse'));
Write('IsStoredProp(O,StoredIntegerConstTrue) : ');
Writeln(IsStoredProp(O,'StoredIntegerConstTrue'));
Write('IsStoredProp(O,StoredIntegerMethod) : ');
Writeln(IsStoredProp(O,'StoredIntegerMethod'));
Write('IsStoredProp(O,StoredIntegerVirtualMethod) : ');
Writeln(IsStoredProp(O,'StoredIntegerVirtualMethod'));
O.Free;
end.
21 PropIsType
-
Declaration
- Function PropIsType(AClass: TClass;
const PropName: string; TypeKind: TTypeKind): Boolean;
Function PropIsType(Instance: TObject;
const PropName: string; TypeKind: TTypeKind): Boolean;
-
Description
- PropIsType
returns True if the property with name PropName
has type TypeKind. It returns False otherwise. The class to be
examined can be specified in one of two ways:
- AClass
- A class pointer.
- Instance
- An instance of the class.
-
Errors
- No checks are done to ensure Instance or AClass are valid
pointers.Specifying an invalid property name in PropName will result
in an EPropertyError exception.
-
See also
- IsPublishedProp, IsStoredProp, PropType
-
Example
program example16;
{ This program demonstrates the PropIsType function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
begin
O:=TMyTestObject.Create;
Writeln('Property tests : ');
Write('PropIsType(O,BooleanField,tkBool) : ');
Writeln(PropIsType(O,'BooleanField',tkBool));
Write('PropIsType(Class,BooleanField,tkBool) : ');
Writeln(PropIsType(O.ClassType,'BooleanField',tkBool));
Write('PropIsType(O,ByteField,tkString) : ');
Writeln(PropisType(O,'ByteField',tkString));
Write('PropIsType(Class,ByteField,tkString) : ');
Writeln(PropIsType(O.ClassType,'ByteField',tkString));
O.Free;
end.
22 PropType
-
Declaration
- Function PropType(AClass: TClass; const PropName: string): TTypeKind;
Function PropType(Instance: TObject; const PropName: string): TTypeKind;
-
Description
- Proptype
returns the type of the property PropName for a class.
The class to be examined can be specified in one of 2 ways:
- AClass
- A class pointer.
- Instance
- An instance of the class.
-
Errors
- No checks are done to ensure Instance or AClass are valid
pointers. Specifying an invalid property name in PropName will result
in an EPropertyError exception.
-
See also
- IsPublishedProp, IsStoredProp, PropIsType
-
Example
program example17;
{ This program demonstrates the PropType function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
begin
O:=TMyTestObject.Create;
Writeln('Property tests : ');
Write('PropType(O,BooleanField) : ');
Writeln(TypeNames[PropType(O,'BooleanField')]);
Write('PropType(Class,BooleanField) : ');
Writeln(TypeNames[PropType(O.ClassType,'BooleanField')]);
Write('PropType(O,ByteField) : ');
Writeln(TypeNames[PropType(O,'ByteField')]);
Write('PropType(Class,ByteField) : ');
Writeln(TypeNames[PropType(O.ClassType,'ByteField')]);
O.Free;
end.
23 SetEnumProp
-
Declaration
- Procedure SetEnumProp(Instance: TObject; const PropInfo: PPropInfo;
const Value: string);
Procedure SetEnumProp(Instance: TObject; const PropName: string;
const Value: string);
-
Description
- SetEnumProp
sets the property described by PropInfo or with name
PropName to Value. Value must be a string with the name
of the enumerate value, i.e. it can be used as an argument to
GetEnumValue.
-
Errors
- No checks are done to ensure Instance or PropInfo are valid
pointers. Specifying an invalid property name in PropName will result
in an EPropertyError exception.
-
See also
- GetEnumProp, SetStrProp, SetFloatProp,
SetInt64Prop,SetMethodProp.
For an example, see GetEnumProp.
24 SetFloatProp
-
Declaration
- Procedure SetFloatProp(Instance : TObject;
PropInfo : PPropInfo;
Value : Extended);
Procedure SetFloatProp(Instance: TObject;
const PropName: string;
Value: Extended);
-
Description
- SetFloatProp
assigns Value to the property described by
PropInfo or with name Propname for the object Instance.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid float property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetFloatProp, SetOrdProp, SetStrProp,
SetInt64Prop,SetMethodProp
For an example, see GetFloatProp.
25 SetInt64Prop
-
Declaration
- Procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo; const Value: Int64);
Procedure SetInt64Prop(Instance: TObject; const PropName: string; const Value: Int64);
-
Description
- SetInt64Prop
assigns Value to the property of type
Int64 that is described by PropInfo or with name Propname
for the object Instance.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid Int64 property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetInt64Prop, GetMethodProp, SetOrdProp, SetStrProp,
SetFloatProp
For an example, see GetInt64Prop.
26 SetMethodProp
-
Declaration
- Procedure SetMethodProp(Instance : TObject;PropInfo : PPropInfo; const Value :
TMethod);
Procedure SetMethodProp(Instance: TObject; const PropName: string; const Value: TMethod);
-
Description
- SetMethodProp
assigns Value to the method the property described
by PropInfo or with name Propname for object Instance.
The type TMethod of the Value parameter is defined in the
SysUtils unit as:
TMethod = packed record
Code, Data: Pointer;
end;
Data should point to the instance of the class with the method Code.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid method property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetMethodProp, SetOrdProp, SetStrProp,
SetFloatProp, SetInt64Prop
For an example, see GetMethodProp.
27 SetObjectProp
-
Declaration
- Procedure SetObjectProp(Instance: TObject;
PropInfo: PPropInfo; Value: TObject);
Procedure SetObjectProp(Instance: TObject;
const PropName: string; Value: TObject);
-
Description
- SetObjectProp
assigns Value to the the object property described by
PropInfo or with name Propname for the object Instance.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid method property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetObjectProp, SetOrdProp, SetStrProp,
SetFloatProp, SetInt64Prop, SetMethodProp
For an example, see GetObjectProp.
28 SetOrdProp
-
Declaration
- Procedure SetOrdProp(Instance : TObject; PropInfo : PPropInfo;
Value : Longint);
Procedure SetOrdProp(Instance: TObject; const PropName: string;
Value: Longint);
-
Description
- SetOrdProp
assigns Value to the the ordinal property described by
PropInfo or with name Propname for the object Instance.
Ordinal properties that can be set include:
- Integers and subranges of integers
- The actual value of the integer must be
passed.
- Enumerated types and subranges of enumerated types
- The ordinal value
of the enumerated type must be passed.
- Subrange types
- of integers or enumerated types. Here the ordinal
value must be passed.
- Sets
- If the base type of the set has less than 31 possible values.
For each possible value; the corresponding bit of Value must be set.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid ordinal property of Instance.
No range checking is performed.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetOrdProp, SetStrProp, SetFloatProp,
SetInt64Prop,SetMethodProp
For an example, see GetOrdProp.
29 SetPropValue
-
Declaration
- Procedure SetPropValue(Instance: TObject;
const PropName: string; const Value: Variant);
-
Description
Due to missing Variant support, this function is not yet implemented;
it is provided for Delphi compatibility only.
-
Errors
-
See also
30 SetSetProp
-
Declaration
- Procedure SetSetProp(Instance: TObject;
const PropInfo: PPropInfo; const Value: string);
Procedure SetSetProp(Instance: TObject;
const PropName: string; const Value: string);
-
Description
- SetSetProp
sets the property specified by PropInfo or
PropName for object Instance to Value. Value is a
string which contains a comma-separated list of values, each value being a
string-representation of the enumerated value that should be included in
the set. The value should be accepted by the StringToSet function.
The value can be formed using the SetToString function.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid ordinal property of Instance.
No range checking is performed.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetSetProp, SetOrdProp, SetStrProp, SetFloatProp,
SetInt64Prop,SetMethodProp, SetToString,
StringToSet
For an example, see GetSetProp.
31 SetStrProp
-
Declaration
- procedure SetStrProp(Instance : TObject; PropInfo : PPropInfo;
const Value : Ansistring);
Procedure SetStrProp(Instance: TObject; const PropName: string;
const Value: AnsiString);
-
Description
- SetStrProp
assigns Value to the string property described by
PropInfo or with name Propname for object Instance.
-
Errors
- No checking is done whether Instance is non-nil, or whether
PropInfo describes a valid string property of Instance.
Specifying an invalid property name in PropName will result in an
EPropertyError exception.
-
See also
- GetStrProp, SetOrdProp, SetFloatProp,
SetInt64Prop,SetMethodProp
For an example, see GetStrProp
32 SetToString
-
Declaration
- function SetToString(PropInfo: PPropInfo;
Value: Integer) : String;
function SetToString(PropInfo: PPropInfo;
Value: Integer; Brackets: Boolean) : String;
-
Description
- SetToString
takes an integer representation of a set (as received e.g.
by GetOrdProp) and turns it into a string representing the elements in
the set, based on the type information found in the PropInfo property
information. By default, the string representation is not surrounded by
square brackets. Setting the Brackets parameter to True will
surround the string representation with brackets.
The function returns the string representation of the set.
-
Errors
- No checking is done to see whether PropInfo points to valid property
information.
-
See also
- GetEnumName, GetEnumValue, StringToSet
-
Example
program example18;
{ This program demonstrates the SetToString function }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
PI : PPropInfo;
I : longint;
begin
O:=TMyTestObject.Create;
PI:=GetPropInfo(O,'SetField');
O.SetField:=[mefirst,meSecond,meThird];
I:=GetOrdProp(O,PI);
Writeln('Set property to string : ');
Writeln('Value : ',SetToString(PI,I,False));
O.SetField:=[mefirst,meSecond];
I:=GetOrdProp(O,PI);
Writeln('Value : ',SetToString(PI,I,True));
I:=StringToSet(PI,'mefirst');
SetOrdProp(O,PI,I);
I:=GetOrdProp(O,PI);
Writeln('Value : ',SetToString(PI,I,False));
I:=StringToSet(PI,'[mesecond,methird]');
SetOrdProp(O,PI,I);
I:=GetOrdProp(O,PI);
Writeln('Value : ',SetToString(PI,I,True));
O.Free;
end.
33 SetVariantProp
-
Declaration
- Procedure SetVariantProp(Instance : TObject;
PropInfo : PPropInfo;
Const Value: Variant);
Procedure SetVariantProp(Instance: TObject;
const PropName: string;
const Value: Variant);
-
Description
Due to missing Variant support, this function is not yet implemented.
Provided for Delphi compatibility only.
-
Errors
-
See also
34 StringToSet
-
Declaration
- function StringToSet(PropInfo: PPropInfo; const Value: string): Integer;
-
Description
- StringToSet
converts the string representation of a set in Value
to a integer representation of the set, using the property information found
in PropInfo. This property information should point to the property
information of a set property. The function returns the integer
representation of the set. (i.e, the set value, typecast to an integer)
The string representation can be surrounded with square brackets, and must
consist of the names of the elements of the base type of the set. The base
type of the set should be an enumerated type. The elements should be
separated by commas, and may be surrounded by spaces.
each of the names will be fed to the GetEnumValue function.
-
Errors
- No checking is done to see whether PropInfo points to valid property
information. If a wrong name is given for an enumerated value, then an
EPropertyError will be raised.
-
See also
- GetEnumName, GetEnumValue, SetToString
For an example, see SetToString.
2002-04-25