{-# LINE 1 "lib/CPython/Types/Complex.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module CPython.Types.Complex
( Complex
, complexType
, toComplex
, fromComplex
) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp
import qualified Data.Complex as C
import CPython.Internal
newtype Complex = Complex (ForeignPtr Complex)
instance Object Complex where
toObject :: Complex -> SomeObject
toObject (Complex x :: ForeignPtr Complex
x) = ForeignPtr Complex -> SomeObject
forall a. Object a => ForeignPtr a -> SomeObject
SomeObject ForeignPtr Complex
x
fromForeignPtr :: ForeignPtr Complex -> Complex
fromForeignPtr = ForeignPtr Complex -> Complex
Complex
instance Concrete Complex where
concreteType :: Complex -> Type
concreteType _ = Type
complexType
complexType :: (Type)
complexType =
C2HSImp.unsafePerformIO $
complexType'_ >>= \res ->
peekStaticObject res >>= \res' ->
return (res')
{-# LINE 41 "lib/CPython/Types/Complex.chs" #-}
toComplex :: C.Complex Double -> IO Complex
toComplex x = raw >>= stealObject where
real = realToFrac $ C.realPart x
imag = realToFrac $ C.imagPart x
raw = pyComplexFromDoubles real imag
fromComplex :: Complex -> IO (C.Complex Double)
fromComplex py = withObject py $ \pyPtr -> do
real <- pyComplexRealAsDouble pyPtr
imag <- pyComplexImagAsDouble pyPtr
return $ realToFrac real C.:+ realToFrac imag
foreign import ccall unsafe "CPython/Types/Complex.chs.h hscpython_PyComplex_Type"
complexType'_ :: (IO (C2HSImp.Ptr ()))
foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_FromDoubles"
pyComplexFromDoubles :: (C2HSImp.CDouble -> (C2HSImp.CDouble -> (IO (C2HSImp.Ptr ()))))
foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_RealAsDouble"
pyComplexRealAsDouble :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CDouble))
foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_ImagAsDouble"
pyComplexImagAsDouble :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CDouble))