unit dnamedserializer;

//
//  Deprecated.
//

{$MODE FPC}
{$MODESWITCH AUTODEREF}
{$MODESWITCH RESULT}

interface

uses
  dtypes,
  dstream;

const
  SERIALIZE_SAVE  = 0;
  SERIALIZE_LOAD  = 1;

type
PSerializer = ^TSerializer;
TSerializer = object
private
  FData: TData;
public
  constructor Init(const Data: TData);
  destructor Done; virtual;
  procedure StartArr(const Name: AnsiString; var Len: LongInt; Mode: LongInt); virtual;
  procedure FinishArr(const Name: AnsiString; Mode: LongInt); virtual;
  procedure StartStruct(const Name: AnsiString; Mode: LongInt); virtual;
  procedure FinishStruct(const Name: AnsiString; Mode: LongInt); virtual;
  procedure SerializeLongInt(const Name: TString; var Value: LongInt; Mode: LongInt); virtual;
  function Stream: PStream;
end;

TSerializerBinary = object(TSerializer)
public
  procedure StartArr(const Name: AnsiString; var Len: LongInt; Mode: LongInt); virtual;
  procedure FinishArr(const Name: AnsiString; Mode: LongInt); virtual;
  procedure StartStruct(const Name: AnsiString; Mode: LongInt); virtual;
  procedure FinishStruct(const Name: AnsiString; Mode: LongInt); virtual;
  procedure SerializeLongInt(const Name: TString; var Value: LongInt; Mode: LongInt); virtual;
end;

procedure Serialize(Serializer: PSerializer; const Name: TString; var Value: LongInt; Mode: LongInt); inline; overload;

implementation

constructor TSerializer.Init(const Data: TData);
begin
  FData := Data;
end;

destructor TSerializer.Done;
begin
  FData.Done;
end;

procedure TSerializer.StartArr(const Name: AnsiString; var Len: LongInt; Mode: LongInt);
begin
end;

procedure TSerializer.FinishArr(const Name: AnsiString; Mode: LongInt);
begin
end;

procedure TSerializer.StartStruct(const Name: AnsiString; Mode: LongInt);
begin
end;

procedure TSerializer.FinishStruct(const Name: AnsiString; Mode: LongInt);
begin
end;

procedure TSerializer.SerializeLongInt(const Name: TString; var Value: LongInt; Mode: LongInt);
begin
end;

function TSerializer.Stream: PStream;
begin
  Result := FData.Stream;
end;

procedure TSerializerBinary.StartArr(const Name: AnsiString; var Len: LongInt; Mode: LongInt);
begin
  if Mode = SERIALIZE_SAVE then begin
    Stream.WriteInt32(Len);
  end else begin
    Len := Stream.ReadInt;
  end;
end;

procedure TSerializerBinary.FinishArr(const Name: AnsiString; Mode: LongInt);
begin
end;

procedure TSerializerBinary.StartStruct(const Name: AnsiString; Mode: LongInt);
begin
end;

procedure TSerializerBinary.FinishStruct(const Name: AnsiString; Mode: LongInt);
begin
end;

procedure TSerializerBinary.SerializeLongInt(const Name: TString; var Value: LongInt; Mode: LongInt);
begin
  if Mode = SERIALIZE_SAVE then begin
    Stream.WriteInt32(Value);
  end else begin
    Value := Stream.ReadInt;
  end;
end;

procedure Serialize(Serializer: PSerializer; const Name: TString; var Value: LongInt; Mode: LongInt);
begin
  Serializer.SerializeLongInt(Name, Value, Mode);
end;

end.
