[fpc-pascal] Streaming of Generics

Jorge Aldo G. de F. Junior jagfj80 at gmail.com
Tue May 29 03:04:16 CEST 2012

Can you create a generic that is based on a class that can stream
itself using rtti regardles of its structure ?

Something like

TFundamentalClass = Class(TObject)
Function GetProperty(Propertyname : String): Variant;
Procedure SetProperty(PropertyName : String; Value : Variant);
Procedure WritePropertiesToStream(Const aStream : TStream); bla bla

Generic TMyGeneric = Class(TFundamentalClass)

TMySpecialization = Specialize TMyGeneric<String>;

Did you tried that ?

2012/5/28 Kornel Kisielewicz <kornel.kisielewicz at gmail.com>:
> Hello all,
> I want to find a way to write a streaming mechanic for Generic data
> structures. For simplicity let's assume that I want to write a class
> or function to stream TFPGList (I do not use them, but my set of
> generic classes is written in a similar manner). However, due to FPC's
> restricted generics support I cannot find a way to reasonably do that.
> 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
> I'd be able to write a generic procedure :
> procedure StreamType( Stream : TStream; Data : T );
> ... but we don't have generic procedures yet. Still, even if using
> classes, the inability to do specializations makes it impossible to
> write a generic streaming system. Is there a way to detect a type
> easily of "any passed value"? It would be enough for me to have a
> function
> procedure StreamType( Stream : TStream;  var Data );
> ... that could detect the type of data, as long as it would accept
> simple types, ansistrings, classes and dynamic arrays.
> --
> regards,
> Kornel Kisielewicz
> http://chaosforge.org/
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

More information about the fpc-pascal mailing list