-- 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/Iterator.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.Iterator
  ( Iterator (..)
  , SomeIterator
  , castToIterator
  , next
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp





import           CPython.Internal

-- | Attempt to convert an object to a generic 'Iterator'. If the object does
-- not implement the iterator protocol, returns 'Nothing'.
castToIterator :: Object a => a -> IO (Maybe SomeIterator)
castToIterator :: a -> IO (Maybe SomeIterator)
castToIterator obj :: a
obj =
  a -> (Ptr () -> IO (Maybe SomeIterator)) -> IO (Maybe SomeIterator)
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject a
obj ((Ptr () -> IO (Maybe SomeIterator)) -> IO (Maybe SomeIterator))
-> (Ptr () -> IO (Maybe SomeIterator)) -> IO (Maybe SomeIterator)
forall a b. (a -> b) -> a -> b
$ \objPtr :: Ptr ()
objPtr -> do
  Bool
isIter <- (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
hscpythonPyIterCheck Ptr ()
objPtr
  Maybe SomeIterator -> IO (Maybe SomeIterator)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe SomeIterator -> IO (Maybe SomeIterator))
-> Maybe SomeIterator -> IO (Maybe SomeIterator)
forall a b. (a -> b) -> a -> b
$ if Bool
isIter
    then SomeIterator -> Maybe SomeIterator
forall a. a -> Maybe a
Just (SomeIterator -> Maybe SomeIterator)
-> SomeIterator -> Maybe SomeIterator
forall a b. (a -> b) -> a -> b
$ a -> SomeIterator
forall a. Object a => a -> SomeIterator
unsafeCastToIterator a
obj
    else Maybe SomeIterator
forall a. Maybe a
Nothing

-- | Return the next value from the iteration, or 'Nothing' if there are no
-- remaining items.
next :: Iterator iter => iter -> IO (Maybe SomeObject)
next :: iter -> IO (Maybe SomeObject)
next iter :: iter
iter =
  iter -> (Ptr () -> IO (Maybe SomeObject)) -> IO (Maybe SomeObject)
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject iter
iter ((Ptr () -> IO (Maybe SomeObject)) -> IO (Maybe SomeObject))
-> (Ptr () -> IO (Maybe SomeObject)) -> IO (Maybe SomeObject)
forall a b. (a -> b) -> a -> b
$ \iterPtr :: Ptr ()
iterPtr -> do
  Ptr ()
raw <- Ptr () -> IO (Ptr ())
pyIterNext Ptr ()
iterPtr
  if raw == nullPtr
    then do
      err <- pyErrOccurred
{-# LINE 47 "lib/CPython/Protocols/Iterator.chs" #-}

      exceptionIf $ err /= nullPtr
      return Nothing
    else fmap Just $ stealObject raw

foreign import ccall safe "CPython/Protocols/Iterator.chs.h hscpython_PyIter_Check"
  hscpythonPyIterCheck :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt))

foreign import ccall safe "CPython/Protocols/Iterator.chs.h PyIter_Next"
  pyIterNext :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

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