Snippets
Created by
Stefan Glienke
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | program ClassExtension;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Generics.Collections,
SysUtils,
Spring.VirtualClass;
type
TFoo = class
end;
TFooBar = class(TFoo)
end;
TRemoveInstanceProc = procedure(const key: TObject) of object;
TObjectExtension = class helper for TObject
strict private
class var proxyClasses: TVirtualClasses;
class var removeProcs: TList<TRemoveInstanceProc>;
class var fields: TList<TObject>;
class procedure FreeInstanceHook(const Self: TObject); static;
class constructor Create;
class destructor Destroy;
class procedure HandleInstanceAdd(Sender: TObject; const Item: TObject;
Action: TCollectionNotification);
protected
class function AddExtension<T>: TDictionary<TObject,T>; static;
end;
TFooHelper = class helper(TObjectExtension) for TFoo
strict private
class constructor Create;
class var fBar: TDictionary<TObject,Integer>;
function GetBar: Integer;
procedure SetBar(const Value: Integer);
public
property Bar: Integer read GetBar write SetBar;
end;
{ TObjectExtension }
class constructor TObjectExtension.Create;
begin
proxyClasses := TVirtualClasses.Create;
removeProcs := TList<TRemoveInstanceProc>.Create;
fields := TObjectList<TObject>.Create;
end;
class destructor TObjectExtension.Destroy;
begin
fields.Free;
removeProcs.Free;
proxyClasses.Free;
end;
class function TObjectExtension.AddExtension<T>: TDictionary<TObject, T>;
begin
Result := TDictionary<TObject, T>.Create;
Result.OnKeyNotify := HandleInstanceAdd;
fields.Add(Result);
removeProcs.Add(Result.Remove);
end;
class procedure TObjectExtension.FreeInstanceHook(const Self: TObject);
var
freeInstance: TFreeInstance;
proc: TRemoveInstanceProc;
begin
freeInstance := GetClassData(Self.ClassParent).FreeInstance;
for proc in removeProcs do
proc(Self);
freeInstance(Self);
end;
class procedure TObjectExtension.HandleInstanceAdd(Sender: TObject;
const Item: TObject; Action: TCollectionNotification);
var
proxyClass: TClass;
begin
if Action = cnAdded then
begin
proxyClass := proxyClasses.GetVirtualClass(Item.ClassType);
GetClassData(proxyClass).FreeInstance := FreeInstanceHook;
proxyClasses.Proxify(Item);
end;
end;
{ TFooHelper }
class constructor TFooHelper.Create;
begin
fBar := AddExtension<Integer>;
end;
function TFooHelper.GetBar: Integer;
begin
fBar.TryGetValue(Self, Result);
end;
procedure TFooHelper.SetBar(const Value: Integer);
begin
fBar.AddOrSetValue(Self, Value);
end;
var
f: TFoo;
b: TFooBar;
begin
f := TFoo.Create;
b := TFooBar.Create;
Writeln(b.Bar);
b.Bar := 56;
Writeln(b.Bar);
Writeln(f.Bar);
f.Bar := 42;
f.Bar := 42;
Writeln(f.Bar);
f.Free;
b.Free;
Readln;
ReportMemoryLeaksOnShutdown := True;
end.
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.