caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* wrapping external classes
@ 2008-12-11  2:20 Alexy Khrabrov
  2008-12-11 10:37 ` [Caml-list] " Mauricio Fernandez
  0 siblings, 1 reply; 4+ messages in thread
From: Alexy Khrabrov @ 2008-12-11  2:20 UTC (permalink / raw)
  To: caml-list

I'm wrapping a C++ library in ocaml bindings.  Originally I wrote an  
in-C++ object pool handler returning integer handles, and wrapped the  
constructor, destructor, and the main compute function in  my  
lmclient.ml file as create, destroy, and compute functions, then  
declared in OCaml as

external  create  : string -> int -> int    = "lmclient_create"
external  destroy : int -> int              = "lmclient_destroy"
external  compute : int -> string -> string = "lmclient_compute"

I can call them as Lmclient.create, etc.  The create returns an  
integer handle which is then used by others to compute and destroy the  
corresponding C++ object inside my pool manager.

Now I want to avoid the explicit destroy, and want to add more  
methods, so I'd like to wrap these three and more into an OCaml  
object.  How do I glue the external definitions to object methods?   
Apparently method external or external method doesn't parse...

Cheers,
Alexy


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] wrapping external classes
  2008-12-11  2:20 wrapping external classes Alexy Khrabrov
@ 2008-12-11 10:37 ` Mauricio Fernandez
  2008-12-11 15:07   ` Alain Frisch
  0 siblings, 1 reply; 4+ messages in thread
From: Mauricio Fernandez @ 2008-12-11 10:37 UTC (permalink / raw)
  To: caml-list

On Wed, Dec 10, 2008 at 09:20:41PM -0500, Alexy Khrabrov wrote:
> I'm wrapping a C++ library in ocaml bindings.  Originally I wrote an  
> in-C++ object pool handler returning integer handles, and wrapped the  
> constructor, destructor, and the main compute function in  my  
> lmclient.ml file as create, destroy, and compute functions, then  
> declared in OCaml as
>
> external  create  : string -> int -> int    = "lmclient_create"
> external  destroy : int -> int              = "lmclient_destroy"
> external  compute : int -> string -> string = "lmclient_compute"
>
> I can call them as Lmclient.create, etc.  The create returns an integer 
> handle which is then used by others to compute and destroy the  
> corresponding C++ object inside my pool manager.
>
> Now I want to avoid the explicit destroy, and want to add more methods, 
> so I'd like to wrap these three and more into an OCaml object.  How do I 
> glue the external definitions to object methods?  Apparently method 
> external or external method doesn't parse...

(* an abstract type is better than int --- no change needed in the
 * C/C++ bindings *)
type handle
external create : string -> int -> handle = "lmclient_create"
external destroy : handle -> unit = "lmclient_destroy"
external compute : handle -> string -> string = "lmclient_compute"

class foo s n =
object(self)
  val handle = create s n

  method compute = compute handle

  initializer
    (* use a destroy method explicitly if you need to make sure the destructor
     * is called --- Gc.finalise doesn't guarantee this (e.g. the program might
     * terminate before the finalisation function is executed) *)
    Gc.finalise destroy handle
end

-- 
Mauricio Fernandez  -   http://eigenclass.org


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] wrapping external classes
  2008-12-11 10:37 ` [Caml-list] " Mauricio Fernandez
@ 2008-12-11 15:07   ` Alain Frisch
  2008-12-11 16:21     ` Florent Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2008-12-11 15:07 UTC (permalink / raw)
  To: caml-list

Mauricio Fernandez wrote:
> class foo s n =
> object(self)
>   val handle = create s n
> 
>   method compute = compute handle
> 
>   initializer
>     (* use a destroy method explicitly if you need to make sure the destructor
>      * is called --- Gc.finalise doesn't guarantee this (e.g. the program might
>      * terminate before the finalisation function is executed) *)
>     Gc.finalise destroy handle
> end

If the handle is really an OCaml integer, it is not possible to attach a 
finalizer to it. The finalizer could be attached to the object that 
wraps the handle; but the best solution is to use custom blocks and C 
finalizers in the C binding.

-- Alain


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] wrapping external classes
  2008-12-11 15:07   ` Alain Frisch
@ 2008-12-11 16:21     ` Florent Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Florent Monnier @ 2008-12-11 16:21 UTC (permalink / raw)
  To: caml-list

> >     (* use a destroy method explicitly if you need to make sure the
> > destructor * is called --- Gc.finalise doesn't guarantee this (e.g. the
> > program might * terminate before the finalisation function is executed)

you can use at_exit to garanty a call before termination

> If the handle is really an OCaml integer, it is not possible to attach a
> finalizer to it. The finalizer could be attached to the object that
> wraps the handle; but the best solution is to use custom blocks and C
> finalizers in the C binding.

if you are not so fluent in C/C++ there is this work-around that does work:
http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php#ref_finalise
but indeed the classic way is to use the custom_operations struct:
http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php#ref_custom


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-12-11 16:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-11  2:20 wrapping external classes Alexy Khrabrov
2008-12-11 10:37 ` [Caml-list] " Mauricio Fernandez
2008-12-11 15:07   ` Alain Frisch
2008-12-11 16:21     ` Florent Monnier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).