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


{-# LINE 1 "lib/CPython/Protocols/Mapping.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.Protocols.Mapping
  ( Mapping (..)
  , SomeMapping
  , castToMapping
  , getItem
  , setItem
  , deleteItem
  , size
  , hasKey
  , keys
  , values
  , items
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified Foreign.Ptr as C2HSImp





import           CPython.Internal

instance Mapping Dictionary where
  toMapping :: Dictionary -> SomeMapping
toMapping = Dictionary -> SomeMapping
forall a. Object a => a -> SomeMapping
unsafeCastToMapping

castToMapping :: Object a => a -> IO (Maybe SomeMapping)
castToMapping :: a -> IO (Maybe SomeMapping)
castToMapping obj :: a
obj =
  a -> (Ptr () -> IO (Maybe SomeMapping)) -> IO (Maybe SomeMapping)
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject a
obj ((Ptr () -> IO (Maybe SomeMapping)) -> IO (Maybe SomeMapping))
-> (Ptr () -> IO (Maybe SomeMapping)) -> IO (Maybe SomeMapping)
forall a b. (a -> b) -> a -> b
$ \objPtr :: Ptr ()
objPtr -> do
  Bool
isMapping <- (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CInt -> Bool
cToBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ Ptr () -> IO CInt
pyMappingCheck Ptr ()
objPtr
  Maybe SomeMapping -> IO (Maybe SomeMapping)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe SomeMapping -> IO (Maybe SomeMapping))
-> Maybe SomeMapping -> IO (Maybe SomeMapping)
forall a b. (a -> b) -> a -> b
$ if Bool
isMapping
    then SomeMapping -> Maybe SomeMapping
forall a. a -> Maybe a
Just (SomeMapping -> Maybe SomeMapping)
-> SomeMapping -> Maybe SomeMapping
forall a b. (a -> b) -> a -> b
$ a -> SomeMapping
forall a. Object a => a -> SomeMapping
unsafeCastToMapping a
obj
    else Maybe SomeMapping
forall a. Maybe a
Nothing

getItem :: (Mapping self, Object key) => (self) -> (key) -> IO ((SomeObject))
getItem :: self -> key -> IO SomeObject
getItem a1 :: self
a1 a2 :: key
a2 =
  self -> (Ptr () -> IO SomeObject) -> IO SomeObject
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject self
a1 ((Ptr () -> IO SomeObject) -> IO SomeObject)
-> (Ptr () -> IO SomeObject) -> IO SomeObject
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  key -> (Ptr () -> IO SomeObject) -> IO SomeObject
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject key
a2 ((Ptr () -> IO SomeObject) -> IO SomeObject)
-> (Ptr () -> IO SomeObject) -> IO SomeObject
forall a b. (a -> b) -> a -> b
$ \a2' :: Ptr ()
a2' -> 
  Ptr () -> Ptr () -> IO (Ptr ())
getItem'_ Ptr ()
a1' Ptr ()
a2' IO (Ptr ()) -> (Ptr () -> IO SomeObject) -> IO SomeObject
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO SomeObject
forall obj a. Object obj => Ptr a -> IO obj
stealObject Ptr ()
res IO SomeObject -> (SomeObject -> IO SomeObject) -> IO SomeObject
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: SomeObject
res' ->
  SomeObject -> IO SomeObject
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeObject
res')

{-# LINE 51 "lib/CPython/Protocols/Mapping.chs" #-}


setItem :: (Mapping self, Object key, Object value) => (self) -> (key) -> (value) -> IO ((()))
setItem a1 a2 a3 =
  withObject a1 $ \a1' -> 
  withObject a2 $ \a2' -> 
  withObject a3 $ \a3' -> 
  setItem'_ a1' a2' a3' >>= \res ->
  checkStatusCode res >>= \res' ->
  return (res')

{-# LINE 58 "lib/CPython/Protocols/Mapping.chs" #-}


deleteItem :: (Mapping self, Object key) => (self) -> (key) -> IO ((()))
deleteItem a1 a2 =
  withObject a1 $ \a1' -> 
  withObject a2 $ \a2' -> 
  deleteItem'_ a1' a2' >>= \res ->
  checkStatusCode res >>= \res' ->
  return (res')

{-# LINE 64 "lib/CPython/Protocols/Mapping.chs" #-}


size :: Mapping self => (self) -> IO ((Integer))
size a1 =
  withObject a1 $ \a1' -> 
  size'_ a1' >>= \res ->
  checkIntReturn res >>= \res' ->
  return (res')

{-# LINE 69 "lib/CPython/Protocols/Mapping.chs" #-}


hasKey :: (Mapping self, Object key) => (self) -> (key) -> IO ((Bool))
hasKey a1 a2 =
  withObject a1 $ \a1' -> 
  withObject a2 $ \a2' -> 
  hasKey'_ a1' a2' >>= \res ->
  let {res' = C2HSImp.toBool res} in
  return (res')

{-# LINE 75 "lib/CPython/Protocols/Mapping.chs" #-}


keys :: Mapping self => (self) -> IO ((List))
keys a1 =
  withObject a1 $ \a1' -> 
  keys'_ a1' >>= \res ->
  stealObject res >>= \res' ->
  return (res')

{-# LINE 80 "lib/CPython/Protocols/Mapping.chs" #-}


values :: Mapping self => (self) -> IO ((List))
values a1 =
  withObject a1 $ \a1' -> 
  values'_ a1' >>= \res ->
  stealObject res >>= \res' ->
  return (res')

{-# LINE 85 "lib/CPython/Protocols/Mapping.chs" #-}


items :: Mapping self => (self) -> IO ((List))
items a1 =
  withObject a1 $ \a1' -> 
  items'_ a1' >>= \res ->
  stealObject res >>= \res' ->
  return (res')

{-# LINE 90 "lib/CPython/Protocols/Mapping.chs" #-}


foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyMapping_Check"
  pyMappingCheck :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyObject_GetItem"
  getItem'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyObject_SetItem"
  setItem'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt))))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyObject_DelItem"
  deleteItem'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyMapping_Size"
  size'_ :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CLong))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyMapping_HasKey"
  hasKey'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyMapping_Keys"
  keys'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyMapping_Values"
  values'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Protocols/Mapping.chs.h PyMapping_Items"
  items'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))