Code formatting
This commit is contained in:
+29
-25
@@ -4,22 +4,24 @@ interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
Myc.Signals, Myc.TaskManager, Myc.Futures;
|
||||
Myc.Signals,
|
||||
Myc.TaskManager,
|
||||
Myc.Futures;
|
||||
|
||||
type
|
||||
TMycFuture<T> = class abstract( TInterfacedObject, IMycFuture<T> )
|
||||
TMycFuture<T> = class abstract(TInterfacedObject, IMycFuture<T>)
|
||||
protected
|
||||
function GetResult: T; virtual; abstract;
|
||||
function GetDone: TState; virtual; abstract;
|
||||
end;
|
||||
|
||||
TMycNullFuture<T> = class( TMycFuture<T> )
|
||||
TMycNullFuture<T> = class(TMycFuture<T>)
|
||||
protected
|
||||
function GetResult: T; override;
|
||||
function GetDone: TState; override;
|
||||
end;
|
||||
|
||||
TMycGateFuncFuture<T> = class( TMycFuture<T> )
|
||||
TMycGateFuncFuture<T> = class(TMycFuture<T>)
|
||||
private
|
||||
FInit: TState.TSubscription;
|
||||
FDone: IMycLatch;
|
||||
@@ -28,7 +30,7 @@ type
|
||||
function GetResult: T; override;
|
||||
function GetDone: TState; override;
|
||||
public
|
||||
constructor Create( const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T> );
|
||||
constructor Create(const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T>);
|
||||
destructor Destroy; override;
|
||||
end;
|
||||
|
||||
@@ -43,39 +45,41 @@ end;
|
||||
|
||||
function TMycNullFuture<T>.GetResult: T;
|
||||
begin
|
||||
Result := Default ( T );
|
||||
Result := Default(T);
|
||||
end;
|
||||
|
||||
{ TMycGateFuncFuture<T> }
|
||||
|
||||
constructor TMycGateFuncFuture<T>.Create( const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T> );
|
||||
constructor TMycGateFuncFuture<T>.Create(const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T>);
|
||||
begin
|
||||
inherited Create;
|
||||
|
||||
FDone := TLatch.Construct( 1 );
|
||||
FDone := TLatch.Construct(1);
|
||||
|
||||
// Subscribe the job execution to AGate.
|
||||
// The job will run when AGate notifies the subscriber returned by Run.
|
||||
FInit := ATaskManager.CreateTask(
|
||||
AGate,
|
||||
procedure
|
||||
begin
|
||||
try
|
||||
try
|
||||
Self.FResult := AProc( );
|
||||
except
|
||||
Self.FResult := Default ( T ); // Set result to Default(T) on error
|
||||
raise; // Re-raise for TaskFactory to handle
|
||||
end;
|
||||
finally
|
||||
Self.FDone.Notify; // Signal that this future is done (successfully or with error)
|
||||
end;
|
||||
end );
|
||||
FInit :=
|
||||
ATaskManager.CreateTask(
|
||||
AGate,
|
||||
procedure
|
||||
begin
|
||||
try
|
||||
try
|
||||
Self.FResult := AProc();
|
||||
except
|
||||
Self.FResult := Default(T); // Set result to Default(T) on error
|
||||
raise; // Re-raise for TaskFactory to handle
|
||||
end;
|
||||
finally
|
||||
Self.FDone.Notify; // Signal that this future is done (successfully or with error)
|
||||
end;
|
||||
end
|
||||
);
|
||||
end;
|
||||
|
||||
destructor TMycGateFuncFuture<T>.Destroy;
|
||||
begin
|
||||
Assert( FDone.State.IsSet );
|
||||
Assert(FDone.State.IsSet);
|
||||
|
||||
FInit.Unsubscribe;
|
||||
inherited Destroy;
|
||||
@@ -88,7 +92,7 @@ end;
|
||||
|
||||
function TMycGateFuncFuture<T>.GetResult: T;
|
||||
begin
|
||||
Assert( FDone.State.IsSet, 'Result is not yet available.' );
|
||||
Assert(FDone.State.IsSet, 'Result is not yet available.');
|
||||
Result := FResult;
|
||||
end;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user