-- 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/Cell.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.Cell
  ( Cell
  , cellType
  , new
  , get
  , set
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp





import           CPython.Internal hiding (new)

newtype Cell = Cell (ForeignPtr Cell)

instance Object Cell where
  toObject :: Cell -> SomeObject
toObject (Cell x :: ForeignPtr Cell
x) = ForeignPtr Cell -> SomeObject
forall a. Object a => ForeignPtr a -> SomeObject
SomeObject ForeignPtr Cell
x
  fromForeignPtr :: ForeignPtr Cell -> Cell
fromForeignPtr = ForeignPtr Cell -> Cell
Cell

instance Concrete Cell where
  concreteType :: Cell -> Type
concreteType _ = Type
cellType

cellType :: (Type)
cellType =
  C2HSImp.unsafePerformIO $
  cellType'_ >>= \res ->
  peekStaticObject res >>= \res' ->
  return (res')

{-# LINE 40 "lib/CPython/Types/Cell.chs" #-}


-- | Create and return a new cell containing the value /obj/.
new :: Object obj => Maybe obj -> IO Cell
new obj =
  maybeWith withObject obj $ \objPtr ->
  pyCellNew objPtr
  >>= stealObject

-- | Return the contents of a cell.
get :: Cell -> IO (Maybe SomeObject)
get cell =
  withObject cell $ \cellPtr ->
  pyCellGet cellPtr
  >>= maybePeek stealObject

-- | Set the contents of a cell to /obj/. This releases the reference to any
-- current content of the cell.
set :: Object obj => Cell -> Maybe obj -> IO ()
set :: Cell -> Maybe obj -> IO ()
set cell :: Cell
cell obj :: Maybe obj
obj =
  Cell -> (Ptr () -> IO ()) -> IO ()
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Cell
cell ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \cellPtr :: Ptr ()
cellPtr ->
  (obj -> (Ptr () -> IO ()) -> IO ())
-> Maybe obj -> (Ptr () -> IO ()) -> IO ()
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith obj -> (Ptr () -> IO ()) -> IO ()
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Maybe obj
obj ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \objPtr :: Ptr ()
objPtr ->
  Ptr () -> Ptr () -> IO CInt
pyCellSet Ptr ()
cellPtr Ptr ()
objPtr
  IO CInt -> (CInt -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CInt -> IO ()
checkStatusCode

foreign import ccall unsafe "CPython/Types/Cell.chs.h hscpython_PyCell_Type"
  cellType'_ :: (IO (C2HSImp.Ptr ()))

foreign import ccall safe "CPython/Types/Cell.chs.h PyCell_New"
  pyCellNew :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Cell.chs.h PyCell_Get"
  pyCellGet :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Cell.chs.h PyCell_Set"
  pyCellSet :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))