-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathpFIBEventLists.pas
213 lines (175 loc) · 5.13 KB
/
pFIBEventLists.pas
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit pFIBEventLists;
interface
{$I FIBPlus.inc}
uses
SyncObjs, SysUtils,Classes;
type
PMethod=^TMethod;
TCallBackList = class(TComponent)
private
FCallBackList:TList;
FCS:TCriticalSection;
function GetCallBackAddr(Index:integer):PMethod;
function GetCount: Integer;
protected
procedure Notification(AComponent: TComponent;Operation: TOperation); override;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy;override;
function RegisterCallBack (const Proc:TMethod ):integer;
procedure UnRegisterCallBack(const Proc:TMethod );
procedure UnRegisterCallBacks(ProcOwner:TObject);
procedure Delete(Index:Integer);
function IndexOfMethod(const Proc:TMethod):integer;
property CallBackAddr[Index:integer] :PMethod read GetCallBackAddr;
property Count:Integer read GetCount;
end;
TNotifyEventList= class(TCallBackList)
private
function Get(Index:integer):TNotifyEvent;
public
function Add(Event:TNotifyEvent):Integer;
procedure Remove(Event:TNotifyEvent);
property Event[Index:integer]:TNotifyEvent read Get;default;
end;
implementation
uses FIBConsts;
{ TCallBackList }
constructor TCallBackList.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FCallBackList:=TList.Create;
FCS:=TCriticalSection.Create;
end;
destructor TCallBackList.Destroy;
var i: integer;
begin
for i:=0 to Pred(FCallBackList.Count) do Dispose(FCallBackList[i]);
FCallBackList.Free;
FCS.Free;
inherited Destroy;
end;
function TCallBackList.GetCallBackAddr(Index: integer): PMethod;
begin
if (Index<0) or (Index>=FCallBackList.Count) then
Result:=nil
else
Result:=FCallBackList[Index]
end;
function TCallBackList.RegisterCallBack(const Proc:TMethod ):integer;
var
p:PMethod;
begin
FCS.Acquire;
try
Result:= IndexOfMethod(Proc);
if Result<0 then
begin
New(p);
p^:=Proc;
Result:= FCallBackList.Add(p);
if TObject(Proc.Data) is TComponent then
TComponent(Proc.Data).FreeNotification(Self);
end;
finally
FCS.Release
end;
end;
procedure TCallBackList.UnRegisterCallBack(const Proc:TMethod );
var
i:integer;
begin
FCS.Acquire;
try
for i:=Pred(FCallBackList.Count) downto 0 do
with PMethod(FCallBackList[i])^ do
if (Code=Proc.Code) and (Data=Proc.Data) then
begin
Dispose(FCallBackList[i]);
FCallBackList.Delete(i);
Exit;
end;
finally
FCS.Release
end;
end;
function TCallBackList.GetCount: Integer;
begin
Result:=FCallBackList.Count
end;
procedure TCallBackList.Notification(AComponent: TComponent;Operation: TOperation);
begin
if Operation = opRemove then UnRegisterCallBacks(AComponent);
inherited Notification(AComponent,Operation);
end;
procedure TCallBackList.UnRegisterCallBacks(ProcOwner: TObject);
var i:integer;
begin
FCS.Acquire;
try
with FCallBackList do
for i:=Pred(Count) downto 0 do
if TObject(TMethod(FCallBackList[i]^).Data)=ProcOwner then
begin
Dispose(FCallBackList[i]);
Delete(i);
end;
finally
FCS.Release;
end;
end;
function TCallBackList.IndexOfMethod(const Proc: TMethod): integer;
var i:integer;
begin
Result:=-1;
for i:=Pred(FCallBackList.Count) downto 0 do
with PMethod(FCallBackList[i])^ do
if (Code=Proc.Code) and (Data=Proc.Data) then
begin
Result:=i;
Exit;
end;
end;
procedure TCallBackList.Delete(Index: Integer);
begin
if (Index>0) and (Index<FCallBackList.Count) then
FCallBackList.Delete(Index);
end;
{ TNotifyEventList }
function TNotifyEventList.Add(Event: TNotifyEvent): Integer;
begin
Result:= RegisterCallBack(TMethod(Event)) ;
end;
function TNotifyEventList.Get(Index:integer): TNotifyEvent;
var p:PMethod;
begin
p:=CallBackAddr[Index];
if p<>nil then
Result:=TNotifyEvent(p^)
else
Result:=nil
end;
procedure TNotifyEventList.Remove(Event: TNotifyEvent);
begin
UnRegisterCallBack(TMethod(Event))
end;
end.