-- GENERATED by C->Haskell Compiler, version 0.17.2 Crystal Seed, 24 Jan 2009 (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 Data.Complex as C

import           CPython.Internal

newtype Complex = Complex (ForeignPtr Complex)

instance Object Complex where
	toObject (Complex x) = SomeObject x
	fromForeignPtr = Complex

instance Concrete Complex where
	concreteType _ = complexType

complexType :: (Type)
complexType =
  unsafePerformIO $
  let {res = complexType'_} in
  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'_ :: (Ptr ())

foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_FromDoubles"
  pyComplexFromDoubles :: (CDouble -> (CDouble -> (IO (Ptr ()))))

foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_RealAsDouble"
  pyComplexRealAsDouble :: ((Ptr ()) -> (IO CDouble))

foreign import ccall safe "CPython/Types/Complex.chs.h PyComplex_ImagAsDouble"
  pyComplexImagAsDouble :: ((Ptr ()) -> (IO CDouble))