[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] | Reference for unit 'Objects' (#rtl)

# RegisterType

Register new object for streaming.

## Declaration

Source position: objects.pp line 667

`procedure RegisterType(`
`var S: TStreamRec`
`);`

## Description

RegisterType registers a new type for streaming. An object cannot be streamed unless it has been registered first. The stream record S needs to have the following fields set:

ObjType: Sw_Word
  This should be a unique identifier. Each possible type should have it's own identifier.
VmtLink: pointer
  This should contain a pointer to the VMT (Virtual Method Table) of the object you try to register.
Load : Pointer
  is a pointer to a method that initializes an instance of that object, and reads the initial values from a stream. This method should accept as it's sole argument a PStream type variable.
Store: Pointer
  is a pointer to a method that stores an instance of the object to a stream. This method should accept as it's sole argument a PStream type variable.

The VMT of the object can be retrieved with the following expression:

    VmtLink: Ofs(TypeOf(MyType)^);

## Errors

In case of error (if a object with the same ObjType) is already registered), run-time error 212 occurs.

## Example

    Unit MyObject;

    Interface

    Uses Objects;

    Type
         PMyObject = ^TMyObject;
         TMyObject = Object(TObject)
           Field : Longint;
           Constructor Init;
           Constructor Load (Var Stream : TStream);
           Destructor Done;
           Procedure Store (Var Stream : TStream);
           Function  GetField : Longint;
           Procedure SetField (Value : Longint);
           end;

    Implementation

    Constructor TMyobject.Init;

    begin
      Inherited Init;
      Field:=-1;
    end;

    Constructor TMyobject.Load (Var Stream : TStream);

    begin
      Stream.Read(Field,Sizeof(Field));
    end;

    Destructor TMyObject.Done;

    begin
    end;

    Function TMyObject.GetField : Longint;

    begin
      GetField:=Field;
    end;

    Procedure TMyObject.SetField (Value : Longint);

    begin
      Field:=Value;
    end;

    Procedure TMyObject.Store (Var Stream : TStream);

    begin
      Stream.Write(Field,SizeOf(Field));
    end;

    Const MyObjectRec : TStreamRec = (
            Objtype : 666;
            vmtlink : Ofs(TypeOf(TMyObject)^);
            Load : @TMyObject.Load;
            Store : @TMyObject.Store;
            );

    begin
      RegisterObjects;
      RegisterType (MyObjectRec);
    end.