forked from sirodcar/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnonCommand.pas
More file actions
454 lines (373 loc) · 11.2 KB
/
AnonCommand.pas
File metadata and controls
454 lines (373 loc) · 11.2 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
unit AnonCommand;
interface
{x$DEFINE ANON_SYNC_EXEC}
uses
debug, System.Classes, System.SysUtils, System.Generics.Collections, better_collections, typex, systemx, managedthread, commandprocessor;
type
EAnonymousThreadException = class(Exception);
Tcmd_Test = class(TCommand)
public
procedure DoExecute;override;
end;
TAnonymousCommand<T> = class(TCommand)
private
Err: Exception;
FThreadFunc: TFunc<T>;
FOnErrorProc: TProc<Exception>;
FOnFinishedProc: TProc<T>;
FResult: T;
FStartSuspended: Boolean;
FSynchronizeFinish: boolean;
FSynchronizeExecute: boolean;
strict private
procedure SyncFinished;
procedure SyncExecute;
procedure SyncError;
protected
procedure DoExecute; override;
public
procedure InitExpense; override;
procedure Detach; override;
property SynchronizeFinish: boolean read FSynchronizeFinish write FSynchronizeFinish;
property SynchronizeExecute: boolean read FSynchronizeExecute write FSynchronizeExecute;
constructor CreateAndTrackX(tracker: TCommandList<TCommand>; AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False);virtual;
constructor Create(AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False;
FreeOnComplete: Boolean = True);reintroduce;
destructor Destroy;override;
// class constructor Create;
// class destructor Destroy;
end;
TAnonymousGUICommand<T> = class(TAnonymousCommand<T>)
//THIS is an ANONYMOUs COMMAND that uses SYNCHRONIZE in OnFinish
//by default. You can choose to synchronize Execution and Finish of any anonymous
//command. This derivative simply has a different default for SynchronizeFinish
public
constructor Create(AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False;
FreeOnComplete: Boolean = True);reintroduce;
end;
TAnonymousIteratorCommand = class(TCommand)
protected
procedure DoExecute; override;
public
iteration: ni;
count: ni;
proc: TProc<ni>;
end;
TAnonymousFunctionCommand = class(TAnonymousCommand<boolean>)
public
constructor CreateInline(AThreadFunc: TProc; ACreateSuspended: Boolean = False;
FreeOnComplete: Boolean = false);reintroduce;
constructor CreateInlineWithGui(AThreadFunc, GuiFunc: TProc; ACreateSuspended: Boolean = False; FreeOnComplete: Boolean = false);reintroduce;
constructor CreateInlineWithGuiEx(AThreadFunc, GuiFunc: TProc; exProc: TProc<string>; ACreateSuspended: Boolean = False; FreeOnComplete: Boolean = false);reintroduce;
end;
TAnonTimerProc = reference to procedure();
TAnonymousTimer = class(TAnonymousCommand<boolean>)
public
timerproc: TAnonTimerProc;
procedure InitExpense;override;
end;
{ TAnonymousTimer }
function InlineProc(proc: TProc): TAnonymousCommand<boolean>;
function InlineIteratorProc(idx: ni; proc: TProc<ni>): TAnonymousIteratorCommand;
function InlineIteratorGroupProc(idx: ni; count: ni; proc: TProc<ni>): TAnonymousIteratorCommand;
function InlineProcWithGui(proc, guiproc: TProc): TAnonymousCommand<boolean>;
function InlineProcWithGuiEx(proc, guiproc: TProc; exProc:TProc<string>): TAnonymousCommand<boolean>;
//function InlineProc<T>(proc: TProc): TAnonymousCommand<T,boolean>;
function SetTimer(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
function SetTimerGUI(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
implementation
{$IFDEF MACOS}
uses
{$IFDEF IOS}
iOSapi.Foundation
{$ELSE}
MacApi.Foundation
{$ENDIF IOS}
;
{$ENDIF MACOS}
{ TAnonymousCommand }
//class constructor TAnonymousCommand<T>.Create;
//begin
// inherited;
//end;
//class destructor TAnonymousCommand<T>.Destroy;
//begin
// inherited;
//end;
procedure TAnonymousCommand<T>.Detach;
begin
Debug.log(self, 'Detaching');
if detached then exit;
inherited;
end;
function InlineProc(proc: TProc): TAnonymousCommand<boolean>;
var
res: TAnonymousCommand<boolean>;
begin
res := TAnonymousFunctionCommand.createinline(proc, false, false);
res.SynchronizeFinish := false;
result := res;
end;
function InlineIteratorProc(idx: ni; proc: TProc<ni>): TAnonymousIteratorCommand;
begin
result := TAnonymousIteratorCommand.create;
result.iteration := idx;
result.count := 1;
result.proc := proc;
// result.CPUExpense := 1.0;
result.start;
end;
function InlineIteratorGroupProc(idx: ni; count: ni; proc: TProc<ni>): TAnonymousIteratorCommand;
begin
result := TAnonymousIteratorCommand.create;
result.iteration := idx;
result.count := count;
result.proc := proc;
// result.CPUExpense := 1.0;
result.start;
end;
function InlineProcWithGui(proc, guiproc: TProc): TAnonymousCommand<boolean>;
var
res: TAnonymousCommand<boolean>;
begin
res := TAnonymousFunctionCommand.createinlinewithgui(proc, guiproc, false, false);
result := res;
result.start;
end;
function InlineProcWithGuiEx(proc, guiproc: TProc; exProc:TProc<string>): TAnonymousCommand<boolean>;
var
res: TAnonymousCommand<boolean>;
begin
res := TAnonymousFunctionCommand.createinlinewithguiex(proc, guiproc, exProc, false, false);
result := res;
result.start;
end;
constructor TAnonymousCommand<T>.Create(AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;
AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False; FreeOnComplete: Boolean = True);
begin
// Debug.Log('Creating '+self.GetObjectDebug);
FOnFinishedProc := AOnFinishedProc;
FOnErrorProc := AOnErrorProc;
FThreadFunc := AThreadFunc;
FireForget := FreeOnComplete;
FStartSuspended := ACreateSuspended;
FSynchronizeFinish := true;
{$IFDEF ANON_SYNC_EXEC}
FSynchronizeExecute := true;
{$ENDIF}
inherited Create();
if not ACreateSuspended then
Start;
end;
constructor TAnonymousCommand<T>.CreateAndTrackX(tracker: TCommandList<TCommand>;
AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;
AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean);
begin
Create(AthreadFunc, AonFinishedProc, AOnErrorProc, ACreateSuspended, false);
tracker.Add(self);
end;
destructor TAnonymousCommand<T>.Destroy;
begin
// Debug.Log('Destroying Inherited '+self.GetObjectDebug);
inherited;
// Debug.Log('Destroying '+self.GetObjectDebug);
end;
procedure TAnonymousCommand<T>.DoExecute;
{$IFDEF MACOS}
var
lPool: NSAutoreleasePool;
{$ENDIF}
begin
inherited;
// Debug.Log(self, 'Executing '+self.GetObjectDebug);
{$IFDEF MACOS}
//Need to create an autorelease pool, otherwise any autorelease objects
//may leak.
//See https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI
lPool := TNSAutoreleasePool.Create;
try
{$ENDIF}
try
// raise ECritical.Create('fuck you');
if FSynchronizeExecute then begin
TThread.Synchronize(self.Thread.realthread, SyncExecute)
end else
FResult := FThreadFunc;
if assigned(FonFinishedProc) then begin
try
if FSynchronizeFinish then
TThread.Synchronize(self.Thread.realthread, SyncFinished)
else
FOnFinishedProc(FResult);
except
on E:Exception do begin
Debug.Log('Exception during synchronized anon finish: '+e.message);
end;
end;
end;
except
on E: Exception do begin
Err := e;
ErrorMessage := e.message;
Error := true;
{$IFNDEF CONSOLE}
if FSynchronizeFinish then
TThread.Synchronize(self.Thread.realthread, SyncError)
else
{$ENDIF}
if assigned(FOnErrorProc) then
FOnErrorProc(E);
end;
end;
{$IFDEF MACOS}
finally
lPool.drain;
end;
{$ENDIF}
end;
procedure TAnonymousCommand<T>.InitExpense;
begin
inherited;
// self.Resources.SetResourceUsage('CTO_Anonymous', 1.0);
cpuExpense := 0.0;
end;
procedure TAnonymousCommand<T>.SyncError;
begin
FOnErrorProc(Err);
end;
procedure TAnonymousCommand<T>.SyncExecute;
begin
FResult := FThreadFunc;
end;
procedure TAnonymousCommand<T>.SyncFinished;
begin
FOnFinishedProc(fResult);
end;
{ Tcmd_Test }
procedure Tcmd_Test.DoExecute;
begin
inherited;
sleep(4000);
end;
procedure TAnonymousTimer.InitExpense;
begin
inherited;
CPuExpense := 0;
end;
function SetTimerGUI(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
begin
result := TAnonymousTimer.create(
function : boolean
begin
sleep(interval);
exit(true);
end,
procedure (b: boolean)
begin
ontimerproc();
end,
procedure (e: exception)
begin
end
);
result.SynchronizeFinish := true;
result.FireForget := true;
result.start;
end;
function SetTimer(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
begin
result := TAnonymousTimer.create(
function : boolean
begin
sleep(interval);
exit(true);
end,
procedure (b: boolean)
begin
ontimerproc();
end,
procedure (e: exception)
begin
end
);
result.SynchronizeFinish := false;
result.FireForget := true;
result.start;
end;
{ TAnonymousGUICommand<T> }
constructor TAnonymousGUICommand<T>.Create(AThreadFunc: TFunc<T>;
AOnFinishedProc: TProc<T>; AOnErrorProc: TProc<Exception>; ACreateSuspended,
FreeOnComplete: Boolean);
begin
inherited;
SynchronizeFinish := true;
end;
{ TAnonymousFunctionCommand }
constructor TAnonymousFunctionCommand.CreateInline(AThreadFunc: TProc;
ACreateSuspended, FreeOnComplete: Boolean);
var
funct: TFunc<boolean>;
begin
funct:= function (): boolean
begin
AthreadFunc();
result := true;
end;
Create(funct, procedure (b: boolean) begin end, procedure (e: exception) begin end);
end;
constructor TAnonymousFunctionCommand.CreateInlineWithGui(AThreadFunc, GuiFunc: TProc;
ACreateSuspended, FreeOnComplete: Boolean);
var
func1: TFunc<boolean>;
func2: TProc<boolean>;
begin
func1:= function (): boolean
begin
AthreadFunc();
result := true;
end;
func2:= procedure (b: boolean)
begin
if assigned(GuiFunc) then
GuiFunc();
end;
Create(func1, func2, procedure (e: exception) begin end, true, false);
SynchronizeFinish := true;
Start;
end;
constructor TAnonymousFunctionCommand.CreateInlineWithGuiEx(AThreadFunc,
GuiFunc: TProc; exProc: TProc<string>; ACreateSuspended,
FreeOnComplete: Boolean);
var
func1: TFunc<boolean>;
func2: TProc<boolean>;
func3: TProc<string>;
begin
func1:= function (): boolean
begin
AthreadFunc();
result := true;
end;
func2:= procedure (b: boolean)
begin
GuiFunc();
end;
func3 := procedure (s: string)
begin
ExProc(s);
end;
Create(func1, func2, procedure (e: exception) begin func3(e.message) end, true, false);
SynchronizeFinish := true;
Start;
end;
{ TAnonymousIteratorCommand }
procedure TAnonymousIteratorCommand.DoExecute;
begin
inherited;
if count = 0 then
count := 1;
for var t:= 0 to count-1 do begin
proc(iteration+t);
end;
end;
end.