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
+29 -28
View File
@@ -8,18 +8,7 @@ uses
Myc.Data.Types;
type
TImplDataEnumValue = class(TInterfacedObject, IDataEnumValue)
private
FDataType: IDataEnumType;
FValue: Integer;
function GetDataType: IDataType;
function GetAsString: string;
function GetValue: Integer;
public
constructor Create(const ADataType: IDataEnumType; const AValue: Integer);
end;
TImplDataEnumType = class(TInterfacedObject, IDataEnumType)
TDataEnumType = class(TInterfacedObject, IDataEnumType)
private
FName: string;
FIdentifiers: TArray<string>;
@@ -38,9 +27,21 @@ type
implementation
{ TImplDataEnumType }
type
TDataEnumValue = class(TInterfacedObject, IDataEnumValue)
private
FDataType: IDataEnumType;
FValue: Integer;
function GetDataType: IDataType;
function GetAsString: string;
function GetValue: Integer;
public
constructor Create(const ADataType: IDataEnumType; const AValue: Integer);
end;
constructor TImplDataEnumType.Create(const AName: string; const AIdentifiers: array of string);
{ TDataEnumType }
constructor TDataEnumType.Create(const AName: string; const AIdentifiers: array of string);
var
i: Integer;
begin
@@ -57,46 +58,46 @@ begin
end;
end;
destructor TImplDataEnumType.Destroy;
destructor TDataEnumType.Destroy;
begin
FIdentifierMap.Free;
inherited;
end;
function TImplDataEnumType.GetName: String;
function TDataEnumType.GetName: String;
begin
Result := FName;
end;
function TImplDataEnumType.GetKind: TDataKind;
function TDataEnumType.GetKind: TDataKind;
begin
Result := dkEnum;
end;
function TImplDataEnumType.GetIdentifier(Idx: Integer): string;
function TDataEnumType.GetIdentifier(Idx: Integer): string;
begin
Result := FIdentifiers[Idx];
end;
function TImplDataEnumType.GetIdentifierCount: Integer;
function TDataEnumType.GetIdentifierCount: Integer;
begin
Result := Length(FIdentifiers);
end;
function TImplDataEnumType.IndexOf(const AIdentifier: string): Integer;
function TDataEnumType.IndexOf(const AIdentifier: string): Integer;
begin
if not FIdentifierMap.TryGetValue(AIdentifier, Result) then
Result := -1;
end;
function TImplDataEnumType.CreateValue(const AValue: Integer): IDataEnumValue;
function TDataEnumType.CreateValue(const AValue: Integer): IDataEnumValue;
begin
if (AValue < 0) or (AValue >= Length(FIdentifiers)) then
raise EArgumentException.Create('Invalid enum value');
Result := TImplDataEnumValue.Create(Self, AValue);
Result := TDataEnumValue.Create(Self, AValue);
end;
function TImplDataEnumType.CreateValue(const AIdentifier: string): IDataEnumValue;
function TDataEnumType.CreateValue(const AIdentifier: string): IDataEnumValue;
var
idx: Integer;
begin
@@ -106,25 +107,25 @@ begin
Result := CreateValue(idx);
end;
{ TImplDataEnumValue }
{ TDataEnumValue }
constructor TImplDataEnumValue.Create(const ADataType: IDataEnumType; const AValue: Integer);
constructor TDataEnumValue.Create(const ADataType: IDataEnumType; const AValue: Integer);
begin
FDataType := ADataType;
FValue := AValue;
end;
function TImplDataEnumValue.GetDataType: IDataType;
function TDataEnumValue.GetDataType: IDataType;
begin
Result := FDataType;
end;
function TImplDataEnumValue.GetAsString: string;
function TDataEnumValue.GetAsString: string;
begin
Result := FDataType.Identifiers[FValue];
end;
function TImplDataEnumValue.GetValue: Integer;
function TDataEnumValue.GetValue: Integer;
begin
Result := FValue;
end;