[fpc-devel] [] property overloads
Ben Grasset
operator97 at gmail.com
Mon Jul 8 21:47:08 CEST 2019
On Mon, Jul 8, 2019 at 2:22 PM Ryan Joseph <genericptr at gmail.com> wrote:
> and it will actually write to the actual record in the array and not a
> returned copy. However due to how the properties are currently structured
> this means we can’t use the setter without passing pointers
>
Ah, I see what you mean. Note you can at least work around this for now
with operator overloading:
program Example;
{$mode Delphi}{$H+}
uses SysUtils;
type
PVec3F = ^TVec3F;
TVec3F = record
X, Y, Z: Single;
class operator Implicit(constref From: TVec3F): PVec3F; inline;
end;
class operator TVec3F.Implicit(constref From: TVec3F): PVec3F;
begin
Result := @From;
end;
type
TList<T> = record
public type
PT = ^T;
strict private
FData: array of T;
private
function GetItem(const I: PtrUInt): PT; inline;
procedure SetItem(const I: PtrUInt; const Val: PT); inline;
function GetLength: PtrInt; inline;
procedure SetLength(const I: PtrInt); inline;
public
property Items[const I: PtrUInt]: PT read GetItem write SetItem;
default;
property Length: PtrInt read GetLength write SetLength;
end;
function TList<T>.GetItem(const I: PtrUInt): PT;
begin
if I < System.Length(FData) then
Result := @FData[I]
else
Result := nil;
end;
procedure TList<T>.SetItem(const I: PtrUInt; const Val: PT);
begin
if I < System.Length(FData) then
FData[I] := Val^;
end;
function TList<T>.GetLength: PtrInt;
begin
Result := System.Length(FData);
end;
procedure TList<T>.SetLength(const I: PtrInt);
begin
System.SetLength(FData, I);
end;
var
I: PtrUInt;
Vec: TVec3F = (
X: 0.0;
Y: 0.0;
Z: 0.0;
);
VecList: TList<TVec3F>;
begin
VecList.Length := 1;
VecList[0] := Vec;
with VecList[0]^ do begin
X := 2.0;
Y := 4.0;
Z := 6.0;
end;
// Access it again, separately...
with VecList[0]^ do
WriteLn(Format('[%f %f %f]', [X, Y, Z]));
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190708/88a4191b/attachment.html>
More information about the fpc-devel
mailing list