The IStreamable interface defines an abstract interface for an object that saved to a stream and retrieved back from a stream. The object may be saved to any TStream descendant. If the object also needs to be streamable by the Delphi Component (design-time) streaming system (TReader and TWriter objects instead of TStream), it should implement the CommonInterfaces.IDTStreamable 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.IStreamable
|
Delphi
|
type
IStreamable = interface
['{B139B46D-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.
|
|
|
LoadFromStream(TStream)
|
The LoadFromStream method loads the data for the interface and underlying object from the Stream parameter. The underlying object should have already been created by the stream user.
|
|
SaveToStream(TStream)
|
The SaveToStream method should save to the Stream any data needed by the object to completely rebuild itself from the stream later.
|
Top
|
To use IStreamable objects, write out the object:
Delphi
|
var
Streamable: IStreamable;
begin
if MyObject.GetInterface( IStreamable, Streamable ) then
begin
// write the class name
Stream.Write(Length(Streamable.GetClassName), SizeOf(Integer));
Stream.Write(Pointer(Streamable.GetClassName)^, Length(Streamable.GetClassName));
Streamable.WriteData( Stream );
end;
end;
|
and read in the object:
Delphi
var
L: Integer;
Obj: TPersistent;
TheClass: String;
Streamable: IStreamable;
begin
L := 0;
Stream.Read(L, SizeOf(Integer));
SetString(TheClass, PChar(nil), L);
Read(Pointer(TheClass)^, L);
Obj := FindClass( TheClass ).Create;
if Obj.GetInterface( IStreamable, Streamable ) then
Streamable.ReadData( Stream );
end;