Files
MycLib/Src/Data/Myc.Data.Types.Tuple.pas
T
Michael Schimmel c9e28a946d Data Types
2025-08-25 14:48:06 +02:00

109 lines
2.5 KiB
ObjectPascal

unit Myc.Data.Types.Tuple;
interface
uses
System.SysUtils,
Myc.Data.Types;
type
// Implements the simple tuple data value.
TImplDataTupleValue = class(TInterfacedObject, IDataValue, IDataTupleValue)
private
FItems: TArray<IDataValue>;
function GetDataType: IDataType;
function GetAsString: string;
function GetItemCount: Integer;
function GetItem(Idx: Integer): IDataValue;
public
constructor Create(const AItems: array of IDataValue);
end;
// Implements the singleton tuple data type.
TImplDataTupleType = class(TInterfacedObject, IDataType, IDataTupleType)
strict private
class var
FSingleton: IDataTupleType;
class constructor CreateClass;
private
function GetName: String;
function GetKind: TDataKind;
public
function CreateValue(const AItems: array of IDataValue): IDataTupleValue;
class property Singleton: IDataTupleType read FSingleton;
end;
implementation
{ TImplDataTupleValue }
constructor TImplDataTupleValue.Create(const AItems: array of IDataValue);
var
i: Integer;
begin
inherited Create;
SetLength(FItems, Length(AItems));
for i := 0 to High(AItems) do
FItems[i] := AItems[i];
end;
function TImplDataTupleValue.GetDataType: IDataType;
begin
Result := TImplDataTupleType.Singleton;
end;
function TImplDataTupleValue.GetAsString: string;
var
sb: TStringBuilder;
i: Integer;
begin
sb := TStringBuilder.Create;
try
sb.Append('(');
for i := 0 to High(FItems) do
begin
sb.Append(FItems[i].AsString);
if (i < High(FItems)) then
sb.Append(', ');
end;
sb.Append(')');
Result := sb.ToString;
finally
sb.Free;
end;
end;
function TImplDataTupleValue.GetItem(Idx: Integer): IDataValue;
begin
Result := FItems[Idx];
end;
function TImplDataTupleValue.GetItemCount: Integer;
begin
Result := Length(FItems);
end;
{ TImplDataTupleType }
class constructor TImplDataTupleType.CreateClass;
begin
FSingleton := TImplDataTupleType.Create;
end;
function TImplDataTupleType.CreateValue(const AItems: array of IDataValue): IDataTupleValue;
begin
Result := TImplDataTupleValue.Create(AItems);
end;
function TImplDataTupleType.GetName: String;
begin
Result := 'Tuple';
end;
function TImplDataTupleType.GetKind: TDataKind;
begin
Result := dkTuple;
end;
end.