Test Refactoring of signals

This commit is contained in:
Michael Schimmel
2025-06-05 22:11:54 +02:00
parent c9817ca200
commit e06a023742
17 changed files with 528 additions and 774 deletions
+110 -51
View File
@@ -61,44 +61,44 @@ type
property IsSet: Boolean read GetIsSet;
end;
// IMycLatch is a specific type of flag that, once set, remains set (non-resettable).
// It typically becomes set when an internal countdown reaches zero.
IMycLatch = interface(IMycSubscriber)
// Provides access to the IMycState interface of the flag.
IMycEvent = interface(IMycSubscriber)
function GetState: TState;
property State: TState read GetState;
end;
TLatch = record
TEvent = record
strict private
class var
FNull: IMycLatch;
FNull: IMycEvent;
class constructor ClassCreate;
private
FLatch: IMycLatch;
FLatch: IMycEvent;
function GetState: TState; inline;
public
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;
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;
class function Construct(Count: Integer): TLatch; static;
// 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;
class function Enqueue(var Gate: TLatch; Count: Integer = 1): TState; static;
// 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 property Null: IMycLatch read FNull;
class function Enqueue(var Gate: TEvent; Count: Integer = 1): TState; static;
class property Null: IMycEvent read FNull;
function Notify: Boolean; inline;
property State: TState read GetState;
end;
// IMycDirty represents a resettable flag, typically indicating if a State is "dirty" (requiring attention) or "clean".
// It inherits from IMycFlag, meaning it has a State, can be notified, and subscribed to.
IMycDirty = interface(IMycSubscriber)
// Provides access to the IMycState interface of the flag.
// IMycFlag represents a resettable flag, typically indicating if a State is "dirty" (requiring attention) or "clean".
IMycFlag = interface(IMycSubscriber)
function GetState: TState;
// Resets the flag to its "clean" (not set / not dirty) State.
// Returns true if the flag was actually dirty before this reset, false otherwise.
@@ -106,14 +106,29 @@ type
property State: TState read GetState;
end;
TDirty = record
TFlag = record
strict private
class var
FNull: IMycDirty;
FNull: IMycFlag;
class constructor ClassCreate;
private
FDirty: IMycFlag;
function GetState: TState; inline;
public
class function Construct: IMycDirty; static;
class property Null: IMycDirty read FNull;
constructor Create(const ADirty: IMycFlag);
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;
function Notify: Boolean; inline;
function Reset: Boolean;
property State: TState read GetState;
end;
implementation
@@ -161,9 +176,9 @@ end;
class function TState.All(const States: TArray<TState>): TState;
var
Latch: IMycLatch;
Latch: IMycEvent;
begin
Latch := TLatch.Construct(Length(States));
Latch := TEvent.CreateLatch(Length(States));
for var i := 0 to High(States) do
States[i].Subscribe(Latch);
Result := Latch.State;
@@ -171,7 +186,7 @@ end;
class function TState.Any(const States: TArray<TState>): TState;
var
Latch: IMycLatch;
Latch: IMycEvent;
begin
if Length(States) = 0 then
begin
@@ -179,7 +194,7 @@ begin
end
else
begin
Latch := TLatch.Construct(1);
Latch := TEvent.CreateLatch(1);
for var i := 0 to High(States) do
States[i].Subscribe(Latch);
Result := Latch.State;
@@ -211,24 +226,75 @@ begin
Result.Create(A);
end;
{ TLatch }
{ TFlag }
class constructor TLatch.ClassCreate;
class constructor TFlag.ClassCreate;
begin
// Create a singleton null latch instance that is initially (and always) set.
FNull := TMycNullLatch.Create; // Calls the instance constructor
FNull := TMycNullFlag.Create;
end;
{ TLatch }
{ TFlag }
constructor TLatch.Create(const ALatch: IMycLatch);
constructor TFlag.Create(const ADirty: IMycFlag);
begin
FDirty := ADirty;
if not Assigned(FDirty) then
FDirty := FNull;
end;
class function TFlag.CreateFlag: IMycFlag;
begin
Result := TMycFlag.Create;
end;
function TFlag.GetState: TState;
begin
Result := FDirty.State;
end;
function TFlag.Notify: Boolean;
begin
Result := FDirty.Notify;
end;
function TFlag.Reset: Boolean;
begin
Result := FDirty.Reset;
end;
class operator TFlag.Implicit(const A: IMycFlag): TFlag;
begin
Result.Create(A);
end;
class operator TFlag.Implicit(const A: TFlag): IMycFlag;
begin
Result := A.FDirty;
end;
class operator TFlag.Initialize(out Dest: TFlag);
begin
Dest.FDirty := FNull;
end;
{ TEvent }
class constructor TEvent.ClassCreate;
begin
// Create a singleton null latch instance that is initially (and always) set.
FNull := TMycNullEvent.Create;
end;
{ TEvent }
constructor TEvent.Create(const ALatch: IMycEvent);
begin
FLatch := ALatch;
if not Assigned(FLatch) then
FLatch := FNull;
end;
class function TLatch.Construct(Count: Integer): TLatch;
class function TEvent.CreateLatch(Count: Integer): TEvent;
begin
if Count > 0 then
Result := TMycLatch.Create(Count)
@@ -236,7 +302,12 @@ begin
Result := FNull;
end;
class function TLatch.Enqueue(var Gate: TLatch; Count: Integer = 1): TState;
class function TEvent.CreateNotifier: TEvent;
begin
Result := TMycNotifyEvent.Create;
end;
class function TEvent.Enqueue(var Gate: TEvent; Count: Integer = 1): TState;
begin
var gateState := Gate.State;
Gate := TMycLatch.Create(Count);
@@ -244,41 +315,29 @@ begin
exit(Gate.State);
end;
function TLatch.GetState: TState;
function TEvent.GetState: TState;
begin
Result := FLatch.State;
end;
function TLatch.Notify: Boolean;
function TEvent.Notify: Boolean;
begin
Result := FLatch.Notify;
end;
class operator TLatch.Implicit(const A: IMycLatch): TLatch;
class operator TEvent.Implicit(const A: IMycEvent): TEvent;
begin
Result.Create(A);
end;
class operator TLatch.Implicit(const A: TLatch): IMycLatch;
class operator TEvent.Implicit(const A: TEvent): IMycEvent;
begin
Result := A.FLatch;
end;
class operator TLatch.Initialize(out Dest: TLatch);
class operator TEvent.Initialize(out Dest: TEvent);
begin
Dest.FLatch := FNull;
end;
{ TDirty }
class constructor TDirty.ClassCreate;
begin
FNull := TMycNullDirty.Create;
end;
class function TDirty.Construct: IMycDirty;
begin
Result := TMycDirty.Create;
end;
end.