caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* coding c++ enum type
@ 2007-02-24  9:10 micha
  2007-02-24 10:52 ` [Caml-list] " Richard Jones
  2007-02-24 21:33 ` Nathaniel Gray
  0 siblings, 2 replies; 3+ messages in thread
From: micha @ 2007-02-24  9:10 UTC (permalink / raw)
  To: caml-list


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?


Michael


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] coding c++ enum type
  2007-02-24  9:10 coding c++ enum type micha
@ 2007-02-24 10:52 ` Richard Jones
  2007-02-24 21:33 ` Nathaniel Gray
  1 sibling, 0 replies; 3+ messages in thread
From: Richard Jones @ 2007-02-24 10:52 UTC (permalink / raw)
  To: micha; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] coding c++ enum type
  2007-02-24  9:10 coding c++ enum type micha
  2007-02-24 10:52 ` [Caml-list] " Richard Jones
@ 2007-02-24 21:33 ` Nathaniel Gray
  1 sibling, 0 replies; 3+ messages in thread
From: Nathaniel Gray @ 2007-02-24 21:33 UTC (permalink / raw)
  To: micha; +Cc: caml-list

On 2/24/07, micha <micha-1@fantasymail.de> 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?

The Unix library uses both techniques -- using a single int for file
permissions but a list of flags in various other cases.

Personally, I think that as long as the bitfield fits in 31 bits then
it's better to keep it as a simple int.  Bitfields allow you to do
some things very simply that would be more painful with a list of
constructors.  For example, testing if flags a, b, and c are set can
be done very quickly and easily with a bitfield, but becomes somewhat
laborious and/or slow with lists.  You can easily provide an
additional "list of variants" interface on the ML side if you think
some users would prefer that.

Cheers,
-n8

-- 
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-02-24 21:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-24  9:10 coding c++ enum type micha
2007-02-24 10:52 ` [Caml-list] " Richard Jones
2007-02-24 21:33 ` Nathaniel Gray

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).