Implemented TLazy + Tests
This commit is contained in:
+87
-24
@@ -24,7 +24,7 @@ type
|
||||
// Unsubscribes from the associated TState.
|
||||
procedure Unsubscribe;
|
||||
class operator Initialize( out Dest: TMycSubscription );
|
||||
property TState: IMycState read GetState;
|
||||
property State: IMycState read GetState;
|
||||
end;
|
||||
|
||||
IMycState = interface
|
||||
@@ -39,6 +39,30 @@ type
|
||||
property IsSet: Boolean read GetIsSet;
|
||||
end;
|
||||
|
||||
TState = record // helper for IMycState
|
||||
strict private
|
||||
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;
|
||||
|
||||
class function All( const States: TArray<IMycState> ): IMycState; static;
|
||||
class function Any( const States: TArray<IMycState> ): IMycState; static;
|
||||
|
||||
class property Null: IMycState read FNull;
|
||||
|
||||
function Subscribe(Subscriber: IMycSubscriber): TMycSubscription; inline;
|
||||
procedure Unsubscribe(Tag: Pointer); inline;
|
||||
|
||||
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 )
|
||||
@@ -48,6 +72,16 @@ type
|
||||
// Inherits IMycSubscriber and State property from IMycFlag. No new members.
|
||||
end;
|
||||
|
||||
TLatch = record
|
||||
strict private
|
||||
class var
|
||||
FNull: IMycLatch;
|
||||
class constructor ClassCreate;
|
||||
public
|
||||
class function Construct(Count: Integer): IMycLatch; static;
|
||||
class property Null: IMycLatch read FNull;
|
||||
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 )
|
||||
@@ -59,19 +93,14 @@ type
|
||||
property State: IMycState read GetState;
|
||||
end;
|
||||
|
||||
TState = record
|
||||
private
|
||||
FState: IMycState;
|
||||
class function GetNull: IMycState; static;
|
||||
TDirty = record
|
||||
strict private
|
||||
class var
|
||||
FNull: IMycDirty;
|
||||
class constructor ClassCreate;
|
||||
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;
|
||||
class function Construct: IMycDirty; static;
|
||||
class property Null: IMycDirty read FNull;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -92,7 +121,7 @@ end;
|
||||
procedure TMycSubscription.Unsubscribe;
|
||||
begin
|
||||
FState.Unsubscribe( FTag );
|
||||
FState := TMycState.Null;
|
||||
FState := TState.Null;
|
||||
FTag := nil;
|
||||
end;
|
||||
|
||||
@@ -103,7 +132,7 @@ end;
|
||||
|
||||
class operator TMycSubscription.Initialize( out Dest: TMycSubscription );
|
||||
begin
|
||||
Dest.FState := TMycState.Null;
|
||||
Dest.FState := TState.Null;
|
||||
Dest.FTag := nil;
|
||||
end;
|
||||
|
||||
@@ -113,19 +142,20 @@ constructor TState.Create(const AState: IMycState);
|
||||
begin
|
||||
FState := AState;
|
||||
if not Assigned(FState) then
|
||||
FState := TMycState.Null;
|
||||
FState := FNull;
|
||||
end;
|
||||
|
||||
class function TState.CreateLatch( Count: Integer ): IMycLatch;
|
||||
class constructor TState.ClassCreate;
|
||||
begin
|
||||
Result := TMycLatch.CreateLatch( Count );
|
||||
// Create a singleton null latch instance that is initially (and always) set.
|
||||
FNull := TMycNullState.Create(); // Calls the instance constructor
|
||||
end;
|
||||
|
||||
class function TState.All( const States: TArray<IMycState> ): IMycState;
|
||||
var
|
||||
Latch: IMycLatch;
|
||||
begin
|
||||
Latch := CreateLatch( Length( States ) );
|
||||
Latch := TLatch.Construct( Length( States ) );
|
||||
for var i := 0 to High( States ) do
|
||||
States[i].Subscribe( Latch );
|
||||
Result := Latch.State;
|
||||
@@ -141,21 +171,26 @@ begin
|
||||
end
|
||||
else
|
||||
begin
|
||||
Latch := CreateLatch( 1 );
|
||||
Latch := TLatch.Construct( 1 );
|
||||
for var i := 0 to High( States ) do
|
||||
States[i].Subscribe( Latch );
|
||||
Result := Latch.State;
|
||||
end;
|
||||
end;
|
||||
|
||||
class function TState.CreateDirty: IMycDirty;
|
||||
function TState.GetIsSet: Boolean;
|
||||
begin
|
||||
Result := TMycDirty.CreateDirty;
|
||||
Result := FState.IsSet;
|
||||
end;
|
||||
|
||||
class function TState.GetNull: IMycState;
|
||||
function TState.Subscribe(Subscriber: IMycSubscriber): TMycSubscription;
|
||||
begin
|
||||
Result := TMycState.Null;
|
||||
Result := FState.Subscribe(Subscriber);
|
||||
end;
|
||||
|
||||
procedure TState.Unsubscribe(Tag: Pointer);
|
||||
begin
|
||||
FState.Unsubscribe(Tag);
|
||||
end;
|
||||
|
||||
class operator TState.Implicit(const A: TState): IMycState;
|
||||
@@ -168,4 +203,32 @@ begin
|
||||
Result.Create( A );
|
||||
end;
|
||||
|
||||
{ TLatch }
|
||||
|
||||
class constructor TLatch.ClassCreate;
|
||||
begin
|
||||
// Create a singleton null latch instance that is initially (and always) set.
|
||||
FNull := TMycNullLatch.Create; // Calls the instance constructor
|
||||
end;
|
||||
|
||||
class function TLatch.Construct(Count: Integer): IMycLatch;
|
||||
begin
|
||||
if Count > 0 then
|
||||
Result := TMycLatch.Create( Count )
|
||||
else
|
||||
Result := FNull;
|
||||
end;
|
||||
|
||||
{ TDirty }
|
||||
|
||||
class constructor TDirty.ClassCreate;
|
||||
begin
|
||||
FNull := TMycNullDirty.Create;
|
||||
end;
|
||||
|
||||
class function TDirty.Construct: IMycDirty;
|
||||
begin
|
||||
Result := TMycDirty.Create;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user