146 lines
3.8 KiB
ObjectPascal
146 lines
3.8 KiB
ObjectPascal
unit Myc.Futures;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
Myc.Signals,
|
|
Myc.TaskManager;
|
|
|
|
type
|
|
// Represents the eventual Value of an asynchronous operation.
|
|
TFuture<T> = record
|
|
type
|
|
IFuture = interface
|
|
{$REGION 'property access'}
|
|
function GetValue: T;
|
|
function GetDone: TState;
|
|
{$ENDREGION}
|
|
// Provides access to the computed Value.
|
|
property Value: T read GetValue;
|
|
// Provides access to the completion state.
|
|
property Done: TState read GetDone;
|
|
end;
|
|
|
|
TFuncConst<S, TResult> = reference to function(const Arg1: S): TResult;
|
|
|
|
{$REGION 'private'}
|
|
strict private
|
|
class var
|
|
FNull: IFuture;
|
|
|
|
class constructor CreateClass;
|
|
class destructor DestroyClass;
|
|
|
|
private
|
|
FFuture: IFuture;
|
|
function GetDone: TState; inline;
|
|
function GetValue: T; inline;
|
|
{$ENDREGION}
|
|
public
|
|
constructor Create(const AFuture: IFuture);
|
|
|
|
class operator Initialize(out Dest: TFuture<T>);
|
|
class operator Implicit(const A: IFuture): TFuture<T>; overload;
|
|
class operator Implicit(const A: TFuture<T>): IFuture; overload;
|
|
|
|
class function Construct(const Proc: TFunc<T>): TFuture<T>; overload; static;
|
|
class function Construct(const Gate: TState.IState; const Proc: TFunc<T>): TFuture<T>; overload; static;
|
|
|
|
class property Null: IFuture read FNull;
|
|
|
|
procedure Manage;
|
|
|
|
function Chain<S>(const Proc: TFunc<T, S>): TFuture<S>; overload;
|
|
function Chain<S>(const Proc: TFuncConst<T, S>): TFuture<S>; overload;
|
|
function WaitFor: T;
|
|
|
|
property Done: TState read GetDone;
|
|
property Value: T read GetValue;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
Myc.Core.Futures,
|
|
Myc.Core.Tasks;
|
|
|
|
constructor TFuture<T>.Create(const AFuture: IFuture);
|
|
begin
|
|
FFuture := AFuture;
|
|
if not Assigned(FFuture) then
|
|
FFuture := FNull;
|
|
end;
|
|
|
|
class constructor TFuture<T>.CreateClass;
|
|
begin
|
|
TMycTaskFactory.AquireTaskManager;
|
|
FNull := TMycNullFuture<T>.Create;
|
|
end;
|
|
|
|
class destructor TFuture<T>.DestroyClass;
|
|
begin
|
|
TMycTaskFactory.ReleaseTaskManager;
|
|
end;
|
|
|
|
function TFuture<T>.Chain<S>(const Proc: TFunc<T, S>): TFuture<S>;
|
|
begin
|
|
var Cap := FFuture;
|
|
Result := TFuture<S>.Construct(FFuture.Done, function: S begin Result := Proc(Cap.Value); end);
|
|
end;
|
|
|
|
function TFuture<T>.Chain<S>(const Proc: TFuncConst<T, S>): TFuture<S>;
|
|
begin
|
|
var Cap := FFuture;
|
|
Result := TFuture<S>.Construct(FFuture.Done, function: S begin Result := Proc(Cap.Value); end);
|
|
end;
|
|
|
|
class function TFuture<T>.Construct(const Proc: TFunc<T>): TFuture<T>;
|
|
begin
|
|
Result := TMycGateFuncFuture<T>.Create(TaskManager, nil, Proc);
|
|
end;
|
|
|
|
class function TFuture<T>.Construct(const Gate: TState.IState; const Proc: TFunc<T>): TFuture<T>;
|
|
begin
|
|
Result := TMycGateFuncFuture<T>.Create(TaskManager, Gate, Proc);
|
|
end;
|
|
|
|
function TFuture<T>.GetDone: TState;
|
|
begin
|
|
Result := FFuture.Done;
|
|
end;
|
|
|
|
function TFuture<T>.GetValue: T;
|
|
begin
|
|
Result := FFuture.Value;
|
|
end;
|
|
|
|
procedure TFuture<T>.Manage;
|
|
begin
|
|
if GetTypeKind(T) = tkClass then
|
|
FFuture := TMycFutureManaged<T>.Create(FFuture);
|
|
end;
|
|
|
|
function TFuture<T>.WaitFor: T;
|
|
begin
|
|
TaskManager.WaitFor(FFuture.Done);
|
|
Result := FFuture.Value;
|
|
end;
|
|
|
|
class operator TFuture<T>.Implicit(const A: IFuture): TFuture<T>;
|
|
begin
|
|
Result.Create(A);
|
|
end;
|
|
|
|
class operator TFuture<T>.Implicit(const A: TFuture<T>): IFuture;
|
|
begin
|
|
Result := A.FFuture;
|
|
end;
|
|
|
|
class operator TFuture<T>.Initialize(out Dest: TFuture<T>);
|
|
begin
|
|
Dest.FFuture := FNull;
|
|
end;
|
|
|
|
end.
|