On Sunday 29 January 2006 23:53, Christian Iversen wrote:
> This is _only_ a proof-of-concept, but it goes to show that it's quite
> doable to link in C++ code. Please once again keep in mind that the access
> method will look much nicer in a possible real version.

Just to correct myself - I clearly wasn't thinking. Here's an improved version 
that removes ALL the g++ cruft from the code. It's SO much cleaner now. (I 
forgot to link to libstdc++ ;-)

-- 
Regards,
Christian Iversen
program test;

{$LINK 'test.o'}
{$LINKLIB 'c'}
{$LINKLIB 'stdc++'}

uses
  LibC, sysutils;

type
  tfrobber = procedure(obj: pointer; x: integer); stdcall;
  tnicater = procedure(obj: pointer); stdcall;

  // VMTs for Foo and Bar

  tfoovmt =
    record
      frob: procedure(obj: pointer; x: integer); stdcall;
      nicate: procedure(obj: pointer); stdcall;
    end;
  pfoovmt = ^tfoovmt;

  tbarvmt =
    record
      parent: tfoovmt;
      frobnicate: procedure(obj: pointer; x: integer); stdcall;
    end;
  pbarvmt = ^tbarvmt;

  // Memory maps for Foo and Bar

  tfoovars =
    record
      x: integer;
    end;

  tbarvars =
    record
      parent: tfoovars;
    end;

  // Class maps for Foo and Bar

  tfooclass =
    record
      vmt: pfoovmt;
      vars: tfoovars;
    end;
  pfooclass = ^tfooclass;

  tbarclass =
    record
      vmt: pbarvmt;
      vars: tbarvars;
    end;
  pbarclass = ^tbarclass;

  // Custom constructors

function new_foo(): pfooclass; external name '_Z7new_foov';
function new_bar(): pbarclass external name '_Z7new_barv';

var
  x: pfooclass;
  y: pbarclass;
begin
  x := new_foo();
  y := new_bar();
 
  x.vmt.frob(x, 42);
  x.vmt.nicate(x);
  writeln('Reading back a variable: ', x.vars.x);

  y.vmt.frobnicate(y, 3);

  y.vmt.parent.frob(y, 17);
  y.vmt.parent.nicate(y);
  writeln('Reading back a variable: ', y.vars.parent.x);
end.

Reply via email to