[fpc-pascal] Streaming of Generics

Kornel Kisielewicz kornel.kisielewicz at gmail.com
Tue May 29 12:24:41 CEST 2012

On Tue, May 29, 2012 at 10:40 AM, kyan <alfasud.ti at gmail.com> wrote:
>> Ideally, I'd only like to write the streaming mechanism for each set
>> of types (normal, anisstring, dynamic array, objects, interfaced
>> objects) once, and use it for every set of items. However, there's no
>> reasonable way to detect the type and do an execution for it. Ideally
> You can use the "magic" function TypeInfo() to detect the type of a
> generic parameter inside a method of a generic class. It returns a
> PTypeInfo pointer so you can use PTypeInfo(TypeInfo(T))^ record to
> determine the data type (Kind) -and other attributes- of the generic
> type.

Seems TypeInfo is the only reasonable way. Somehow function
overloading is resolved before Generic type substitution so that won't
work. The set of generic classes is very basic, so speed is up the
essence (no variants).

I did manage to create a TStream helper, with the following prototypes

procedure ReadTyped( Ptr : Pointer; Size : Integer; Typ : PTypeInfo );
procedure WriteTyped( Ptr : Pointer; Size : Integer; Typ : PTypeInfo );

Surprisingly, trying to wrap it up nicely into

procedure ReadTyped( var Data );

... doesn't work because TypeInfo is lost (tkUnknown).
Kornel Kisielewicz

More information about the fpc-pascal mailing list