[fpc-pascal] Streaming of Generics

Sven Barth pascaldragon at googlemail.com
Tue May 29 14:30:59 CEST 2012


Am 29.05.2012 12:24, schrieb Kornel Kisielewicz:
> 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).

This isn't surprising, as TypeInfo resolves the type at compile time 
based on the expression you give it, but "var Data" has no type, thus 
tkUnknown.

Regards,
Sven



More information about the fpc-pascal mailing list