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;
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.
|