New type system

This commit is contained in:
Michael Schimmel
2025-08-26 14:57:35 +02:00
parent 8e8f139785
commit e9608a746a
5 changed files with 199 additions and 132 deletions
+42
View File
@@ -10,6 +10,10 @@ uses
type
// Implements the array data type.
TImplDataArrayType = class(TInterfacedObject, IDataType, IDataArrayType)
strict private
// Class-level registry to cache and reuse array type definitions.
class var
FArrayTypeRegistry: TDictionary<IDataType, IDataArrayType>;
private
FElementType: IDataType;
FEmptyValue: IDataArrayValue; // Cached empty array value for this type
@@ -18,6 +22,12 @@ type
function GetKind: TDataKind;
public
constructor Create(const AElementType: IDataType);
class constructor CreateClass;
class destructor DestroyClass;
// Factory method to get a cached or new array type instance.
class function GetInstance(const AElementType: IDataType): IDataArrayType; static;
function CreateValue(const AItems: array of IDataValue): IDataArrayValue;
end;
@@ -216,6 +226,38 @@ end;
{ TImplDataArrayType }
class constructor TImplDataArrayType.CreateClass;
begin
// Initialize the global registry for array types.
FArrayTypeRegistry := TDictionary<IDataType, IDataArrayType>.Create;
end;
class destructor TImplDataArrayType.DestroyClass;
begin
FArrayTypeRegistry.Free;
FArrayTypeRegistry := nil;
end;
class function TImplDataArrayType.GetInstance(const AElementType: IDataType): IDataArrayType;
var
res: IDataArrayType;
begin
if not Assigned(AElementType) then
raise EArgumentException.Create('Cannot create an array type with a nil element type.');
TMonitor.Enter(FArrayTypeRegistry);
try
if not FArrayTypeRegistry.TryGetValue(AElementType, res) then
begin
res := TImplDataArrayType.Create(AElementType);
FArrayTypeRegistry.Add(AElementType, res);
end;
finally
TMonitor.Exit(FArrayTypeRegistry);
end;
Result := res;
end;
constructor TImplDataArrayType.Create(const AElementType: IDataType);
begin
inherited Create;