[fpc-pascal] Streaming of Generics
kyan
alfasud.ti at gmail.com
Tue May 29 10:40:17 CEST 2012
> 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.
For the lack of generic procedures, you can substitute a generic
procedure with a generic class method:
type
generic TStreamer<T> = class
public
procedure StreamType( Stream : TStream; Data : T );
end;
procedure TStreamer.StreamType( Stream : TStream; Data : T );
begin
case PTypeInfo(TypeInfo(Data))^.Kind of
tkString:
...
tkInteger:
..
end;
end;
HTH
Constantine.
More information about the fpc-pascal
mailing list