, generics . , , . , , .
, InterlockedIncrement, , , Delphi, AtomicIncrement. , , AtomicIncrement, Delphi, . asm. , , , ASM, .
{$IFNDEF AtomicFunctionsAvailable}
function AtomicIncrement(var Target: Integer): Integer;
asm
....
end;
{$ENDIF}
, @TLama, TInterlocked System.SyncObjs .
, , . , SetLength(...) , . , :
unit COWArray;
interface
type
TCOWArray<T> = record
private
FItems: TArray<T>;
function GetLength: Integer;
procedure SetLength(Value: Integer);
function GetItem(Index: Integer): T;
procedure SetItem(Index: Integer; const Value: T);
public
class function New(const Values: array of T): TCOWArray<T>; static;
property Length: Integer read GetLength write SetLength;
property Items[Index: Integer]: T read GetItem write SetItem; default;
end;
implementation
function TCOWArray<T>.GetLength: Integer;
begin
Result := System.Length(FItems);
end;
procedure TCOWArray<T>.SetLength(Value: Integer);
begin
System.SetLength(FItems, Value); // SetLength enforces uniqueness
end;
function TCOWArray<T>.GetItem(Index: Integer): T;
begin
Result := FItems[Index];
end;
procedure TCOWArray<T>.SetItem(Index: Integer; const Value: T);
begin
System.SetLength(FItems, System.Length(FItems)); // SetLength enforces uniqueness
FItems[Index] := Value;
end;
class function TCOWArray<T>.New(const Values: array of T): TCOWArray<T>;
var
i: Integer;
begin
System.SetLength(Result.FItems, System.Length(Values));
for i := 0 to high(Values) do
Result.FItems[i] := Values[i];
end;
end.