is it possible to stream TObjectList? lazarus / 31-LCL ---------------------------------------------------------------------- Michael Collier · 2018-04-22 19:32 Is possible to stream the objects held within TObjectList? I have a class structure as follows.. A base class from which all other classes are derived 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   t_cmd_base = class ( TComponent )  end; A list class that is derived from the base class , but contains TObjectList 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   t_cmd_list_base = class (t_cmd_base)  private    f_object_list : TObjectList ;  protected    procedure DefineProperties(Filer: TFiler); override;    procedure do_read(Reader: TReader);    procedure do_write(Writer: TWriter);  public    // create & free object list ----    constructor Create(AOwner: TComponent); override;    destructor  Destroy; override;    // ------------------------------  end;  OK, so any class I define comes from t_cmd_base, and it can have as its property a t_cmd_list_base. The t_cmd_list_base always holds items that inherit from the t_cmd_base. The idea being that a "tree structure" can be populated from these classes. I'd like to stream this structure using WriteComponentAsTextToStream .. i.e. 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 my_cmd_base.WriteComponentAsTextToStream To ensure the list held within my_cmd_base is streamed I have included GetChildren in its definition 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 t_cmd_c.GetChildren(Proc: TGetChildProc; Root: TComponent);begin  inherited GetChildren(Proc, Root);  Proc(f_cmd_list);end;  To handle the list, I implemented DefineProperties method, and created 2 routines to read/write the individual items in the list.. Here is code to write the tobjectlist items to the stream, as you can see I have commented out the code that writes the actual component ( because it gives stream read error), and left the code that simply streams the classname.. 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 t_cmd_list_base.do_write(Writer: TWriter);var i : integer ;begin  with Writer do begin    WriteListBegin;    WriteInteger(f_object_list.Count);    for i := 0 to f_object_list.Count-1 do begin      WriteString( f_object_list[i].ClassName );      //WriteComponent( TComponent(f_object_list[i]) );    end;    WriteListEnd;  end;end; Here is a sample of the stream as text.. object t_unit   dbg = 'copy'   object t_cmd_c     dbg = 'dbg'     object t_cmd_list       t_cmd_list = (         2         't_cmd_a'         't_cmd_b'       )     end   end end Am I an the right road to being able to do this? Ideally I'd like to stream an object and for it to recursively stream itself and and list classes it holds... I tried a few other methods using TCollection, but found it didn't handle the range of classes that I need (it seemed to handle only 1 class of object). Thanks in advance.. ---------------------------------------------------------------------- fpcoder · 2018-04-23 02:27 You want to write a component to stream. Someone hopefully pops in and shows exactly how to do that. I have done something similar, saving object data to streams, field by field. This is more work of course. Anyway, lets say you want to save an instance of TMyObject this way. Add a SaveToStream procedure to TMyObject and iterate through the list of objects, calling SaveToStream for each object in the list. In the example below there is only one field in the class. If there are more fields, they have to be saved in the same manner. 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 type  TMyObject = class  public    Name: string;    procedure SaveToStream(AStream: TStream);    procedure LoadFromStream(AStream: TStream);  end; procedure TMyObject.SaveToStream(AStream: TStream);var  Len: Integer;begin  Len := Length(Name);  AStream.Write(Len, SizeOf(Len));  AStream.Write(PChar(Name)^, Len);end; procedure TMyObject.LoadFromStream(AStream: TStream);var   Len: Integer;begin  AStream.Read(Len, SizeOf(Len));  SetString(Name, PChar(nil), Len);  AStream.Read(PChar(Name)^, Len);end; ---------------------------------------------------------------------- Michael Collier · 2018-04-23 15:03 Thanks, I can write to streams if I'm forced to but I'd like to use the existing streaming mechanism, specifically the WriteComponent() procedure, if possible. As you can see in my example I have commented that line out because it gives stream read error when I use it, I can write any other stream data though ( string, integer etc). Maybe the WriteComponent should not be used, it doesn't say anything here https://www.freepascal.org/docs- html/rtl/classes/twriter.writecomponent.html ---------------------------------------------------------------------- Thaddy · 2018-04-23 15:49 Here's a nice article https://www.codeproject.com/articles/1060345/dfm-serialization-in- depth Also applies to Lazarus/FPC ---------------------------------------------------------------------- mse · 2018-04-23 16:59 Quote from: Michael Collier on April 22, 2018, 09:32:06 pmIs possible to stream the objects held within TObjectList? Normally a TCollection descendant or a specialized "parent" component must be used as container in order to use component array streaming. In MSEgui there is also TPersistentArrayProp. ---------------------------------------------------------------------- BeniBela · 2018-04-23 17:18 Quote from: Michael Collier on April 22, 2018, 09:32:06 pmIs possible to stream the objects held within TObjectList? Depends. Are those objects videos? ---------------------------------------------------------------------- Michael Collier · 2018-04-23 17:34 Quote from: BeniBela on April 23, 2018, 07:18:51 pmDepends. Are those objects videos? No - just ordinary TComponents, http://wiki.freepascal.org/Streaming_components maybe you are thinking of streaming in terms of sound/vision.. ---------------------------------------------------------------------- PatBayford · 2018-04-23 22:12 Two thoughts - 1) It is possible to write ANY object to a stream, but you have do do ALL the donkey work yourself - the Filer/Reader/Writer concept inherited from Delphi is so poorly documented as to be unusable for mere mortals. However, several Delphi add-ons have addressed some of these issues, and should be portable to FPC/Lazarus. 2) Your assumption about TCollection, and all it's descendants - they are designed to hold just one type of object per collection. However, if you can accept that limitation, it is possible to achieve your objective. ---------------------------------------------------------------------- taazz · 2018-04-23 22:47 Quote from: Michael Collier on April 22, 2018, 09:32:06 pmIs possible to stream the objects held within TObjectList? I have a class structure as follows.. It is possible you just need to take in to account 2 things, 1) there is no single line to save a object list you need to provide the loop to save them one by one. 2) keep track of how many objects you need to read. in short save the objectlist.count first then save one by one your components. there is a good example of this method in the simplegraph component. If you can't find it post a complete sample and I'll add it in for you. ---------------------------------------------------------------------- Michael Collier · 2018-04-24 14:54 Quote from: taazz on April 24, 2018, 12:47:05 amthere is a good example of this method in the simplegraph component. If you can't find it post a complete sample and I'll add it in for you. Thanks, I have attached an example project laz_example.zip I don't seem to have simplegraph on my hard drive, it would be good to see how you did it because I am using some workarounds that I'd like to fix.. They are commented in the example code. The example I have attached creates a hierarchy 2 levels deep, then uses built in streaming routines to make a copy of the hierarchy and dump its contents as text, see screenshot. I hope this can be made to work because my existing (non streaming) method of copy/paste requires a lot of code that this streaming method may not require.. :-) ---------------------------------------------------------------------- taazz · 2018-04-26 14:00 Quote from: Michael Collier on April 24, 2018, 04:54:04 pmQuote from: taazz on April 24, 2018, 12:47:05 amthere is a good example of this method in the simplegraph component. If you can't find it post a complete sample and I'll add it in for you. Thanks, I have attached an example project laz_example.zip I don't seem to have simplegraph on my hard drive, it would be good to see how you did it because I am using some workarounds that I'd like to fix.. They are commented in the example code. The example I have attached creates a hierarchy 2 levels deep, then uses built in streaming routines to make a copy of the hierarchy and dump its contents as text, see screenshot. I hope this can be made to work because my existing (non streaming) method of copy/paste requires a lot of code that this streaming method may not require.. :-) sorry mate I just show the sample I'll work on it later tonight and post something tomorrow or the the day after tomorrow. ---------------------------------------------------------------------- Michael Collier · 2018-04-26 16:20 Thanks taazz.. much appreciated..