187 lines
4.8 KiB
ObjectPascal
187 lines
4.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;
|
|
TProcConst<S, TResult> = reference to function(const Arg1: S): TResult;
|
|
|
|
{$REGION 'private'}
|
|
strict private
|
|
class var
|
|
FNull: IFuture;
|
|
|
|
class constructor CreateClass;
|
|
private
|
|
FFuture: IFuture;
|
|
function GetDone: TState.IState; 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 Chain(const Proc: TProcConst<T, TState>): TState; overload;
|
|
function WaitFor: T;
|
|
|
|
property Done: TState.IState read GetDone;
|
|
property Value: T read GetValue;
|
|
end;
|
|
|
|
IObjectRef<T: class> = interface
|
|
function GetObj: T;
|
|
function Pop: T;
|
|
property Obj: T read GetObj;
|
|
end;
|
|
|
|
TObjectRef<T: class> = class(TInterfacedObject, IObjectRef<T>)
|
|
private
|
|
FObj: T;
|
|
function GetObj: T;
|
|
public
|
|
constructor Create(const ARef: T);
|
|
destructor Destroy; override;
|
|
function Pop: T;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
Myc.Core.Futures;
|
|
|
|
constructor TFuture<T>.Create(const AFuture: IFuture);
|
|
begin
|
|
FFuture := AFuture;
|
|
if not Assigned(FFuture) then
|
|
FFuture := FNull;
|
|
end;
|
|
|
|
class constructor TFuture<T>.CreateClass;
|
|
begin
|
|
FNull := TMycNullFuture<T>.Create;
|
|
end;
|
|
|
|
function TFuture<T>.Chain(const Proc: TProcConst<T, TState>): TState;
|
|
begin
|
|
var Done := TLatch.CreateLatch(1);
|
|
Result := Done.State;
|
|
|
|
var future := FFuture;
|
|
TaskManager.RunTask(future.Done, procedure begin Proc(future.Value).Subscribe(Done); end);
|
|
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.IState;
|
|
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;
|
|
|
|
constructor TObjectRef<T>.Create(const ARef: T);
|
|
begin
|
|
inherited Create;
|
|
FObj := ARef;
|
|
end;
|
|
|
|
destructor TObjectRef<T>.Destroy;
|
|
begin
|
|
FObj.Free;
|
|
inherited Destroy;
|
|
end;
|
|
|
|
function TObjectRef<T>.GetObj: T;
|
|
begin
|
|
Result := FObj;
|
|
end;
|
|
|
|
function TObjectRef<T>.Pop: T;
|
|
begin
|
|
Result := FObj;
|
|
FreeAndNil(FObj);
|
|
end;
|
|
|
|
end.
|