unit Myc.Futures; interface uses System.SysUtils, Myc.Signals, Myc.TaskManager; type // Represents the eventual result of an asynchronous operation. IMycFuture = interface {$REGION 'property access'} function GetResult: T; function GetDone: IMycState; {$ENDREGION} // Provides access to the computed result. property Result: T read GetResult; // Provides access to the completion state. property Done: IMycState read GetDone; end; Future = record strict private FFuture: IMycFuture; function GetDone: IMycState; inline; function GetResult: T; inline; class constructor CreateClass; class destructor DestroyClass; public constructor Create(const AFuture: IMycFuture); class operator Implicit(const A: IMycFuture): Future; overload; class operator Implicit(const A: Future): IMycFuture; overload; class function Construct(const Proc: TFunc): IMycFuture; overload; static; class function Construct(const Gate: IMycState; const Proc: TFunc): IMycFuture; overload; static; function Chain(const Proc: TFunc): IMycFuture; function WaitFor: T; property Done: IMycState read GetDone; property Result: T read GetResult; end; implementation uses Myc.Core.Futures, Myc.Core.Tasks; constructor Future.Create(const AFuture: IMycFuture); begin FFuture := AFuture; end; class constructor Future.CreateClass; begin TMycTaskFactory.AquireTaskManager; end; class destructor Future.DestroyClass; begin TMycTaskFactory.ReleaseTaskManager; end; function Future.Chain(const Proc: TFunc): IMycFuture; begin var Cap := FFuture; Result := Future.Construct( FFuture.Done, function: S begin Result := Proc( Cap.Result ); end ); end; class function Future.Construct(const Proc: TFunc): IMycFuture; begin Result := TMycInitStateFuncFuture.Create( TaskManager, nil, Proc ); end; class function Future.Construct(const Gate: IMycState; const Proc: TFunc): IMycFuture; begin Result := TMycInitStateFuncFuture.Create( TaskManager, Gate, Proc ); end; function Future.GetDone: IMycState; begin Result := FFuture.Done; end; function Future.GetResult: T; begin Result := FFuture.Result; end; function Future.WaitFor: T; begin TaskManager.WaitFor( FFuture.Done ); Result := FFuture.Result; end; class operator Future.Implicit(const A: IMycFuture): Future; begin Result.Create( A ); end; class operator Future.Implicit(const A: Future): IMycFuture; begin Result := A.FFuture; end; end.