[Haskell-cafe] Storable class?

Evan Laforge qdunkan at gmail.com
Tue Apr 22 16:29:26 EDT 2008


This uses hsc2hs.  As far as I can tell, alignment is unused.  I've
never had an error from using 'undefined' there.

> #include "c_interface.h"
>
> instance Storable Color where
>     sizeOf _ = #size Color
>     alignment _ = undefined
>     peek = peek_color
>     poke = poke_color
>
> peek_color colorp = do
>     r <- (#peek Color, r) colorp :: IO CUChar
>     g <- (#peek Color, g) colorp :: IO CUChar
>     b <- (#peek Color, b) colorp :: IO CUChar
>     a <- (#peek Color, a) colorp :: IO CUChar
>     return $ Color (d r) (d g) (d b) (d a)
>     where d uchar = fromIntegral uchar / 255.0
>
> poke_color colorp (Color r g b a) = do
>     (#poke Color, r) colorp (c r)
>     (#poke Color, g) colorp (c g)
>     (#poke Color, b) colorp (c b)
>     (#poke Color, a) colorp (c a)
>     where c double = Util.c_uchar (floor (double*255))


More information about the Haskell-Cafe mailing list