[gambit-list] ffi object is calling twice

emil brusic emil.brusic at gmail.com
Tue May 20 11:09:24 EDT 2014


Greetings to everybody!

I have recently tried to use Gambit-C for interfacing OpenCascade (OCE)
library which is written in C++.
So, I am using Gambit-C based on g++ compiler.
I have started with simple vector class i.e. gp_XY which has 2 members, x
and y coordinate.
>From OCE source:

inline void gp_XY::Add (const gp_XY& Other) {
  x += Other.x;
  y += Other.y;
}

This is from my gpXY.scm :

(##namespace ("oce-gp-xy#"))

(##include "~~/lib/gambit#.scm")

(declare (standard-bindings)
         (block))

(c-declare #<<c-declare-end
  #include <gp_XY.hxx>
  extern "C" {
  typedef struct h_gp_XY h_gp_XY;
  typedef struct h_gp_Mat2d h_gp_Mat2d;
  h_gp_XY* gp_XY_create() {
    return reinterpret_cast<h_gp_XY*>(new gp_XY());
  }
  h_gp_XY* gp_XY_create_ff(const Standard_Real x,const Standard_Real y) {
    return reinterpret_cast<h_gp_XY*>(new gp_XY(x,y));
  }
  void gp_XY_destroy(h_gp_XY* h) {
    delete reinterpret_cast<gp_XY*>(h);
  }
  void my_add(gp_XY& Me, const gp_XY& Other) {
     Me.SetX(Me.X() + Other.X());
     Me.SetY(Me.Y() + Other.Y());
  }
}
c-declare-end
)

(c-define-type XY "h_gp_XY")
(c-define-type XY* (pointer XY))
(c-define-type Mat2d "h_gp_Mat2d")
(c-define-type Mat2d* (pointer Mat2d))
...
; problem with add
(define add
  (c-lambda (XY* XY*) void
#<<c-lambda-end
      const gp_XY& other = *(reinterpret_cast<gp_XY*>(___arg2));
      gp_XY* th = reinterpret_cast<gp_XY*>(___arg1);
      th->Add(other);
;      reinterpret_cast<gp_XY*>(___arg1)->Add(other);
c-lambda-end
))

That file is compiled and linked with OCE libraries.
And in gsi interpreter:
> (load "gpXY")
"/home/emil/projects/scheme/gambit/oce/c++/src/gpXY.o1"
> (##include "gpXY#.scm")
> (define p1 (create-ff 1.0 2.0))
> (define p2 (create-ff 10.0 5.0))
> (add p1 p2)
> (x p1)
21.
> (y p1)
12.

Obviously there is bug somewhere, because results should be 11 and 7.
So I wrote my_add function and it calculated add vector correctly.
Is it something wrong in my code?
I do not have experience in Gambit-C.
Any help or hint will be appreciated.

Regards,
Emil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20140520/87b8c1c5/attachment.htm>


More information about the Gambit-list mailing list