Recent

Author Topic: Creating a linked list of unknown types  (Read 447 times)

seany

  • Full Member
  • ***
  • Posts: 120
Creating a linked list of unknown types
« on: January 24, 2023, 11:08:59 pm »
Hi

I want to build a markdown/groff/troff renderer with a little bit of custom functionality.

I do not like HTML/Javascript, So I am trying to use the FPC graphics stack.

I have this in a file called TRenderObjects.pas.

Code: Pascal  [Select][+][-]
  1. unit TRenderObjects;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.     Section         = class
  12.       text            : Array of String;
  13.       imgSrc          : Array of String;
  14.       oper            : Array of String;
  15.       textFont        : String;
  16.       textColor       : Array of Integer;
  17.       textFontVariant : (Bold, Italic, Underline, Strikethrough);
  18.       isRendered      : Boolean;
  19.  
  20.     end;
  21.     ImageBlob       = class
  22.  
  23.       // border
  24.  
  25.       borderWidth     : Integer;
  26.       boderColor      : Array of Integer;
  27.  
  28.  
  29.     end;
  30.  
  31.  
  32. implementation
  33.  
  34. end.
  35.                
  36.  


In a second file called TDocument.pas, i want to have

Code: Pascal  [Select][+][-]
  1. unit TDocument;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, TRenderObjects;
  9.  
  10. type
  11.   markedDocument = class
  12.     // Sections : Array of TRenderObjects.Section;
  13.     SectionNodePTR : ^NodeStruct;
  14.     NodeStruct  : packed record    // writing = instead of : in this line and the previous fails with message : expected = found
  15.       rendObj   : T;
  16.       ID        : Integer;
  17.       Parent    : SectionNodePTR;
  18.       Children  : Array of SectionNodePTR;
  19.       end;
  20.  
  21.     public :
  22.       var
  23.          DocumentTree :  NodeStruct;
  24.          procedure AppendChild (ChildObj : T);
  25.   end;
  26.  
  27.  
  28. implementation
  29.  
  30. end.      

I want to define a template of a tree in the class, and then use the append procedure to add either TRenderObjects.Section or TRenderObjects.ImageBlob. That is why I used T as a template Type. But this fails with:

Projekt kompilieren, Ziel: notebook: Exit code 1, Fehler: 7
tdocument.pas(15,19) Error: Identifier not found "T"
tdocument.pas(15,20) Error: Error in type definition
tdocument.pas(17,19) Error: Identifier not found "SectionNodePTR"
tdocument.pas(17,33) Error: Error in type definition
tdocument.pas(18,28) Error: Identifier not found "SectionNodePTR"
tdocument.pas(18,42) Error: Error in type definition
tdocument.pas(21,12) Fatal: Syntax error, "identifier" expected but ":" found


Please let me know how I can create a tree with unspecified types inside a class, initialize it, and then use an overloaded append or delete procedure to modify the tree. Thank you.
My projects, among others:

https://linktr.ee/siderealNight

VisualLab

  • Sr. Member
  • ****
  • Posts: 332
Re: Creating a linked list of unknown types
« Reply #1 on: January 25, 2023, 01:14:21 am »
Suppose this is a generic tree. So each tree node will contain a list of child nodes. The number of child nodes in this list can be any number. I would use the existing generic classes from the FCL library to create the tree class. The tree class schema could be:

Code: Pascal  [Select][+][-]
  1. {$mode Delphi}
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, Generics.Collections;
  7.  
  8. TTreeNode<T> = class;
  9.  
  10. TSubNodeList = class(TObjectList<TTreeNode>);
  11.  
  12. TTreeNode<T> = class(TObject)
  13. private
  14.   FData: T;
  15.   FSubNodes: TSubNodeList;
  16.   procedure SetData(Value: T);
  17. public
  18.   constructor Create;
  19.   destructor Destroy; override;
  20.   property Data: T read FData write SetData;
  21.   property SubNodes: TSubNodeList read FSubNodes;
  22. end;
  23.  
  24. TDocumentTree<T> = class(TObject)
  25. private
  26.   FRoot: TTreeNode<T>;
  27. public
  28.   constructor Create;
  29.   destructor Destroy; override;
  30.   property Root: TTreeNode<T> read FRoot;
  31. end;
  32.  
  33. implementation
  34.  
  35. constructor TTreeNode<T>Create;
  36. begin
  37.   FSubNodes := TSubNodeList.Create(True);
  38. end;
  39.  
  40. destructor TTreeNode<T>Destroy;
  41. begin
  42.   FSubNodes.Free;
  43. end;
  44.  
  45. constructor TDocumentTree<T>Create;
  46. begin
  47.   FRoot := TTreeNode.Create;
  48. end;
  49.  
  50. destructor TDocumentTree<T>Destroy;
  51. begin
  52.   FRoot.Free;
  53. end;

This is a very general outline.

seany

  • Full Member
  • ***
  • Posts: 120
Re: Creating a linked list of unknown types
« Reply #2 on: January 25, 2023, 04:47:50 am »
Thank you. I will use this and bump up this thread in future if i need some help.
My projects, among others:

https://linktr.ee/siderealNight

 

TinyPortal © 2005-2018