caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] What's the best practice for bindings for c++ templates?
@ 2015-07-28 18:40 Goswin von Brederlow
  2015-07-29  9:27 ` Xavier Leroy
  2015-07-29 12:36 ` AW: " Bauer, Christoph
  0 siblings, 2 replies; 4+ messages in thread
From: Goswin von Brederlow @ 2015-07-28 18:40 UTC (permalink / raw)
  To: OCaml Mailing List

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-07-30 18:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-28 18:40 [Caml-list] What's the best practice for bindings for c++ templates? Goswin von Brederlow
2015-07-29  9:27 ` Xavier Leroy
2015-07-29 12:36 ` AW: " Bauer, Christoph
2015-07-30 18:00   ` Goswin von Brederlow

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).