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
t_cmd_base = class ( TComponent )
end;
A
list class that is derived from the base class , but contains TObjectList
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.
my_cmd_base.WriteComponentAsTextToStream
To ensure the list held within my_cmd_base is streamed I have included
GetChildren in its definition
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..
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..