Enum nested classes in a class lazarus / 62-General ---------------------------------------------------------------------- Zaher · 2018-06-27 00:37 I want to enumerate/get list of classes that nested inside a parent class, is there any way using RTTI or something else? Without manually adding to list. Code: [Select]  TParentClass = class(TObject)   protected     type       TNestedClass = class(TObject)       public       end;       TAnotherNestedClass = class(TObject)       public       end;   end; ---------------------------------------------------------------------- taazz · 2018-06-27 00:57 hopefully not, two main reasons 1) tobject does not support RTTI you need to inherit from TPersistent or above to allow that and 2) if its nested inside an other class then no outside code should be able to know or find out any information about the type including RTTI, unless the types are declared as public in which case don't make them nested. ---------------------------------------------------------------------- PascalDragon · 2018-06-27 13:44 Quote from: taazz on June 27, 2018, 02:57:20 amhopefully not, two main reasons 1) tobject does not support RTTI you need to inherit from TPersistent or above to allow that and 2) if its nested inside an other class then no outside code should be able to know or find out any information about the type including RTTI, unless the types are declared as public in which case don't make them nested. Of course TObject supports RTTI. The only difference of TPersistent is that it enables published properties using {$M+}. However even then enumerating classes inside another class is not possible. In the future the type list of unit Rtti can be used, though one manually needs to check for a "parent" class. ---------------------------------------------------------------------- Zaher · 2018-06-27 17:18 Quote from: PascalDragon on June 27, 2018, 03:44:31 pmIn the future the type list of unit Rtti can be used So it still not exists yet? ---------------------------------------------------------------------- Thaddy · 2018-06-27 17:39 If you are "persistent" enough and use your imagination or knowledge of some internals of the language it is already possible.... Just requires an explicit interface, but some people are irrationally allergic to such code. Sven is going to hide that from us... ---------------------------------------------------------------------- Zaher · 2018-06-27 18:00 I can change to use persistent, it is easy, but i need a tip to start searching for it. Also i want it compatible with Delphi. ---------------------------------------------------------------------- Thaddy · 2018-06-27 18:48 Quote from: Zaher on June 27, 2018, 08:00:59 pmI can change to use persistent, it is easy, but i need a tip to start searching for it. Also i want it compatible with Delphi. well, I can provide you with an example, but as far as Delphi compatibility goes: FPC is actively developed. Delphi is not! (the fpc core team in now about 800% larger than the delphi core "team" and they do it in their spare time.). ---------------------------------------------------------------------- Zaher · 2018-06-28 18:58 Ok let us drop "Also i want it compatible with Delphi." ---------------------------------------------------------------------- PascalDragon · 2018-06-28 20:20 Quote from: Zaher on June 27, 2018, 07:18:13 pmQuote from: PascalDragon on June 27, 2018, 03:44:31 pmIn the future the type list of unit Rtti can be used So it still not exists yet? It exists in trunk, but it's still incomplete (including some missing binary data) and currently considered experimental. ---------------------------------------------------------------------- engkin · 2018-06-30 19:26 Quote from: Zaher on June 28, 2018, 08:58:26 pmOk let us drop "Also i want it compatible with Delphi." While waiting for Thaddy, here is my crime: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$mode objfpc}{$H+} uses  typinfo; type  TParentClass = class(TObject)  protected    type       TNestedClass = class(TObject)      public      end;       TAnotherNestedClass = class(TObject)      public      end;  end;   {$PACKRECORDS 1}  { *** Memory Layout ***  //Header TTypeInfo then: }   TClassInfo = record // from typinfo.TTypeData for classes    ClassType: TClass;    ParentInfo: PTypeInfo;    PropCount: SmallInt; { including parents }  end;   { then:    UnitName: ShortString;  { variable length }     { TPropInfo }    thisClassCount: word; { excluding parents }    here the properties follow as array[0..thisClassCount-1] of TPropInfo. Check typInfo.GetPropInfos     *** End of Memory Layout ***    }   PClassInfo = ^TClassInfo; type  TClasses=array of TClass; function EnumNestedClasses(AMainClass: TClass; out AClasses: TClasses): integer;var  p: pByte;  PossibleInfo: PTypeInfo;  ClassInfo: PClassInfo;  i: Integer;  found: Boolean;begin  Result := 0;  PossibleInfo := PTypeInfo(AMainClass.ClassInfo);  if PossibleInfo^.Kind=tkClass then  begin    p := Pointer(PossibleInfo);    p := p - SizeOf(TClassInfo) - Length(AMainClass.UnitName);    repeat      found := False;      for i := 1 to 255 do      begin      dec(p);        if (PByte(p-1)^=Byte(tkClass)) then        begin        PossibleInfo := PTypeInfo(p-1);          ClassInfo := PClassInfo(pByte(@PossibleInfo^.name)+Length(PossibleInfo^.name)+1);           try            if (ClassInfo^.ClassType.UnitName<>AMainClass.UnitName) { same unit }             or (Length(AMainClass.ClassName) >= Length(ClassInfo^.ClassType.ClassName)) { Parent name is included }             or (ClassInfo^.ClassType.ClassName[Length(AMainClass.ClassName)+1]<>'.') { Of form: ParentClsName.ChildClsName }               or (Pos(AMainClass.ClassName, ClassInfo^.ClassType.ClassName)<>1)               then continue;          except            continue; { access violation }          end;           found := True;           inc(Result);          SetLength(AClasses, Result);          AClasses[Result-1] := ClassInfo^.ClassType;           Break;        end;      end;      p := pointer(PossibleInfo)-1;    until not found;  end;end; var  NestedClasses: TClasses;  NestedClass: TClass; begin  if Length(TParentClass.TAnotherNestedClass.ClassName)>Length(TParentClass .TNestedClass.ClassName) then ;//to force the compiler to include the classes.   EnumNestedClasses(TParentClass, NestedClasses);  try    for NestedClass in NestedClasses do      WriteLn(NestedClass.ClassName);  finally    SetLength(NestedClasses, 0);  end;  ReadLn;end. Nested classes must be used for the compiler to include their data. Does not matter what class they descent from. Caller responsible to free the array of classes. Works with FPC 3.0.4. Tested on Win32. ---------------------------------------------------------------------- PascalDragon · 2018-07-02 18:40 Please note that this will likely break if a class contains properties, especially once Extended RTTI is integrated. Not to mention that it is in no way guaranteed that classes are written in that order. ---------------------------------------------------------------------- engkin · 2018-07-04 12:50 Quote from: PascalDragon on July 02, 2018, 08:40:42 pmPlease note that this will likely break if a class contains properties, especially once Extended RTTI is integrated. Not to mention that it is in no way guaranteed that classes are written in that order. I called it "crime" as no one should need to use something like that. I assume the compiler will provide a better mechanism in the near future ;-) I did mention the class properties in the memory layout, but yes, did not account for them. Adding class properties should be easy considering that Name is the only variable part of the record and located at the end, and knowing that all Pascal identifiers are limited to ['A'..'Z','a'..'z','0'..'9','_'] and does not start with a number. NameIndex could help as well. So finding identifier names could use something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure RSearchIdentifierName(var p: pByte);const  AllowedChars: set of char = ['A'..'Z','a'..'z','0'..'9','_'];var  lastChar: pByte;begin{Identifiers consist of between 1 and 127 significant characters(letters, digits and the underscore character),of which the first must be a letter (a-z or A-Z), or an underscore (_).}  repeat  { Skip non-allowed chars }    while not (char(p^) in AllowedChars) do      dec(p);     lastChar := p;     { Find possible beginning}    while true do    begin      if (p+p^<=lastChar) then exit;      if not (char(p^) in AllowedChars) then        break;      dec(p);    end;  until false;end; to find classes, then we need to check the byte preceding the possible identifier name for tkClass, then we have more means to make sure we landed on a class. Repeating the process forward is easier than looking in reverse direction. OTOH, scanning the whole .data section, where our classes are kept, is also possible. As for breakage, you guys can, rightfully, cause this code to break. And we will have to fix it. Luckily, that does not happen frequently. Honestly, I posted that code to get Thaddy to show us his example. Alas, I failed. :-| ---------------------------------------------------------------------- PascalDragon · 2018-07-06 13:35 Quote from: engkin on July 04, 2018, 02:50:05 pmI did mention the class properties in the memory layout, but yes, did not account for them. Adding class properties should be easy considering that Name is the only variable part of the record and located at the end, and knowing that all Pascal identifiers are limited to ['A'..'Z','a'..'z','0'..'9','_'] and does not start with a number. NameIndex could help as well. Please note that the statement that Pascal identifiers are limited to that set of characters is wrong as soon as Unicode identifiers are supported which will result in UTF8 identifier names. (Note: it's currently not planned, so just as a heads up) ---------------------------------------------------------------------- engkin · 2018-07-06 19:30 Quote from: PascalDragon on July 06, 2018, 03:35:20 pmPlease note that the statement that Pascal identifiers are limited to that set of characters is wrong as soon as Unicode identifiers are supported which will result in UTF8 identifier names. (Note: it's currently not planned, so just as a heads up) That would be fantastic news:   😁 := 😊 + 1; UTF8 identifier names are easy to find because of UTF8 itself. Meanwhile according to the current docs: QuoteIdentifiers consist of between 1 and 127 significant characters (letters, digits and the underscore character), of which the first must be a letter (a-z or A-Z), or an underscore (_).