1 {-# LANGUAGE ForeignFunctionInterface #-}
11 import Prelude hiding (min, max)
13 import Control.Monad (void)
14 import Foreign ( ForeignPtr
20 import Foreign.C (CInt(..), CString, withCString)
21 import Foreign.Storable
34 withSample :: Sample a -> (Ptr HklSample -> IO r) -> IO r
37 withForeignPtr fptr fun
39 newSample :: Sample a -> IO (ForeignPtr HklSample)
40 newSample (Sample name l ux uy uz) =
41 withCString name $ \cname -> do
42 sample <- c_hkl_sample_new cname
43 withLattice l $ \lattice -> do
44 c_hkl_sample_lattice_set sample lattice
45 go sample ux c_hkl_sample_ux_get c_hkl_sample_ux_set
46 go sample uy c_hkl_sample_uy_get c_hkl_sample_uy_set
47 go sample uz c_hkl_sample_uz_get c_hkl_sample_uz_set
48 newForeignPtr c_hkl_sample_free sample
50 go s p getter setter = do
51 fptr <- copyParameter =<< (getter s)
52 withForeignPtr fptr $ \ptr -> do
54 void $ setter s ptr nullPtr
56 foreign import ccall unsafe "hkl.h hkl_sample_new"
57 c_hkl_sample_new:: CString -> IO (Ptr HklSample)
59 foreign import ccall unsafe "hkl.h hkl_sample_lattice_set"
60 c_hkl_sample_lattice_set :: Ptr HklSample -> Ptr HklLattice -> IO ()
62 foreign import ccall unsafe "hkl.h &hkl_sample_free"
63 c_hkl_sample_free :: FunPtr (Ptr HklSample -> IO ())
65 foreign import ccall unsafe "hkl.h hkl_sample_ux_get"
66 c_hkl_sample_ux_get :: Ptr HklSample
69 foreign import ccall unsafe "hkl.h hkl_sample_uy_get"
70 c_hkl_sample_uy_get :: Ptr HklSample
73 foreign import ccall unsafe "hkl.h hkl_sample_uz_get"
74 c_hkl_sample_uz_get :: Ptr HklSample
77 foreign import ccall unsafe "hkl.h hkl_sample_ux_set"
78 c_hkl_sample_ux_set :: Ptr HklSample
83 foreign import ccall unsafe "hkl.h hkl_sample_uy_set"
84 c_hkl_sample_uy_set :: Ptr HklSample
89 foreign import ccall unsafe "hkl.h hkl_sample_uz_set"
90 c_hkl_sample_uz_set :: Ptr HklSample