Refactoring

This commit is contained in:
Michael Schimmel
2025-06-05 22:59:39 +02:00
parent e06a023742
commit 7ba42b0c7c
13 changed files with 860 additions and 306 deletions
+175 -60
View File
@@ -7,36 +7,79 @@ uses
type
IMycSubscriber = interface
// Interface for an entity that can be notified by a TState or TState.
// Returns true if this subscriber expects further notifications from the source, false otherwise.
function Notify: Boolean;
end;
IMycState = interface
TSubscriptionTag = Pointer;
IMycSignal = interface
function Subscribe(Subscriber: IMycSubscriber): TSubscriptionTag;
procedure Unsubscribe(Tag: TSubscriptionTag);
end;
TSubscription = record
private
FState: IMycSignal;
FTag: TSubscriptionTag;
constructor Create(const AState: IMycSignal; ATag: Pointer);
public
class operator Initialize(out Dest: TSubscription);
// Unsubscribes from the associated TState.
procedure Unsubscribe;
end;
TSignal = record
strict private
class var
FNull: IMycSignal;
class constructor ClassCreate;
private
FSignal: IMycSignal;
public
constructor Create(const ASignal: IMycSignal);
class operator Initialize(out Dest: TSignal);
class operator Implicit(const A: IMycSignal): TSignal; overload;
class operator Implicit(const A: TSignal): IMycSignal; overload;
function Subscribe(Subscriber: IMycSubscriber): TSubscription; inline;
procedure Unsubscribe(Tag: Pointer); inline;
class property Null: IMycSignal read FNull;
end;
IMycEvent = interface( IMycSignal )
procedure Trigger;
end;
TEvent = record
strict private
class var
FNull: IMycEvent;
class constructor ClassCreate;
private
FEvent: IMycEvent;
public
constructor Create(const AEvent: IMycEvent);
class operator Initialize(out Dest: TEvent);
class operator Implicit(const A: IMycEvent): TEvent; overload;
class operator Implicit(const A: TEvent): IMycEvent; overload;
class property Null: IMycEvent read FNull;
function Subscribe(Subscriber: IMycSubscriber): TSubscription; inline;
procedure Unsubscribe(Tag: Pointer); inline;
end;
IMycState = interface(IMycSignal)
// Represents a subscribable TState that can be queried.
{$REGION 'property access'}
function GetIsSet: Boolean;
{$ENDREGION}
// Subscribes a given subscriber to this TState.
function Subscribe(Subscriber: IMycSubscriber): Pointer;
procedure Unsubscribe(Tag: Pointer);
// IsSet is true if the TState has been reached or the condition is met.
property IsSet: Boolean read GetIsSet;
end;
TState = record // helper for IMycState
type
TSubscription = record
private
FState: IMycState;
FTag: Pointer; // Tag identifying this subscription within the notifier list.
constructor Create(const AState: IMycState; ATag: Pointer);
public
class operator Initialize(out Dest: TSubscription);
// Unsubscribes from the associated TState.
procedure Unsubscribe;
end;
strict private
class var
FNull: IMycState;
@@ -46,7 +89,7 @@ type
function GetIsSet: Boolean; inline;
public
constructor Create(const AState: IMycState);
class operator Initialize(out Dest: TState);
class operator Implicit(const A: IMycState): TState; overload;
class operator Implicit(const A: TState): IMycState; overload;
@@ -61,36 +104,33 @@ type
property IsSet: Boolean read GetIsSet;
end;
IMycEvent = interface(IMycSubscriber)
IMycLatch = interface(IMycSubscriber)
function GetState: TState;
property State: TState read GetState;
end;
TEvent = record
TLatch = record
strict private
class var
FNull: IMycEvent;
FNull: IMycLatch;
class constructor ClassCreate;
private
FLatch: IMycEvent;
FLatch: IMycLatch;
function GetState: TState; inline;
public
constructor Create(const ALatch: IMycEvent);
class operator Initialize(out Dest: TEvent);
class operator Implicit(const A: IMycEvent): TEvent; overload;
class operator Implicit(const A: TEvent): IMycEvent; overload;
// A notifier is an event that acts as a multi-cast event. Its state is always set and it forwards all notifications the all subscribers.
class function CreateNotifier: TEvent; static;
constructor Create(const ALatch: IMycLatch);
class operator Initialize(out Dest: TLatch);
class operator Implicit(const A: IMycLatch): TLatch; overload;
class operator Implicit(const A: TLatch): IMycLatch; overload;
// A latch is a specific type of event that, once set, remains set (non-resettable).
// It becomes set when the internal countdown reaches zero.
class function CreateLatch(Count: Integer): TEvent; static;
class function CreateLatch(Count: Integer): TLatch; static;
class function Enqueue(var Gate: TEvent; Count: Integer = 1): TState; static;
class function Enqueue(var Gate: TLatch; Count: Integer = 1): TState; static;
class property Null: IMycEvent read FNull;
class property Null: IMycLatch read FNull;
function Notify: Boolean; inline;
@@ -118,9 +158,9 @@ type
public
constructor Create(const ADirty: IMycFlag);
class operator Initialize(out Dest: TFlag);
class operator Implicit(const A: IMycFlag): TFlag; overload;
class operator Implicit(const A: TFlag): IMycFlag; overload;
class operator Initialize(out Dest: TFlag);
class function CreateFlag: IMycFlag; static;
class property Null: IMycFlag read FNull;
@@ -139,21 +179,21 @@ uses
{ TState.TSubscription }
constructor TState.TSubscription.Create(const AState: IMycState; ATag: TMycNotifyList<IMycSubscriber>.TTag);
constructor TSubscription.Create(const AState: IMycSignal; ATag: Pointer);
begin
Assert(Assigned(AState));
FState := AState;
FTag := ATag;
end;
procedure TState.TSubscription.Unsubscribe;
procedure TSubscription.Unsubscribe;
begin
FState.Unsubscribe(FTag);
FState := TState.Null;
FTag := nil;
end;
class operator TState.TSubscription.Initialize(out Dest: TSubscription);
class operator TSubscription.Initialize(out Dest: TSubscription);
begin
Dest.FState := TState.Null;
Dest.FTag := nil;
@@ -176,9 +216,9 @@ end;
class function TState.All(const States: TArray<TState>): TState;
var
Latch: IMycEvent;
Latch: IMycLatch;
begin
Latch := TEvent.CreateLatch(Length(States));
Latch := TLatch.CreateLatch(Length(States));
for var i := 0 to High(States) do
States[i].Subscribe(Latch);
Result := Latch.State;
@@ -186,7 +226,7 @@ end;
class function TState.Any(const States: TArray<TState>): TState;
var
Latch: IMycEvent;
Latch: IMycLatch;
begin
if Length(States) = 0 then
begin
@@ -194,7 +234,7 @@ begin
end
else
begin
Latch := TEvent.CreateLatch(1);
Latch := TLatch.CreateLatch(1);
for var i := 0 to High(States) do
States[i].Subscribe(Latch);
Result := Latch.State;
@@ -226,6 +266,50 @@ begin
Result.Create(A);
end;
class operator TState.Initialize(out Dest: TState);
begin
Dest.FState := FNull;
end;
{ TEvent }
constructor TEvent.Create(const AEvent: IMycEvent);
begin
FEvent := AEvent;
if not Assigned(FEvent) then
FEvent := FNull;
end;
class constructor TEvent.ClassCreate;
begin
FNull := TMycNullEvent.Create();
end;
function TEvent.Subscribe(Subscriber: IMycSubscriber): TSubscription;
begin
Result := TSubscription.Create(FEvent, FEvent.Subscribe(Subscriber));
end;
procedure TEvent.Unsubscribe(Tag: Pointer);
begin
FEvent.Unsubscribe(Tag);
end;
class operator TEvent.Implicit(const A: TEvent): IMycEvent;
begin
Result := A.FEvent;
end;
class operator TEvent.Implicit(const A: IMycEvent): TEvent;
begin
Result.Create(A);
end;
class operator TEvent.Initialize(out Dest: TEvent);
begin
Dest.FEvent := FNull;
end;
{ TFlag }
class constructor TFlag.ClassCreate;
@@ -233,8 +317,6 @@ begin
FNull := TMycNullFlag.Create;
end;
{ TFlag }
constructor TFlag.Create(const ADirty: IMycFlag);
begin
FDirty := ADirty;
@@ -277,24 +359,23 @@ begin
Dest.FDirty := FNull;
end;
{ TEvent }
{ TLatch }
class constructor TEvent.ClassCreate;
class constructor TLatch.ClassCreate;
begin
// Create a singleton null latch instance that is initially (and always) set.
FNull := TMycNullEvent.Create;
FNull := TMycNullLatch.Create;
end;
{ TEvent }
constructor TEvent.Create(const ALatch: IMycEvent);
constructor TLatch.Create(const ALatch: IMycLatch);
begin
FLatch := ALatch;
if not Assigned(FLatch) then
FLatch := FNull;
end;
class function TEvent.CreateLatch(Count: Integer): TEvent;
class function TLatch.CreateLatch(Count: Integer): TLatch;
begin
if Count > 0 then
Result := TMycLatch.Create(Count)
@@ -302,12 +383,7 @@ begin
Result := FNull;
end;
class function TEvent.CreateNotifier: TEvent;
begin
Result := TMycNotifyEvent.Create;
end;
class function TEvent.Enqueue(var Gate: TEvent; Count: Integer = 1): TState;
class function TLatch.Enqueue(var Gate: TLatch; Count: Integer = 1): TState;
begin
var gateState := Gate.State;
Gate := TMycLatch.Create(Count);
@@ -315,29 +391,68 @@ begin
exit(Gate.State);
end;
function TEvent.GetState: TState;
function TLatch.GetState: TState;
begin
Result := FLatch.State;
end;
function TEvent.Notify: Boolean;
function TLatch.Notify: Boolean;
begin
Result := FLatch.Notify;
end;
class operator TEvent.Implicit(const A: IMycEvent): TEvent;
class operator TLatch.Implicit(const A: IMycLatch): TLatch;
begin
Result.Create(A);
end;
class operator TEvent.Implicit(const A: TEvent): IMycEvent;
class operator TLatch.Implicit(const A: TLatch): IMycLatch;
begin
Result := A.FLatch;
end;
class operator TEvent.Initialize(out Dest: TEvent);
class operator TLatch.Initialize(out Dest: TLatch);
begin
Dest.FLatch := FNull;
end;
class constructor TSignal.ClassCreate;
begin
FNull := TMycNullSignal.Create();
end;
{ TSignal }
constructor TSignal.Create(const ASignal: IMycSignal);
begin
FSignal := ASignal;
if not Assigned(FSignal) then
FSignal := FNull;
end;
function TSignal.Subscribe(Subscriber: IMycSubscriber): TSubscription;
begin
Result := TSubscription.Create(FSignal, FSignal.Subscribe(Subscriber));
end;
procedure TSignal.Unsubscribe(Tag: Pointer);
begin
FSignal.Unsubscribe(Tag);
end;
class operator TSignal.Implicit(const A: TSignal): IMycSignal;
begin
Result := A.FSignal;
end;
class operator TSignal.Implicit(const A: IMycSignal): TSignal;
begin
Result.Create(A);
end;
class operator TSignal.Initialize(out Dest: TSignal);
begin
Dest.FSignal := FNull;
end;
end.