From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 871077EEF8 for ; Wed, 29 Jul 2015 11:27:44 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.15,569,1432591200"; d="scan'208";a="171990769" Received: from estephe.inria.fr (HELO [128.93.11.95]) ([128.93.11.95]) by mail2-relais-roc.national.inria.fr with ESMTP; 29 Jul 2015 11:27:44 +0200 Message-ID: <55B89C90.1040600@inria.fr> Date: Wed, 29 Jul 2015 11:27:44 +0200 From: Xavier Leroy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: caml-list@inria.fr References: <20150728184042.GA28321@frosties> In-Reply-To: <20150728184042.GA28321@frosties> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] What's the best practice for bindings for c++ templates? On 28/07/2015 20:40, Goswin von Brederlow wrote: > 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. [...] > > 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? My C++ is rusty, but perhaps you could use the "smart pointer" pattern to equip OCaml's "value" type with appropriate constructors, destructors and assignment operations ("appropriate" meaning that they register and unregister the address of the value with the GC as needed). Then, you would instantiate your template with this "smart value" type. Just an idea. - Xavier Leroy