Data Types

This commit is contained in:
Michael Schimmel
2025-08-25 15:31:15 +02:00
parent c9e28a946d
commit 42110e8471
10 changed files with 980 additions and 193 deletions
+89 -25
View File
@@ -6,9 +6,30 @@ uses
System.SysUtils,
Myc.Data.Types;
type
// Implements the float data type.
TDataFloatType = class(TInterfacedObject, IDataType, IDataFloatType)
strict private
class var
FSingleton: IDataFloatType;
FZero: IDataFloatValue;
FNaN: IDataFloatValue;
class constructor CreateClass;
public
function GetName: String;
function GetKind: TDataKind;
function CreateValue(Init: Double): IDataFloatValue;
class property Singleton: IDataFloatType read FSingleton;
end;
implementation
uses
System.Math;
type
// Implements the float data value.
TImplDataFloatValue = class(TInterfacedObject, IDataValue, IDataFloatValue)
TDataFloatValue = class(TInterfacedObject, IDataValue, IDataFloatValue)
private
FValue: Double;
public
@@ -22,64 +43,107 @@ type
function GetValue: Double;
end;
// Implements the float data type.
TImplDataFloatType = class(TInterfacedObject, IDataType, IDataFloatType)
strict private
class var
FSingleton: IDataFloatType;
class constructor CreateClass;
// Special implementation for the value 0.0 (Singleton)
TDataFloatValueZero = class(TInterfacedObject, IDataValue, IDataFloatValue)
public
function GetName: String;
function GetKind: TDataKind;
function CreateValue(Init: Double): IDataFloatValue;
class property Singleton: IDataFloatType read FSingleton;
function GetDataType: IDataType;
function GetAsString: string;
function GetValue: Double;
end;
implementation
// Special implementation for NaN (Not a Number) (Singleton)
TDataFloatValueNaN = class(TInterfacedObject, IDataValue, IDataFloatValue)
public
function GetDataType: IDataType;
function GetAsString: string;
function GetValue: Double;
end;
{ TImplDataFloatValue }
{ TDataFloatValue }
constructor TImplDataFloatValue.Create(const AValue: Double);
constructor TDataFloatValue.Create(const AValue: Double);
begin
inherited Create;
FValue := AValue;
end;
function TImplDataFloatValue.GetDataType: IDataType;
function TDataFloatValue.GetDataType: IDataType;
begin
Result := TImplDataFloatType.Singleton;
Result := TDataFloatType.Singleton;
end;
function TImplDataFloatValue.GetAsString: string;
function TDataFloatValue.GetAsString: string;
begin
Result := FloatToStr(FValue);
end;
function TImplDataFloatValue.GetValue: Double;
function TDataFloatValue.GetValue: Double;
begin
Result := FValue;
end;
{ TImplDataFloatType }
{ TDataFloatValueZero }
class constructor TImplDataFloatType.CreateClass;
function TDataFloatValueZero.GetAsString: string;
begin
FSingleton := TImplDataFloatType.Create;
Result := '0';
end;
function TImplDataFloatType.GetName: String;
function TDataFloatValueZero.GetDataType: IDataType;
begin
Result := TDataFloatType.Singleton;
end;
function TDataFloatValueZero.GetValue: Double;
begin
Result := 0.0;
end;
{ TDataFloatValueNaN }
function TDataFloatValueNaN.GetAsString: string;
begin
Result := 'NaN';
end;
function TDataFloatValueNaN.GetDataType: IDataType;
begin
Result := TDataFloatType.Singleton;
end;
function TDataFloatValueNaN.GetValue: Double;
begin
Result := NaN;
end;
{ TDataFloatType }
class constructor TDataFloatType.CreateClass;
begin
FSingleton := TDataFloatType.Create;
FZero := TDataFloatValueZero.Create;
FNaN := TDataFloatValueNaN.Create;
end;
function TDataFloatType.GetName: String;
begin
Result := 'Float';
end;
function TImplDataFloatType.GetKind: TDataKind;
function TDataFloatType.GetKind: TDataKind;
begin
Result := dkFloat;
end;
function TImplDataFloatType.CreateValue(Init: Double): IDataFloatValue;
function TDataFloatType.CreateValue(Init: Double): IDataFloatValue;
begin
Result := TImplDataFloatValue.Create(Init);
// Return singleton instances for 0.0 and NaN to avoid unnecessary allocations.
if (Init = 0.0) then
Result := FZero
else if IsNaN(Init) then
Result := FNaN
else
Result := TDataFloatValue.Create(Init);
end;
end.