Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] question about how to bind c++ classes to ocaml

88 views
Skip to first unread message

micha

unread,
Aug 13, 2006, 4:46:18 PM8/13/06
to caml...@yquem.inria.fr

when binding an ocaml class to a c++ class, what's the preferred
way to access member variables of the c++ class?
One is just to implement the get and set function in ocaml to call
the native get/set functions of the c++ class. That way you allways have
some calls from ocaml to c only to get a value of a c++ object.
Another way would be to add similar member variables to the ocaml class
and everytime the c++ side changes a member it updates the ocaml side
too (through direct access). This way you have an additional binding
(the c++ object knows it's ocaml object), but you can access the member
variables in ocaml through normal ocaml methods.

any comments?

thanks,
Michael

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Jacques Garrigue

unread,
Aug 13, 2006, 8:50:57 PM8/13/06
to mic...@fantasymail.de
From: micha <mic...@fantasymail.de>

> when binding an ocaml class to a c++ class, what's the preferred
> way to access member variables of the c++ class?
> One is just to implement the get and set function in ocaml to call
> the native get/set functions of the c++ class. That way you allways have
> some calls from ocaml to c only to get a value of a c++ object.
> Another way would be to add similar member variables to the ocaml class
> and everytime the c++ side changes a member it updates the ocaml side
> too (through direct access). This way you have an additional binding
> (the c++ object knows it's ocaml object), but you can access the member
> variables in ocaml through normal ocaml methods.

I suppose this very much depends on how you intend to use this
binding, but trying to synchronize members between C++ and ocaml seems
a lot of work. The only advantage is faster read access, so this
should only be done if you have to read this member often, and the
cost is significant. There is also the induced cost of calling back
ocaml every time the member is updated.
Calls from ocaml to C are very cheap. If your access function doesn't
do any allocation (i.e. never calls the GC), you can even make it
faster by marking it as "noalloc". Beware many functions do
allocate, including copy_double or copy_string.

Jacques Garrigue

Jonathan Roewen

unread,
Aug 14, 2006, 3:07:25 AM8/14/06
to Jacques Garrigue
> Calls from ocaml to C are very cheap. If your access function doesn't
> do any allocation (i.e. never calls the GC), you can even make it
> faster by marking it as "noalloc". Beware many functions do
> allocate, including copy_double or copy_string.

Is this a special annotation for the external keyword? A quick glance
over the ocaml manual doesn't show up anything.

Jonathan

Michael Wohlwend

unread,
Aug 14, 2006, 3:24:07 AM8/14/06
to Jacques Garrigue

-------- Original-Nachricht --------
Datum: Mon, 14 Aug 2006 09:20:46 +0900 (JST)
Von: Jacques Garrigue <garr...@math.nagoya-u.ac.jp>
An: mic...@fantasymail.de
Betreff: Re: [Caml-list] question about how to bind c++ classes to ocaml

> Calls from ocaml to C are very cheap. If your access function doesn't
> do any allocation (i.e. never calls the GC), you can even make it
> faster by marking it as "noalloc". Beware many functions do
> allocate, including copy_double or copy_string.

thanks for pointing this out. The "noalloc"-thing isn't really described in the docu? :-)

Another question which came up was: you can only declare normal functions (not methods) as "external", so you have to write a c function for every c++ member function. Do you rebuild the inheritance hierachy somehow through the type system (maybe with those mysterious phantom types?) or just put an ocaml class system on top of it?

cheers
Michael

ps: I know there is the gtk binding, but understanding the complete source is... not that easy :-)
--


Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer

Jacques Garrigue

unread,
Aug 14, 2006, 4:48:57 AM8/14/06
to mic...@fantasymail.de
From: "Michael Wohlwend" <mic...@fantasymail.de>

> > Calls from ocaml to C are very cheap. If your access function doesn't
> > do any allocation (i.e. never calls the GC), you can even make it
> > faster by marking it as "noalloc". Beware many functions do
> > allocate, including copy_double or copy_string.
>
> thanks for pointing this out. The "noalloc"-thing isn't really
> described in the docu? :-)

I suppose this is because it is a bit tricky. You have to look at all
the dependencies to be sure that the function will never trigger the
GC, and you might easily get it wrong.

> Another question which came up was: you can only declare normal
> functions (not methods) as "external", so you have to write a c
> function for every c++ member function. Do you rebuild the
> inheritance hierachy somehow through the type system (maybe with
> those mysterious phantom types?) or just put an ocaml class system
> on top of it?

My approach in lablgtk was to first use phantom types, ie use
parameters in abstract types to simulate subtyping. Then there is a
second layer using ocaml objects, but I'm not sure you need it in
general. It isonly needed because GTK+ has so many classes that it is
difficult to track which function comes from which class.

In Labltk for instance, there are only phantom types, and without
subtyping (it uses a coercion function "coe" instead). This works well
if you have a hierarchy with few layers (only two in labltk)

Cheers,

Jacques Garrigue

0 new messages