[fpc-pascal]RTTI
Michael Van Canneyt
michael.vancanneyt at wisa.be
Mon Jan 21 11:09:45 CET 2002
On Mon, 21 Jan 2002, Jean-Pierre PLANAS wrote:
> Hello
>
> Here a piece of delphi code i want to port to fpc.
>
> uses TypInfo;
>
> type
> TJour=(jrLundi,jrMardi,jrMercredi,jrJeudi,jrVendredi,jrSamedi,jrDimanche);
>
> procedure TForm1.btnClick(Sender: TObject);
> begin
> ShowMessage(IntToStr(GetEnumValue(TypeInfo(TJour),'jrMardi')));
> ShowMessage(GetEnumName(TypeInfo(TJour),1));
> end;
>
> I saw that fpc have a typeInfo.pp unit with GetEnumValue and GetEnumName
> functions.
> But i can use them like with delphi.
>
> Writeln(GetEnumValue(???(TJour),'jrMardi'));
> Writeln(GetEnumName(???(TJour),1));
>
> What can replace ??? ?
Version 1.1 contains the TypInfo call.
For version 1.0.x the following workaround can be done;
Type
{$M+} // VERY Important !!
TMyEnumClass = Class
FMyEnym : TJour;
Published
property MyEnum : TJour Read GMyEnum Write FMyEnum;
end;
Now you can do:
Var
PI : PPropInfo;
TI : PtypeInfo;
// Store TI in global variable.
begin
PI:=GetPropInfo(TMyEnuMClass,'MyEnum');
If PI<>Nil then
begin
TI:=PI^.PropType;
If TI=Nil then
begin
writeln(stderr,'Could not find type information for Myenum');
Halt(1)
end;
end;
The TI can now be used in calls to GetEnumValue:
IntToStr(GetEnumValue(TI,'jrMardi'))
GetEnumName(TI,1);
If you have multiple enumeration types, store them in 1 class, and
repeat the above for each type, storing the PTypeInfo pointers in
variables.
Michael.
More information about the fpc-pascal
mailing list