[PATCH] Move management operator
Original Reporter info from Mantis: ryan_joseph @genericptr
-
Reporter name: Ryan Joseph
Original Reporter info from Mantis: ryan_joseph @genericptr
- Reporter name: Ryan Joseph
Description:
Introduces a new "Move" management operator which works as a companion to "Copy" to improve efficiency.
Currently during all assignments the Copy operator is called, even if the record being copied is "temporary" and thus doesn't have a static memory address. With the proposed patch the Move operator will now be called in these situations which gives the programmer the option to simply move memory instead of performing an actual copy. If the Move operator is not present the Copy operator will be called always.
Additional information:
{$mode objfpc}
{$modeswitch advancedrecords}
program test;
type
TMyRecord = record
data: TData;
constructor Create(val: integer);
class operator Copy(constref aSrc: TMyRecord; var aDst: TMyRecord);
class operator Move(constref aSrc: TMyRecord; var aDst: TMyRecord);
end;
constructor TMyRecord.Create(val: integer);
begin
data := TData.Create(val);
end;
class operator TMyRecord.Copy(constref aSrc: TMyRecord; var aDst: TMyRecord);
begin
writeln('TMyRecord.Copy ', HexStr(@aSrc), ' to ', HexStr(@aDst));
{ ...do something... }
aSrc.data := TData.Create;
end;
class operator TMyRecord.Move(constref aSrc: TMyRecord; var aDst: TMyRecord);
begin
writeln('TMyRecord.Move ', HexStr(@aSrc), ' to ', HexStr(@aDst));
aSrc.data := ADst.data;
end;
operator := (right: integer): TMyRecord;
begin
{ Move operator will be called because right hand side is temporary }
result := TMyRecord.Create(right);
end;
var
a, b: TMyRecord;
begin
{ Move operator is called twice saving two copies without the move operator defined }
a := 1;
{ Copy operator is called because "a" has static address }
b := a;
end.
Mantis conversion info:
- Mantis ID: 35823
- Version: 3.3.1