Implemented TLazy + Tests

This commit is contained in:
Michael Schimmel
2025-06-03 17:10:55 +02:00
parent 38c9e2830d
commit 254eb7b114
18 changed files with 1329 additions and 149 deletions
+25 -10
View File
@@ -7,15 +7,19 @@ uses
Myc.Signals, Myc.TaskManager, Myc.Futures;
type
// Abstract base class for IMycFuture<T> implementations.
TMycFuture<T> = class abstract( TInterfacedObject, IMycFuture<T> )
protected
function GetResult: T; virtual; abstract;
function GetDone: IMycState; virtual; abstract;
end;
// Concrete future implementation triggered by an initial state, executing a TFunc<T>.
TMycInitStateFuncFuture<T> = class( TMycFuture<T> )
TMycNullFuture<T> = class( TMycFuture<T> )
protected
function GetResult: T; override;
function GetDone: IMycState; override;
end;
TMycGateFuncFuture<T> = class(TMycFuture<T>)
private
FInit: TMycSubscription;
FDone: IMycLatch;
@@ -24,20 +28,31 @@ type
function GetResult: T; override;
function GetDone: IMycState; override;
public
// Creates a future that executes AProc after AGate is set, using ATaskFactory.
constructor Create( const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T> );
destructor Destroy; override;
end;
implementation
{ TMycInitStateFuncFuture<T> }
{ TMycNullFuture<T> }
constructor TMycInitStateFuncFuture<T>.Create( const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T> );
function TMycNullFuture<T>.GetDone: IMycState;
begin
Result := TState.Null;
end;
function TMycNullFuture<T>.GetResult: T;
begin
Result := Default(T);
end;
{ TMycGateFuncFuture<T> }
constructor TMycGateFuncFuture<T>.Create(const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T>);
begin
inherited Create;
FDone := TState.CreateLatch( 1 );
FDone := TLatch.Construct( 1 );
// Subscribe the job execution to AGate.
// The job will run when AGate notifies the subscriber returned by Run.
@@ -58,7 +73,7 @@ begin
end );
end;
destructor TMycInitStateFuncFuture<T>.Destroy;
destructor TMycGateFuncFuture<T>.Destroy;
begin
Assert( FDone.State.IsSet );
@@ -66,12 +81,12 @@ begin
inherited Destroy;
end;
function TMycInitStateFuncFuture<T>.GetDone: IMycState;
function TMycGateFuncFuture<T>.GetDone: IMycState;
begin
Result := FDone.State;
end;
function TMycInitStateFuncFuture<T>.GetResult: T;
function TMycGateFuncFuture<T>.GetResult: T;
begin
Assert( FDone.State.IsSet, 'Result is not yet available.' );
Result := FResult;