172 lines
5.1 KiB
ObjectPascal
172 lines
5.1 KiB
ObjectPascal
unit Myc.Signals;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils;
|
|
|
|
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;
|
|
|
|
TMycSubscription = record
|
|
private
|
|
FState: IMycState;
|
|
FTag: Pointer; // Tag identifying this subscription within the notifier list.
|
|
function GetState: IMycState;
|
|
public
|
|
constructor Create( const AState: IMycState; ATag: Pointer );
|
|
// Unsubscribes from the associated TState.
|
|
procedure Unsubscribe;
|
|
class operator Initialize( out Dest: TMycSubscription );
|
|
property TState: IMycState read GetState;
|
|
end;
|
|
|
|
IMycState = interface
|
|
// 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 ): TMycSubscription;
|
|
procedure Unsubscribe( Tag: Pointer );
|
|
// IsSet is true if the TState has been reached or the condition is met.
|
|
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.
|
|
function GetState: IMycState;
|
|
property State: IMycState read GetState;
|
|
// Inherits IMycSubscriber and State property from IMycFlag. No new members.
|
|
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.
|
|
function GetState: IMycState;
|
|
// Resets the flag to its "clean" (not set / not dirty) State.
|
|
// Returns true if the flag was actually dirty before this reset, false otherwise.
|
|
function Reset: Boolean;
|
|
property State: IMycState read GetState;
|
|
end;
|
|
|
|
TState = record
|
|
private
|
|
FState: IMycState;
|
|
class function GetNull: IMycState; static;
|
|
public
|
|
constructor Create(const AState: IMycState);
|
|
class function CreateLatch( Count: Integer ): IMycLatch; static;
|
|
class function CreateDirty: IMycDirty; static;
|
|
class function All( const States: TArray<IMycState> ): IMycState; static;
|
|
class function Any( const States: TArray<IMycState> ): IMycState; static;
|
|
class operator Implicit(const A: IMycState): TState; overload;
|
|
class operator Implicit(const A: TState): IMycState; overload;
|
|
class property Null: IMycState read GetNull;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
Myc.Core.Notifier, Myc.Core.Signals;
|
|
|
|
{ TMycSubscription }
|
|
|
|
constructor TMycSubscription.Create( const AState: IMycState; ATag:
|
|
TMycNotifyList<IMycSubscriber>.TTag );
|
|
begin
|
|
Assert( Assigned( AState ) );
|
|
FState := AState;
|
|
FTag := ATag;
|
|
end;
|
|
|
|
procedure TMycSubscription.Unsubscribe;
|
|
begin
|
|
FState.Unsubscribe( FTag );
|
|
FState := TMycState.Null;
|
|
FTag := nil;
|
|
end;
|
|
|
|
function TMycSubscription.GetState: IMycState;
|
|
begin
|
|
Result := FState
|
|
end;
|
|
|
|
class operator TMycSubscription.Initialize( out Dest: TMycSubscription );
|
|
begin
|
|
Dest.FState := TMycState.Null;
|
|
Dest.FTag := nil;
|
|
end;
|
|
|
|
{ TState }
|
|
|
|
constructor TState.Create(const AState: IMycState);
|
|
begin
|
|
FState := AState;
|
|
if not Assigned(FState) then
|
|
FState := TMycState.Null;
|
|
end;
|
|
|
|
class function TState.CreateLatch( Count: Integer ): IMycLatch;
|
|
begin
|
|
Result := TMycLatch.CreateLatch( Count );
|
|
end;
|
|
|
|
class function TState.All( const States: TArray<IMycState> ): IMycState;
|
|
var
|
|
Latch: IMycLatch;
|
|
begin
|
|
Latch := CreateLatch( Length( States ) );
|
|
for var i := 0 to High( States ) do
|
|
States[i].Subscribe( Latch );
|
|
Result := Latch.State;
|
|
end;
|
|
|
|
class function TState.Any( const States: TArray<IMycState> ): IMycState;
|
|
var
|
|
Latch: IMycLatch;
|
|
begin
|
|
if Length( States ) = 0 then
|
|
begin
|
|
Result := TState.Null;
|
|
end
|
|
else
|
|
begin
|
|
Latch := CreateLatch( 1 );
|
|
for var i := 0 to High( States ) do
|
|
States[i].Subscribe( Latch );
|
|
Result := Latch.State;
|
|
end;
|
|
end;
|
|
|
|
class function TState.CreateDirty: IMycDirty;
|
|
begin
|
|
Result := TMycDirty.CreateDirty;
|
|
end;
|
|
|
|
class function TState.GetNull: IMycState;
|
|
begin
|
|
Result := TMycState.Null;
|
|
end;
|
|
|
|
class operator TState.Implicit(const A: TState): IMycState;
|
|
begin
|
|
Result := A.FState;
|
|
end;
|
|
|
|
class operator TState.Implicit(const A: IMycState): TState;
|
|
begin
|
|
Result.Create( A );
|
|
end;
|
|
|
|
end.
|