From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 4116CBB84 for ; Thu, 11 Dec 2008 11:37:26 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.33,752,1220220000"; d="scan'208";a="20231812" Received: from discorde.inria.fr ([192.93.2.38]) by mail3-smtp-sop.national.inria.fr with ESMTP; 11 Dec 2008 11:37:26 +0100 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id mBBAbP7o009958 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 11 Dec 2008 11:37:25 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtICAKh7QEnVBJXybGdsb2JhbACBbJFhDQgNBw8FtleCdw X-IronPort-AV: E=Sophos;i="4.33,752,1220220000"; d="scan'208";a="20231809" Received: from outmailhost.telefonica.net (HELO ctsmtpout2.frontal.correo) ([213.4.149.242]) by mail3-smtp-sop.national.inria.fr with ESMTP; 11 Dec 2008 11:37:24 +0100 Received: from NANA.localdomain (83.54.187.18) by ctsmtpout2.frontal.correo (7.2.056.6) (authenticated as ferferse$telefonica.net) id 493F744100046694 for caml-list@inria.fr; Thu, 11 Dec 2008 11:37:24 +0100 Received: from mfp by NANA.localdomain with local (Exim 4.69) (envelope-from ) id 1LAiv5-000278-FQ for caml-list@inria.fr; Thu, 11 Dec 2008 11:37:23 +0100 Date: Thu, 11 Dec 2008 11:37:23 +0100 From: Mauricio Fernandez To: caml-list@inria.fr Subject: Re: [Caml-list] wrapping external classes Message-ID: <20081211103723.GE18977@NANA.localdomain> Mail-Followup-To: caml-list@inria.fr References: <8119C403-2D50-4DFA-901C-FC3F8409843E@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8119C403-2D50-4DFA-901C-FC3F8409843E@gmail.com> User-Agent: Mutt/1.5.17+20080114 (2008-01-14) X-Miltered: at discorde with ID 4940ED65.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 bindings:01 ocaml:01 bindings:01 foo:01 val:01 10,:98 wrote:01 wrote:01 abstract:01 integer:01 integer:01 terminate:01 caml-list:01 functions:01 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