Class TgoProtocolBuffer

DescriptionHierarchyInternal TypesFieldsMethodsProperties

Unit

Declaration

type TgoProtocolBuffer = class(TObject)

Description

Static class used for (de)serializing records in Protocol Buffer format

Hierarchy

  • TObject
  • TgoProtocolBuffer

Overview

Internal Types

Published TDeserializeProc = procedure(const AReader: TReader; const ARecord: PByte; const ATag, AOffset: Integer; const AParam: TObject);
Published TInitializeProc = procedure(Self: Pointer);
Published TSerializeProc = procedure(const AWriter: TWriter; const ARecord: PByte; const ATag, AOffset: Integer; const AParam: TObject);

Methods

Public class function Serialize<T: record>(const ARecord: T): TBytes; overload; static; inline;
Public class function Serialize(const ARecordType: Pointer; const ARecord): TBytes; overload; static;
Public class procedure Serialize<T: record>(const ARecord: T; const AFilename: String); overload; static; inline;
Public class procedure Serialize(const ARecordType: Pointer; const ARecord; const AFilename: String); overload; static;
Public class procedure Serialize<T: record>(const ARecord: T; const AStream: TStream); overload; static; inline;
Public class procedure Serialize(const ARecordType: Pointer; const ARecord; const AStream: TStream); overload; static;
Public class procedure Deserialize<T: record>(out ARecord: T; const AData: TBytes); overload; static; inline;
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const AData: TBytes); overload; static;
Public class procedure Deserialize<T: record>(out ARecord: T; const ABuffer: Pointer; const ABufferSize: Integer); overload; static; inline;
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const ABuffer: Pointer; const ABufferSize: Integer); overload; static;
Public class procedure Deserialize<T: record>(out ARecord: T; const AFilename: String); overload; static; inline;
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const AFilename: String); overload; static;
Public class procedure Deserialize<T: record>(out ARecord: T; const AStream: TStream); overload; static; inline;
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const AStream: TStream); overload; static;
Public class procedure Register(const ARecordType: Pointer; const ARecord; const AInitializeProc: TInitializeProc; const AFields: array of const); static;

Description

Internal Types

Published TDeserializeProc = procedure(const AReader: TReader; const ARecord: PByte; const ATag, AOffset: Integer; const AParam: TObject);
 
Published TInitializeProc = procedure(Self: Pointer);
 
Published TSerializeProc = procedure(const AWriter: TWriter; const ARecord: PByte; const ATag, AOffset: Integer; const AParam: TObject);
 

Methods

Public class function Serialize<T: record>(const ARecord: T): TBytes; overload; static; inline;

Serializes a record to an array of bytes.

Parameters
T
the type of the record to serialize.
ARecord
the record (of type T) to serialize.
Returns

A byte array containing the serialized record.

Public class function Serialize(const ARecordType: Pointer; const ARecord): TBytes; overload; static;

Serializes a record to an array of bytes.

Parameters
ARecordType
the type of the record to serialize. This is the result from a TypeInfo(TMyRecord) call.
ARecord
the record (of ARecordType) to serialize.
Returns

A byte array containing the serialized record.

Public class procedure Serialize<T: record>(const ARecord: T; const AFilename: String); overload; static; inline;

Serializes a record to a file.

Parameters
T
the type of the record to serialize.
ARecord
the record (of type T) to serialize.
AFilename
the name of the file to serialize the record to.
Public class procedure Serialize(const ARecordType: Pointer; const ARecord; const AFilename: String); overload; static;

Serializes a record to a file.

Parameters
ARecordType
the type of the record to serialize. This is the result from a TypeInfo(TMyRecord) call.
ARecord
the record (of ARecordType) to serialize.
AFilename
the name of the file to serialize the record to.
Public class procedure Serialize<T: record>(const ARecord: T; const AStream: TStream); overload; static; inline;

Serializes a record to a stream.

Parameters
T
the type of the record to serialize.
ARecord
the record (of type T) to serialize.
AStream
the stream to serialize the record to.
Public class procedure Serialize(const ARecordType: Pointer; const ARecord; const AStream: TStream); overload; static;

Serializes a record to a stream.

Parameters
ARecordType
the type of the record to serialize. This is the result from a TypeInfo(TMyRecord) call.
ARecord
the record (of ARecordType) to serialize.
AStream
the stream to serialize the record to.
Public class procedure Deserialize<T: record>(out ARecord: T; const AData: TBytes); overload; static; inline;

Deserializes a record from an array of bytes. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
T
the type of the record to deserialize.
ARecord
the record (of type T) to deserialize.
AData
the byte array containing the serialized record.
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const AData: TBytes); overload; static;

