Data Types
This commit is contained in:
@@ -24,7 +24,8 @@ type
|
||||
implementation
|
||||
|
||||
uses
|
||||
System.SyncObjs;
|
||||
System.SyncObjs,
|
||||
System.Rtti;
|
||||
|
||||
type
|
||||
// Implements the array data value.
|
||||
@@ -34,6 +35,7 @@ type
|
||||
FItems: TArray<IDataValue>;
|
||||
function GetDataType: IDataType;
|
||||
function GetAsString: string;
|
||||
function AsTValue: TValue;
|
||||
function GetElementCount: Integer;
|
||||
function GetItem(Idx: Integer): IDataValue;
|
||||
public
|
||||
@@ -42,10 +44,15 @@ type
|
||||
|
||||
// Optimized implementation for an array with 0 elements.
|
||||
TDataArrayValue0 = class(TInterfacedObject, IDataValue, IDataArrayValue)
|
||||
strict private
|
||||
class var
|
||||
FEmptyTValue: TValue; // Singleton TValue for an empty array
|
||||
class constructor CreateClass;
|
||||
private
|
||||
FArrayType: IDataArrayType;
|
||||
function GetDataType: IDataType;
|
||||
function GetAsString: string;
|
||||
function AsTValue: TValue;
|
||||
function GetElementCount: Integer;
|
||||
function GetItem(Idx: Integer): IDataValue;
|
||||
public
|
||||
@@ -59,6 +66,7 @@ type
|
||||
FItem0: IDataValue;
|
||||
function GetDataType: IDataType;
|
||||
function GetAsString: string;
|
||||
function AsTValue: TValue;
|
||||
function GetElementCount: Integer;
|
||||
function GetItem(Idx: Integer): IDataValue;
|
||||
public
|
||||
@@ -104,6 +112,17 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TDataArrayValue.AsTValue: TValue;
|
||||
var
|
||||
tvalueArray: TArray<TValue>;
|
||||
i: Integer;
|
||||
begin
|
||||
SetLength(tvalueArray, Length(FItems));
|
||||
for i := 0 to High(FItems) do
|
||||
tvalueArray[i] := FItems[i].AsTValue;
|
||||
Result := TValue.From<TArray<TValue>>(tvalueArray);
|
||||
end;
|
||||
|
||||
function TDataArrayValue.GetElementCount: Integer;
|
||||
begin
|
||||
Result := Length(FItems);
|
||||
@@ -116,6 +135,12 @@ end;
|
||||
|
||||
{ TDataArrayValue0 }
|
||||
|
||||
class constructor TDataArrayValue0.CreateClass;
|
||||
begin
|
||||
// Create a singleton TValue representing an empty dynamic array.
|
||||
FEmptyTValue := TValue.From<TArray<TValue>>([]);
|
||||
end;
|
||||
|
||||
constructor TDataArrayValue0.Create(const AArrayType: IDataArrayType);
|
||||
begin
|
||||
inherited Create;
|
||||
@@ -127,6 +152,11 @@ begin
|
||||
Result := '[]';
|
||||
end;
|
||||
|
||||
function TDataArrayValue0.AsTValue: TValue;
|
||||
begin
|
||||
Result := FEmptyTValue;
|
||||
end;
|
||||
|
||||
function TDataArrayValue0.GetDataType: IDataType;
|
||||
begin
|
||||
Result := FArrayType;
|
||||
@@ -156,6 +186,15 @@ begin
|
||||
Result := '[' + FItem0.AsString + ']';
|
||||
end;
|
||||
|
||||
function TDataArrayValue1.AsTValue: TValue;
|
||||
var
|
||||
tvalueArray: TArray<TValue>;
|
||||
begin
|
||||
SetLength(tvalueArray, 1);
|
||||
tvalueArray[0] := FItem0.AsTValue;
|
||||
Result := TValue.From<TArray<TValue>>(tvalueArray);
|
||||
end;
|
||||
|
||||
function TDataArrayValue1.GetDataType: IDataType;
|
||||
begin
|
||||
Result := FArrayType;
|
||||
@@ -181,14 +220,13 @@ constructor TDataArrayType.Create(const AElementType: IDataType);
|
||||
begin
|
||||
inherited Create;
|
||||
FElementType := AElementType;
|
||||
FEmptyValue := nil;
|
||||
FEmptyValue := TDataArrayValue0.Create(Self);
|
||||
end;
|
||||
|
||||
function TDataArrayType.CreateValue(const AItems: array of IDataValue): IDataArrayValue;
|
||||
var
|
||||
item: IDataValue;
|
||||
i: Integer;
|
||||
newValue: IDataArrayValue;
|
||||
begin
|
||||
// Validate that all items match the element type of this array type.
|
||||
i := 0;
|
||||
@@ -203,17 +241,7 @@ begin
|
||||
|
||||
// Use optimized implementations for arrays with 0 or 1 elements.
|
||||
case Length(AItems) of
|
||||
0:
|
||||
begin
|
||||
// Thread-safe lazy initialization for the cached empty array value.
|
||||
if FEmptyValue = nil then
|
||||
begin
|
||||
newValue := TDataArrayValue0.Create(Self);
|
||||
if TInterlocked.CompareExchange(Pointer(FEmptyValue), Pointer(newValue), nil) <> nil then
|
||||
newValue := nil; // Another thread won, discard our instance.
|
||||
end;
|
||||
Result := FEmptyValue;
|
||||
end;
|
||||
0: Result := FEmptyValue;
|
||||
1: Result := TDataArrayValue1.Create(Self, AItems);
|
||||
else
|
||||
// Use the generic implementation for arrays with more than 1 element.
|
||||
|
||||
Reference in New Issue
Block a user