From: Goswin von Brederlow <goswin-v-b@web.de>
To: OCaml Mailing List <caml-list@inria.fr>
Subject: [Caml-list] What's the best practice for bindings for c++ templates?
Date: Tue, 28 Jul 2015 20:40:42 +0200 [thread overview]
Message-ID: <20150728184042.GA28321@frosties> (raw)
Hi,
I'm halfway done writing a c++ library for a network protocol and
considering writing some bindings for ocaml. The library uses
templates to make objects customizable and am wondering what the best
practice is there.
For a simple example say I have the following:
----------------------------------------------------------------------
template <typename T>
class Foo {
public:
Foo(T t);
~Foo();
T swap(T t);
T get();
};
----------------------------------------------------------------------
type 'a t
val make : 'a -> 'a t
val swap : 'a t -> 'a -> 'a
val get : 'a t -> 'a
----------------------------------------------------------------------
1) For the stub code I need to instantiate the template with 'a, which
obviously doesn't work since the C++ stub has no idea about what type
ocaml will use in the future.
So do I instantiate it with void *? uintptr_t? value?
2) I'm storing ocaml values in C++ structures and getting them back.
How do I make that play nice with the GC?
3) I have the option of changing the libraries API as it's not set in
stone yet. What would be beneficial to interfacing with ocaml (and
maybe python) without making the c++ API to horrible?
E.g. Foo could have:
void set_T_destructor(void (*destructor)(T *t));
Then ~Foo() would call 'destructor(&t)' to "free" the stored T, which
would unregister it from the GC.
Does that make sense? Is it a bad idea?
Would it be better to add a second template parameter for an Allocator
class that handles interfacing with the GC in the stubs?
Any other little tricks to add to the c++ library to make writing
bindings and working with GCs easier?
What's your experience there?
MfG
Goswin
next reply other threads:[~2015-07-28 18:40 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-28 18:40 Goswin von Brederlow [this message]
2015-07-29 9:27 ` Xavier Leroy
2015-07-29 12:36 ` AW: " Bauer, Christoph
2015-07-30 18:00 ` Goswin von Brederlow
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=20150728184042.GA28321@frosties \
--to=goswin-v-b@web.de \
--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