From: Michael Ekstrand <michael+ocaml@elehack.net>
To: caml-list@inria.fr
Subject: Re: How to wrap around C++?
Date: Sun, 07 Feb 2010 22:06:16 -0600 [thread overview]
Message-ID: <hko2jm$r2a$1@ger.gmane.org> (raw)
In-Reply-To: <28fa90931002071813k7330ad34s7a2ec8b4cf1c3d11@mail.gmail.com>
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
next prev parent reply other threads:[~2010-02-08 4:07 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-08 2:13 Luca de Alfaro
2010-02-08 4:06 ` Michael Ekstrand [this message]
2010-02-08 4:17 ` Michael Ekstrand
2010-02-08 16:03 ` [Caml-list] " Luca de Alfaro
2010-02-08 16:33 ` Luca de Alfaro
2010-02-08 17:44 ` Guillaume Yziquel
2010-02-08 17:57 ` Luca de Alfaro
2010-02-09 6:53 ` Basile STARYNKEVITCH
2010-02-08 17:54 ` Guillaume Yziquel
2010-02-09 0:25 ` Michael Ekstrand
2010-02-08 12:38 ` [Caml-list] " Guillaume Yziquel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='hko2jm$r2a$1@ger.gmane.org' \
--to=michael+ocaml@elehack.net \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox