-
Notifications
You must be signed in to change notification settings - Fork 66
/
TwoLevelTypeIndex.dpr
79 lines (62 loc) · 1.49 KB
/
TwoLevelTypeIndex.dpr
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
program TwoLevelTypeIndex;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
TTypeIndex<TCategory> = class // static
private type
TIndex<T> = class // static
private
class var FValue: Integer;
public
class constructor Create;
end;
private
class var FNextIndex: Integer;
public
class constructor Create;
public
class function Get<T>: Integer; inline; static;
end;
{ TTypeIndex<TCategory> }
class constructor TTypeIndex<TCategory>.Create;
begin
FNextIndex := 0;
end;
class function TTypeIndex<TCategory>.Get<T>: Integer;
begin
Result := TIndex<T>.FValue;
end;
{ TTypeIndex<TCategory>.TIndex<T> }
class constructor TTypeIndex<TCategory>.TIndex<T>.Create;
begin
FValue := FNextIndex;
Inc(FNextIndex);
end;
{ TypeIndexExample }
type
TCategory1 = type Integer;
TCategory2 = type Integer;
procedure TypeIndexExample;
begin
WriteLn('Type index for category 1, type Integer: ',
TTypeIndex<TCategory1>.Get<Integer>);
WriteLn('Type index for category 1, type Single: ',
TTypeIndex<TCategory1>.Get<Single>);
WriteLn('Type index for category 1, type String: ',
TTypeIndex<TCategory1>.Get<String>);
WriteLn('Type index for category 1, type Single: ',
TTypeIndex<TCategory1>.Get<Single>);
WriteLn('Type index for category 2, type Single: ',
TTypeIndex<TCategory2>.Get<Single>);
end;
begin
try
TypeIndexExample;
ReadLn;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.