Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Basile Starynkevitch [local] <basile.starynkevitch@i...>
Subject: Re: [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions?
On Wed, Jul 07, 2004 at 10:56:04AM +0200, Claudio Trento wrote:
> 
> on the subject of interfacing OCaml with C/C++, it is not clear
> to me whether and how OCaml objects of sum types can be passed
> to a C/C++ function.  For example, suppose I have the declaration
> 
> type expr =

(* I Basile added the 2 cases below for explanations *)
       Nonsense
     | Bizarre

>    | Variable of int
>    | Coefficient of int
>    | UPlus of expr
>    | UMinus of expr
>    | Sum of (expr * expr)
>    | Difference of (expr * expr)
>    | Product of (expr * expr)
> 
> What is the best way to pass an expr object to C/C++?

It is documented in the reference manual:
http://caml.inria.fr/ocaml/htmlman/manual032.html sections 18.2.2 and
18.3.4

The two cases Nonsense and Bizarre I added (for the example) have no
argument (i.e. no "of" keyword - for representation issues, Foo of unit
is not like just Foo). So they are represented by small non-negative
integer, so Nonsense is 0 and Bizarre is 1.

The other cases have an "of" keyword, so are represented as tagged
blocks. The first case Variable has tag 0, the next Coefficient has
tag 1, etc

> How can C/C++ code visit such a structure?

Test if a value is a tagged integer, and otherwise it is a block, test
its tag:

in Ocaml: 
   external c_fun : expr -> unit = "c_fun_ml"

in C // untested code

value c_fun_ml(value ex) {
   CAMLparam1(ex);
   switch (ex) {
   case Val_int(0): /* Nonsense */
     break;
   case Val_int(1): /* Bizarre */
     break;
   default: {
     int tag = Tag_val(ex);
     switch (tag) {
       case 0: /* Variable varnum */
         { int varnum = Int_val(Field(ex,0); 
           break; }
       case 1: /* Coefficient */ 
       default: 
           break;
      } /* end switch tag */
     break;
   };
   } /* end switch ex */
   CAMLreturn(Val_unit);
}


I hope you got the idea. I advise you to avoid coding in C, and to
call C functions only inside your Ocaml wrappers (which do appropriate
checks, raise exceptions, etc...)

Regards.

-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners