Flow = record
public
class function _Case<T>(AValue: T): ICaseOf<T>; static;
class function _CaseEx<T>(Func: TFunc<T>): ICaseOf<T>; static;
end;
implementation
uses
Generics.Defaults;
procedure ObjHelper.Lock(Proc: TProc);
begin
Lock(Self, Proc);
end;
class procedure ObjHelper.Lock(O: TObject; Proc: TProc);
begin
TMonitor.Enter(O);
try
Proc();
finally
TMonitor.Exit(O);
end;
end;
procedure ObjHelper.Using<T>(Proc: TProc<T>);
begin
Using<T>(Self, Proc);
end;
class procedure ObjHelper.Using<T>(O: T; Proc: TProc<T>);
begin
try
Proc(O);
finally
O.Free;
end;
end;
{ TLifetimeWatcher }
constructor TLifetimeWatcher.Create(const AWhenDone: TProc);
begin
FWhenDone := AWhenDone;
end;
destructor TLifetimeWatcher.Destroy;
begin
if Assigned(FWhenDone) then
FWhenDone;
inherited;
end;
{ TSmartPointer<T> }
constructor TSmartPointer<T>.Create(const AValue: T);
begin
FValue := AValue;
FLifetime := TLifetimeWatcher.Create(procedure
begin
AValue.Free;
end);
end;
class operator TSmartPointer<T>.Implicit(const AValue: T): TSmartPointer<T>;
begin
Result := TSmartPointer<T>.Create(AValue);
end;
{ TBlockResultImpl }
function TBlockResultImpl.GetBlockResult: TBlockResult;
begin
Result := FBlockResult;
end;
procedure TBlockResultImpl.SetBlockResult(Value: TBlockResult);
begin
FBlockResult := Value;
end;
{ Flow }
class function Flow._Case<T>(AValue: T): ICaseOf<T>;
begin
Result := TCaseOf<T>.Create(AValue);
end;
class function Flow._CaseEx<T>(Func: TFunc<T>): ICaseOf<T>;
var
AValue: T;
begin
if Assigned(Func) then
AValue := Func()
else
AValue := Default(T);
Result := TCaseOf<T>.Create(AValue);
end;
procedure TCaseOf<T>._Else(Proc: TProc<T>);
begin
if (not FMatched) and Assigned(Proc) then
begin
FMatched := True;
Proc(FValue);
end;
end;
function TCaseOf<T>._Of(AValue: T; Proc: TProc<T>): ICaseOf<T>;
begin
Result := Self;
if (not FMatched) and Assigned(Proc) and
(TComparer<T>.Default.Compare(AValue, FValue) = 0) then
begin
FMatched := True;
Proc(FValue);
end;
end;