unit DimValue;
interface
uses
System.Generics.Collections;
type
IVariable<T> =
interface
function GetValue: T;
procedure SetValue(
const Value: T );
property Value: T
read GetValue
write SetValue;
function GetV(
index: Integer ): IVariable<T>;
procedure SetV(
index: Integer; Value: IVariable<T> );
property V[
index: Integer]: IVariable<T>
read GetV
write SetV;
default;
end;
TVariable<T> =
class( TInterfacedObject, IVariable<T> )
private
FValue: T;
FVarDict: TDictionary<Integer, IVariable<T>>;
private
function GetValue: T;
procedure SetValue(
const Value: T );
function GetV(
index: Integer ): IVariable<T>;
procedure SetV(
index: Integer; Value: IVariable<T> );
private
constructor Create;
destructor Destroy;
override;
public
class function Construct: IVariable<T>;
end;
implementation
{ TVariable<T> }
class function TVariable<T>.Construct: IVariable<T>;
begin
Result := TVariable<T>.Create;
end;
constructor TVariable<T>.Create;
begin
inherited;
FVarDict := TDictionary < Integer, IVariable < T >>.Create;
end;
destructor TVariable<T>.Destroy;
begin
FVarDict.Free;
inherited;
end;
function TVariable<T>.GetV(
index: Integer ): IVariable<T>;
begin
if not FVarDict.TryGetValue(
index, Result )
then
begin
Result := TVariable<T>.Construct;
FVarDict.Add(
index, Result );
end;
end;
function TVariable<T>.GetValue: T;
begin
Result := FValue;
end;
procedure TVariable<T>.SetV(
index: Integer; Value: IVariable<T> );
begin
FVarDict.AddOrSetValue(
index, Value );
end;
procedure TVariable<T>.SetValue(
const Value: T );
begin
FValue := Value;
end;
end.