Test Refactoring of signals
This commit is contained in:
+116
-48
@@ -8,32 +8,38 @@ uses
|
||||
Myc.Signals;
|
||||
|
||||
type
|
||||
TMycState = class abstract(TInterfacedObject, IMycState)
|
||||
protected
|
||||
function GetIsSet: Boolean; virtual; abstract;
|
||||
public
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer; virtual; abstract;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); virtual; abstract;
|
||||
property IsSet: Boolean read GetIsSet;
|
||||
end;
|
||||
|
||||
// TMycNullState implements a "null object" pattern for IMycState.
|
||||
// This state is always considered "set".
|
||||
TMycNullState = class(TInterfacedObject, IMycState)
|
||||
protected
|
||||
// IMycState
|
||||
function GetIsSet: Boolean;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
public
|
||||
constructor Create;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
end;
|
||||
|
||||
// A state that acts as an event. It's state ist always set and it will always notify it's subscribers, if it gets notified by itself.
|
||||
TMycNotifyEvent = class(TInterfacedObject, IMycState, IMycEvent)
|
||||
strict private
|
||||
FSubscribers: TMycNotifyList<IMycSubscriber>;
|
||||
protected
|
||||
function GetIsSet: Boolean;
|
||||
function GetState: TState;
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
function Notify: Boolean;
|
||||
end;
|
||||
|
||||
// TMycLatch implements a countdown latch.
|
||||
// It is initialized with a count. Calls to its Notify method (as an IMycSubscriber)
|
||||
// decrement this count. When the count reaches zero, the latch transitions to the "set"
|
||||
// state (IsSet becomes true), notifies its current subscribers once, and then remains set.
|
||||
TMycLatch = class(TMycState, IMycLatch)
|
||||
TMycLatch = class(TInterfacedObject, IMycState, IMycEvent)
|
||||
strict private
|
||||
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers waiting for this latch to be set.
|
||||
private
|
||||
@@ -41,7 +47,7 @@ type
|
||||
FCount: Integer; // The internal countdown value for the latch.
|
||||
function GetState: TState; // Implementation for IMycLatch.GetState and IMycFlag.GetState.
|
||||
protected
|
||||
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet.
|
||||
function GetIsSet: Boolean;
|
||||
public
|
||||
// Creates the latch with an initial count.
|
||||
// If ACount is 0 or less, the latch is effectively pre-set (delegates to FNull via CreateLatch factory).
|
||||
@@ -49,8 +55,8 @@ type
|
||||
destructor Destroy; override;
|
||||
|
||||
// IMycSignal implementation
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer; override;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); override;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
|
||||
// IMycSubscriber implementation for TMycLatch itself.
|
||||
// Decrements the internal count. If the count reaches zero,
|
||||
@@ -58,16 +64,16 @@ type
|
||||
function Notify: Boolean;
|
||||
end;
|
||||
|
||||
TMycNullLatch = class(TInterfacedObject, IMycLatch)
|
||||
TMycNullEvent = class(TInterfacedObject, IMycEvent)
|
||||
private
|
||||
function GetState: TState;
|
||||
function Notify: Boolean;
|
||||
end;
|
||||
|
||||
// TMycDirty implements a resettable "dirty flag".
|
||||
// TMycFlag implements a resettable "dirty flag".
|
||||
// It can be explicitly set to dirty (via Notify) or reset to clean (via Reset).
|
||||
// Subscribers are notified of relevant state changes.
|
||||
TMycDirty = class(TMycState, IMycDirty)
|
||||
TMycFlag = class(TInterfacedObject, IMycState, IMycFlag)
|
||||
strict private
|
||||
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers interested in state changes of this dirty flag.
|
||||
private
|
||||
@@ -75,27 +81,27 @@ type
|
||||
FFlag: Boolean; // Internal state: true if dirty/set, false if clean/reset.
|
||||
function GetState: TState;
|
||||
protected
|
||||
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet (true if dirty).
|
||||
function GetIsSet: Boolean;
|
||||
public
|
||||
// Creates a new dirty flag, initially set to dirty (true).
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
|
||||
// Factory method to create a new TMycDirty instance.
|
||||
class function CreateDirty: IMycDirty; static;
|
||||
// Factory method to create a new TMycFlag instance.
|
||||
class function CreateDirty: IMycFlag; static;
|
||||
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer; override;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); override;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
|
||||
// IMycSubscriber implementation for TMycDirty.
|
||||
// IMycSubscriber implementation for TMycFlag.
|
||||
// Sets this flag to dirty (true). Notifies subscribers if it transitioned from clean to dirty.
|
||||
function Notify: Boolean;
|
||||
|
||||
// IMycDirty implementation. Resets the flag to clean (false).
|
||||
// IMycFlag implementation. Resets the flag to clean (false).
|
||||
function Reset: Boolean;
|
||||
end;
|
||||
|
||||
TMycNullDirty = class(TInterfacedObject, IMycDirty)
|
||||
TMycNullFlag = class(TInterfacedObject, IMycFlag)
|
||||
private
|
||||
function GetState: TState;
|
||||
function Notify: Boolean;
|
||||
@@ -135,14 +141,76 @@ begin
|
||||
// Unsubscribing from a null state has no effect.
|
||||
end;
|
||||
|
||||
{ TMycNullLatch }
|
||||
{ TMycNotifyEvent }
|
||||
|
||||
function TMycNullLatch.GetState: TState;
|
||||
constructor TMycNotifyEvent.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
FSubscribers.Create;
|
||||
end;
|
||||
|
||||
destructor TMycNotifyEvent.Destroy;
|
||||
begin
|
||||
FSubscribers.Destroy;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TMycNotifyEvent.GetIsSet: Boolean;
|
||||
begin
|
||||
Result := true;
|
||||
end;
|
||||
|
||||
function TMycNotifyEvent.GetState: TState;
|
||||
begin
|
||||
Result := Self;
|
||||
end;
|
||||
|
||||
function TMycNotifyEvent.Notify: Boolean;
|
||||
begin
|
||||
FSubscribers.Lock;
|
||||
try
|
||||
FSubscribers.Notify(function(Subscriber: IMycSubscriber): Boolean begin Result := Subscriber.Notify; end);
|
||||
finally
|
||||
FSubscribers.Release;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TMycNotifyEvent.Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
begin
|
||||
Result := nil;
|
||||
if not Assigned(Subscriber) then
|
||||
exit;
|
||||
|
||||
FSubscribers.Lock;
|
||||
try
|
||||
Subscriber.Notify;
|
||||
Result := FSubscribers.Advise(Subscriber);
|
||||
finally
|
||||
FSubscribers.Release;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TMycNotifyEvent.Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
begin
|
||||
if Tag = nil then
|
||||
exit;
|
||||
|
||||
FSubscribers.Lock;
|
||||
try
|
||||
FSubscribers.Unadvise(Tag);
|
||||
finally
|
||||
FSubscribers.Release;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TMycNullEvent }
|
||||
|
||||
function TMycNullEvent.GetState: TState;
|
||||
begin
|
||||
Result := TState.Null;
|
||||
end;
|
||||
|
||||
function TMycNullLatch.Notify: Boolean;
|
||||
function TMycNullEvent.Notify: Boolean;
|
||||
begin
|
||||
Result := false;
|
||||
end;
|
||||
@@ -246,63 +314,63 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TMycNullDirty }
|
||||
{ TMycNullFlag }
|
||||
|
||||
function TMycNullDirty.GetState: TState;
|
||||
function TMycNullFlag.GetState: TState;
|
||||
begin
|
||||
Result := TState.Null;
|
||||
end;
|
||||
|
||||
function TMycNullDirty.Notify: Boolean;
|
||||
function TMycNullFlag.Notify: Boolean;
|
||||
begin
|
||||
Result := false;
|
||||
end;
|
||||
|
||||
function TMycNullDirty.Reset: Boolean;
|
||||
function TMycNullFlag.Reset: Boolean;
|
||||
begin
|
||||
Result := true;
|
||||
end;
|
||||
|
||||
{ TMycDirty }
|
||||
{ TMycFlag }
|
||||
|
||||
constructor TMycDirty.Create;
|
||||
constructor TMycFlag.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
FFlag := true; // A new dirty flag is initially considered dirty.
|
||||
FSubscribers.Create;
|
||||
end;
|
||||
|
||||
class function TMycDirty.CreateDirty: IMycDirty;
|
||||
class function TMycFlag.CreateDirty: IMycFlag;
|
||||
begin
|
||||
// Factory method to create a new TMycDirty instance.
|
||||
Result := TMycDirty.Create;
|
||||
// Factory method to create a new TMycFlag instance.
|
||||
Result := TMycFlag.Create;
|
||||
end;
|
||||
|
||||
destructor TMycDirty.Destroy;
|
||||
destructor TMycFlag.Destroy;
|
||||
begin
|
||||
FSubscribers.Destroy; // Clean up the subscriber list.
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TMycDirty.Reset: Boolean;
|
||||
function TMycFlag.Reset: Boolean;
|
||||
begin
|
||||
// Sets the flag to false (clean) and returns true if it was previously true (dirty).
|
||||
Result := TInterlocked.Exchange(FFlag, false);
|
||||
end;
|
||||
|
||||
function TMycDirty.GetIsSet: Boolean;
|
||||
function TMycFlag.GetIsSet: Boolean;
|
||||
begin
|
||||
// IsSet is true if the flag is dirty (FFlag = true).
|
||||
Result := FFlag;
|
||||
end;
|
||||
|
||||
function TMycDirty.GetState: TState;
|
||||
function TMycFlag.GetState: TState;
|
||||
begin
|
||||
// TMycDirty itself implements IMycState.
|
||||
// TMycFlag itself implements IMycState.
|
||||
Result := Self;
|
||||
end;
|
||||
|
||||
function TMycDirty.Notify: Boolean;
|
||||
function TMycFlag.Notify: Boolean;
|
||||
var
|
||||
wasPreviouslyClean: Boolean;
|
||||
begin
|
||||
@@ -316,7 +384,7 @@ begin
|
||||
FSubscribers.Notify(function(Subscriber: IMycSubscriber): Boolean begin Result := Subscriber.Notify; end);
|
||||
end;
|
||||
// The return value of this Notify (as an IMycSubscriber) indicates if this
|
||||
// TMycDirty instance itself would want more notifications if it were subscribed to something.
|
||||
// TMycFlag instance itself would want more notifications if it were subscribed to something.
|
||||
// Here, it reflects whether a state change to dirty occurred due to this call.
|
||||
Result := wasPreviouslyClean;
|
||||
finally
|
||||
@@ -324,12 +392,12 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TMycDirty.Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
function TMycFlag.Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
begin
|
||||
if not Assigned(Subscriber) then
|
||||
exit(nil);
|
||||
|
||||
// For TMycDirty, we always add the subscriber and then check if we need to notify immediately.
|
||||
// For TMycFlag, we always add the subscriber and then check if we need to notify immediately.
|
||||
// Ref counting for the subscriber interface is assumed to be handled by TMycNotifyList.Advise/Unadvise.
|
||||
// Or, if explicit management is needed like in TMycLatch, it should be added.
|
||||
// For consistency with TMycLatch, let's add AddRef/Release here too.
|
||||
@@ -346,7 +414,7 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TMycDirty.Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
procedure TMycFlag.Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
begin
|
||||
if Tag = nil then
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user