-- 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/Capsule.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.Capsule
  ( Capsule
  , capsuleType
  --, new
  , getPointer
  --, getDestructor
  , getContext
  , getName
  , importNamed
  , isValid
  , setPointer
  --, setDestructor
  , setContext
  --, setName
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp





import           Data.Text (Text)

import           CPython.Internal hiding (new)

-- type Destructor = Ptr () -> IO ()
newtype Capsule = Capsule (ForeignPtr Capsule)

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

instance Concrete Capsule where
  concreteType :: Capsule -> Type
concreteType _ = Type
capsuleType

capsuleType :: (Type)
capsuleType :: Type
capsuleType =
  IO Type -> Type
forall a. IO a -> a
C2HSImp.unsafePerformIO (IO Type -> Type) -> IO Type -> Type
forall a b. (a -> b) -> a -> b
$
  IO (Ptr ())
capsuleType'_ IO (Ptr ()) -> (Ptr () -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO Type
forall obj a. Object obj => Ptr a -> IO obj
peekStaticObject Ptr ()
res IO Type -> (Type -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Type
res' ->
  Type -> IO Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
res')

{-# LINE 51 "lib/CPython/Types/Capsule.chs" #-}


-- new :: Ptr () -> Maybe Text -> Destructor -> IO Capsule
-- new = undefined

-- | Retrieve the pointer stored in the capsule. On failure, throws an
-- exception.
--
-- The name parameter must compare exactly to the name stored in the capsule.
-- If the name stored in the capsule is 'Nothing', the name passed in must
-- also be 'Nothing'. Python uses the C function strcmp() to compare capsule
-- names.
getPointer :: Capsule -> Maybe Text -> IO (Ptr ())
getPointer :: Capsule -> Maybe Text -> IO (Ptr ())
getPointer py :: Capsule
py name :: Maybe Text
name =
  Capsule -> (Ptr () -> IO (Ptr ())) -> IO (Ptr ())
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Capsule
py ((Ptr () -> IO (Ptr ())) -> IO (Ptr ()))
-> (Ptr () -> IO (Ptr ())) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ \pyPtr :: Ptr ()
pyPtr ->
  (Text -> (Ptr CChar -> IO (Ptr ())) -> IO (Ptr ()))
-> Maybe Text -> (Ptr CChar -> IO (Ptr ())) -> IO (Ptr ())
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith Text -> (Ptr CChar -> IO (Ptr ())) -> IO (Ptr ())
forall a. Text -> (Ptr CChar -> IO a) -> IO a
withText Maybe Text
name ((Ptr CChar -> IO (Ptr ())) -> IO (Ptr ()))
-> (Ptr CChar -> IO (Ptr ())) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ \namePtr :: Ptr CChar
namePtr ->
  Ptr () -> Ptr CChar -> IO (Ptr ())
pyCapsuleGetPointer Ptr ()
pyPtr Ptr CChar
namePtr

-- getDestructor :: Capsule -> IO (Maybe Destructor)
-- getDestructor = undefined

-- | Return the current context stored in the capsule, which might be @NULL@.
getContext :: Capsule -> IO (Ptr ())
getContext :: Capsule -> IO (Ptr ())
getContext py :: Capsule
py =
  Capsule -> (Ptr () -> IO (Ptr ())) -> IO (Ptr ())
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Capsule
py ((Ptr () -> IO (Ptr ())) -> IO (Ptr ()))
-> (Ptr () -> IO (Ptr ())) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ \pyPtr :: Ptr ()
pyPtr -> do
  IO ()
pyErrClear
{-# LINE 76 "lib/CPython/Types/Capsule.chs" #-}

  ptr <- pyCapsuleGetContext pyPtr
  if ptr /= nullPtr
    then return ptr
    else do
      exc <- pyErrOccurred
{-# LINE 81 "lib/CPython/Types/Capsule.chs" #-}

      exceptionIf $ exc /= nullPtr
      return ptr

-- | Return the current name stored in the capsule, which might be 'Nothing'.
getName :: Capsule -> IO (Maybe Text)
getName :: Capsule -> IO (Maybe Text)
getName py :: Capsule
py =
  Capsule -> (Ptr () -> IO (Maybe Text)) -> IO (Maybe Text)
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Capsule
py ((Ptr () -> IO (Maybe Text)) -> IO (Maybe Text))
-> (Ptr () -> IO (Maybe Text)) -> IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ \pyPtr :: Ptr ()
pyPtr -> do
  IO ()
pyErrClear
{-# LINE 89 "lib/CPython/Types/Capsule.chs" #-}

  ptr <- pyCapsuleGetName pyPtr
  if ptr /= nullPtr
    then Just `fmap` peekText ptr
    else do
      exc <- pyErrOccurred
{-# LINE 94 "lib/CPython/Types/Capsule.chs" #-}

      exceptionIf $ exc /= nullPtr
      return Nothing

-- | Import a pointer to a C object from a capsule attribute in a module.
-- The name parameter should specify the full name to the attribute, as in
-- @\"module.attribute\"@. The name stored in the capsule must match this
-- string exactly. If the second parameter is 'False', import the module
-- without blocking (using @PyImport_ImportModuleNoBlock()@). Otherwise,
-- imports the module conventionally (using @PyImport_ImportModule()@).
--
-- Return the capsule&#x2019;s internal pointer on success. On failure, throw
-- an exception. If the module could not be imported, and if importing in
-- non-blocking mode, returns 'Nothing'.
importNamed :: Text -> Bool -> IO (Maybe (Ptr ()))
importNamed :: Text -> Bool -> IO (Maybe (Ptr ()))
importNamed name :: Text
name block :: Bool
block =
  Text -> (Ptr CChar -> IO (Maybe (Ptr ()))) -> IO (Maybe (Ptr ()))
forall a. Text -> (Ptr CChar -> IO a) -> IO a
withText Text
name ((Ptr CChar -> IO (Maybe (Ptr ()))) -> IO (Maybe (Ptr ())))
-> (Ptr CChar -> IO (Maybe (Ptr ()))) -> IO (Maybe (Ptr ()))
forall a b. (a -> b) -> a -> b
$ \namePtr :: Ptr CChar
namePtr ->
  let noBlock :: CInt
noBlock = Bool -> CInt
cFromBool (Bool -> Bool
not Bool
block) in do
  IO ()
pyErrClear
{-# LINE 112 "lib/CPython/Types/Capsule.chs" #-}

  ptr <- pyCapsuleImport namePtr noBlock
  if ptr /= nullPtr
    then return $ Just ptr
    else do
      exc <- pyErrOccurred
{-# LINE 117 "lib/CPython/Types/Capsule.chs" #-}

      exceptionIf $ exc /= nullPtr
      return Nothing

-- | Determines whether or not a capsule is valid. A valid capsule's type is
-- 'capsuleType', has a non-NULL pointer stored in it, and its internal name
-- matches the name parameter. (See 'getPointer' for information on how
-- capsule names are compared.)
--
-- In other words, if 'isValid' returns 'True', calls to any of the
-- accessors (any function starting with @get@) are guaranteed to succeed.
isValid :: Capsule -> Maybe Text -> IO Bool
isValid :: Capsule -> Maybe Text -> IO Bool
isValid py :: Capsule
py name :: Maybe Text
name =
  Capsule -> (Ptr () -> IO Bool) -> IO Bool
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Capsule
py ((Ptr () -> IO Bool) -> IO Bool) -> (Ptr () -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \pyPtr :: Ptr ()
pyPtr ->
  (Text -> (Ptr CChar -> IO Bool) -> IO Bool)
-> Maybe Text -> (Ptr CChar -> IO Bool) -> IO Bool
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith Text -> (Ptr CChar -> IO Bool) -> IO Bool
forall a. Text -> (Ptr CChar -> IO a) -> IO a
withText Maybe Text
name ((Ptr CChar -> IO Bool) -> IO Bool)
-> (Ptr CChar -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \namePtr :: Ptr CChar
namePtr ->
  Ptr () -> Ptr CChar -> IO CInt
pyCapsuleIsValid Ptr ()
pyPtr Ptr CChar
namePtr
  IO CInt -> (CInt -> IO Bool) -> IO Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CInt -> IO Bool
checkBoolReturn

-- | Set the void pointer inside the capsule. The pointer may not be @NULL@.
setPointer :: (Capsule) -> (Ptr ()) -> IO ((()))
setPointer :: Capsule -> Ptr () -> IO ()
setPointer a1 :: Capsule
a1 a2 :: Ptr ()
a2 =
  Capsule -> (Ptr () -> IO ()) -> IO ()
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Capsule
a1 ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  let {a2' :: Ptr ()
a2' = Ptr () -> Ptr ()
forall a. a -> a
id Ptr ()
a2} in 
  Ptr () -> Ptr () -> IO CInt
setPointer'_ Ptr ()
a1' Ptr ()
a2' IO CInt -> (CInt -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: CInt
res ->
  CInt -> IO ()
checkStatusCode CInt
res IO () -> (() -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: ()
res' ->
  () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return (()
res')

{-# LINE 139 "lib/CPython/Types/Capsule.chs" #-}


-- setDestructor :: Capsule -> Maybe Destructor -> IO ()
-- setDestructor = undefined

-- | Set the context pointer inside the capsule.
setContext :: (Capsule) -> (Ptr ()) -> IO ((()))
setContext :: Capsule -> Ptr () -> IO ()
setContext a1 :: Capsule
a1 a2 :: Ptr ()
a2 =
  Capsule -> (Ptr () -> IO ()) -> IO ()
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Capsule
a1 ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  let {a2' :: Ptr ()
a2' = Ptr () -> Ptr ()
forall a. a -> a
id Ptr ()
a2} in 
  Ptr () -> Ptr () -> IO CInt
setContext'_ Ptr ()
a1' Ptr ()
a2' IO CInt -> (CInt -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: CInt
res ->
  CInt -> IO ()
checkStatusCode CInt
res IO () -> (() -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: ()
res' ->
  () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return (()
res')

{-# LINE 148 "lib/CPython/Types/Capsule.chs" #-}


-- setName :: Capsule -> Maybe Text -> IO ()
-- setName = undefined

foreign import ccall unsafe "CPython/Types/Capsule.chs.h hscpython_PyCapsule_Type"
  capsuleType'_ :: (IO (C2HSImp.Ptr ()))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_GetPointer"
  pyCapsuleGetPointer :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyErr_Clear"
  pyErrClear :: (IO ())

foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_GetContext"
  pyCapsuleGetContext :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyErr_Occurred"
  pyErrOccurred :: (IO (C2HSImp.Ptr ()))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_GetName"
  pyCapsuleGetName :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_Import"
  pyCapsuleImport :: ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CInt -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_IsValid"
  pyCapsuleIsValid :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr C2HSImp.CChar) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_SetPointer"
  setPointer'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_SetContext"
  setContext'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))