i need to know how can i resolve Unions and Type** (e.g. int**) with the FFI? I know that i need a Storable instance for structs, could i use it for unions too?
a union like so:
typedef union {
int i;
char c;
} my_union;
This would typically be represented in Haskell as:
data MyUnion = I CInt | C CChar
My question is how would you marshall (define an Storable instance for) myUnion into my_union? It's my understanding that an instance my_union would take up sizeof(int) bytes in memory, i.e. the size of it's largest member. So to store this we would write something along the lines of:
instance Storable myUnion where
size _ = #{size my_union} -- <-- hsc2hs shortcut
alignment _ = alignment undefined::CInt -- <-- What should this really be?
peek ptr = do -- <-- How are you supposed to know which element to extract?
poke ptr (I i) = poke ptr i -- <-- Or should this be #{poke my_union, i} ptr i ?
poke ptr (C c) = poke ptr c
Also, how can you represent a int**
with the FFI?
when i got a function like int foo(int i1, int* i2);
the signature would be: foo -> CInt -> Ptr CInt -> CInt
but what if there is: int foo(int i1, int** i2);