TMutable
This commit is contained in:
+112
-5
@@ -8,14 +8,53 @@ uses
|
||||
Myc.Lazy;
|
||||
|
||||
type
|
||||
TMycNullLazy<T> = class(TInterfacedObject, IMycLazy<T>)
|
||||
TMycNullMutable<T> = class(TInterfacedObject, TMutable<T>.IMutable)
|
||||
protected
|
||||
function GetChanged: TSignal;
|
||||
function GetValue: T;
|
||||
end;
|
||||
|
||||
TMycMutableBase<T> = class(TInterfacedObject, TMutable<T>.IMutable)
|
||||
strict private
|
||||
FChanged: TEvent;
|
||||
FChangeState: TSignal.TSubscription;
|
||||
function GetChanged: TSignal;
|
||||
protected
|
||||
function GetValue: T; virtual; abstract;
|
||||
public
|
||||
constructor Create(const AChanged: TSignal);
|
||||
destructor Destroy; override;
|
||||
end;
|
||||
|
||||
TMycFuncMutable<T> = class(TMycMutableBase<T>)
|
||||
private
|
||||
FProc: TFunc<T>;
|
||||
protected
|
||||
function GetValue: T; override;
|
||||
public
|
||||
constructor Create(const AChanged: TSignal; const AProc: TFunc<T>);
|
||||
end;
|
||||
|
||||
TMycWriteableMutable<T> = class(TInterfacedObject, TMutable<T>.IMutable, TMutable<T>.IWriteableMutable)
|
||||
private
|
||||
FValue: T;
|
||||
FChanged: TEvent;
|
||||
protected
|
||||
function GetChanged: TSignal;
|
||||
function GetValue: T;
|
||||
public
|
||||
constructor Create(const AValue: T);
|
||||
procedure SetValue(const Value: T);
|
||||
end;
|
||||
|
||||
TMycNullLazy<T> = class(TInterfacedObject, TLazy<T>.ILazy)
|
||||
protected
|
||||
function GetChanged: TState;
|
||||
public
|
||||
function Pop(out Res: T): Boolean;
|
||||
end;
|
||||
|
||||
TMycLazyBase<T> = class(TInterfacedObject, IMycLazy<T>)
|
||||
TMycLazyBase<T> = class(TInterfacedObject, TLazy<T>.ILazy)
|
||||
strict private
|
||||
FChanged: TFlag;
|
||||
FChangeState: TSignal.TSubscription;
|
||||
@@ -39,15 +78,72 @@ type
|
||||
|
||||
TMycChainedLazy<T> = class(TMycLazyBase<T>)
|
||||
private
|
||||
FValue: IMycLazy<T>;
|
||||
FValue: TLazy<T>.ILazy;
|
||||
protected
|
||||
function GetValue: T; override;
|
||||
public
|
||||
constructor Create(const AValue: IMycLazy<T>);
|
||||
constructor Create(const AValue: TLazy<T>.ILazy);
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TMycNullMutable<T> }
|
||||
|
||||
function TMycNullMutable<T>.GetChanged: TSignal;
|
||||
begin
|
||||
Result := TSignal.Null;
|
||||
end;
|
||||
|
||||
function TMycNullMutable<T>.GetValue: T;
|
||||
begin
|
||||
Result := Default(T);
|
||||
end;
|
||||
|
||||
{ TMycMutableBase<T> }
|
||||
|
||||
constructor TMycMutableBase<T>.Create(const AChanged: TSignal);
|
||||
begin
|
||||
inherited Create;
|
||||
FChanged := TEvent.CreateEvent;
|
||||
FChangeState := AChanged.Subscribe(FChanged);
|
||||
end;
|
||||
|
||||
destructor TMycMutableBase<T>.Destroy;
|
||||
begin
|
||||
FChangeState.Unsubscribe;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TMycMutableBase<T>.GetChanged: TSignal;
|
||||
begin
|
||||
Result := FChanged.Signal;
|
||||
end;
|
||||
|
||||
{ TMycWriteableMutable<T> }
|
||||
|
||||
constructor TMycWriteableMutable<T>.Create(const AValue: T);
|
||||
begin
|
||||
inherited Create;
|
||||
FValue := AValue;
|
||||
FChanged := TEvent.CreateEvent;
|
||||
end;
|
||||
|
||||
function TMycWriteableMutable<T>.GetChanged: TSignal;
|
||||
begin
|
||||
Result := FChanged.Signal;
|
||||
end;
|
||||
|
||||
function TMycWriteableMutable<T>.GetValue: T;
|
||||
begin
|
||||
Result := FValue;
|
||||
end;
|
||||
|
||||
procedure TMycWriteableMutable<T>.SetValue(const Value: T);
|
||||
begin
|
||||
FValue := Value;
|
||||
FChanged.Notify;
|
||||
end;
|
||||
|
||||
{ TMycNullLazy<T> }
|
||||
|
||||
function TMycNullLazy<T>.GetChanged: TState;
|
||||
@@ -107,7 +203,7 @@ end;
|
||||
|
||||
{ TMycChainedLazy<T> }
|
||||
|
||||
constructor TMycChainedLazy<T>.Create(const AValue: IMycLazy<T>);
|
||||
constructor TMycChainedLazy<T>.Create(const AValue: TLazy<T>.ILazy);
|
||||
begin
|
||||
inherited Create(AValue.Changed);
|
||||
FValue := AValue;
|
||||
@@ -119,4 +215,15 @@ begin
|
||||
raise Exception.Create('Lazy chain already popped');
|
||||
end;
|
||||
|
||||
constructor TMycFuncMutable<T>.Create(const AChanged: TSignal; const AProc: TFunc<T>);
|
||||
begin
|
||||
inherited Create(AChanged);
|
||||
FProc := AProc;
|
||||
end;
|
||||
|
||||
function TMycFuncMutable<T>.GetValue: T;
|
||||
begin
|
||||
Result := FProc();
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user