This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadvutils.action.pas
607 lines (508 loc) · 14.6 KB
/
advutils.action.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
(******************************************************************************)
(* AdvPascalUtils *)
(* delphi and object pascal library of utils data structures *)
(* *)
(* Copyright (c) 2021 Ivan Semenkov *)
(* https://github.com/isemenkov/advpascalutils ivan@semenkov.pro *)
(* Ukraine *)
(******************************************************************************)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"), *)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be included in *)
(* all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(* *)
(******************************************************************************)
unit advutils.action;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
{$IFOPT D+}
{$DEFINE DEBUG}
{$ENDIF}
interface
uses
SysUtils, utils.any, container.hashtable, container.multihash, utils.functor;
type
TActionState = (
ACTION_RUN,
ACTION_FREEZE,
ACTION_UNFREEZE,
ACTION_DELETE,
ACTION_ANY
);
{ Action run strategies. }
TRunStrategy = class
type
{ Base run strategy class. }
TStrategy = class
public
function After : TActionState; virtual; abstract;
end;
{ Runs normal. }
TNormal = class(TStrategy)
public
function After : TActionState; override;
end;
{ Runs only one times. }
TOnce = class(TStrategy)
public
function After : TActionState; override;
end;
{ Runs a specific number of times. }
TTimes = class(TStrategy)
public
constructor Create(ATimes : Cardinal);
function After : TActionState; override;
protected
FTimes : Cardinal;
FRunsCount : Cardinal;
end;
end;
{ Action freeze strategies. }
TFreezeStrategy = class
type
{ Base freeze strategy class. }
TStrategy = class
{ Freeze action. }
procedure Freeze; virtual; abstract;
{ Unfreeze action. }
procedure UnFreeze; virtual; abstract;
{ Reset freeze counter. }
procedure FreezeReset; virtual; abstract;
{ Check if action is freeze. }
function IsFreeze : Boolean; virtual; abstract;
end;
{ Action cann't freeze. }
TNone = class(TStrategy)
public
procedure Freeze; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
procedure UnFreeze; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
procedure FreezeReset; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
function IsFreeze : Boolean; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
end;
{ Action can be freeze and unfreeze regular. }
TNormal = class(TStrategy)
public
constructor Create;
procedure Freeze; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
procedure UnFreeze; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
procedure FreezeReset; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
function IsFreeze : Boolean; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
protected
FFreeze : Boolean;
end;
TTimes = class(TStrategy)
public
constructor Create;
procedure Freeze; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
procedure UnFreeze; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
procedure FreezeReset; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
function IsFreeze : Boolean; override;
{$IFNDEF DEBUG}inline;{$ENDIF}
protected
FFreezeCount : Integer;
end;
end;
{ Action unique ID type. }
TActionID = type Cardinal;
{ Action base class. }
TAction = class
public
constructor Create(ActionID : TActionID; ARunStrategy :
TRunStrategy.TStrategy; AFreezeStrategy : TFreezeStrategy.TStrategy);
destructor Destroy; override;
{ Run action. }
function Run (AData : TAnyValue) : TActionState;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Freeze action. }
procedure Freeze;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Unfreeze action. }
procedure UnFreeze;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Action varranty unfreeze. }
procedure FreezeReset;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Check if action is freeze. }
function IsFreeze : Boolean;
protected
{ Do something. }
procedure RunAction (AData : TAnyValue); virtual; abstract;
protected
FActionID : TActionID;
FRunStrategy : TRunStrategy.TStrategy;
FFreezeStrategy : TFreezeStrategy.TStrategy;
public
property ID : TActionID read FActionID;
end;
{ Action manager. }
TActionManager = class
public
type
{ Action subscribe callback. }
TActionCallback = procedure (AData : TAnyValue; AdditionalData :
TAnyValue) of object;
public
constructor Create;
destructor Destroy; override;
{ Register action. }
procedure Register(Action : TAction);
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Run action. }
function Run(ActionID : TActionID; AData : TAnyValue = nil) : Boolean;
overload;
{ Register action and run it. }
function Run(Action : TAction; AData : TAnyValue = nil) : Boolean; overload;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Freeze action. }
procedure Freeze(ActionID : TActionID);
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Unfreeze action. }
procedure UnFreeze(ActionID : TActionID);
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Action varranty unfreeze. }
procedure FreezeReset(ActionID : TActionID);
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Check if action is freeze. }
function IsFreeze(ActionID : TActionID) : Boolean;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Subscribe for action state. }
procedure Subscribe(ActionID : TActionID; AState : TActionState; ACallback :
TActionCallback; AdditionalData : TAnyValue = nil);
protected
type
TActionIDCompareFunctor = class
({$IFDEF FPC}specialize{$ENDIF} TBinaryFunctor<TActionID, Integer>)
public
function Call(AValue1, AValue2 : TActionID) : Integer; override;
end;
TActionsContainer = {$IFDEF FPC}specialize{$ENDIF} THashTable<TActionID,
TAction, TActionIDCompareFunctor>;
TSubscribeItem = record
State : TActionState;
AdditionalData : TAnyValue;
Callback : TActionCallback;
end;
TSubscribeItemUnsortableFunctor = class
({$IFDEF FPC}specialize{$ENDIF} TUnsortableFunctor<TSubscribeItem>);
TSubscribersContainer = {$IFDEF FPC}specialize{$ENDIF}
TMultiHash<TActionID, TSubscribeItem, TActionIDCompareFunctor,
{$IFDEF FPC}specialize{$ENDIF} TUnsortableFunctor<TSubscribeItem> >;
protected
{ Search action. }
function SearchAction(ActionID : TActionID) : TAction;
{$IFNDEF DEBUG}inline;{$ENDIF}
procedure NotifySubscribers(ActionID : TActionID; AState : TActionState;
AData : TAnyValue);
protected
FActions : TActionsContainer;
FSubscribers : TSubscribersContainer;
end;
{ Generate a hash key for TActionID value. }
function HashActionID(ActionID : TActionID) : Cardinal;
implementation
function HashActionID(ActionID : TActionID) : Cardinal;
begin
Result := HashInteger(Cardinal(ActionID));
end;
{ TRunStrategy.TNormal }
function TRunStrategy.TNormal.After : TActionState;
begin
Result := ACTION_RUN;
end;
{ TRunStrategy.TOnce }
function TRunStrategy.TOnce.After : TActionState;
begin
Result := ACTION_DELETE;
end;
{ TRunStrategy.TTimes }
constructor TRunStrategy.TTimes.Create(ATimes : Cardinal);
begin
FTimes := ATimes;
FRunsCount := 1;
end;
function TRunStrategy.TTimes.After : TActionState;
begin
Inc(FRunsCount);
if FRunsCount > FTimes then
begin
Result := ACTION_DELETE;
Exit;
end;
Result := ACTION_RUN;
end;
{ TFreezeStrategy.TNone }
procedure TFreezeStrategy.TNone.Freeze;
begin
end;
procedure TFreezeStrategy.TNone.UnFreeze;
begin
end;
procedure TFreezeStrategy.TNone.FreezeReset;
begin
end;
function TFreezeStrategy.TNone.IsFreeze : Boolean;
begin
Result := False;
end;
{ TFreezeStrategy.TNormal }
constructor TFreezeStrategy.TNormal.Create;
begin
FFreeze := False;
end;
procedure TFreezeStrategy.TNormal.Freeze;
begin
FFreeze := True;
end;
procedure TFreezeStrategy.TNormal.UnFreeze;
begin
FFreeze := False;
end;
procedure TFreezeStrategy.TNormal.FreezeReset;
begin
FFreeze := False;
end;
function TFreezeStrategy.TNormal.IsFreeze : Boolean;
begin
Result := FFreeze;
end;
{ TFreezeStrategy.TTimes }
constructor TFreezeStrategy.TTimes.Create;
begin
FFreezeCount := 0;
end;
procedure TFreezeStrategy.TTimes.Freeze;
begin
Inc(FFreezeCount);
end;
procedure TFreezeStrategy.TTimes.UnFreeze;
begin
Dec(FFreezeCount);
if FFreezeCount < 0 then
begin
FFreezeCount := 0;
end;
end;
procedure TFreezeStrategy.TTimes.FreezeReset;
begin
FFreezeCount := 0;
end;
function TFreezeStrategy.TTimes.IsFreeze : Boolean;
begin
Result := FFreezeCount <> 0;
end;
{ TAction }
constructor TAction.Create(ActionID : TActionID; ARunStrategy :
TRunStrategy.TStrategy; AFreezeStrategy : TFreezeStrategy.TStrategy);
begin
FActionID := ActionID;
FRunStrategy := ARunStrategy;
FFreezeStrategy := AFreezeStrategy;
end;
destructor TAction.Destroy;
begin
FreeAndNil(FRunStrategy);
FreeAndNil(FFreezeStrategy);
inherited Destroy;
end;
procedure TAction.Freeze;
begin
FFreezeStrategy.Freeze;
end;
procedure TAction.UnFreeze;
begin
FFreezeStrategy.UnFreeze;
end;
procedure TAction.FreezeReset;
begin
FFreezeStrategy.FreezeReset;
end;
function TAction.IsFreeze : Boolean;
begin
Result := FFreezeStrategy.IsFreeze;
end;
function TAction.Run (AData : TAnyValue) : TActionState;
begin
if not FFreezeStrategy.IsFreeze then
begin
RunAction(AData);
Result := FRunStrategy.After;
Exit;
end;
Result := ACTION_RUN;
end;
{ TActionManager }
function TActionManager.TActionIDCompareFunctor.Call(AValue1, AValue2 :
TActionID) : Integer;
begin
if AValue1 < AValue2 then
Result := -1
else if AValue2 < AValue1 then
Result := 1
else
Result := 0;
end;
constructor TActionManager.Create;
begin
FActions := TActionsContainer.Create(@HashActionID);
FSubscribers := TSubscribersContainer.Create(@HashActionID);
end;
destructor TActionManager.Destroy;
begin
FreeAndNil(FActions);
FreeAndNil(FSubscribers);
inherited Destroy;
end;
function TActionManager.SearchAction(ActionID : TActionID) : TAction;
begin
try
Result := FActions.Search(ActionID);
except on e : EKeyNotExistsException do
begin
Result := nil;
end;
end;
end;
procedure TActionManager.Register(Action : TAction);
begin
FActions.Insert(Action.ID, Action);
end;
function TActionManager.Run(ActionID : TActionID; AData : TAnyValue) : Boolean;
var
Action : TAction;
State : TActionState;
begin
Action := SearchAction(ActionID);
if (Action <> nil) and (not Action.IsFreeze) then
begin
State := Action.Run(AData);
case State of
ACTION_RUN :
begin
NotifySubscribers(ActionID, ACTION_RUN, AData);
Result := True;
Exit;
end;
ACTION_DELETE :
begin
NotifySubscribers(ActionID, ACTION_RUN, AData);
FActions.Remove(ActionID);
NotifySubscribers(ActionID, ACTION_DELETE, AData);
Result := True;
Exit;
end;
ACTION_FREEZE,
ACTION_UNFREEZE,
ACTION_ANY :
begin
raise Exception.Create('Impossible action state.');
end;
end;
end;
Result := False;
end;
function TActionManager.Run(Action: TAction; AData : TAnyValue) : Boolean;
begin
Register(Action);
Result := Run(Action.ID, AData);
end;
procedure TActionManager.Freeze(ActionID : TActionID);
var
Action : TAction;
begin
Action := SearchAction(ActionID);
if Action <> nil then
begin
Action.Freeze;
end;
end;
procedure TActionManager.UnFreeze(ActionID : TActionID);
var
Action : TAction;
begin
Action := SearchAction(ActionID);
if Action <> nil then
begin
Action.UnFreeze;
end;
end;
procedure TActionManager.FreezeReset(ActionID : TActionID);
var
Action : TAction;
begin
Action := SearchAction(ActionID);
if Action <> nil then
begin
Action.FreezeReset;
end;
end;
function TActionManager.IsFreeze(ActionID : TActionID) : Boolean;
var
Action : TAction;
begin
Action := SearchAction(ActionID);
if Action <> nil then
begin
Result := Action.IsFreeze;
Exit;
end;
Result := False;
end;
procedure TActionManager.Subscribe(ActionID : TActionID; AState : TActionState;
ACallback : TActionCallback; AdditionalData : TAnyValue);
var
SubscribeItem : TSubscribeItem;
begin
SubscribeItem.State := AState;
SubscribeItem.Callback := ACallback;
SubscribeItem.AdditionalData := AdditionalData;
FSubscribers.Insert(ActionID, SubscribeItem);
end;
procedure TActionManager.NotifySubscribers(ActionID : TActionID; AState :
TActionState; AData : TAnyValue);
var
Subscribers : TSubscribersContainer.TMultiValue;
SubscribeItem : TSubscribeItem;
begin
try
Subscribers := FSubscribers.Search(ActionID);
except
on e: EKeyNotExistsException do
begin
Exit;
end;
end;
for SubscribeItem in Subscribers do
begin
if SubscribeItem.State = AState then
begin
SubscribeItem.Callback(AData, SubscribeItem.AdditionalData);
end;
end;
end;
end.