The IDTStreamable interface defines an abstract interface for an object that can be saved and loaded from the Delphi Component streaming system (TReader and TWriter), which are called by the WriteComponent-type methods). If the object also needs to be usable by any stream, it should implement the CommonInterfaces.IStreamable interface instead.
Note
|
The objects that implement this interface should also call the RegisterClass method to register the object so that the Delphi FindClass method can find the class from its class name.
|
Namespace: CommonInterfaces
IInterface
CommonInterfaces.IDTStreamable
|
Delphi
|
type
IDTStreamable = interface
['{B139B46E-3C87-11D4-BA0E-00C04F8EDA5F}']
end;
|
|
Name
|
Description
|
|
GetClassName
|
The GetClassName function should return the ClassName of the underlying object. This method is necessary so that the stream user can write the object class name that implements the interface. The class name is then used on reading to create the underlying object that implements the IStreamable interface.
Note
|
The objects that implement this interface should also call the RegisterClass method to register the object so that the Delphi FindClass method can find the class from its class name.
|
|
|
ReadData(TReader)
|
The ReadData method loads the data for the interface and underlying object from the TReader parameter. The underlying object should have already been created by the stream user.
|
|
WriteData(TWriter)
|
The WriteData method should save to the TWriter object any data needed by the object to completely rebuild itself from the stream later.
|
Top
|
The following code shows how to use the IDTStreamable interface to write and read a list of IStreamable implementing objects as part of the design-time interface of a component. The component is assumed to have a Functions property of TIList that is settable at design-time:
First, the TFiler object needs to be told about the "hidden" property by overriding DefineProperties:
Delphi
|
...
protected
procedure DefineProperties(Filer: TFiler); override;
procedure LoadFunctionsProperty(Reader: TReader);
procedure StoreFunctionsProperty(Writer: TWriter);
...
procedure TUserPackage.DefineProperties(Filer: TFiler);
begin
inherited; { allow base classes to define properties }
Filer.DefineProperty('PackageFunctions', LoadFunctionsProperty, StoreFunctionsProperty, True);
end;
procedure TUserPackage.LoadFunctionsProperty(Reader: TReader);
var
UF: TPersistent;
Streamable: IDTStreamable;
begin
Functions.Clear;
Reader.ReadListBegin;
while not Reader.EndOfList do
begin
// Read class name and create
UF := FindClass(Reader.ReadString).Create;
if UF.GetInterface( IDTStreamable, Streamable ) then
begin
Streamable.ReadData( Reader );
Functions.Add( Streamable as IUserFunction );
end;
end;
Reader.ReadListEnd;
end;
procedure TUserPackage.StoreFunctionsProperty(Writer: TWriter);
var
i: Integer;
Streamable: IDTStreamable;
begin
Writer.WriteListBegin;
try
// Stream out any functions that implement the IDTStreamable interface
for i := 0 to Functions.Count - 1 do
if Functions[i].QueryInterface( IDTStreamable, Streamable ) <> E_NoInterface then
begin
Writer.WriteString( Streamable.GetClassName );
Streamable.WriteData( Writer );
end;
finally
Writer.WriteListEnd;
end;
end;
|