unit CollectionExt;
interface
Uses SysUtils, Classes;
Type
TJsCollection =
class(TCollection)
private
FCollectionname :
String;
FIntValue : Integer;
function GetFormatSignature:
String;
public
procedure SaveToFile(
const Filename : TFilename);
procedure SaveToStream(Stream : TStream);
procedure LoadFromFile(
const Filename : TFilename);
procedure LoadFromStream(Stream : TStream);
published
property Collectionname :
String read FCollectionname
write FCollectionname;
property IntValue : Integer
read FIntValue
write FIntValue;
end;
TWriterExt =
class(TWriter)
public
procedure WriteCollection(Value: TCollection);
procedure WriteCollectionProperties(Value : TCollection);
end;
TReaderExt =
class(TReader)
public
procedure ReadCollection(Value: TCollection);
procedure ReadCollectionProperties(Value: TCollection);
end;
implementation
uses TypInfo;
const
iFilerBufferSize = 4096;
{ TJsCollection }
function TJsCollection.GetFormatSignature:
String;
begin
Result := ItemClass.ClassName;
end;
procedure TJsCollection.LoadFromFile(
const Filename: TFilename);
var
FileStream : TFileStream;
begin
Clear;
FileStream:=TFileStream.Create(Filename,fmOpenRead);
Try
LoadFromStream(FileStream);
Finally
FileStream.Free;
end;
end;
procedure TJsCollection.LoadFromStream(Stream: TStream);
var
Reader : TReaderExt;
begin
Reader:=TReaderExt.Create(Stream,iFilerBufferSize);
Try
Reader.ReadCollection(Self);
Finally
Reader.Free;
end;
end;
procedure TJsCollection.SaveToFile(
const Filename: TFilename);
var
FileStream : TFileStream;
begin
FileStream:=TFileStream.Create(Filename,fmCreate);
Try
SaveToStream(FileStream);
Finally
FileStream.Free;
end;
end;
procedure TJsCollection.SaveToStream(Stream: TStream);
var
Writer : TWriterExt;
begin
Writer:=TWriterExt.Create(Stream,iFilerBufferSize);
Try
Writer.WriteCollection(Self);
Finally
Writer.Free;
end;
end;
{ TWriterExt }
procedure TWriterExt.WriteCollection(Value: TCollection);
begin
WriteCollectionProperties(Value);
inherited WriteCollection(Value);
end;
procedure TWriterExt.WriteCollectionProperties(Value: TCollection);
begin
WriteProperties(Value);
end;
{ TReaderExt }
procedure TReaderExt.ReadCollection(Value: TCollection);
begin
ReadCollectionProperties(Value);
ReadValue;
inherited ReadCollection(Value);
end;
procedure TReaderExt.ReadCollectionProperties(Value: TCollection);
var
PropList : TPropList;
PropCount : Integer;
iCnt : Integer;
begin
PropCount:=GetPropList(Value.ClassInfo,tkProperties,@PropList);
For iCnt:=0
to PropCount-1
do
ReadProperty(Value);
end;
end.