Data Types
This commit is contained in:
@@ -4,7 +4,8 @@ interface
|
||||
|
||||
uses
|
||||
System.Generics.Collections,
|
||||
System.SysUtils;
|
||||
System.SysUtils,
|
||||
System.RTTI;
|
||||
|
||||
type
|
||||
TDataKind = (dkOrdinal, dkFloat, dkText, dkTimestamp, dkRecord, dkTuple, dkArray, dkEnum);
|
||||
@@ -19,6 +20,7 @@ type
|
||||
IDataValue = interface
|
||||
function GetDataType: IDataType;
|
||||
function GetAsString: string;
|
||||
function AsTValue: TValue;
|
||||
property DataType: IDataType read GetDataType;
|
||||
property AsString: string read GetAsString;
|
||||
end;
|
||||
@@ -122,6 +124,7 @@ type
|
||||
strict private
|
||||
FDataType: IDataType;
|
||||
function GetName: String; inline;
|
||||
function GetKind: TDataKind; inline;
|
||||
|
||||
class var
|
||||
FArrayTypeRegistry: TDictionary<IDataType, IDataArrayType>;
|
||||
@@ -132,6 +135,8 @@ type
|
||||
public
|
||||
constructor Create(const ADataType: IDataType);
|
||||
|
||||
class procedure ClearRegistries; static;
|
||||
|
||||
class operator Implicit(const A: IDataType): TDataType; overload;
|
||||
class operator Implicit(const A: TDataType): IDataType; overload;
|
||||
|
||||
@@ -146,8 +151,19 @@ type
|
||||
class function RecordOf(const Fields: array of TRecordField): IDataRecordType; overload; static;
|
||||
class function EnumOf(const AName: string; const AIdentifiers: array of string): IDataEnumType; static;
|
||||
|
||||
// Casting
|
||||
function AsOrdinal: IDataOrdinalType;
|
||||
function AsFloat: IDataFloatType;
|
||||
function AsText: IDataTextType;
|
||||
function AsTimestamp: IDataTimestampType;
|
||||
function AsRecord: IDataRecordType;
|
||||
function AsTuple: IDataTupleType;
|
||||
function AsArray: IDataArrayType;
|
||||
function AsEnum: IDataEnumType;
|
||||
|
||||
property DataType: IDataType read FDataType;
|
||||
property Name: String read GetName;
|
||||
property Kind: TDataKind read GetKind;
|
||||
end;
|
||||
|
||||
TDataValue = record
|
||||
@@ -239,6 +255,69 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
// Implemented missing caster functions
|
||||
function TDataType.AsArray: IDataArrayType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkArray) then
|
||||
raise EInvalidCast.Create('Array expected');
|
||||
Result := IDataArrayType(FDataType);
|
||||
end;
|
||||
|
||||
function TDataType.AsEnum: IDataEnumType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkEnum) then
|
||||
raise EInvalidCast.Create('Enum expected');
|
||||
Result := IDataEnumType(FDataType);
|
||||
end;
|
||||
|
||||
function TDataType.AsFloat: IDataFloatType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkFloat) then
|
||||
raise EInvalidCast.Create('Float expected');
|
||||
Result := IDataFloatType(FDataType);
|
||||
end;
|
||||
|
||||
function TDataType.AsOrdinal: IDataOrdinalType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkOrdinal) then
|
||||
raise EInvalidCast.Create('Ordinal expected');
|
||||
Result := IDataOrdinalType(FDataType);
|
||||
end;
|
||||
|
||||
function TDataType.AsRecord: IDataRecordType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkRecord) then
|
||||
raise EInvalidCast.Create('Record expected');
|
||||
Result := IDataRecordType(FDataType);
|
||||
end;
|
||||
|
||||
function TDataType.AsText: IDataTextType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkText) then
|
||||
raise EInvalidCast.Create('Text expected');
|
||||
Result := IDataTextType(FDataType);
|
||||
end;
|
||||
|
||||
function TDataType.AsTimestamp: IDataTimestampType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkTimestamp) then
|
||||
raise EInvalidCast.Create('Timestamp expected');
|
||||
Result := IDataTimestampType(FDataType);
|
||||
end;
|
||||
|
||||
function TDataType.AsTuple: IDataTupleType;
|
||||
begin
|
||||
if (FDataType.Kind <> dkTuple) then
|
||||
raise EInvalidCast.Create('Tuple expected');
|
||||
Result := IDataTupleType(FDataType);
|
||||
end;
|
||||
|
||||
class procedure TDataType.ClearRegistries;
|
||||
begin
|
||||
FArrayTypeRegistry.Clear;
|
||||
FRecordTypeRegistry.Clear;
|
||||
end;
|
||||
|
||||
class function TDataType.Float: IDataFloatType;
|
||||
begin
|
||||
Result := TDataFloatType.Singleton;
|
||||
@@ -287,6 +366,11 @@ begin
|
||||
Result := TDataEnumType.Create(AName, AIdentifiers);
|
||||
end;
|
||||
|
||||
function TDataType.GetKind: TDataKind;
|
||||
begin
|
||||
Result := FDataType.Kind;
|
||||
end;
|
||||
|
||||
class function TDataType.Text: IDataTextType;
|
||||
begin
|
||||
Result := TDataTextType.Singleton;
|
||||
|
||||
Reference in New Issue
Block a user