-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoneshottimer.pas
More file actions
265 lines (235 loc) · 8.09 KB
/
Copy pathoneshottimer.pas
File metadata and controls
265 lines (235 loc) · 8.09 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//-----------------------------------------------------------------------------------
// Toolkit Package © 2026 by Alexander Tverskoy
// Licensed under the MIT License
// You may obtain a copy of the License at https://opensource.org/licenses/MIT
//-----------------------------------------------------------------------------------
// Module for delayed one-shot procedure calls via a timer.
//
// Key features:
// 1. SetTimeout(Delay, Callback) - call a procedure without parameters.
// 2. SetTimeout(Delay, Callback, Data: Pointer) - pass an arbitrary pointer.
// 3. SetTimeout(Delay, Callback, Args: array of const) - pass a list of mixed-type parameters.
//
// In all variants you can get a timer reference (for cancellation) via out parameter.
// Cancellation is done by ClearTimeout(var Timer).
// All timers are freed automatically; calling ClearTimeout twice is safe.
//-----------------------------------------------------------------------------------
unit OneShotTimer;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ExtCtrls, Forms;
type
PTimer = ^TTimer;
// Original callback without parameters
TOneShotCallback = procedure of object;
// Callback that receives a Pointer to user data
TOneShotCallbackWithData = procedure(Data: Pointer) of object;
// Callback that receives an array of const (multiple typed parameters)
TOneShotArrayCallback = procedure(const Args: array of const) of object;
// Existing overloads (no data)
procedure SetTimeout(Delay: cardinal; Callback: TOneShotCallback); overload;
procedure SetTimeout(out Timer: TTimer; Delay: cardinal; Callback: TOneShotCallback); overload;
// Existing overloads with Pointer data
procedure SetTimeout(Delay: cardinal; Callback: TOneShotCallbackWithData; Data: Pointer); overload;
procedure SetTimeout(out Timer: TTimer; Delay: cardinal; Callback: TOneShotCallbackWithData; Data: Pointer); overload;
// New overloads with array of const
procedure SetTimeout(Delay: cardinal; Callback: TOneShotArrayCallback; const Args: array of const); overload;
procedure SetTimeout(out Timer: TTimer; Delay: cardinal; Callback: TOneShotArrayCallback; const Args: array of const); overload;
// Cancels a pending timer
procedure ClearTimeout(var Timer: TTimer);
implementation
var
FOneShotTimers: TList; // Stores all active timers
type
TOneShotTimer = class(TTimer)
private
FCallback: TOneShotCallback;
FCallbackWithData: TOneShotCallbackWithData;
FArrayCallback: TOneShotArrayCallback;
FUserVar: ^TTimer;
FData: Pointer;
FArgs: array of TVarRec; // Copy of array of const
procedure CopyArgs(const A: array of const);
procedure ClearArgs;
public
constructor CreateWith(ADelay: cardinal; ACallback: TOneShotCallback; AUserVar: PTimer);
constructor CreateWithData(ADelay: cardinal; ACallback: TOneShotCallbackWithData; AUserVar: PTimer; AData: Pointer);
constructor CreateWithArray(ADelay: cardinal; ACallback: TOneShotArrayCallback; AUserVar: PTimer; const A: array of const);
destructor Destroy; override;
procedure TimerFire(Sender: TObject);
end;
// Helper to deep copy TVarRec array (supports common simple types)
procedure TOneShotTimer.CopyArgs(const A: array of const);
var
i: Integer;
begin
SetLength(FArgs, Length(A));
for i := 0 to High(A) do
begin
FArgs[i] := A[i];
// For string types we need to copy the string content,
// because the original may be destroyed after the call.
case FArgs[i].VType of
vtAnsiString:
AnsiString(FArgs[i].VAnsiString) := AnsiString(A[i].VAnsiString);
vtUnicodeString:
UnicodeString(FArgs[i].VUnicodeString) := UnicodeString(A[i].VUnicodeString);
vtWideString:
WideString(FArgs[i].VWideString) := WideString(A[i].VWideString);
vtInterface: begin
// Save the pointer and increase reference count
FArgs[i].VInterface := A[i].VInterface;
if FArgs[i].VInterface <> nil then
IInterface(FArgs[i].VInterface)._AddRef;
end;
// For other types (integers, booleans, chars, floats) value is stored directly
end;
end;
end;
procedure TOneShotTimer.ClearArgs;
var
i: Integer;
begin
for i := 0 to High(FArgs) do
begin
case FArgs[i].VType of
vtAnsiString: AnsiString(FArgs[i].VAnsiString) := '';
vtUnicodeString: UnicodeString(FArgs[i].VUnicodeString) := '';
vtWideString: WideString(FArgs[i].VWideString) := '';
vtInterface:
if FArgs[i].VInterface <> nil then
IInterface(FArgs[i].VInterface)._Release;
end;
end;
SetLength(FArgs, 0);
end;
constructor TOneShotTimer.CreateWith(ADelay: cardinal; ACallback: TOneShotCallback; AUserVar: PTimer);
begin
inherited Create(nil);
FCallback := ACallback;
FCallbackWithData := nil;
FArrayCallback := nil;
FUserVar := AUserVar;
FData := nil;
Interval := ADelay;
OnTimer := @TimerFire;
Enabled := True;
if FUserVar <> nil then
FUserVar^ := Self;
FOneShotTimers.Add(Self);
end;
constructor TOneShotTimer.CreateWithData(ADelay: cardinal; ACallback: TOneShotCallbackWithData; AUserVar: PTimer; AData: Pointer);
begin
inherited Create(nil);
FCallback := nil;
FCallbackWithData := ACallback;
FArrayCallback := nil;
FUserVar := AUserVar;
FData := AData;
Interval := ADelay;
OnTimer := @TimerFire;
Enabled := True;
if FUserVar <> nil then
FUserVar^ := Self;
FOneShotTimers.Add(Self);
end;
constructor TOneShotTimer.CreateWithArray(ADelay: cardinal; ACallback: TOneShotArrayCallback; AUserVar: PTimer; const A: array of const);
begin
inherited Create(nil);
FCallback := nil;
FCallbackWithData := nil;
FArrayCallback := ACallback;
FUserVar := AUserVar;
FData := nil;
CopyArgs(A); // Store a deep copy of the arguments
Interval := ADelay;
OnTimer := @TimerFire;
Enabled := True;
if FUserVar <> nil then
FUserVar^ := Self;
FOneShotTimers.Add(Self);
end;
destructor TOneShotTimer.Destroy;
begin
FOneShotTimers.Remove(Self);
if FUserVar <> nil then
FUserVar^ := nil;
FUserVar := nil;
ClearArgs; // Free copied strings/interfaces
inherited Destroy;
end;
procedure TOneShotTimer.TimerFire(Sender: TObject);
begin
Enabled := False;
if FUserVar <> nil then
begin
FUserVar^ := nil;
FUserVar := nil;
end;
try
if Assigned(FCallback) then
FCallback()
else if Assigned(FCallbackWithData) then
FCallbackWithData(FData)
else if Assigned(FArrayCallback) then
FArrayCallback(FArgs); // Pass the stored args
finally
Free; // Destructor will clear args
end;
end;
// Existing implementations...
procedure SetTimeout(Delay: cardinal; Callback: TOneShotCallback);
begin
TOneShotTimer.CreateWith(Delay, Callback, nil);
end;
procedure SetTimeout(out Timer: TTimer; Delay: cardinal; Callback: TOneShotCallback);
begin
Timer := nil;
TOneShotTimer.CreateWith(Delay, Callback, @Timer);
end;
procedure SetTimeout(Delay: cardinal; Callback: TOneShotCallbackWithData; Data: Pointer);
begin
TOneShotTimer.CreateWithData(Delay, Callback, nil, Data);
end;
procedure SetTimeout(out Timer: TTimer; Delay: cardinal; Callback: TOneShotCallbackWithData; Data: Pointer);
begin
Timer := nil;
TOneShotTimer.CreateWithData(Delay, Callback, @Timer, Data);
end;
procedure SetTimeout(Delay: cardinal; Callback: TOneShotArrayCallback; const Args: array of const);
begin
TOneShotTimer.CreateWithArray(Delay, Callback, nil, Args);
end;
procedure SetTimeout(out Timer: TTimer; Delay: cardinal; Callback: TOneShotArrayCallback; const Args: array of const);
begin
Timer := nil;
TOneShotTimer.CreateWithArray(Delay, Callback, @Timer, Args);
end;
procedure ClearTimeout(var Timer: TTimer);
begin
if Timer = nil then Exit;
if Timer is TOneShotTimer then
begin
Timer.Enabled := False;
TOneShotTimer(Timer).FUserVar := nil;
FreeAndNil(Timer);
end
else
FreeAndNil(Timer);
end;
procedure FreeAllOneShotTimers;
var
i: Integer;
begin
for i := 0 to FOneShotTimers.Count - 1 do
TOneShotTimer(FOneShotTimers[i]).FUserVar := nil;
while FOneShotTimers.Count > 0 do
TOneShotTimer(FOneShotTimers[0]).Free;
end;
initialization
FOneShotTimers := TList.Create;
finalization
FreeAllOneShotTimers;
FOneShotTimers.Free;
end.