-- 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/WeakReference.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.WeakReference
  ( Reference
  , Proxy
  , newReference
  , newProxy
  , getObject
  ) where
import qualified Foreign.Ptr as C2HSImp





import           CPython.Internal

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

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

-- | Return a weak reference for the object. This will always return a new
-- reference, but is not guaranteed to create a new object; an existing
-- reference object may be returned. The second parameter, /callback/, can
-- be a callable object that receives notification when /obj/ is garbage
-- collected; it should accept a single parameter, which will be the weak
-- reference object itself. If ob is not a weakly-referencable object, or if
-- /callback/ is not callable, this will throw a @TypeError@.
newReference :: (Object obj, Object callback) => obj -> Maybe callback -> IO Reference
newReference :: obj -> Maybe callback -> IO Reference
newReference obj :: obj
obj cb :: Maybe callback
cb =
  obj -> (Ptr () -> IO Reference) -> IO Reference
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject obj
obj ((Ptr () -> IO Reference) -> IO Reference)
-> (Ptr () -> IO Reference) -> IO Reference
forall a b. (a -> b) -> a -> b
$ \objPtr :: Ptr ()
objPtr ->
  (callback -> (Ptr () -> IO Reference) -> IO Reference)
-> Maybe callback -> (Ptr () -> IO Reference) -> IO Reference
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith callback -> (Ptr () -> IO Reference) -> IO Reference
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Maybe callback
cb ((Ptr () -> IO Reference) -> IO Reference)
-> (Ptr () -> IO Reference) -> IO Reference
forall a b. (a -> b) -> a -> b
$ \cbPtr :: Ptr ()
cbPtr ->
  Ptr () -> Ptr () -> IO (Ptr ())
pyWeakrefNewRef Ptr ()
objPtr Ptr ()
cbPtr
  IO (Ptr ()) -> (Ptr () -> IO Reference) -> IO Reference
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr () -> IO Reference
forall obj a. Object obj => Ptr a -> IO obj
stealObject

-- | Return a weak reference proxy for the object. This will always return a
-- new reference, but is not guaranteed to create a new object; an existing
-- proxy may be returned. The second parameter, /callback/, can be a callable
-- object that receives notification when /obj/ is garbage collected; it
-- should accept a single parameter, which will be the weak reference object
-- itself. If ob is not a weakly-referencable object, or if /callback/ is not
-- callable, this will throw a @TypeError@.
newProxy :: (Object obj, Object callback) => obj -> Maybe callback -> IO Proxy
newProxy :: obj -> Maybe callback -> IO Proxy
newProxy obj :: obj
obj cb :: Maybe callback
cb =
  obj -> (Ptr () -> IO Proxy) -> IO Proxy
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject obj
obj ((Ptr () -> IO Proxy) -> IO Proxy)
-> (Ptr () -> IO Proxy) -> IO Proxy
forall a b. (a -> b) -> a -> b
$ \objPtr :: Ptr ()
objPtr ->
  (callback -> (Ptr () -> IO Proxy) -> IO Proxy)
-> Maybe callback -> (Ptr () -> IO Proxy) -> IO Proxy
forall a b c.
(a -> (Ptr b -> IO c) -> IO c)
-> Maybe a -> (Ptr b -> IO c) -> IO c
maybeWith callback -> (Ptr () -> IO Proxy) -> IO Proxy
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Maybe callback
cb ((Ptr () -> IO Proxy) -> IO Proxy)
-> (Ptr () -> IO Proxy) -> IO Proxy
forall a b. (a -> b) -> a -> b
$ \cbPtr :: Ptr ()
cbPtr ->
  Ptr () -> Ptr () -> IO (Ptr ())
pyWeakrefNewProxy Ptr ()
objPtr Ptr ()
cbPtr
  IO (Ptr ()) -> (Ptr () -> IO Proxy) -> IO Proxy
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr () -> IO Proxy
forall obj a. Object obj => Ptr a -> IO obj
stealObject

-- | Return the referenced object from a weak reference. If the referent is
-- no longer live, returns @None@.
getObject :: (Reference) -> IO ((SomeObject))
getObject :: Reference -> IO SomeObject
getObject a1 :: Reference
a1 =
  withObject a1 $ \a1' -> 
  Ptr () -> IO (Ptr ())
getObject'_ Ptr ()
a1' 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
peekObject 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 72 "lib/CPython/Types/WeakReference.chs" #-}


foreign import ccall safe "CPython/Types/WeakReference.chs.h PyWeakref_NewRef"
  pyWeakrefNewRef :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/WeakReference.chs.h PyWeakref_NewProxy"
  pyWeakrefNewProxy :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/WeakReference.chs.h PyWeakref_GetObject"
  getObject'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))