Version française
Home     About     Download     Resources     Contact us    
Browse thread
question about how to bind c++ classes to ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] question about how to bind c++ classes to ocaml
From: micha <micha-1@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