-- GENERATED by C->Haskell Compiler, version 0.28.7 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "lib/CPython/Types/Complex.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}

-- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

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))