summaryrefslogtreecommitdiff
path: root/src/Storage.hs.old
blob: 2697c394f95d7ec505d1d82f4799305823e7c0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{-# LANGUAGE ForeignFunctionInterface #-}

module Storage(
    PrefStorage,
    createStorage,
    pokePref,
    peekPref
    ) where




import Foreign
import Foreign.C




foreign import ccall "create_pref_array"
    c_createPrefArray :: CInt -> CInt -> IO (Ptr PrefArray)

foreign import ccall "free_pref_array"
    c_freePrefArray :: CInt -> CInt -> Ptr PrefArray -> IO ()

foreign import ccall "wrapper"
    wrap :: (Ptr PrefArray -> IO ()) -> IO (FunPtr (Ptr PrefArray -> IO ()))

foreign import ccall "poke_pref_array"
    c_pokePrefArray :: CInt -> CInt -> Ptr PrefArray -> CInt -> CInt -> CInt -> IO ()

foreign import ccall "peek_pref_array"
    c_peekPrefArray :: CInt -> CInt -> Ptr PrefArray -> CInt -> CInt -> CInt -> IO CInt




newtype PrefArray = PrefArray (Ptr PrefArray)

data PrefStorage = PrefStorage { pointer      :: ForeignPtr PrefArray
                               , numBallots   :: Int
                               , sizeOfBallot :: Int }




createStorage :: Int -> Int -> IO PrefStorage
createStorage n s = do
    x <- c_createPrefArray (fromIntegral n) (fromIntegral s)
    f <- wrap (c_freePrefArray (fromIntegral n) (fromIntegral s))
    y <- newForeignPtr f x
    return (PrefStorage y n s)




pokePref :: PrefStorage -> Int -> Int -> Int -> IO ()
pokePref p n s r = do
    let numBal = fromIntegral (numBallots p)
        sizeBal = fromIntegral (sizeOfBallot p)
        func a = c_pokePrefArray numBal sizeBal a (fromIntegral n) (fromIntegral s) (fromIntegral r)
    withForeignPtr (pointer p) func




peekPref :: PrefStorage -> Int -> Int -> Int -> IO Bool
peekPref p n s r = do
    let numBal = fromIntegral (numBallots p)
        sizeBal = fromIntegral (sizeOfBallot p)
        func a = c_peekPrefArray numBal sizeBal a (fromIntegral n) (fromIntegral s) (fromIntegral r)
    result <- withForeignPtr (pointer p) func
    return (result /= 0)