HDIRECT/FFI - C enums

Sigbjorn Finne sof@galois.com
Sat, 29 Dec 2001 15:56:54 -0800


This is just what we agreed on a couple of months ago, no?
I'm afraid this has been sitting on my HDirect ToDo list since
then, as I've been busy with misc other Haskell-related matters.
However, I've unqueued the item & implemented something
which is close to what you're after, I believe. It's just
been checked into the hdirect portion of the CVS repo.
Have a go & let me know whether this scratches your
itch or not.

--sigbjorn

----- Original Message -----
From: "Mike Thomas" <miketh@brisbane.paradigmgeo.com>
To: <glasgow-haskell-users@haskell.org>
Sent: Thursday, December 27, 2001 23:34
Subject: HDIRECT/FFI - C enums


> Hi all.
>
> HDirect can generate code as follows:
>
> ------------------------------------------------------------
> data CONST_DDWAITVBFLAGS
>  = DDWAITVB_BLOCKBEGIN
>  | DDWAITVB_BLOCKBEGINEVENT
>  | DDWAITVB_BLOCKEND
>
> instance Prelude.Enum (CONST_DDWAITVBFLAGS) where
>   fromEnum v =
>     case v of
>        DDWAITVB_BLOCKBEGIN -> 1
>        DDWAITVB_BLOCKBEGINEVENT -> 2
>        DDWAITVB_BLOCKEND -> 4
>
>   toEnum v =
>     case v of
>        1 -> DDWAITVB_BLOCKBEGIN
>        2 -> DDWAITVB_BLOCKBEGINEVENT
>        4 -> DDWAITVB_BLOCKEND
>        _ -> Prelude.error "unmarshallCONST_DDWAITVBFLAGS: illegal enum
value
> "
>
> ------------------------------------------------------------
>
> Unfortunately, if you want to interface to a C function which takes a
> combination of flags added together in a specific argument eg (C):
>
>   bar ( DDWAITVB_BLOCKBEGIN + DDWAITVB_BLOCKBEGINEVENT );
>
> (Haskell)
>
>   bar (toEnum (fromEnum DDWAITVB_BLOCKBEGIN) + (fromEnum
> DDWAITVB_BLOCKBEGINEVENT ))
>
> the toEnum function can't deal with the comnbination - it generates a
> run-time error.
>
> Is there a simple way to deal with this situation (the need to associate
> symbols with specific values, while retaining the ability to lump the
values
> together in a manner reflecting the underlying C semantics)?
>
> Should there be another FFI type CEnum?
>
> Should the Haskell Enum type be operable with +/&/| or whatever?
>
> Merry Christmas
>
> Mike Thomas.
>