From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 DC4F9BC37 for ; Mon, 8 Feb 2010 05:07:08 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAEACodb0tQW+UMgWdsb2JhbACbPgEBFiS6OoRUBIMr X-IronPort-AV: E=Sophos;i="4.49,425,1262559600"; d="scan'208";a="44003219" Received: from lo.gmane.org ([80.91.229.12]) by mail3-smtp-sop.national.inria.fr with ESMTP; 08 Feb 2010 05:06:39 +0100 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NeKtP-0004Uo-J9 for caml-list@inria.fr; Mon, 08 Feb 2010 05:06:35 +0100 Received: from elehack.net ([216.243.177.100]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 08 Feb 2010 05:06:35 +0100 Received: from michael+ocaml by elehack.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 08 Feb 2010 05:06:35 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Michael Ekstrand Subject: Re: How to wrap around C++? Date: Sun, 07 Feb 2010 22:06:16 -0600 Message-ID: References: <28fa90931002071813k7330ad34s7a2ec8b4cf1c3d11@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: elehack.net User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.7) Gecko/20100111 Lightning/1.0b1 Thunderbird/3.0.1 In-Reply-To: <28fa90931002071813k7330ad34s7a2ec8b4cf1c3d11@mail.gmail.com> Sender: news X-Spam: no; 0.00; ocaml:01 implements:01 ocaml:01 swig:01 wrappers:01 interfacing:01 stub:01 extern:01 compiler:01 swig:01 ocaml's:01 pointer:01 finalizer:01 stubs:01 higher-level:01 On 02/07/2010 08:13 PM, Luca de Alfaro wrote: > Essentially, the C++ object implements access to a file via some > compression, etc, mechanism. In C++, one creates the object, calls > write and read methods, calls the method for closing the file, and > deletes the object. > > How can I wrap around such an object in Ocaml? Is it possible? Any > advice? It is possible. One way would be to use Swig. The other is to write the wrappers yourself, providing OCaml external functions for each of the methods you want to call. If it's just a few methods, it isn't too difficult. The documentation on interfacing with OCaml from C (in the manual) is quite helpful with several examples, albeit all for C. For C++, there are just a few additional things to think about (that I know of): * Make sure no C++ exceptions leak to OCaml. * Wrap your OCaml includes in 'extern "C" { ... }" * Export all your stub functions with C linkage (extern "C") * Compiling is tricky, since the OCaml compiler driver doesn't know what to do with C++. The Swig documentation[1] has a workaround for this, useful even if you don't use Swig. You can use OCaml's custom object support to manage the class instnaces. Your custom block will store a pointer to the object. The finalizer will then need to take care of deleting the object (or decreasing its reference count, or whatever is appropriate). The basic thing you'll do is to create an ML file defining an abstract type for your object, external functions for your wrapper stubs (which need to be plain C functions taking and returning OCaml values), and then whatever higher-level convenience functions (or classes, if you want to re-wrap the C++ code in an OCaml object-oriented interface) you want to expose for actually using the module. - Michael