From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA06005; Wed, 14 Nov 2001 10:52:00 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA05994 for ; Wed, 14 Nov 2001 10:51:59 +0100 (MET) Received: from lancelot.crf.canon.fr (lancelot.crf.canon.fr [194.2.158.66]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id fAE9pwT13503 for ; Wed, 14 Nov 2001 10:51:58 +0100 (MET) Received: (from smap@localhost) by lancelot.crf.canon.fr (8.11.4/8.11.4) id fAE9m5g27582 for ; Wed, 14 Nov 2001 10:48:06 +0100 (MET) X-Authentication-Warning: lancelot.crf.canon.fr: smap set sender to using -f Received: from mercure.crf.canon.fr(intranet.crf.canon.fr 194.2.81.249) by lancelot via smap (V2.0beta) id xma027578; Wed, 14 Nov 01 10:47:51 +0100 Received: from crf.canon.fr ([194.2.81.32]) by mercure.crf.canon.fr (Netscape Messaging Server 4.15) with ESMTP id GMSALG00.SIF; Wed, 14 Nov 2001 10:34:28 +0100 Message-ID: <3BF23EE4.BB444C5C@crf.canon.fr> Date: Wed, 14 Nov 2001 10:52:36 +0100 From: "NASSOR Eric" Organization: Canon Research Centre France X-Mailer: Mozilla 4.7 [en] (WinNT; I) X-Accept-Language: en,fr,ja MIME-Version: 1.0 To: Dmitry Bely CC: caml-list@inria.fr Subject: Re: [Caml-list] camlidl and pointer to function References: Content-Type: multipart/mixed; boundary="------------AED5C692BBCF19056F5A76CE" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk This is a multi-part message in MIME format. --------------AED5C692BBCF19056F5A76CE Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Dmitry Bely wrote: > I have a C library function with interface like following: > > typedef [abstract] void* SomeType; > typedef int (*CallBack)(int); > void convert( SomeType* in, SomeType* out, CallBack c); > > It would be nice to use it in caml the following way: > > external convert: someType->someType->(int->int)->unit = "something" > ... > convert stIn stOut ((+) 1) > ... > > Unfortinately CamIDL language does not allow the pointer to function as a > valid type, but maybe there is some common solution/workaround for this > problem? Or the only way is writing necessary stubs and conversion > functions by hands? Is is possible at all? > It is possible to call back caml code from a C code which was called from caml, but you need to write a few lines of code. here is an example extracted from my application (with a few simplifications). the resulting caml function iter_tree can be called recursively, it can also raise exceptions. Another possible solution was proposed in a previous mail by Thierry Bravier, if you want to use C++ http://caml.inria.fr/bin/wilma_hiliter/caml-list/199704/msg00029.html Eric Nassor ------------------------------------------------------------- in the original C library typedef tree (*walk_tree_fn) (tree *, void *); /* walk_tree iters on the tree data structure and calls the function func with the current tree value and the data */ tree walk_tree (tree *tp, walk_tree_fn func, void *data) ------------------------------------------------------------- in C_to_ml interface /* * intermediary function called by walk_tree * it calls the caml callback function which is given as argument */ static tree iter_tree_func(tree * t,void * fun) { CAMLparam0(); CAMLlocal1(tr); /* if you want to stress the memory system */ /* Garbage_collection_function() ;*/ tr = camlidl_c2ml_type_tree(t,NULL); callback(*(value *)fun, tr); CAMLreturn(NULL_TREE); /* caml exceptions are used if needed instead of return values */ } /* * function to apply walk_tree to a CAML function */ value iter_tree (value v_function, value v_tree) { CAMLparam2(v_tree, v_function); tree t ; camlidl_ml2c_type_tree(v_tree,&t,NULL); /* v_function is protected but its value may change because of the gc so we have to take the address */ walk_tree(&t, iter_tree_func, &v_function); CAMLreturn (Val_unit); } camlidl_ml2c_type_tree and camlidl_c2ml_type_tree are generated by camlidl to translate tree between C and caml ------------------------------------------------------------- in the caml code external iter_tree : (tree -> unit) -> tree -> unit = "iter_tree" ------------------------------------------------------------- --------------AED5C692BBCF19056F5A76CE Content-Type: text/x-vcard; charset=us-ascii; name="nassor.vcf" Content-Transfer-Encoding: 7bit Content-Description: Card for Eric Nassor Content-Disposition: attachment; filename="nassor.vcf" begin:vcard n:Nassor;Eric tel;fax:+33 (0)2.99.84.11.30 tel;work:+33 (0)2 99 87 68 31 x-mozilla-html:FALSE url:http://www.crf.canon.fr org:Canon Research Centre France;Digital Design Department version:2.1 email;internet:nassor@crf.canon.fr adr;quoted-printable:;;Rue de la Touche Lambert=0D=0A;CESSON-SEVIGNE ;;35517 Cedex;France fn:Eric Nassor end:vcard --------------AED5C692BBCF19056F5A76CE-- ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr