* [Caml-list] C++ for Ocaml FFI bindings @ 2017-01-03 5:54 Chet Murthy 2017-01-03 10:34 ` Fabrice Le Fessant 0 siblings, 1 reply; 3+ messages in thread From: Chet Murthy @ 2017-01-03 5:54 UTC (permalink / raw) To: Caml List Hi, I've been watching the discussions over the years regarding FFI bindings from Ocaml. I've built a ton of such bindings using Camlidl. I've used some done using Ctypes. And it's always hard. But recently, I've come to the conclusion that bindings against C++ would be (shockingly, yes I know) -easier- than against C. To demonstrate this, and because I had need of it, I've writte a little IDL compiler, along with an example FFI wrappering of the C++ Rocksdb API. Only as an example. The code is raw, and really .... raw. But it works, works well, and is dreamily easy to add new entry-points to. I'm hoping that over the next few weeks, as I use this, I'll add tests, more examples, and documentation. --> oboy, does it need documentation (if anybody but me is -ever- gonna use it) Right now though, I just thought I'd send this note, to try to raise the subject that .... Maybe C++ is a ripe target for FFIs from Ocaml? I've released the code (such as it is) on Github: https://github.com/chetmurthy/ocaml-cppffigen https://github.com/chetmurthy/ocaml-rocksdb Please don't take this as an advertisement for the code. But do take it as an advertisement for the -idea-. Cheers, --chet-- ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] C++ for Ocaml FFI bindings 2017-01-03 5:54 [Caml-list] C++ for Ocaml FFI bindings Chet Murthy @ 2017-01-03 10:34 ` Fabrice Le Fessant 2017-01-03 12:59 ` immanuel litzroth 0 siblings, 1 reply; 3+ messages in thread From: Fabrice Le Fessant @ 2017-01-03 10:34 UTC (permalink / raw) To: Chet Murthy, Caml List [-- Attachment #1: Type: text/plain, Size: 2670 bytes --] Hi Chet, I wrote a very basic tool to generate OCaml bindings for the WxWidgets C++ library (https://github.com/OCamlPro/ocplib-wxOCaml), and then a very different one for LLDB (https://github.com/OCamlPro/typerex-lldb). In both cases, methods of C++ class A are mapped to functions of OCaml module A. The main difference is that WxWidgets makes heavy use of inheritance, while LLDB does not use it at all. Consequently, there are many "cast" functions in WxOCaml, while there are none for LLDB. Also, LLDB is very strict on the syntax of its include files, which makes it very easy to parse them directly from OCaml (there is no need for a DSL), whereas for WxWidgets, I had to copy and slightly modify the C++ method definitions into a subset (the DSL) that could be used from OCaml. So, in your bindings, which OCaml type do you target for a C++ class ? Also, is there some support for garbage collection ? How do you detect that a C++ object is removed, so that the corresponding OCaml object can be removed ? Is it generalizable to any C++ framework ? Cheers, --Fabrice On Tue, Jan 3, 2017 at 6:55 AM Chet Murthy <murthy.chet@gmail.com> wrote: > Hi, I've been watching the discussions over the years regarding FFI > bindings from Ocaml. I've built a ton of such bindings using Camlidl. > I've used some done using Ctypes. And it's always hard. But > recently, I've come to the conclusion that bindings against C++ would > be (shockingly, yes I know) -easier- than against C. > > To demonstrate this, and because I had need of it, I've writte a > little IDL compiler, along with an example FFI wrappering of the C++ > Rocksdb API. Only as an example. The code is raw, and really > .... raw. But it works, works well, and is dreamily easy to add new > entry-points to. I'm hoping that over the next few weeks, as I use > this, I'll add tests, more examples, and documentation. > > --> oboy, does it need documentation (if anybody but me is -ever- > gonna use it) > > Right now though, I just thought I'd send this note, to try to raise > the subject that .... > > Maybe C++ is a ripe target for FFIs from Ocaml? > > I've released the code (such as it is) on Github: > > https://github.com/chetmurthy/ocaml-cppffigen > > https://github.com/chetmurthy/ocaml-rocksdb > > Please don't take this as an advertisement for the code. But do take > it as an advertisement for the -idea-. > > Cheers, > --chet-- > > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > [-- Attachment #2: Type: text/html, Size: 4580 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] C++ for Ocaml FFI bindings 2017-01-03 10:34 ` Fabrice Le Fessant @ 2017-01-03 12:59 ` immanuel litzroth 0 siblings, 0 replies; 3+ messages in thread From: immanuel litzroth @ 2017-01-03 12:59 UTC (permalink / raw) To: Fabrice Le Fessant; +Cc: Chet Murthy, Caml List [-- Attachment #1: Type: text/plain, Size: 3303 bytes --] It is possible to write generic C++ for mapping any C++ class as a new type in ocaml by wrapping it in a shared_ptr or unique_ptr, in such a way that the object can be kept alive by the c++ side or by the ocaml side. There is some code on the net that explores that idea here: https://github.com/ygrek/scraps/blob/master/cxx_wrapped.h but that seems to be using the older auto_ptr at the moment. The code I have written for this is proprietary. Immanuel On Tue, Jan 3, 2017 at 11:34 AM, Fabrice Le Fessant < Fabrice.Le_fessant@inria.fr> wrote: > Hi Chet, > > I wrote a very basic tool to generate OCaml bindings for the WxWidgets > C++ library (https://github.com/OCamlPro/ocplib-wxOCaml), and then a very > different one for LLDB (https://github.com/OCamlPro/typerex-lldb). In > both cases, methods of C++ class A are mapped to functions of OCaml module > A. The main difference is that WxWidgets makes heavy use of inheritance, > while LLDB does not use it at all. Consequently, there are many "cast" > functions in WxOCaml, while there are none for LLDB. Also, LLDB is very > strict on the syntax of its include files, which makes it very easy to > parse them directly from OCaml (there is no need for a DSL), whereas for > WxWidgets, I had to copy and slightly modify the C++ method definitions > into a subset (the DSL) that could be used from OCaml. > > So, in your bindings, which OCaml type do you target for a C++ class ? > Also, is there some support for garbage collection ? How do you detect that > a C++ object is removed, so that the corresponding OCaml object can be > removed ? Is it generalizable to any C++ framework ? > > Cheers, > --Fabrice > > > > On Tue, Jan 3, 2017 at 6:55 AM Chet Murthy <murthy.chet@gmail.com> wrote: > >> Hi, I've been watching the discussions over the years regarding FFI >> bindings from Ocaml. I've built a ton of such bindings using Camlidl. >> I've used some done using Ctypes. And it's always hard. But >> recently, I've come to the conclusion that bindings against C++ would >> be (shockingly, yes I know) -easier- than against C. >> >> To demonstrate this, and because I had need of it, I've writte a >> little IDL compiler, along with an example FFI wrappering of the C++ >> Rocksdb API. Only as an example. The code is raw, and really >> .... raw. But it works, works well, and is dreamily easy to add new >> entry-points to. I'm hoping that over the next few weeks, as I use >> this, I'll add tests, more examples, and documentation. >> >> --> oboy, does it need documentation (if anybody but me is -ever- >> gonna use it) >> >> Right now though, I just thought I'd send this note, to try to raise >> the subject that .... >> >> Maybe C++ is a ripe target for FFIs from Ocaml? >> >> I've released the code (such as it is) on Github: >> >> https://github.com/chetmurthy/ocaml-cppffigen >> >> https://github.com/chetmurthy/ocaml-rocksdb >> >> Please don't take this as an advertisement for the code. But do take >> it as an advertisement for the -idea-. >> >> Cheers, >> --chet-- >> >> >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa.inria.fr/sympa/arc/caml-list >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs >> > [-- Attachment #2: Type: text/html, Size: 6684 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-01-03 13:00 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2017-01-03 5:54 [Caml-list] C++ for Ocaml FFI bindings Chet Murthy 2017-01-03 10:34 ` Fabrice Le Fessant 2017-01-03 12:59 ` immanuel litzroth
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox