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
+62 -1
View File
@@ -5,11 +5,16 @@ interface
uses
System.SysUtils,
System.Rtti,
System.Generics.Collections,
Myc.Data.Types;
type
// Implements the IDataMethodType interface.
TImplDataMethodType = class(TInterfacedObject, IDataMethodType)
strict private
// Class-level registry to cache and reuse method type definitions.
class var
FMethodTypeRegistry: TDictionary<TPair<IDataType, IDataType>, IDataMethodType>;
private
FArgType: IDataType;
FResultType: IDataType;
@@ -20,12 +25,19 @@ type
function CreateValue(const AValue: TDataMethodProc): IDataMethodValue;
public
constructor Create(const AArgType, AResultType: IDataType);
class constructor CreateClass;
class destructor DestroyClass;
// Factory method to get a cached or new method type instance.
class function GetInstance(const AArgType, AResultType: IDataType): IDataMethodType; static;
end;
implementation
uses
System.Classes;
System.Classes,
System.SyncObjs,
System.Generics.Defaults;
type
// Implements the IDataMethodValue interface.
@@ -43,6 +55,55 @@ type
{ TImplDataMethodType }
class constructor TImplDataMethodType.CreateClass;
begin
// Initialize the global registry for method types using a custom comparer for the TPair key.
FMethodTypeRegistry :=
TDictionary<TPair<IDataType, IDataType>, IDataMethodType>.Create(
TEqualityComparer<TPair<IDataType, IDataType>>.Construct(
function(const Left, Right: TPair<IDataType, IDataType>): Boolean
begin
// Compare interface pointers.
Result := (Left.Key = Right.Key) and (Left.Value = Right.Value);
end,
function(const Value: TPair<IDataType, IDataType>): Integer
var
hash1, hash2: NativeInt;
begin
// Combine hashes of interface pointers.
hash1 := NativeInt(Value.Key);
hash2 := NativeInt(Value.Value);
Result := Integer(hash1 xor hash2);
end
)
);
end;
class destructor TImplDataMethodType.DestroyClass;
begin
FMethodTypeRegistry.Free;
FMethodTypeRegistry := nil;
end;
class function TImplDataMethodType.GetInstance(const AArgType, AResultType: IDataType): IDataMethodType;
var
key: TPair<IDataType, IDataType>;
res: IDataMethodType;
begin
key := TPair<IDataType, IDataType>.Create(AArgType, AResultType);
TMonitor.Enter(FMethodTypeRegistry);
try
if not FMethodTypeRegistry.TryGetValue(key, res) then
begin
res := TImplDataMethodType.Create(AArgType, AResultType);
FMethodTypeRegistry.Add(key, res);
end;
finally
TMonitor.Exit(FMethodTypeRegistry);
end;
Result := res;
end;
constructor TImplDataMethodType.Create(const AArgType, AResultType: IDataType);
begin
inherited Create;