Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutual referencing records in Delphi (Win32)

Is there any work-around to create mutual referencing records in Delphi? Here's the simplified version of the code:

MyRec1 = record
  arr: MyRec2Array;
end;

MyRec2 = record
  mr: MyRec1;
end;

MyRec2Array = array of MyRec2;

Apparently forward declaration of record types

MyRec2 = record;

doesn't work in Delphi for Win32.

Switching to classes instead of records is not good, because this will increase memory consumption and code complexity, so I'd rather stay with records.

Any suggestions?

like image 903
Max Avatar asked Jul 11 '10 10:07

Max


3 Answers

Records are value types, not reference types. That means that all records used as members of a larger data structure are placed inline in the structure itself instead of as a pointer. Trying to create two records which contain each other would throw the compiler into an infinite loop while it tries to figure out the structure of the records. That's probably the reason why you can't forward-declare a record, and even though you're trying to insert a reference type (dynamic array) here, you still can't violate the language rules.

But what you can do is declare a pointer-to-record type as a forward declaration, like so:

PMyRec2 = ^MyRec2
...
MyRec2 = record
  ...
end;

Of course, once you start using pointers to records, you have to worry about allocating and freeing the memory, and the code complexity you were trying to avoid by not using classes appears in your project. Bottom line: do this with classes. Make one of the records, if not both of them, a class. It's the simplest way, really.

And the extra memory overhead is negligible. It comes out to a pointer to the object for each reference, which you'd need for pointers to objects anyway, plus one hidden field (4 bytes) per instance before D2009 or two of them (8 bytes) on D2009 or later. That's not very much at all.

like image 74
Mason Wheeler Avatar answered Nov 06 '22 18:11

Mason Wheeler


While I totally agree with Mason, there's a way to hack around the limitation. Basically, you can use a record helper to define necessary functionality after the MyRec2 has been declared.

type
  MyRec1 = record
    arr: array of byte;
  end;

  MyRec2 = record
    mr: MyRec1;
  end;

  MyRec1Helper = record helper for MyRec1
    procedure AllocateMyRec2(numItems: integer);
    function  GetMyRec2(i: integer): MyRec2;
    procedure SetMyRec2(i: integer; const value: MyRec2);
    property Rec2[i: integer]: MyRec2 read GetMyRec2 write SetMyRec2;
  end;

procedure MyRec1Helper.AllocateMyRec2(numItems: integer);
begin
  SetLength(arr, numItems * SizeOf(myRec2));
end;

function MyRec1Helper.GetMyRec2(i: integer): MyRec2;
begin
  Move(arr[i*SizeOf(MyRec2)], Result, SizeOf(MyRec2));
end;

procedure MyRec1Helper.SetMyRec2(i: integer; const value: MyRec2);
begin
  Move(value, arr[i*SizeOf(MyRec2)], SizeOf(MyRec2));
end;

var
  my: MyRec2;

begin
  my.mr.AllocateMyRec2(2);
  my.mr.Rec2[0].mr.AllocateMyRec2(3);
end.
like image 40
gabr Avatar answered Nov 06 '22 19:11

gabr


The question looks like a joke for me - it is not about mutual references, it is about an infinite type definition loop. As for mutual references, they can be resolved by defining types within a record (I used Delphi 2009):

type
  MyRec2 = record
  type
    MyRec2Array = array of MyRec2;
  type
    MyRec1 = record
      arr: MyRec2Array;
    end;
  var
    mr: MyRec1;
  end;

But how to use the above record type? Really funny. :)

procedure TForm1.Button1Click(Sender: TObject);
var
  R: MyRec2;

begin
  SetLength(R.mr.arr, 1);
//  R.mr.arr[0]:= ???;
end;
like image 42
kludg Avatar answered Nov 06 '22 17:11

kludg