From: "Bauer, Christoph" <bauerchristoph@siemens.com>
To: Goswin von Brederlow <goswin-v-b@web.de>,
OCaml Mailing List <caml-list@inria.fr>
Subject: AW: [Caml-list] What's the best practice for bindings for c++ templates?
Date: Wed, 29 Jul 2015 12:36:53 +0000 [thread overview]
Message-ID: <B604D632D4692C45A95E5EB6E3E0FFF204866B48@DEKOMMBX002.net.plm.eds.com> (raw)
In-Reply-To: <20150728184042.GA28321@frosties>
Hi,
> 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?
1.) It's value, but I think you ocaml api would need an additional parameter in make to describe the type 'a (cmp to float32 in the bigarray library).
> 2) I'm storing ocaml values in C++ structures and getting them back.
> How do I make that play nice with the GC?
My advice: don't do this. It may work for a while but then the GC moves the values and your
program crashes. It would be better to keep the values in OCaml e.g. in a hashtable and to store the key
(integer or string) in C++. With callbacks to ocaml, you could define some setters/getters for the content
of the value itself. This approach means some overhead, but it should work quite well.
Sometimes it is also a better idea to make just a copy of OCaml value (std::string(String_val(value))).
> 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.
So you need something like this (I omit the template stuff):
class Bar {
~Bar() {
// call the custom destructor
}
}
#define CppBar_val(x) (*((Bar**) Data_custom_val(x)))
static
void finalize_cppbar( value v ) {
delete CppBar_val(v);
}
static struct custom_operations cpp_bar_ops = {
"de.web.cpp.bar",
finalize_cppbar,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static value alloc_cppbar(Bar * bar)
{
value v = alloc_custom(&cpp_bar_ops, sizeof(Bar *), 0, 1);
CppBar_val(v) = bar;
return v;
}
As Xavier Leroy suggested, you could also use a shared_ptr<T> instead of Bar* and call reset() on it in the finalize_cppbar.
I didn't tried it.
Regards,
Christoph Bauer
-----------------
Siemens Industry Software GmbH; Anschrift: Franz-Geuer-Str. 10, 50823 Köln; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Urban August, Daniel Trebes; Sitz der Gesellschaft: Köln; Registergericht: Amtsgericht Köln, HRB 84564
next prev parent reply other threads:[~2015-07-29 12:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-28 18:40 Goswin von Brederlow
2015-07-29 9:27 ` Xavier Leroy
2015-07-29 12:36 ` Bauer, Christoph [this message]
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=B604D632D4692C45A95E5EB6E3E0FFF204866B48@DEKOMMBX002.net.plm.eds.com \
--to=bauerchristoph@siemens.com \
--cc=caml-list@inria.fr \
--cc=goswin-v-b@web.de \
/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;
as well as URLs for NNTP newsgroup(s).