Files
MycLib/Src/Data/Myc.Data.Types.Text.pas
T
Michael Schimmel 947060566d Data Types
2025-08-25 18:00:15 +02:00

138 lines
3.0 KiB
ObjectPascal

unit Myc.Data.Types.Text;
interface
uses
System.SysUtils,
Myc.Data.Types;
type
TDataTextType = class(TInterfacedObject, IDataType, IDataTextType)
strict private
class var
FSingleton: IDataTextType;
class constructor CreateClass;
public
function GetName: String;
function GetKind: TDataKind;
function CreateValue(const AValue: string): IDataTextValue;
class property Singleton: IDataTextType read FSingleton;
end;
implementation
uses
System.Rtti;
type
TDataTextValue = class(TInterfacedObject, IDataValue, IDataTextValue)
private
FValue: string;
function GetDataType: IDataType;
function GetAsString: string;
function GetValue: string;
function AsTValue: TValue;
public
constructor Create(const AValue: string);
end;
// Singleton implementation for the empty string value.
TDataTextValueEmpty = class(TInterfacedObject, IDataValue, IDataTextValue)
strict private
class var
FSingleton: IDataTextValue;
FEmptyTValue: TValue;
class constructor CreateClass;
private
function GetDataType: IDataType;
function GetAsString: string;
function GetValue: string;
function AsTValue: TValue;
public
class property Singleton: IDataTextValue read FSingleton;
end;
{ TDataTextType }
class constructor TDataTextType.CreateClass;
begin
FSingleton := TDataTextType.Create;
end;
function TDataTextType.CreateValue(const AValue: string): IDataTextValue;
begin
// Use the singleton for empty strings.
if (AValue = '') then
Result := TDataTextValueEmpty.Singleton
else
Result := TDataTextValue.Create(AValue);
end;
function TDataTextType.GetName: String;
begin
Result := 'Text';
end;
function TDataTextType.GetKind: TDataKind;
begin
Result := dkText;
end;
{ TDataTextValue }
constructor TDataTextValue.Create(const AValue: string);
begin
inherited Create;
FValue := AValue;
end;
function TDataTextValue.GetDataType: IDataType;
begin
Result := TDataTextType.Singleton;
end;
function TDataTextValue.GetAsString: string;
begin
Result := FValue;
end;
function TDataTextValue.GetValue: string;
begin
Result := FValue;
end;
function TDataTextValue.AsTValue: TValue;
begin
Result := TValue.From<string>(FValue);
end;
{ TDataTextValueEmpty }
class constructor TDataTextValueEmpty.CreateClass;
begin
FSingleton := TDataTextValueEmpty.Create;
FEmptyTValue := TValue.From<string>('');
end;
function TDataTextValueEmpty.GetAsString: string;
begin
Result := '';
end;
function TDataTextValueEmpty.GetDataType: IDataType;
begin
Result := TDataTextType.Singleton;
end;
function TDataTextValueEmpty.GetValue: string;
begin
Result := '';
end;
function TDataTextValueEmpty.AsTValue: TValue;
begin
Result := FEmptyTValue;
end;
end.