-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuGarbageCollector.pas
More file actions
123 lines (97 loc) · 2.46 KB
/
uGarbageCollector.pas
File metadata and controls
123 lines (97 loc) · 2.46 KB
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
unit uGarbageCollector;
interface
uses
System.Generics.Collections, Rtti, System.Classes,
System.SysUtils;
type
IAutoCollect<T> = interface
function GetInstance:T;
end;
TAutoCollect<T: class> = class(TInterfacedObject, IAutoCollect<T>)
private
FInstance: T;
constructor Create(AInstance: T);
destructor Destroy; override;
public
property Instance: T read FInstance;
class function New(AInstance: T):IAutoCollect<T>;
function GetInstance:T;
end;
TGarbageCollector = class(TComponent)
private
FClassDictionary: TObjectDictionary<TObject, TClass>;
function Add<T: class>(AClass: T; AClassType: TClass): T; overload;
public
destructor Destroy; override;
constructor Create(AOwner: TComponent); override;
function Add<T: class>(AClass: T): T; overload;
procedure Collect(AObject: TObject);
procedure Clear;
end;
var
Garbage: TGarbageCollector;
implementation
uses
System.Types;
constructor TGarbageCollector.Create(AOwner: TComponent);
begin
inherited;
FClassDictionary := TObjectDictionary<TObject, TClass>.Create([doOwnsKeys]);
end;
destructor TGarbageCollector.Destroy;
begin
FClassDictionary.Free;
inherited Destroy;
end;
function TGarbageCollector.Add<T>(AClass: T): T;
begin
Result := Add<T>(AClass, T);
end;
function TGarbageCollector.Add<T>(AClass: T; AClassType: TClass): T;
var
obj: TObject;
begin
if not(AClass is AClassType) then
raise Exception.Create('The added object is not an instance of ' + AClassType.ClassName);
obj := AClass as TObject;
if FClassDictionary.ContainsKey(obj) then
raise Exception.Create('The object has already been added to the garbage collector');
FClassDictionary.Add(obj, AClassType);
Result := AClass;
end;
procedure TGarbageCollector.Clear;
var
LObj: TObject;
begin
for LObj in FClassDictionary.Keys do
FClassDictionary.Remove(LObj);
end;
procedure TGarbageCollector.Collect(AObject: TObject);
var
obj: TObject;
begin
if FClassDictionary.ContainsKey(AObject) then
begin
FClassDictionary.Remove(AObject);
end;
end;
{ TAutoFree<T> }
constructor TAutoCollect<T>.Create(AInstance: T);
begin
inherited Create;
FInstance := AInstance;
end;
destructor TAutoCollect<T>.Destroy;
begin
FInstance.Free;
inherited;
end;
function TAutoCollect<T>.GetInstance: T;
begin
Result:= FInstance;
end;
class function TAutoCollect<T>.New(AInstance: T): IAutoCollect<T>;
begin
Result:= TAutoCollect<T>.Create(AInstance);
end;
end.