{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module CCast
( Cast(cast)
, CComplex(CComplex)
) where
import Data.Coerce
import Data.Complex
import Data.Int
import Data.Word
import Foreign.C.Types
import Foreign.Ptr
import Foreign.Storable
import Bit64Only
class Cast a b where
cast :: Coercible a b => a -> b
cast = coerce;
data CComplex a = CComplex !a !a
deriving (Eq)
instance Storable a => Storable (CComplex a) where
sizeOf _ = sizeOf (undefined :: a) * 2
alignment _ = alignment (undefined :: a)
poke p (CComplex x y) = do
pokeElemOff (castPtr p) 0 x
pokeElemOff (castPtr p) 1 y
peek p = CComplex
<$> peekElemOff (castPtr p) 0
<*> peekElemOff (castPtr p) 1
instance Cast CChar Int8 where
instance Cast Int8 CChar where
instance Cast CSChar Int8 where
instance Cast Int8 CSChar where
instance Cast CUChar Word8 where
instance Cast Word8 CUChar where
instance Cast CShort Int16 where
instance Cast Int16 CShort where
instance Cast CUShort Word16 where
instance Cast Word16 CUShort where
instance Cast CInt Int32 where
instance Cast Int32 CInt where
instance Cast CUInt Word32 where
instance Cast Word32 CUInt where
instance Cast CInt Int where; cast = fromIntegral;
instance Cast Int CInt where; cast = fromIntegral;
instance Cast CLong Int64 where
instance Cast Int64 CLong where
instance Cast CULong Word64 where
instance Cast Word64 CULong where
instance Cast CFloat Float where
instance Cast Float CFloat where
instance Cast CDouble Double where
instance Cast Double CDouble where
instance Cast (CComplex CFloat) (Complex Float) where; cast (CComplex x y) = cast x :+ cast y; {-# INLINE cast #-}
instance Cast (Complex Float) (CComplex CFloat) where; cast (x :+ y) = CComplex (cast x) (cast y); {-# INLINE cast #-}
instance Cast (CComplex CDouble) (Complex Double) where; cast (CComplex x y) = cast x :+ cast y; {-# INLINE cast #-}
instance Cast (Complex Double) (CComplex CDouble) where; cast (x :+ y) = CComplex (cast x) (cast y); {-# INLINE cast #-}
instance Cast CPtrdiff Int64 where
instance Cast Int64 CPtrdiff where
instance Cast CSize Word64 where
instance Cast Word64 CSize where
instance Cast CWchar Int32 where
instance Cast Int32 CWchar where
instance Cast CSigAtomic Int32 where
instance Cast Int32 CSigAtomic where
instance Cast CLLong Int64 where
instance Cast CULLong Word64 where
instance Cast CBool Word8 where
instance Cast Word8 CBool where
instance Cast CIntPtr Int64 where
instance Cast Int64 CIntPtr where
instance Cast CUIntPtr Word64 where
instance Cast Word64 CUIntPtr where
instance Cast CIntMax Int64 where
instance Cast Int64 CIntMax where
instance Cast CClock Int64 where
instance Cast Int64 CClock where
instance Cast CTime Int64 where
instance Cast Int64 CTime where
instance Cast CUSeconds Word32 where
instance Cast Word32 CUSeconds where
instance Cast CSUSeconds Int64 where
instance Cast Int64 CSUSeconds where
myArchitecture :: Refined MustBe64 Int
myArchitecture = $$(refineTH (sizeOf (undefined :: Int)))