Files
MycLib/Src/Myc.Lazy.pas
T
2025-06-05 23:27:48 +02:00

95 lines
2.2 KiB
ObjectPascal

unit Myc.Lazy;
interface
uses
System.SysUtils,
Myc.Signals;
type
IMycMutable<T> = interface
function GetChanged: TState;
function GetValue: T;
property Changed: TState read GetChanged;
property Value: T read GetValue;
end;
IMycLazy<T> = interface
{$REGION 'property access'}
function GetChanged: TState;
{$ENDREGION}
function Pop(out Res: T): Boolean;
property Changed: TState read GetChanged;
end;
TLazy<T> = record
private
FLazy: IMycLazy<T>;
function GetChanged: TState.IState; inline;
class var
FNull: IMycLazy<T>;
class constructor CreateClass;
public
constructor Create(const ALazy: IMycLazy<T>);
class function Construct(const Changing: TState.IState; const Proc: TFunc<T>): IMycLazy<T>; overload; static;
class function Construct(const Value: IMycLazy<T>): IMycLazy<T>; overload; static;
class operator Implicit(const A: IMycLazy<T>): TLazy<T>; overload;
class operator Implicit(const A: TLazy<T>): IMycLazy<T>; overload;
function Pop(out Res: T): Boolean; inline;
property Changed: TState.IState read GetChanged;
end;
implementation
uses
Myc.Core.Lazy;
constructor TLazy<T>.Create(const ALazy: IMycLazy<T>);
begin
FLazy := ALazy;
if not Assigned(FLazy) then
FLazy := FNull;
end;
class function TLazy<T>.Construct(const Changing: TState.IState; const Proc: TFunc<T>): IMycLazy<T>;
begin
Result := TMycFuncLazy<T>.Create(Changing, Proc);
end;
class constructor TLazy<T>.CreateClass;
begin
FNull := TMycNullLazy<T>.Create;
end;
class function TLazy<T>.Construct(const Value: IMycLazy<T>): IMycLazy<T>;
begin
Result := TMycChainedLazy<T>.Create(Value);
end;
function TLazy<T>.GetChanged: TState.IState;
begin
Result := FLazy.Changed;
end;
function TLazy<T>.Pop(out Res: T): Boolean;
begin
Result := FLazy.Pop(Res);
end;
class operator TLazy<T>.Implicit(const A: IMycLazy<T>): TLazy<T>;
begin
Result.Create(A);
end;
class operator TLazy<T>.Implicit(const A: TLazy<T>): IMycLazy<T>;
begin
Result := A.FLazy;
end;
end.