HDIRECT/FFI - C enums

Mike Thomas Mike Thomas" <[email protected]
Fri, 28 Dec 2001 17:34:39 +1000


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.