module Foreign.Matlab.Internal (
CBool, boolC, cBool,
MIO,
MType(..),
MXClassID, MXClass(..),
MXChar, MChar,
MXLogical, MLogical,
MXDouble, MDouble,
MXSingle, MSingle,
MXInt8, MInt8,
MXInt16, MInt16,
MXInt32, MInt32,
MXInt64, MInt64,
MXUint8, MUint8,
MXUint16, MUint16,
MXUint32, MUint32,
MXUint64, MUint64,
MXArrayType,
MXArrayPtr, MXArray(..),
mkMXArray, withMXArray,
unsafeCastMXArray,
MAny, MAnyArray,
MNull, mNullArray, isMNull,
MCell(..),
MStruct(..),
MFun,
MWSize, MWIndex, MWSignedIndex
) where
import Foreign
import Foreign.C.Types
import qualified Data.Char
import Foreign.Matlab.Util
type MIO a = IO a
type CBool = Word8
boolC :: CBool -> Bool
boolC = (0 /=)
cBool :: Bool -> CBool
cBool = ii . fromEnum
type MXClassID = Word32
data MXClass =
MXClassNull
| MXClassCell
| MXClassStruct
| MXClassLogical
| MXClassChar
| MXClassDouble
| MXClassSingle
| MXClassInt8
| MXClassUint8
| MXClassInt16
| MXClassUint16
| MXClassInt32
| MXClassUint32
| MXClassInt64
| MXClassUint64
| MXClassFun
| MXClassObject
deriving (Eq, Show)
class MType mx a | a -> mx where
hs2mx :: a -> mx
mx2hs :: mx -> a
mxClassOf :: a -> MXClass
instance MType MXClassID MXClass where
mx2hs (5) = MXClassNull
mx2hs (1) = MXClassCell
mx2hs (2) = MXClassStruct
mx2hs (3) = MXClassLogical
mx2hs (4) = MXClassChar
mx2hs (6) = MXClassDouble
mx2hs (7) = MXClassSingle
mx2hs (8) = MXClassInt8
mx2hs (9) = MXClassUint8
mx2hs (10) = MXClassInt16
mx2hs (11) = MXClassUint16
mx2hs (12) = MXClassInt32
mx2hs (13) = MXClassUint32
mx2hs (14) = MXClassInt64
mx2hs (15) = MXClassUint64
mx2hs (16)= MXClassFun
mx2hs (18) = MXClassObject
mx2hs c = error ("MXClass: unknown mxClassID " ++ show c)
hs2mx MXClassNull = 5
hs2mx MXClassCell = 1
hs2mx MXClassStruct = 2
hs2mx MXClassLogical = 3
hs2mx MXClassChar = 4
hs2mx MXClassDouble = 6
hs2mx MXClassSingle = 7
hs2mx MXClassInt8 = 8
hs2mx MXClassUint8 = 9
hs2mx MXClassInt16 = 10
hs2mx MXClassUint16 = 11
hs2mx MXClassInt32 = 12
hs2mx MXClassUint32 = 13
hs2mx MXClassInt64 = 14
hs2mx MXClassUint64 = 15
hs2mx MXClassFun = 16
hs2mx MXClassObject = 18
mxClassOf _ = error "mxClassOf: no class for MXClassID"
type MXChar = Word16
type MChar = Char
instance MType MXChar MChar where
hs2mx = ii . Data.Char.ord
mx2hs = Data.Char.chr . ii
mxClassOf _ = MXClassChar
type MXLogical = Word8
type MLogical = Bool
instance MType MXLogical MLogical where
hs2mx = cBool
mx2hs = boolC
mxClassOf _ = MXClassLogical
type MXDouble = Double
type MDouble = Double
instance MType MXDouble MDouble where
hs2mx = id
mx2hs = id
mxClassOf _ = MXClassDouble
type MXSingle = Float
type MSingle = Float
instance MType MXSingle MSingle where
hs2mx = id
mx2hs = id
mxClassOf _ = MXClassSingle
type MXInt8 = Int8
type MInt8 = Int8
instance MType MXInt8 MInt8 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassInt8 }
type MXInt16 = Int16
type MInt16 = Int16
instance MType MXInt16 MInt16 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassInt16 }
type MXInt32 = Int32
type MInt32 = Int32
instance MType MXInt32 MInt32 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassInt32 }
type MXInt64 = Int64
type MInt64 = Int64
instance MType MXInt64 MInt64 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassInt64 }
type MXUint8 = Word8
type MUint8 = Word8
instance MType MXUint8 MUint8 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassUint8 }
type MXUint16 = Word16
type MUint16 = Word16
instance MType MXUint16 MUint16 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassUint16 }
type MXUint32 = Word32
type MUint32 = Word32
instance MType MXUint32 MUint32 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassUint32 }
type MXUint64 = Word64
type MUint64 = Word64
instance MType MXUint64 MUint64 where { hs2mx = id ; mx2hs = id ; mxClassOf _ = MXClassUint64 }
data MXArrayType
type MXArrayPtr = Ptr MXArrayType
newtype MXArray a = MXArray { mxArray :: MXArrayPtr }
mkMXArray :: MXArrayPtr -> IO (MXArray a)
mkMXArray = return . MXArray
withMXArray :: With (MXArray x) MXArrayPtr a
withMXArray (MXArray a) f = f a
unsafeCastMXArray :: MXArray a -> MXArray b
unsafeCastMXArray = MXArray . castPtr . mxArray
isMNull :: MXArray a -> Bool
isMNull (MXArray a) = nullPtr == a
data MAny
type MAnyArray = MXArray MAny
data MNull
instance MType MNull MNull where
hs2mx = id
mx2hs = id
mxClassOf _ = MXClassNull
mNullArray :: MXArray MNull
mNullArray = MXArray nullPtr
newtype MCell = MCell { mCell :: MAnyArray }
instance MType MCell MCell where
hs2mx = id
mx2hs = id
mxClassOf _ = MXClassCell
newtype MStruct = MStruct { mStruct :: [(String,MAnyArray)] }
instance MType MStruct MStruct where
hs2mx = id
mx2hs = id
mxClassOf _ = MXClassStruct
type MXFun = CInt -> Ptr MXArrayPtr -> CInt -> Ptr MXArrayPtr -> IO ()
type MFun =
[MAnyArray]
-> Int
-> IO [MAnyArray]
instance MType MXFun MFun where
hs2mx fun outn outp argn argp = do
arg <- map MXArray =.< peekArray (ii argn) argp
out <- fun arg (ii outn)
pokeArray outp $ map mxArray out
mx2hs fun arg no =
withArrayLen (map mxArray arg) $ \argn argp ->
allocaArray no $ \outp -> do
fun (ii no) outp (ii argn) argp
map MXArray =.< peekArray no outp
mxClassOf _ = MXClassFun
type MWSize = Word64
type MWIndex = Word64
type MWSignedIndex = Int64