RTL and Data value refactoring and defered values

This commit is contained in:
Michael Schimmel
2025-09-18 13:35:25 +02:00
parent 4d380c8f98
commit 2c55a120f1
11 changed files with 783 additions and 365 deletions
+250 -91
View File
@@ -8,25 +8,48 @@ uses
Myc.Data.Series;
type
TDataValueKind = (vkVoid, vkScalar, vkInterface, vkText, vkSeries, vkRecordSeries, vkRecord, vkMemberSeries, vkGeneric, vkMethod);
TDataValueKind = (vkVoid, vkScalar, vkText, vkSeries, vkRecordSeries, vkRecord, vkMemberSeries, vkMethod, vkGeneric);
TDataValue = record
public
type
TVal<T> = class(TInterfacedObject)
Value: T;
constructor Create(const AValue: T);
end;
TFunc = reference to function(const ArgNodes: TArray<TDataValue>): TDataValue;
private
type
IVal = interface
{$ifdef DEBUG}
function GetTypeHandle: Pointer;
{$endif}
end;
TVal<T> = class(TInterfacedObject, IVal)
Value: T;
{$ifdef DEBUG}
TypeHandle: Pointer;
function GetTypeHandle: Pointer;
{$endif}
constructor Create(const AValue: T);
end;
ILazy = interface
function GetKind: TDataValueKind;
function GetValue: TDataValue;
property Kind: TDataValueKind read GetKind;
property Value: TDataValue read GetValue;
end;
TInternalKind = (vkLazy = Ord(High(TDataValueKind)) + 1);
TInternalKindOrdinal = 0..Ord(High(TInternalKind));
var
FKind: TDataValueKind;
FKind: TInternalKindOrdinal;
FScalar: TScalar;
FInterface: IInterface;
function AsLazy: ILazy; inline;
function GetKind: TDataValueKind; inline;
function GetIsVoid: Boolean; inline;
public
class operator Initialize(out Dest: TDataValue);
class function Void: TDataValue; inline; static;
@@ -35,178 +58,292 @@ type
class operator Implicit(const AValue: String): TDataValue; overload; inline;
class operator Implicit(const AValue: TDataValue.TFunc): TDataValue; overload; inline;
class function From<T: record>(const [ref] AValue: T): TDataValue; static; inline;
function AsVal<T: record>: TVal<T>; inline;
class function FromGeneric<T>(const [ref] AValue: T): TDataValue; static; inline;
function AsGeneric<T>: T; inline;
class function FromSeries(const [ref] AValue: TScalarSeries): TDataValue; static; inline;
class function FromRecordSeries(const [ref] AValue: TScalarRecordSeries): TDataValue; static; inline;
class function Defer(const Value: TDataValue; const Calc: TFunc): TDataValue; overload; static;
class function FromSeries(const AValue: IWriteableSeries): TDataValue; static; inline;
class function FromRecordSeries(const AValue: IRecordSeries): TDataValue; static; inline;
class function FromRecord(const [ref] AValue: TScalarRecord): TDataValue; static; inline;
class function FromMemberSeries(const [ref] AValue: TScalarMemberSeries): TDataValue; static; inline;
class function FromInterface(const [ref] AValue: IInterface): TDataValue; static; inline;
class function FromMemberSeries(const AValue: ISeries): TDataValue; static; inline;
function AsScalar: TScalar; inline;
function AsInterface: IInterface; inline;
function AsMethod: TFunc; inline;
function AsText: String; inline;
function AsRecordSeries: TVal<TScalarRecordSeries>; inline;
function AsRecord: TVal<TScalarRecord>; inline;
function AsMemberSeries: TVal<TScalarMemberSeries>; inline;
function AsSeries: TVal<TScalarSeries>; inline;
procedure SetVoid;
function AsRecordSeries: IRecordSeries; inline;
function AsRecord: TScalarRecord; inline;
function AsMemberSeries: ISeries; inline;
function AsSeries: IWriteableSeries; inline;
function ToString: String;
property IsVoid: Boolean read GetIsVoid;
property Kind: TDataValueKind read GetKind;
end;
type
TDataValueKindHelper = record helper for TDataValueKind
public
function ToString: string;
end;
implementation
uses
TypInfo;
type
TManagedLazy = class(TInterfacedObject, TDataValue.ILazy)
private
FKind: TDataValueKind;
FVal: IInterface;
FCalc: TDataValue.TFunc;
function GetValue: TDataValue;
function GetKind: TDataValueKind;
public
constructor Create(AKind: TDataValueKind; const AVal: IInterface; const ACalc: TDataValue.TFunc);
end;
TScalarLazy = class(TInterfacedObject, TDataValue.ILazy)
private
FVal: TScalar;
FCalc: TDataValue.TFunc;
function GetKind: TDataValueKind;
function GetValue: TDataValue;
public
constructor Create(const AVal: TScalar; const ACalc: TDataValue.TFunc);
end;
constructor TManagedLazy.Create(AKind: TDataValueKind; const AVal: IInterface; const ACalc: TDataValue.TFunc);
begin
inherited Create;
FKind := AKind;
Assert(FKind <> vkVoid);
FVal := AVal;
FCalc := ACalc;
end;
function TManagedLazy.GetKind: TDataValueKind;
begin
Result := FKind;
end;
function TManagedLazy.GetValue: TDataValue;
var
dv: TDataValue;
begin
dv.FKind := Ord(FKind);
dv.FInterface := FVal;
Result := FCalc([dv]);
end;
constructor TScalarLazy.Create(const AVal: TScalar; const ACalc: TDataValue.TFunc);
begin
inherited Create;
FVal := AVal;
FCalc := ACalc;
end;
function TScalarLazy.GetKind: TDataValueKind;
begin
Result := vkScalar;
end;
function TScalarLazy.GetValue: TDataValue;
begin
Result := FCalc([FVal]);
end;
{ TDataValue.TVal<T> }
constructor TDataValue.TVal<T>.Create(const AValue: T);
begin
inherited Create;
Value := AValue;
{$ifdef DEBUG}
TypeHandle := TypeInfo(T);
{$endif}
end;
{$ifdef DEBUG}
function TDataValue.TVal<T>.GetTypeHandle: Pointer;
begin
Result := TypeHandle;
end;
{$endif}
function TDataValue.AsLazy: ILazy;
begin
Assert(FKind = Ord(vkLazy));
Result := ILazy(FInterface);
end;
{ TDataValue }
class operator TDataValue.Initialize(out Dest: TDataValue);
begin
Dest.FKind := vkVoid;
Dest.FKind := Ord(vkVoid);
Dest.FInterface := nil;
end;
function TDataValue.AsInterface: IInterface;
function TDataValue.AsMemberSeries: ISeries;
begin
if (FKind <> vkInterface) then
raise EInvalidCast.Create('Cannot read value as an Interface.');
Result := FInterface;
end;
function TDataValue.AsMemberSeries: TVal<TScalarMemberSeries>;
begin
if (FKind <> vkMemberSeries) then
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsMemberSeries);
if (FKind <> Ord(vkMemberSeries)) then
raise EInvalidCast.Create('Cannot read value as MemberSeries.');
Result := TVal<TScalarMemberSeries>(FInterface);
Result := ISeries(FInterface);
end;
function TDataValue.AsMethod: TFunc;
begin
if (FKind <> vkMethod) then
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsMethod);
if (FKind <> Ord(vkMethod)) then
raise EInvalidCast.Create('Cannot read value as a method.');
Result := TFunc(FInterface);
end;
function TDataValue.AsVal<T>: TVal<T>;
function TDataValue.AsGeneric<T>: T;
begin
if (FKind <> vkGeneric) then
raise EInvalidCast.Create('Cannot read value as ' + String(PTypeInfo(TypeInfo(T)).Name) + '.');
Result := TVal<T>(FInterface);
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsGeneric<T>);
if FKind <> Ord(vkGeneric) then
raise EInvalidCast.Create('Cannot read value as generic of ' + String(PTypeInfo(TypeInfo(T)).Name) + '.');
{$ifdef DEBUG}
if TVal<T>(FInterface).TypeHandle <> TypeInfo(T) then
raise EInvalidCast.Create('Generic type is not a ' + String(PTypeInfo(TypeInfo(T)).Name) + '.');
{$endif}
Result := TVal<T>(FInterface).Value;
end;
function TDataValue.AsRecord: TVal<TScalarRecord>;
function TDataValue.AsRecord: TScalarRecord;
begin
if (FKind <> vkRecord) then
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsRecord);
if (FKind <> Ord(vkRecord)) then
raise EInvalidCast.Create('Cannot read value as Record.');
Result := (FInterface as TVal<TScalarRecord>);
Result := (FInterface as TVal<TScalarRecord>).Value;
end;
function TDataValue.AsRecordSeries: TVal<TScalarRecordSeries>;
function TDataValue.AsRecordSeries: IRecordSeries;
begin
if (FKind <> vkRecordSeries) then
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsRecordSeries);
if (FKind <> Ord(vkRecordSeries)) then
raise EInvalidCast.Create('Cannot read value as RecordSeries.');
Result := TVal<TScalarRecordSeries>(FInterface);
Result := IRecordSeries(FInterface);
end;
function TDataValue.AsScalar: TScalar;
begin
if (FKind <> vkScalar) then
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsScalar);
if (FKind <> Ord(vkScalar)) then
raise EInvalidCast.Create('Cannot read value as a Scalar.');
Result := FScalar;
end;
function TDataValue.AsSeries: TVal<TScalarSeries>;
function TDataValue.AsSeries: IWriteableSeries;
begin
if (FKind <> vkSeries) then
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsSeries);
if (FKind <> Ord(vkSeries)) then
raise EInvalidCast.Create('Cannot read value as Series.');
Result := TVal<TScalarSeries>(FInterface);
Result := IWriteableSeries(FInterface);
end;
function TDataValue.AsText: String;
begin
if (FKind <> vkText) then
if FKind = Ord(vkLazy) then
exit(AsLazy.Value.AsText);
if (FKind <> Ord(vkText)) then
raise EInvalidCast.Create('Cannot read value as Text.');
Result := (FInterface as TVal<String>).Value;
end;
class function TDataValue.From<T>(const [ref] AValue: T): TDataValue;
class function TDataValue.Defer(const Value: TDataValue; const Calc: TFunc): TDataValue;
begin
Result.FKind := vkGeneric;
Result.FKind := Ord(vkLazy);
case Value.FKind of
Ord(vkScalar): Result.FInterface := TScalarLazy.Create(Value.AsScalar, Calc);
Ord(vkLazy):
begin
// Create a new lazy object that wraps the existing one, passing the original
// kind and the inner lazy interface.
var cCalc: TFunc := Calc;
Result.FInterface :=
TManagedLazy.Create(
Value.AsLazy.Kind,
Value.FInterface,
function(const ArgNodes: TArray<TDataValue>): TDataValue
begin
// Chain lazy computations. The new calculation function will first evaluate
// the inner lazy value and then pass the result to the new calculation.
// The reconstructed TDataValue in ArgNodes[0] contains the inner ILazy interface.
Assert(Length(ArgNodes) = 1, 'Expected 1 argument in lazy computation chain');
Result := cCalc([ILazy(ArgNodes[0].FInterface).Value]);
end
);
end;
else
Result.FInterface := TManagedLazy.Create(Value.Kind, Value.FInterface, Calc);
end;
end;
class function TDataValue.FromGeneric<T>(const [ref] AValue: T): TDataValue;
begin
Result.FKind := Ord(vkGeneric);
Result.FInterface := TVal<T>.Create(AValue);
end;
class function TDataValue.FromInterface(const [ref] AValue: IInterface): TDataValue;
class function TDataValue.FromMemberSeries(const AValue: ISeries): TDataValue;
begin
Result.FKind := vkInterface;
Result.FKind := Ord(vkMemberSeries);
Result.FInterface := AValue;
end;
class function TDataValue.FromMemberSeries(const [ref] AValue: TScalarMemberSeries): TDataValue;
begin
Result.FKind := vkMemberSeries;
Result.FInterface := TVal<TScalarMemberSeries>.Create(AValue);
end;
class function TDataValue.FromRecord(const [ref] AValue: TScalarRecord): TDataValue;
begin
Result.FKind := vkRecord;
Result.FKind := Ord(vkRecord);
Result.FInterface := TVal<TScalarRecord>.Create(AValue);
end;
class function TDataValue.FromRecordSeries(const [ref] AValue: TScalarRecordSeries): TDataValue;
class function TDataValue.FromRecordSeries(const AValue: IRecordSeries): TDataValue;
begin
Result.FKind := vkRecordSeries;
Result.FInterface := TVal<TScalarRecordSeries>.Create(AValue);
Result.FKind := Ord(vkRecordSeries);
Result.FInterface := AValue;
end;
class function TDataValue.FromSeries(const [ref] AValue: TScalarSeries): TDataValue;
class function TDataValue.FromSeries(const AValue: IWriteableSeries): TDataValue;
begin
Result.FKind := vkSeries;
Result.FInterface := TVal<TScalarSeries>.Create(AValue);
Result.FKind := Ord(vkSeries);
Result.FInterface := AValue;
end;
class operator TDataValue.Implicit(const AValue: TScalar): TDataValue;
begin
Result.FKind := vkScalar;
Result.FKind := Ord(vkScalar);
Result.FScalar := AValue;
Result.FInterface := nil;
end;
class operator TDataValue.Implicit(const AValue: String): TDataValue;
begin
Result.FKind := vkText;
Result.FKind := Ord(vkText);
Result.FInterface := TVal<String>.Create(AValue);
end;
function TDataValue.GetIsVoid: Boolean;
begin
Result := FKind = vkVoid;
Result := FKind = Ord(vkVoid);
end;
function TDataValue.GetKind: TDataValueKind;
begin
Result := FKind;
end;
procedure TDataValue.SetVoid;
begin
FKind := vkVoid;
FInterface := nil;
if FKind = Ord(vkLazy) then
exit(AsLazy.Kind);
Result := TDataValueKind(FKind);
end;
function TDataValue.ToString: String;
@@ -216,18 +353,17 @@ var
first: Boolean;
begin
case FKind of
vkScalar: Result := FScalar.ToString;
vkInterface: Result := '<interface>';
vkText: Result := '"' + AsText + '"';
vkSeries:
Ord(vkScalar): Result := FScalar.ToString;
Ord(vkText): Result := '"' + AsText + '"';
Ord(vkSeries):
begin
var series := AsSeries.Value;
var series := AsSeries;
// Add type and count for series
Result := Format('<series<%s>[%d]>', [series.Kind.ToString, series.Items.Count]);
Result := Format('<series<%s>[%d]>', [series.Kind.ToString, series.Count]);
end;
vkRecordSeries:
Ord(vkRecordSeries):
begin
var series := AsRecordSeries.Value;
var series := AsRecordSeries;
sb := TStringBuilder.Create;
try
sb.Append('{');
@@ -246,9 +382,9 @@ begin
sb.Free;
end;
end;
vkRecord:
Ord(vkRecord):
begin
var rec := AsRecord.Value;
var rec := AsRecord;
sb := TStringBuilder.Create;
try
sb.Append('{');
@@ -267,18 +403,23 @@ begin
sb.Free;
end;
end;
vkMemberSeries:
Ord(vkMemberSeries):
begin
var series := AsMemberSeries.Value;
var series := AsMemberSeries;
// Add type and count for member series
Result := Format('<member_series<%s>[%d]>', [series.Kind.ToString, series.Count]);
end;
vkVoid: Result := '<void>';
vkMethod: Result := '<method>';
vkGeneric:
Ord(vkVoid): Result := '<void>';
Ord(vkMethod): Result := '<method>';
Ord(vkGeneric):
// Getting meaningful type information for vkGeneric is not easily possible
// without storing additional RTTI, as the specific type of T in TVal<T> is lost.
{$ifdef DEBUG}
Result := '<' + String(PTypeInfo(TDataValue.IVal(FInterface).GetTypeHandle).Name) + '>';
{$else}
Result := '<generic>';
{$endif}
Ord(vkLazy): Result := Format('<lazy[%s]>', [AsLazy.Kind.ToString]);
else
Result := '[Unknown DataValue]';
end;
@@ -291,8 +432,26 @@ end;
class operator TDataValue.Implicit(const AValue: TDataValue.TFunc): TDataValue;
begin
Result.FKind := vkMethod;
Result.FKind := Ord(vkMethod);
TFunc(Result.FInterface) := AValue;
end;
{ TDataValueKindHelper }
function TDataValueKindHelper.ToString: string;
begin
case Self of
vkVoid: Result := 'Void';
vkScalar: Result := 'Scalar';
vkText: Result := 'Text';
vkSeries: Result := 'Series';
vkRecordSeries: Result := 'RecordSeries';
vkRecord: Result := 'Record';
vkMemberSeries: Result := 'MemberSeries';
vkMethod: Result := 'Method';
else
Result := 'unknown';
end;
end;
end.