Refactoring
This commit is contained in:
+50
-52
@@ -12,53 +12,52 @@ type
|
||||
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 State: 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;
|
||||
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.
|
||||
function GetState: IMycState;
|
||||
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;
|
||||
class var
|
||||
FNull: IMycState;
|
||||
class constructor ClassCreate;
|
||||
private
|
||||
FState: IMycState;
|
||||
function GetIsSet: Boolean; inline;
|
||||
public
|
||||
constructor Create(const AState: IMycState);
|
||||
class operator Implicit(const A: IMycState): TState; overload;
|
||||
class operator Implicit(const A: TState): IMycState; overload;
|
||||
constructor Create( const AState: IMycState );
|
||||
|
||||
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 function All( const States: TArray<TState> ): TState; static;
|
||||
class function Any( const States: TArray<TState> ): TState; static;
|
||||
|
||||
class property Null: IMycState read FNull;
|
||||
|
||||
function Subscribe(Subscriber: IMycSubscriber): TMycSubscription; inline;
|
||||
procedure Unsubscribe(Tag: Pointer); inline;
|
||||
function Subscribe( Subscriber: IMycSubscriber ): TSubscription; inline;
|
||||
procedure Unsubscribe( Tag: Pointer ); inline;
|
||||
|
||||
property IsSet: Boolean read GetIsSet;
|
||||
end;
|
||||
@@ -67,18 +66,17 @@ type
|
||||
// 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.
|
||||
function GetState: TState;
|
||||
property State: TState read GetState;
|
||||
end;
|
||||
|
||||
TLatch = record
|
||||
strict private
|
||||
class var
|
||||
FNull: IMycLatch;
|
||||
class var
|
||||
FNull: IMycLatch;
|
||||
class constructor ClassCreate;
|
||||
public
|
||||
class function Construct(Count: Integer): IMycLatch; static;
|
||||
class function Construct( Count: Integer ): IMycLatch; static;
|
||||
class property Null: IMycLatch read FNull;
|
||||
end;
|
||||
|
||||
@@ -86,17 +84,17 @@ type
|
||||
// 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;
|
||||
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.
|
||||
function Reset: Boolean;
|
||||
property State: IMycState read GetState;
|
||||
property State: TState read GetState;
|
||||
end;
|
||||
|
||||
TDirty = record
|
||||
strict private
|
||||
class var
|
||||
FNull: IMycDirty;
|
||||
class var
|
||||
FNull: IMycDirty;
|
||||
class constructor ClassCreate;
|
||||
public
|
||||
class function Construct: IMycDirty; static;
|
||||
@@ -108,9 +106,9 @@ implementation
|
||||
uses
|
||||
Myc.Core.Notifier, Myc.Core.Signals;
|
||||
|
||||
{ TMycSubscription }
|
||||
{ TState.TSubscription }
|
||||
|
||||
constructor TMycSubscription.Create( const AState: IMycState; ATag:
|
||||
constructor TState.TSubscription.Create( const AState: IMycState; ATag:
|
||||
TMycNotifyList<IMycSubscriber>.TTag );
|
||||
begin
|
||||
Assert( Assigned( AState ) );
|
||||
@@ -118,19 +116,19 @@ begin
|
||||
FTag := ATag;
|
||||
end;
|
||||
|
||||
procedure TMycSubscription.Unsubscribe;
|
||||
procedure TState.TSubscription.Unsubscribe;
|
||||
begin
|
||||
FState.Unsubscribe( FTag );
|
||||
FState := TState.Null;
|
||||
FTag := nil;
|
||||
end;
|
||||
|
||||
function TMycSubscription.GetState: IMycState;
|
||||
function TState.TSubscription.GetState: IMycState;
|
||||
begin
|
||||
Result := FState
|
||||
end;
|
||||
|
||||
class operator TMycSubscription.Initialize( out Dest: TMycSubscription );
|
||||
class operator TState.TSubscription.Initialize( out Dest: TSubscription );
|
||||
begin
|
||||
Dest.FState := TState.Null;
|
||||
Dest.FTag := nil;
|
||||
@@ -138,20 +136,20 @@ end;
|
||||
|
||||
{ TState }
|
||||
|
||||
constructor TState.Create(const AState: IMycState);
|
||||
constructor TState.Create( const AState: IMycState );
|
||||
begin
|
||||
FState := AState;
|
||||
if not Assigned(FState) then
|
||||
if not Assigned( FState ) then
|
||||
FState := FNull;
|
||||
end;
|
||||
|
||||
class constructor TState.ClassCreate;
|
||||
begin
|
||||
// Create a singleton null latch instance that is initially (and always) set.
|
||||
FNull := TMycNullState.Create(); // Calls the instance constructor
|
||||
FNull := TMycNullState.Create( ); // Calls the instance constructor
|
||||
end;
|
||||
|
||||
class function TState.All( const States: TArray<IMycState> ): IMycState;
|
||||
class function TState.All( const States: TArray<TState> ): TState;
|
||||
var
|
||||
Latch: IMycLatch;
|
||||
begin
|
||||
@@ -161,7 +159,7 @@ begin
|
||||
Result := Latch.State;
|
||||
end;
|
||||
|
||||
class function TState.Any( const States: TArray<IMycState> ): IMycState;
|
||||
class function TState.Any( const States: TArray<TState> ): TState;
|
||||
var
|
||||
Latch: IMycLatch;
|
||||
begin
|
||||
@@ -183,22 +181,22 @@ begin
|
||||
Result := FState.IsSet;
|
||||
end;
|
||||
|
||||
function TState.Subscribe(Subscriber: IMycSubscriber): TMycSubscription;
|
||||
function TState.Subscribe( Subscriber: IMycSubscriber ): TSubscription;
|
||||
begin
|
||||
Result := FState.Subscribe(Subscriber);
|
||||
Result := TSubscription.Create( FState, FState.Subscribe( Subscriber ) );
|
||||
end;
|
||||
|
||||
procedure TState.Unsubscribe(Tag: Pointer);
|
||||
procedure TState.Unsubscribe( Tag: Pointer );
|
||||
begin
|
||||
FState.Unsubscribe(Tag);
|
||||
FState.Unsubscribe( Tag );
|
||||
end;
|
||||
|
||||
class operator TState.Implicit(const A: TState): IMycState;
|
||||
class operator TState.Implicit( const A: TState ): IMycState;
|
||||
begin
|
||||
Result := A.FState;
|
||||
end;
|
||||
|
||||
class operator TState.Implicit(const A: IMycState): TState;
|
||||
class operator TState.Implicit( const A: IMycState ): TState;
|
||||
begin
|
||||
Result.Create( A );
|
||||
end;
|
||||
@@ -211,7 +209,7 @@ begin
|
||||
FNull := TMycNullLatch.Create; // Calls the instance constructor
|
||||
end;
|
||||
|
||||
class function TLatch.Construct(Count: Integer): IMycLatch;
|
||||
class function TLatch.Construct( Count: Integer ): IMycLatch;
|
||||
begin
|
||||
if Count > 0 then
|
||||
Result := TMycLatch.Create( Count )
|
||||
|
||||
Reference in New Issue
Block a user