Data Types
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
unit Myc.Data.Types.Enum;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Generics.Collections,
|
||||
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)
|
||||
private
|
||||
FName: string;
|
||||
FIdentifiers: TArray<string>;
|
||||
FIdentifierMap: TDictionary<string, Integer>;
|
||||
function GetName: String;
|
||||
function GetKind: TDataKind;
|
||||
function GetIdentifier(Idx: Integer): string;
|
||||
function GetIdentifierCount: Integer;
|
||||
function IndexOf(const AIdentifier: string): Integer;
|
||||
function CreateValue(const AValue: Integer): IDataEnumValue; overload;
|
||||
function CreateValue(const AIdentifier: string): IDataEnumValue; overload;
|
||||
public
|
||||
constructor Create(const AName: string; const AIdentifiers: array of string);
|
||||
destructor Destroy; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TImplDataEnumType }
|
||||
|
||||
constructor TImplDataEnumType.Create(const AName: string; const AIdentifiers: array of string);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
inherited Create;
|
||||
FName := AName;
|
||||
FIdentifierMap := TDictionary<string, Integer>.Create;
|
||||
SetLength(FIdentifiers, Length(AIdentifiers));
|
||||
for i := 0 to High(AIdentifiers) do
|
||||
begin
|
||||
if FIdentifierMap.ContainsKey(AIdentifiers[i]) then
|
||||
raise EArgumentException.CreateFmt('Duplicate identifier: %s', [AIdentifiers[i]]);
|
||||
FIdentifiers[i] := AIdentifiers[i];
|
||||
FIdentifierMap.Add(AIdentifiers[i], i);
|
||||
end;
|
||||
end;
|
||||
|
||||
destructor TImplDataEnumType.Destroy;
|
||||
begin
|
||||
FIdentifierMap.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
function TImplDataEnumType.GetName: String;
|
||||
begin
|
||||
Result := FName;
|
||||
end;
|
||||
|
||||
function TImplDataEnumType.GetKind: TDataKind;
|
||||
begin
|
||||
Result := dkEnum;
|
||||
end;
|
||||
|
||||
function TImplDataEnumType.GetIdentifier(Idx: Integer): string;
|
||||
begin
|
||||
Result := FIdentifiers[Idx];
|
||||
end;
|
||||
|
||||
function TImplDataEnumType.GetIdentifierCount: Integer;
|
||||
begin
|
||||
Result := Length(FIdentifiers);
|
||||
end;
|
||||
|
||||
function TImplDataEnumType.IndexOf(const AIdentifier: string): Integer;
|
||||
begin
|
||||
if not FIdentifierMap.TryGetValue(AIdentifier, Result) then
|
||||
Result := -1;
|
||||
end;
|
||||
|
||||
function TImplDataEnumType.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);
|
||||
end;
|
||||
|
||||
function TImplDataEnumType.CreateValue(const AIdentifier: string): IDataEnumValue;
|
||||
var
|
||||
idx: Integer;
|
||||
begin
|
||||
idx := IndexOf(AIdentifier);
|
||||
if idx < 0 then
|
||||
raise EArgumentException.CreateFmt('Unknown identifier: %s', [AIdentifier]);
|
||||
Result := CreateValue(idx);
|
||||
end;
|
||||
|
||||
{ TImplDataEnumValue }
|
||||
|
||||
constructor TImplDataEnumValue.Create(const ADataType: IDataEnumType; const AValue: Integer);
|
||||
begin
|
||||
FDataType := ADataType;
|
||||
FValue := AValue;
|
||||
end;
|
||||
|
||||
function TImplDataEnumValue.GetDataType: IDataType;
|
||||
begin
|
||||
Result := FDataType;
|
||||
end;
|
||||
|
||||
function TImplDataEnumValue.GetAsString: string;
|
||||
begin
|
||||
Result := FDataType.Identifiers[FValue];
|
||||
end;
|
||||
|
||||
function TImplDataEnumValue.GetValue: Integer;
|
||||
begin
|
||||
Result := FValue;
|
||||
end;
|
||||
|
||||
end.
|
||||
Reference in New Issue
Block a user