77 lines
2.0 KiB
ObjectPascal
77 lines
2.0 KiB
ObjectPascal
unit Myc.Futures;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
Myc.Signals, Myc.TaskManager;
|
|
|
|
type
|
|
// Represents the eventual result of an asynchronous operation.
|
|
IMycFuture<T> = 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<T> = record
|
|
strict private
|
|
FFuture: IMycFuture<T>;
|
|
class constructor CreateClass;
|
|
class destructor DestroyClass;
|
|
constructor Create(const AFuture: IMycFuture<T>);
|
|
public
|
|
class function Construct(const Proc: TFunc<T>): Future<T>; overload; static;
|
|
class function Construct(const Gate: IMycState; const Proc: TFunc<T>): Future<T>; overload; static;
|
|
|
|
function Chain<S>(const Proc: TFunc<T, S>): Future<S>;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
Myc.Core.Futures, Myc.Core.Tasks;
|
|
|
|
constructor Future<T>.Create(const AFuture: IMycFuture<T>);
|
|
begin
|
|
FFuture := AFuture;
|
|
end;
|
|
|
|
class constructor Future<T>.CreateClass;
|
|
begin
|
|
TMycTaskFactory.AquireTaskManager;
|
|
end;
|
|
|
|
class destructor Future<T>.DestroyClass;
|
|
begin
|
|
TMycTaskFactory.ReleaseTaskManager;
|
|
end;
|
|
|
|
function Future<T>.Chain<S>(const Proc: TFunc<T, S>): Future<S>;
|
|
begin
|
|
var Cap := FFuture;
|
|
|
|
Result := Future<S>.Construct( FFuture.Done,
|
|
function: S
|
|
begin
|
|
Result := Proc( Cap.Result );
|
|
end );
|
|
end;
|
|
|
|
class function Future<T>.Construct(const Proc: TFunc<T>): Future<T>;
|
|
begin
|
|
Result.Create( TMycInitStateFuncFuture<T>.Create( TaskManager, nil, Proc ) );
|
|
end;
|
|
|
|
class function Future<T>.Construct(const Gate: IMycState; const Proc: TFunc<T>): Future<T>;
|
|
begin
|
|
Result.Create( TMycInitStateFuncFuture<T>.Create( TaskManager, Gate, Proc ) );
|
|
end;
|
|
|
|
end.
|