1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
{ Tests of generics, both FPC and Delphi style. }
unit ok_generic;
interface
{ Units to include for Generics }
uses Generics.Collections, FGL;
type
{ FPC generics tests ------------------------------------------------------- }
generic TMyList<T> = class(TFPSList)
private
type PT = ^T;
public
function Blah: PT;
end;
{ }
TGeometryAttrib = class
Name: string;
AType: TGeometryAttribType;
Offset: Integer;
end;
TGeometryAttribsList2 = specialize TMyList<TALBuffersCache>;
{ You can also specialize and make a descendant at the same time. }
TGeometryAttribsList = class(specialize TMyList<TGeometryAttrib>)
public
function Find(const Name: string): TGeometryAttrib;
end;
{ Delphi generics tests ---------------------------------------------------- }
{ A simple Test-Object }
TMyObject = class(TObject);
TMyGenericList = class(TObjectList<TMyObject>)
public
// To Something here
end;
TMyNewGeneric<T1,T2> = class
private
type PT = ^T1;
public
function Blah: PT;
end;
{ Sample for a generic with more than one type. TPair is a Key-Value-Relation }
TAnotherGenericType = class(TDictionary<TObject,TObject>);
{ All standard Generics: }
TArray = class end;
TEnumerator<T> = class abstract end;
TEnumerable<T> = class abstract end;
TList<T> = class(TEnumerable<T>) end;
TQueue<T> = class(TEnumerable<T>) end;
TStack<T> = class(TEnumerable<T>) end;
TPair<TKey,TValue> = record end;
TDictionary<TKey,TValue> = class(TEnumerable<TPair<TKey,TValue>>) end;
TObjectList<T: class> = class(TList<T>) end;
TObjectQueue<T: class> = class(TQueue<T>) end;
TObjectStack<T: class> = class(TStack<T>) end;
TObjectDictionary<TKey,TValue> = class(TDictionary<TKey,TValue>) end;
implementation
uses Classes;
procedure SampleProc();
var
TestList: TMyGenericList;
TestList2: TObjectList<TStringList>; // also allowed for vars
begin
TestList:=TMyGenericList.Create();
TestList2:=TObjectList<TStringList>.Create();
TestList.Free;
TestList2.Free;
end;
end.
|