FFI を使用して Union と Type** (int** など) を解決する方法を知りたいですか? 構造体には Storable インスタンスが必要ですが、共用体にも使用できますか?
そのようなユニオン:
typedef union {
int i;
char c;
} my_union;
これは通常、Haskell では次のように表されます。
data MyUnion = I CInt | C CChar
私の質問は、どのように myUnion を my_union にマーシャリング (Storable インスタンスを定義) しますか? インスタンス my_union がメモリ内の sizeof(int) バイト、つまり最大のメンバーのサイズを占めることは私の理解です。したがって、これを保存するには、次の行に沿って何かを記述します。
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
int**
また、どのように FFI でa を表すことができますか? int foo(int i1, int* i2);
署名のような関数を取得すると、次のようになります。foo -> CInt -> Ptr CInt -> CInt
しかし、もしあれば:int foo(int i1, int** i2);