|
| 1 | +program SimpleTypeIndex; |
| 2 | + |
| 3 | +{$APPTYPE CONSOLE} |
| 4 | + |
| 5 | +{$R *.res} |
| 6 | + |
| 7 | +uses |
| 8 | + System.SysUtils, |
| 9 | + System.Classes; |
| 10 | + |
| 11 | +type |
| 12 | + { Utility to generate type indices (partiallly) at compile time } |
| 13 | + TTypeIndex<T> = class // static |
| 14 | + private |
| 15 | + class var FValue: Integer; |
| 16 | + public |
| 17 | + class constructor Create; |
| 18 | + |
| 19 | + { Returns the type index for the type parameter T } |
| 20 | + class property Value: Integer read FValue; |
| 21 | + end; |
| 22 | + |
| 23 | +var |
| 24 | + GNextTypeIndex: Integer = 0; |
| 25 | + |
| 26 | +{ TTypeIndex<T> } |
| 27 | + |
| 28 | +class constructor TTypeIndex<T>.Create; |
| 29 | +begin |
| 30 | + FValue := GNextTypeIndex; |
| 31 | + Inc(GNextTypeIndex); |
| 32 | +end; |
| 33 | + |
| 34 | +type |
| 35 | + TIntegerAlias = Integer; |
| 36 | + TDistinctInteger = type Integer; |
| 37 | + |
| 38 | +{ TypeIndexExample } |
| 39 | + |
| 40 | +procedure TypeIndexExample; |
| 41 | +begin |
| 42 | + WriteLn('Index for type Integer: ', TTypeIndex<Integer>.Value); |
| 43 | + WriteLn('Index for type String: ', TTypeIndex<String>.Value); |
| 44 | + WriteLn('Index for type TStream: ', TTypeIndex<TStream>.Value); |
| 45 | + |
| 46 | + { To check if the type index for String is still the same as before: } |
| 47 | + WriteLn('Index for type String: ', TTypeIndex<String>.Value); |
| 48 | + |
| 49 | + { Type aliases have the same index as their aliased type: } |
| 50 | + WriteLn('Index for type TIntegerAlias: ', TTypeIndex<TIntegerAlias>.Value); |
| 51 | + |
| 52 | + { Discinct types will have their own unique index though: } |
| 53 | + WriteLn('Index for type TDistinctInteger: ', TTypeIndex<TDistinctInteger>.Value); |
| 54 | +end; |
| 55 | + |
| 56 | +type |
| 57 | + TTypeMap = record |
| 58 | + private |
| 59 | + FBits: UInt32; |
| 60 | + public |
| 61 | + procedure Init; inline; |
| 62 | + procedure Include<T>; inline; |
| 63 | + procedure Exclude<T>; inline; |
| 64 | + function Has<T>: Boolean; inline; |
| 65 | + end; |
| 66 | + |
| 67 | +{ TTypeMap } |
| 68 | + |
| 69 | +procedure TTypeMap.Init; |
| 70 | +begin |
| 71 | + FBits := 0; |
| 72 | +end; |
| 73 | + |
| 74 | +procedure TTypeMap.Include<T>; |
| 75 | +var |
| 76 | + Index: Integer; |
| 77 | +begin |
| 78 | + Index := TTypeIndex<T>.Value; |
| 79 | + Assert(Index < 32); |
| 80 | + FBits := FBits or (1 shl Index); |
| 81 | +end; |
| 82 | + |
| 83 | +procedure TTypeMap.Exclude<T>; |
| 84 | +var |
| 85 | + Index: Integer; |
| 86 | +begin |
| 87 | + Index := TTypeIndex<T>.Value; |
| 88 | + Assert(Index < 32); |
| 89 | + FBits := FBits and not (1 shl Index); |
| 90 | +end; |
| 91 | + |
| 92 | +function TTypeMap.Has<T>: Boolean; |
| 93 | +var |
| 94 | + Index: Integer; |
| 95 | +begin |
| 96 | + Index := TTypeIndex<T>.Value; |
| 97 | + Assert(Index < 32); |
| 98 | + Result := ((FBits and (1 shl Index)) <> 0); |
| 99 | +end; |
| 100 | + |
| 101 | +{ TypeMapExample } |
| 102 | + |
| 103 | +procedure TypeMapExample; |
| 104 | +var |
| 105 | + TypeMap: TTypeMap; |
| 106 | +begin |
| 107 | + TypeMap.Init; |
| 108 | + |
| 109 | + { "Register" Integer and TStream types } |
| 110 | + TypeMap.Include<Integer>; |
| 111 | + TypeMap.Include<TStream>; |
| 112 | + |
| 113 | + Assert(TypeMap.Has<Integer>); |
| 114 | + Assert(TypeMap.Has<TStream>); |
| 115 | + Assert(not TypeMap.Has<String>); |
| 116 | + |
| 117 | + { "Unregister" Integer type } |
| 118 | + TypeMap.Exclude<Integer>; |
| 119 | + |
| 120 | + Assert(not TypeMap.Has<Integer>); |
| 121 | + Assert(TypeMap.Has<TStream>); |
| 122 | +end; |
| 123 | + |
| 124 | +{ Entry Point } |
| 125 | + |
| 126 | +begin |
| 127 | + try |
| 128 | + TypeIndexExample; |
| 129 | + TypeMapExample; |
| 130 | + ReadLn; |
| 131 | + except |
| 132 | + on E: Exception do |
| 133 | + Writeln(E.ClassName, ': ', E.Message); |
| 134 | + end; |
| 135 | +end. |
0 commit comments