Deserializes a record from an array of bytes. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
ARecordType
the type of the record to deserialize. This is the result from a TypeInfo(TMyRecord) call.
ARecord
the record (of ARecordType) to deserialize.
AData
the byte array containing the serialized record.
Public class procedure Deserialize<T: record>(out ARecord: T; const ABuffer: Pointer; const ABufferSize: Integer); overload; static; inline;

Deserializes a record from a memory buffer. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
T
the type of the record to deserialize.
ARecord
the record (of type T) to deserialize.
ABuffer
the memory buffer containing the serialized record.
ABufferSize
the size of the buffer
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const ABuffer: Pointer; const ABufferSize: Integer); overload; static;

Deserializes a record from a stream. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
T
the type of the record to deserialize.
ARecordType
the type of the record to deserialize. This is the result from a TypeInfo(TMyRecord) call.
ARecord
the record (of ARecordType) to deserialize.
ABuffer
the memory buffer containing the serialized record.
ABufferSize
the size of the buffer
Public class procedure Deserialize<T: record>(out ARecord: T; const AFilename: String); overload; static; inline;

Deserializes a record from a file. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
T
the type of the record to deserialize.
ARecord
the record (of type T) to deserialize.
AFilename
the name of the file containing the serialized record.
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const AFilename: String); overload; static;

Deserializes a record from a file. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
ARecordType
the type of the record to deserialize. This is the result from a TypeInfo(TMyRecord) call.
ARecord
the record (of ARecordType) to deserialize.
AFilename
the name of the file containing the serialized record.
Public class procedure Deserialize<T: record>(out ARecord: T; const AStream: TStream); overload; static; inline;

Deserializes a record from a stream. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
T
the type of the record to deserialize.
ARecord
the record (of type T) to deserialize.
AStream
the stream containing the serialized record.
Public class procedure Deserialize(const ARecordType: Pointer; out ARecord; const AStream: TStream); overload; static;

Deserializes a record from a stream. When the record contains a parameterless Initialize procedure, then that procedure will be called before the record is deserialized.

Parameters
T
the type of the record to deserialize.
ARecordType
the type of the record to deserialize. This is the result from a TypeInfo(TMyRecord) call.
ARecord
the record (of ARecordType) to deserialize.
AStream
the stream containing the serialized record.
Public class procedure Register(const ARecordType: Pointer; const ARecord; const AInitializeProc: TInitializeProc; const AFields: array of const); static;

Registers a record for serialization in Protocol Buffer format. This is only needed for use with Free Pascal. When using Delphi, the record type is automatically registered when it is first (de)serialized.

You can also use this method to register 3rd party or RTL record types.

Every record type you want to use for serialization must be registered once and only once. You usually do this at application startup. If a record has fields of other record types, then those other record types must be registered as well.

To register a record, you must declare a dummy variable of the record type and then call this method as in the following example:

      var
        P: TPerson; // Dummy variable
      begin
        TgoProtocolBuffer.Register(TypeInfo(TPerson), P, @TPerson.Initialize,
          [@P.Name, 1, @P.Id, 2, @P.Email, 3, @P.Phone, 4]);
      end;

The parameters are the TypeInfo(..) of the record type, the dummy variable, the optional address of an Initialize method and an array of tags and field addresses.

The dummy variable is just used as a helper to pass the field addresses. You don't need to initialize this variable with any data.

Before a record of this type is deserialized, its fields are always cleared (set to 0 or nil). You can perform additional initialization after this by specifying an Initialize method. If set, that method will be called after clearing all fields, but before deserializing. Pass nil to clear the record only.

The final parameter is an array of tags and field addresses. Each tag uniquely identifies the corresponding field. Tags must be in the range 1-536870911. Tags must be unique within the record and each tag must have a corresponding field address. If the number of tags doesn't match the number of addresses, or if there are any other invalid parameters, then an exception is raised.

You can pass the tags and field addresses in any way you want, as long as you pass the tags in the same order as the corresponding field addresses. You can use the (Field, Tag)* order as in the example above, or the opposite (Tag, Field)* order as in this example:

[1, @P.Name, 2, @P.Id, 3, @P.Email, 4, @P.Phone]

You can also specify all fields first, followed by all tags, or the other way around:

[@P.Name, @P.Id, @P.Email, @P.Phone, 1, 2, 3, 4]

Or any other combination that makes sense in the declaration:

      [@P.Name,  @P.Id,
       1      ,  2,
       @P.Email, @P.Phone,
       3       , 4]

Just remember that the order of the tags must match the order of the fields.

Parameters
ARecordType
the type of the record to register. This is the result from a TypeInfo(TMyRecord) call.
ARecord
a dummy instance of the record type to register (of type ARecordType).
AInitializeProc
the record method to call to initialize the record with its default values.
AFields
an array of tags and record field addresses. See the documentation above for more information.

Generated by P2PasDoc 0.13.0 on 2017-04-25 12:54:26