Files
MycLib/Src/Myc.Core.Futures.pas
T
Michael Schimmel a9aff8c41a AuraTrader Sample
2025-06-17 21:49:09 +02:00

149 lines
3.5 KiB
ObjectPascal

unit Myc.Core.Futures;
interface
uses
System.SysUtils,
Myc.Signals,
Myc.TaskManager,
Myc.Futures;
type
TMycFuture<T> = class abstract(TInterfacedObject, TFuture<T>.IFuture)
protected
function GetValue: T; virtual; abstract;
function GetDone: TState; virtual; abstract;
public
procedure BeforeDestruction; override;
end;
TMycNullFuture<T> = class(TMycFuture<T>)
protected
function GetValue: T; override;
function GetDone: TState; override;
end;
TMycGateFuncFuture<T> = class(TMycFuture<T>)
private
FInit: TSignal.TSubscription;
FDone: TLatch.ILatch;
FResult: T;
protected
function GetValue: T; override;
function GetDone: TState; override;
public
constructor Create(const ATaskManager: IMycTaskManager; const AGate: TState.IState; AProc: TFunc<T>);
destructor Destroy; override;
end;
TMycFutureManaged<T> = class(TMycFuture<T>)
private
FFuture: TFuture<T>.IFuture;
protected
function GetValue: T; override;
function GetDone: TState; override;
public
constructor Create(const AFuture: TFuture<T>.IFuture);
destructor Destroy; override;
end;
implementation
{ TMycFuture<T> }
procedure TMycFuture<T>.BeforeDestruction;
begin
inherited;
Assert(GetDone.IsSet, 'Trying to destroy an unfinished future');
end;
{ TMycNullFuture<T> }
function TMycNullFuture<T>.GetDone: TState;
begin
Result := TState.Null;
end;
function TMycNullFuture<T>.GetValue: T;
begin
Result := Default(T);
end;
{ TMycGateFuncFuture<T> }
constructor TMycGateFuncFuture<T>.Create(const ATaskManager: IMycTaskManager; const AGate: TState.IState; AProc: TFunc<T>);
begin
inherited Create;
FDone := TLatch.CreateLatch(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
);
end;
destructor TMycGateFuncFuture<T>.Destroy;
begin
FInit.Unsubscribe;
inherited Destroy;
end;
function TMycGateFuncFuture<T>.GetDone: TState;
begin
Result := FDone.State;
end;
function TMycGateFuncFuture<T>.GetValue: T;
begin
Assert(FDone.State.IsSet, 'Result is not yet available.');
Result := FResult;
end;
{ TMycFutureManaged<T> }
constructor TMycFutureManaged<T>.Create(const AFuture: TFuture<T>.IFuture);
begin
inherited Create;
FFuture := AFuture;
end;
destructor TMycFutureManaged<T>.Destroy;
begin
if GetTypeKind(T) = tkClass then
begin
var val := GetValue;
var obj := TObject(PPointer(@val)^);
if obj <> nil then
obj.Free;
end;
inherited;
end;
function TMycFutureManaged<T>.GetDone: TState;
begin
Result := FFuture.Done;
end;
function TMycFutureManaged<T>.GetValue: T;
begin
Result := FFuture.Value;
end;
end.