Last active
December 22, 2015 14:28
-
-
Save deech/6485616 to your computer and use it in GitHub Desktop.
Array inside a struct ....
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE CPP #-} | |
module CHSTest where | |
import Control.Monad | |
import Foreign | |
import Foreign.C | |
import Foreign.Ptr | |
#c | |
typedef struct { | |
int x; | |
} A; | |
typedef struct { | |
A* as; | |
} B; | |
#endc | |
{#pointer *A as APtr newtype #} | |
{#pointer *B as BPtr newtype #} | |
data MyA = MyA { | |
x :: CInt | |
} | |
data MyB = MyB { | |
as :: [MyA] | |
} | |
instance Storable MyA where | |
sizeOf _ = {# sizeof A #} | |
alignment _ = {# alignof A #} | |
poke p a = {#set A->x #} p (x a) | |
peek p = do | |
x' <- {#get A->x #} p | |
return $ MyA x' | |
instance Storable MyB where | |
sizeOf _ = {# sizeof B #} | |
alignment _ = {# alignof B #} | |
poke p b = do | |
ptr <- mallocArray (length $ as b) | |
pokeArray ptr (as b) | |
{#set B->as #} p ptr | |
peek _ = undefined | |
-- compiler error | |
src/CHSTest.chs:36:60: | |
Couldn't match expected type `APtr' with actual type `Ptr MyA' | |
In the second argument of `\ ptr val | |
-> do { pokeByteOff ptr 0 (val :: APtr) }', namely | |
`ptr' | |
In a stmt of a 'do' block: | |
(\ ptr val -> do { pokeByteOff ptr 0 (val :: APtr) }) p ptr | |
In the expression: | |
do { ptr <- mallocArray (length $ as b); | |
pokeArray ptr (as b); | |
(\ ptr val -> do { pokeByteOff ptr 0 (val :: APtr) }) p ptr } | |
-- Generated Output | |
-- GENERATED by C->Haskell Compiler, version 0.16.5 Crystal Seed, 24 Jan 2009 (Haskell) | |
-- Edit the ORIGNAL .chs file instead! | |
{-# LINE 1 "src/CHSTest.chs" #-}{-# LANGUAGE CPP #-} | |
module CHSTest where | |
import Control.Monad | |
import Foreign | |
import Foreign.C | |
import Foreign.Ptr | |
type APtr = ForeignPtr (MyA) | |
{-# LINE 15 "src/CHSTest.chs" #-} | |
newtype BPtr = BPtr (Ptr (BPtr)) | |
{-# LINE 16 "src/CHSTest.chs" #-} | |
data MyA = MyA { | |
x :: CInt | |
} | |
data MyB = MyB { | |
as :: [MyA] | |
} | |
instance Storable MyA where | |
sizeOf _ = 4 | |
{-# LINE 24 "src/CHSTest.chs" #-} | |
alignment _ = 4 | |
{-# LINE 25 "src/CHSTest.chs" #-} | |
poke p a = (\ptr val -> do {pokeByteOff ptr 0 (val::CInt)}) p (x a) | |
peek p = do | |
x' <- (\ptr -> do {peekByteOff ptr 0 ::IO CInt}) p | |
return $ MyA x' | |
instance Storable MyB where | |
sizeOf _ = 8 | |
{-# LINE 32 "src/CHSTest.chs" #-} | |
alignment _ = 8 | |
{-# LINE 33 "src/CHSTest.chs" #-} | |
poke p b = do | |
ptr <- mallocArray (length $ as b) | |
pokeArray ptr (as b) | |
(\ptr val -> do {pokeByteOff ptr 0 (val::(Ptr (MyA)))}) p ptr | |
peek _ = undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment