Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force `const` to pass by reference (aka the missing `in` parameter)

Delphi has:

var : pass by reference; parameter is both input and output.
out : pass by reference; parameter is output only.
const: pass by ..... well it depends; parameter is input only.
in : pass by reference; parameter is input only and will not be changed there is no "in".

I don't mind that there is no spoon, but I miss in; considering the following code, is there a cleaner way of doing this?

type TFastDiv = record 
strict private
  FBuffer: Int64;
  other fields
....

//Must be `var` because `const` would pass a Int64 by value
//                      |||
//                      VVV
function DivideFixedI32(var Buffer: Int64; x: integer): integer;
asm  
  mov  r9,rcx                   
  imul dword ptr[rcx]    // do stuff with the buffer
  ..
  mov     ecx, [r9+4]    // do other stuff with the rest of the buffer  

{Changing the code to imul ecx;...;shr r9,32;mov ecx,r9d would allow pass by value, but let's assume the code must not be changed.}

class operator TFastDiv.IntDivide(x:integer; const buffer:TFastDiv):integer;
begin
  Result:= DivideFixedI32(Int64((@buffer.FBuffer)^), abs(x)); <<-- Ugly
  if (x < 0) then Result:= - Result;
end;

DivideFixed will never change the buffer. The whole point of the routine is that buffer is a precalculated value that does not change.

In the class operator I declare buffer as const, because the record must not change.

The question is:
If I insist on declaring the buffer parameter in IntDivide as const is there a cleaner way of coding or am I stuck in the pointer_to/points_to hack?

like image 744
Johan Avatar asked Sep 30 '13 12:09

Johan


1 Answers

Newer compiler versions (from XE3 onwards) support the [Ref] decorator:

procedure Foo(const [Ref] Arg1: Integer; [Ref] const Arg2: Byte);

Example adapted from the documentation, which emphasises the [Ref] can go either before or after the const keyword.

like image 159
Chris Rolliston Avatar answered Oct 14 '22 15:10

Chris Rolliston