This commit is contained in:
Michael Schimmel
2025-06-06 01:22:25 +02:00
parent 73d656cda5
commit c4d8607d17
13 changed files with 393 additions and 173 deletions
+118 -28
View File
@@ -7,41 +7,80 @@ uses
Myc.Signals;
type
IMycMutable<T> = interface
function GetChanged: TState;
function GetValue: T;
property Changed: TState read GetChanged;
property Value: T read GetValue;
end;
TMutable<T> = record
type
IMutable = interface
{$REGION 'property access'}
function GetChanged: TSignal;
function GetValue: T;
{$ENDREGION}
property Changed: TSignal 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;
IWriteableMutable = interface(IMutable)
procedure SetValue(const Value: T);
end;
{$REGION 'private'}
strict private
class var
FNull: IMutable;
class constructor CreateClass;
private
FMutable: IMutable;
function GetChanged: TSignal; inline;
{$ENDREGION}
public
constructor Create(const AMutable: IMutable);
class operator Initialize(out Dest: TMutable<T>);
class operator Implicit(const A: IMutable): TMutable<T>; overload;
class operator Implicit(const A: TMutable<T>): IMutable; overload;
class property Null: IMutable read FNull;
class function Construct(const Changing: TSignal; const Proc: TFunc<T>): TMutable<T>; overload; static;
class function CreateWriteable(const Init: T): IWriteableMutable; overload; static;
property Changed: TSignal read GetChanged;
end;
TLazy<T> = record
private
FLazy: IMycLazy<T>;
function GetChanged: TState.IState; inline;
type
ILazy = interface
{$REGION 'property access'}
function GetChanged: TState;
{$ENDREGION}
function Pop(out Res: T): Boolean;
property Changed: TState read GetChanged;
end;
{$REGION 'private'}
strict private
class var
FNull: IMycLazy<T>;
FNull: ILazy;
class constructor CreateClass;
private
FLazy: ILazy;
function GetChanged: TState; inline;
{$ENDREGION}
public
constructor Create(const ALazy: IMycLazy<T>);
constructor Create(const ALazy: ILazy);
class operator Initialize(out Dest: TLazy<T>);
class operator Implicit(const A: ILazy): TLazy<T>; overload;
class operator Implicit(const A: TLazy<T>): ILazy; overload;
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 function Construct(const Changing: TState; const Proc: TFunc<T>): TLazy<T>; overload; static;
class function Construct(const Value: ILazy): TLazy<T>; overload; static;
class operator Implicit(const A: IMycLazy<T>): TLazy<T>; overload;
class operator Implicit(const A: TLazy<T>): IMycLazy<T>; overload;
class property Null: ILazy read FNull;
function Pop(out Res: T): Boolean; inline;
property Changed: TState.IState read GetChanged;
property Changed: TState read GetChanged;
end;
implementation
@@ -49,14 +88,60 @@ implementation
uses
Myc.Core.Lazy;
constructor TLazy<T>.Create(const ALazy: IMycLazy<T>);
{ TMutable<T> }
constructor TMutable<T>.Create(const AMutable: IMutable);
begin
FMutable := AMutable;
if not Assigned(FMutable) then
FMutable := FNull;
end;
class constructor TMutable<T>.CreateClass;
begin
FNull := TMycNullMutable<T>.Create;
end;
class function TMutable<T>.Construct(const Changing: TSignal; const Proc: TFunc<T>): TMutable<T>;
begin
Result := TMycFuncMutable<T>.Create(Changing, Proc);
end;
class function TMutable<T>.CreateWriteable(const Init: T): IWriteableMutable;
begin
Result := TMycWriteableMutable<T>.Create(Init);
end;
function TMutable<T>.GetChanged: TSignal;
begin
Result := FMutable.Changed;
end;
class operator TMutable<T>.Implicit(const A: TMutable<T>): IMutable;
begin
Result := A.FMutable;
end;
class operator TMutable<T>.Implicit(const A: IMutable): TMutable<T>;
begin
Result.Create(A);
end;
class operator TMutable<T>.Initialize(out Dest: TMutable<T>);
begin
Dest.FMutable := FNull;
end;
{ TLazy<T> }
constructor TLazy<T>.Create(const ALazy: ILazy);
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>;
class function TLazy<T>.Construct(const Changing: TState; const Proc: TFunc<T>): TLazy<T>;
begin
Result := TMycFuncLazy<T>.Create(Changing, Proc);
end;
@@ -66,12 +151,12 @@ begin
FNull := TMycNullLazy<T>.Create;
end;
class function TLazy<T>.Construct(const Value: IMycLazy<T>): IMycLazy<T>;
class function TLazy<T>.Construct(const Value: ILazy): TLazy<T>;
begin
Result := TMycChainedLazy<T>.Create(Value);
end;
function TLazy<T>.GetChanged: TState.IState;
function TLazy<T>.GetChanged: TState;
begin
Result := FLazy.Changed;
end;
@@ -81,14 +166,19 @@ begin
Result := FLazy.Pop(Res);
end;
class operator TLazy<T>.Implicit(const A: IMycLazy<T>): TLazy<T>;
class operator TLazy<T>.Implicit(const A: ILazy): TLazy<T>;
begin
Result.Create(A);
end;
class operator TLazy<T>.Implicit(const A: TLazy<T>): IMycLazy<T>;
class operator TLazy<T>.Implicit(const A: TLazy<T>): ILazy;
begin
Result := A.FLazy;
end;
class operator TLazy<T>.Initialize(out Dest: TLazy<T>);
begin
Dest.FLazy := FNull;
end;
end.