Version française
Home     About     Download     Resources     Contact us    
Browse thread
coding c++ enum type
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] coding c++ enum type
On Sat, Feb 24, 2007 at 10:10:27AM +0100, micha wrote:
> 
> when interfacing to c, what is the preferred method to represent enums 
> which are used as flags in c?
> I can choose between:
> 1. using a variant type and  a list of those variants to represent the 
> or-ed flags. Then I have to iterate over the list to calculate the 
> combined flag value.
> 
> 2. I can export global variables initialized with the real value of the 
> flags and a function which combines  (with "or") them together,
> 
> Is one method better than the other?

I don't know about preferred, but most libraries seem to be using the
list approach (1).  See for example to source to Unix.openfile:

http://camlcvs.inria.fr/cgi-bin/cvsweb/ocaml/otherlibs/unix/open.c?rev=1.8.2.3;content-type=text%2Fx-cvsweb-markup

(actually, convert_flag_list:
http://camlcvs.inria.fr/cgi-bin/cvsweb/ocaml/byterun/alloc.c?rev=1.29;content-type=text%2Fx-cvsweb-markup )

Rich.

-- 
Richard Jones
Red Hat UK Limited