Implemented TLazy + Tests
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
unit Myc.Core.Lazy;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
Myc.Signals,
|
||||
Myc.Lazy;
|
||||
|
||||
type
|
||||
TMycLazy<T> = class abstract( TInterfacedObject, IMycLazy<T> )
|
||||
protected
|
||||
function GetChanged: IMycState; virtual; abstract;
|
||||
public
|
||||
function Pop( out Res: T ): Boolean; virtual; abstract;
|
||||
end;
|
||||
|
||||
TMycNullLazy<T> = class( TMycLazy<T> )
|
||||
protected
|
||||
function GetChanged: IMycState; override;
|
||||
public
|
||||
function Pop( out Res: T ): Boolean; override;
|
||||
end;
|
||||
|
||||
TMycFuncLazy<T> = class( TMycLazy<T> )
|
||||
private
|
||||
FChanged: IMycDirty;
|
||||
FChangeState: TMycSubscription;
|
||||
FProc: TFunc<T>;
|
||||
protected
|
||||
function GetChanged: IMycState; override;
|
||||
public
|
||||
constructor Create( const AChanged: IMycState; const AProc: TFunc<T> );
|
||||
destructor Destroy; override;
|
||||
function Pop( out Res: T ): Boolean; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TMycNullLazy<T> }
|
||||
|
||||
function TMycNullLazy<T>.GetChanged: IMycState;
|
||||
begin
|
||||
Result := TState.Null;
|
||||
end;
|
||||
|
||||
function TMycNullLazy<T>.Pop( out Res: T ): Boolean;
|
||||
begin
|
||||
Res := Default ( T );
|
||||
Result := true;
|
||||
end;
|
||||
|
||||
{ TMycFuncLazy<T> }
|
||||
|
||||
constructor TMycFuncLazy<T>.Create( const AChanged: IMycState; const AProc: TFunc<T> );
|
||||
begin
|
||||
inherited Create;
|
||||
FChanged := TDirty.Construct;
|
||||
FProc := AProc;
|
||||
FChangeState := AChanged.Subscribe( FChanged );
|
||||
end;
|
||||
|
||||
destructor TMycFuncLazy<T>.Destroy;
|
||||
begin
|
||||
FChangeState.Unsubscribe;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TMycFuncLazy<T>.GetChanged: IMycState;
|
||||
begin
|
||||
Result := FChanged.State;
|
||||
end;
|
||||
|
||||
function TMycFuncLazy<T>.Pop( out Res: T ): Boolean;
|
||||
begin
|
||||
Result := FChanged.State.IsSet;
|
||||
if Result then
|
||||
begin
|
||||
if Assigned( FProc ) then
|
||||
Res := FProc;
|
||||
FChanged.Reset;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
Reference in New Issue
Block a user