Re: gobject introspection



On Mon, 2005-05-09 at 12:48 -0400, Matthias Clasen wrote:
> Hi,
> 
> I have put my prototype for full gobject introspection in cvs now,
> module gobject-introspection. 
> 
> But the real reason why I'm posting here is to ask how the different
> language bindings handle unions (in particular GdkEvent). The
> introspection prototype currently doesn't handle unions at all, apart
> from the fact that fields are supposed to have struct offset
> information, so unions could be modeled by describing the overlapping
> fields. CORBA does seem to have a concept of discriminated union,
> which would probably cover the GdkEvent case, but trying to describe
> arbitrary anonymous unions in the middle of structs sounds like adding 
> a lot of complexity. It would be very tempting to just require
> accessors to make those kinds of C-specific things bindable.
> 
> So, how do language bindings handle GdkEvent currently ?

Well in Haskell...

data Event
  = Event {
    sent        :: Bool }
  | Expose {
    sent        :: Bool,
    area        :: Rectangle,
    region      :: Region,
    count       :: Int }
  | Motion {
    sent        :: Bool,
    time        :: Integer,
    x,y         :: Double,
    modif       :: Modifier,
    isHint      :: Bool,
    xRoot,
    yRoot       :: Double }
... etc

it's modeled exactly as a native Haskell disjoint union. Then we marshal
from GdkEvent to the native Event type:

marshalEvent :: Ptr GdkEvent -> IO Event
marshalEvent ptr = do
  (eType::#type GdkEventType) <- #{peek GdkEventAny,type} ptr
  (case eType of
    #{const GDK_EXPOSE}         -> marshExpose
    #{const GDK_MOTION_NOTIFY}  -> marshMotion
    #{const GDK_BUTTON_PRESS}   -> marshButton SingleClick
  ... etc

(The funny #{} stuff is not ordinary haskell but directives to the FFI
pre-processor.)

Hope this helps.

Duncan




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]