IROCustomStreamableType

Overview

The IROCustomStreamableType interface is used to introduce a serialization ability which is provided by the TROSerializer class.

Use case

Here is a simple demo showing the usage of the IROCustomStreamableType interface:

program IROCustomStreamableType_Demo;

{$APPTYPE CONSOLE}

uses
  Classes, Math, SysUtils, uROSerializer, uROStreamSerializer;

type
  ProprietaryRecord = packed record
    ByteChunk : array [0..4] of byte;
    Id        : cardinal;
  end;

  TCustomValue = class (TInterfacedObject, IROCustomStreamableType)
  private
    fSomeData : ProprietaryRecord;
  protected
    //IROCustomStreamableType
     procedure Write(aSerializer: TROSerializer; const aName: string);
     procedure Read(aSerializer: TROSerializer; const aName: string);
  public
    constructor Create (const s: string; id : cardinal);
    function    AsString  : string;
  end;

function TCustomValue.AsString: string;
begin
  Result := IntToStr (fSomeData.Id) + ' "' + PChar (@fSomeData.ByteChunk) + '"';
end;

constructor TCustomValue.Create(const s: string; id : cardinal);
begin
  inherited Create;
  Move (PChar (s)^, fSomeData.ByteChunk, Min (sizeof (fSomeData.ByteChunk) - 1, Length (s)));
  fSomeData.Id := id;
end;

//implementation of IROCustomStreamableType.Read
procedure TCustomValue.Read(aSerializer: TROSerializer; const aName: string);
var
  buf   : TStream;
begin
  aSerializer.ReadBinary (aName, buf);
  buf.Read (fSomeData, sizeof (fSomeData));
  buf.Free;
end;

type
  //to avoid unnecessary memory moves
  TManagedMemoryStream = class (TCustomMemoryStream)
  public
    procedure SetPointer(Ptr: Pointer; Size: Longint);
  end;

procedure TManagedMemoryStream.SetPointer(Ptr: Pointer; Size: Integer);
begin
  inherited;
end;

//implementation of IROCustomStreamableType.Write
procedure TCustomValue.Write(aSerializer: TROSerializer; const aName: string);
var
  buf   : TManagedMemoryStream;
begin
(*$WARNINGS OFF*)
  buf := TManagedMemoryStream.Create;
(*$WARNINGS ON*)
  buf.SetPointer (@fSomeData, sizeof (fSomeData));
  aSerializer.WriteBinary (aName, buf);
  buf.Free;
end;

procedure doTest;
var
  v1        : TCustomValue;
  v2        : TCustomValue;
  serializer: TROStreamSerializer;
  media     : TMemoryStream;
begin
  media := TMemoryStream.Create;
  serializer := TROStreamSerializer.Create (media);

  v1 := TCustomValue.Create ('a', 1);
  v2 := TCustomValue.Create ('b', 2);
  try
    WriteLn ('v1: ', v1.AsString, ', v2: ', v2.AsString);
    v1.Write (serializer, 'Tag1');
    v2.Write (serializer, 'Tag2');

    media.Position := 0;

    //restore in an inverse order
    v2.Read (serializer, 'Tag1');
    v1.Read (serializer, 'Tag2');
    WriteLn ('v1: ', v1.AsString, ', v2: ', v2.AsString);
  finally
    v2.Free;
    v1.Free;
    serializer.Free;
    media.Free;
  end;
end;

begin
  doTest;
end.

Location


Properties


TypeName

property TypeName: string read write

Required Methods


CanImplementType

function CanImplementType(const aName: string): Boolean

Parameters:

  • aName:

Read

Retrieves an object state named aName from aSerializer into this instance.

procedure Read(aSerializer: TROSerializer; const aName: string; ArrayElementId: Integer)

Parameters:

  • aSerializer: Serializer used to restore an object state.
  • aName: Object state identifier.
  • ArrayElementId:

Write

Stores a state of this instance designated as aName into aSerializer.

procedure Write(aSerializer: TROSerializer; const aName: string; ArrayElementId: Integer)

Parameters:

  • aSerializer: Serializer used to store an object state.
  • aName: Object state identifier.
  • ArrayElementId: