Recent

Author Topic: How to Iterate class fields, methods and properties in FPC-wheres the RTTI unit?  (Read 6236 times)

MSGEndoDoc

  • New Member
  • *
  • Posts: 11
Hi Everyone. 

My first post on the Lazarus forum.

I am coming from Delphi which has become so ridiculously expensive, so I'm porting to Lazarus.  Thanks to the Lazarus community for making such a great open source project. 

I want to iterate through a class's fields, methods and properties to get and set various information.   This has to be generic for any class, so it needs to discover what fields, methods and properties are present.   In Delphi I can use the code below (see https://stackoverflow.com/questions/4466629/iterate-member-variables)

Excuse my ignorance. In Lazarus/FPC I can not find any rtti unit.   How to do this in Lazarus/FPC?

Many thanks

Martin

{******************************************}

uses rtti; 

///

procedure GetInfo(obj: TObject);
var
  context: TRttiContext;
  rType: TRttiType;
  field: TRttiField;
  method: TRttiMethod;
  prop: TRttiProperty;
  SL: TStringlist;

begin
  context := TRttiContext.Create;
  rType := context.GetType(obj.ClassType);
  SL := TStringlist.create;

  for field in rType.GetFields do
   If field <> nil then
  SL.Add('Field name = ' + Field.Name);

    ;//do something here
  for method in rType.GetMethods do
     If method <> nil then
     SL.Add('Method name = ' + method.Name);
    ;//do something here

  for prop in rType.GetProperties do
    ;//do something here
       If prop <> nil then
       SL.Add('Property name = ' + prop.Name);

  SL.SaveToFile('C:...[whateverpath]...\PTester.txt');
  SL.Free;
end;
Martin

PatBayford

  • Full Member
  • ***
  • Posts: 125
While there is no RTTI unit per se, you will find a TypInfo unit which does most of what the old Delphi code did. You will also find, in a standard install, a whole page of RTTI related components.
I suggest you start off with looking up TypInfo in the Help files.
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
While there is no RTTI unit per se, you will find a TypInfo unit which does most of what the old Delphi code did. You will also find, in a standard install, a whole page of RTTI related components.
I suggest you start off with looking up TypInfo in the Help files.

In FPC trunk, you will find a rtti unit, although I have not used it, I think it should be similar to that of Delphi.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Extended RTTI (a compiler feature) and the RTTI unit are a feature of later Delphi's (D2010+?).

Work is being done in trunk, but afaik still ongoing.

FPC/Lazarus and delphi versions before D2010 did have a more limited RTTI for published properties and methods of classes (as used in the form designer). This is what the typinfo unit handles in 3.0.x versions.

So it is best if you can see work with that, or take your chances with the development version.

MSGEndoDoc

  • New Member
  • *
  • Posts: 11
Thanks everyone. Yes, I'm using Delphi 10.1 so it  has the up to date RTTI unit. 

Will see what I can do with TypInfo in Lazarus.

I think I found the FPC version of RTTI in development at

https://github.com/graemeg/freepascal/blob/master/packages/rtl-objpas/src/inc/rtti.pp

and might give that a run too.  Anyone know how close it is to being finished? 

Cheers,

Martin   
Martin

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
I don't know. It might also not be 100% compatible.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
The official location is under /packages/rtl-objpas/src/inc/
It is not (yet) 100% compaible. There are a lot of tests for it under /tests/test/trtti*.pp

examining the test will serve as documentation. And use trunk to reflect current status.
« Last Edit: January 24, 2018, 02:42:09 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
The official location is under /packages/rtl-objpas/src/inc/
It is not (yet) 100% compaible. There are a lot of tests for it under /tests/test/trtti*.pp

The tests for the RTTI unit are in /packages/rtl-objpas/tests/tests.rtti.pas (and tests.rtti.invoke.pas), the tests that you mentioned are for the RTTI in general.

I don't know. It might also not be 100% compatible.

It's geared to be compatible (the test works in Delphi as well with minor differences), but what it definitely is not is 100 % complete. Especially missing are the capabilities to iterate fields, properties and methods which MSGEndoDoc needs.

Anyone know how close it is to being finished? 
As said above the capabilities that you need are not yet supported. You'd need to use published fields, properties and methods together with the TypInfo unit for now.

MSGEndoDoc

  • New Member
  • *
  • Posts: 11
Thanks for the heads up PascalDragon and Thaddy.   Will code in Delphi 10.1 and its RTTI, and get it working, then wait for the Lazarus RTTI version to mature and / or implement the code with just TypInfo methods in Lazarus.   
Martin

 

TinyPortal © 2005-2018