{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson and Iñaki García Etxebarria
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- t'GI.Gio.Interfaces.PollableInputStream.PollableInputStream' is implemented by @/GInputStreams/@ that
-- can be polled for readiness to read. This can be used when
-- interfacing with a non-GIO API that expects
-- UNIX-file-descriptor-style asynchronous I\/O rather than GIO-style.
-- 
-- /Since: 2.28/

#if (MIN_VERSION_haskell_gi_overloading(1,0,0) && !defined(__HADDOCK_VERSION__))
#define ENABLE_OVERLOADING
#endif

module GI.Gio.Interfaces.PollableInputStream
    ( 

-- * Exported types
    PollableInputStream(..)                 ,
    IsPollableInputStream                   ,
    toPollableInputStream                   ,


 -- * Methods
-- | 
-- 
--  === __Click to display all available methods, including inherited ones__
-- ==== Methods
-- [bindProperty]("GI.GObject.Objects.Object#g:method:bindProperty"), [bindPropertyFull]("GI.GObject.Objects.Object#g:method:bindPropertyFull"), [canPoll]("GI.Gio.Interfaces.PollableInputStream#g:method:canPoll"), [clearPending]("GI.Gio.Objects.InputStream#g:method:clearPending"), [close]("GI.Gio.Objects.InputStream#g:method:close"), [closeAsync]("GI.Gio.Objects.InputStream#g:method:closeAsync"), [closeFinish]("GI.Gio.Objects.InputStream#g:method:closeFinish"), [createSource]("GI.Gio.Interfaces.PollableInputStream#g:method:createSource"), [forceFloating]("GI.GObject.Objects.Object#g:method:forceFloating"), [freezeNotify]("GI.GObject.Objects.Object#g:method:freezeNotify"), [getv]("GI.GObject.Objects.Object#g:method:getv"), [hasPending]("GI.Gio.Objects.InputStream#g:method:hasPending"), [isClosed]("GI.Gio.Objects.InputStream#g:method:isClosed"), [isFloating]("GI.GObject.Objects.Object#g:method:isFloating"), [isReadable]("GI.Gio.Interfaces.PollableInputStream#g:method:isReadable"), [notify]("GI.GObject.Objects.Object#g:method:notify"), [notifyByPspec]("GI.GObject.Objects.Object#g:method:notifyByPspec"), [read]("GI.Gio.Objects.InputStream#g:method:read"), [readAll]("GI.Gio.Objects.InputStream#g:method:readAll"), [readAllAsync]("GI.Gio.Objects.InputStream#g:method:readAllAsync"), [readAllFinish]("GI.Gio.Objects.InputStream#g:method:readAllFinish"), [readAsync]("GI.Gio.Objects.InputStream#g:method:readAsync"), [readBytes]("GI.Gio.Objects.InputStream#g:method:readBytes"), [readBytesAsync]("GI.Gio.Objects.InputStream#g:method:readBytesAsync"), [readBytesFinish]("GI.Gio.Objects.InputStream#g:method:readBytesFinish"), [readFinish]("GI.Gio.Objects.InputStream#g:method:readFinish"), [readNonblocking]("GI.Gio.Interfaces.PollableInputStream#g:method:readNonblocking"), [ref]("GI.GObject.Objects.Object#g:method:ref"), [refSink]("GI.GObject.Objects.Object#g:method:refSink"), [runDispose]("GI.GObject.Objects.Object#g:method:runDispose"), [skip]("GI.Gio.Objects.InputStream#g:method:skip"), [skipAsync]("GI.Gio.Objects.InputStream#g:method:skipAsync"), [skipFinish]("GI.Gio.Objects.InputStream#g:method:skipFinish"), [stealData]("GI.GObject.Objects.Object#g:method:stealData"), [stealQdata]("GI.GObject.Objects.Object#g:method:stealQdata"), [thawNotify]("GI.GObject.Objects.Object#g:method:thawNotify"), [unref]("GI.GObject.Objects.Object#g:method:unref"), [watchClosure]("GI.GObject.Objects.Object#g:method:watchClosure").
-- 
-- ==== Getters
-- [getData]("GI.GObject.Objects.Object#g:method:getData"), [getProperty]("GI.GObject.Objects.Object#g:method:getProperty"), [getQdata]("GI.GObject.Objects.Object#g:method:getQdata").
-- 
-- ==== Setters
-- [setData]("GI.GObject.Objects.Object#g:method:setData"), [setDataFull]("GI.GObject.Objects.Object#g:method:setDataFull"), [setPending]("GI.Gio.Objects.InputStream#g:method:setPending"), [setProperty]("GI.GObject.Objects.Object#g:method:setProperty").

#if defined(ENABLE_OVERLOADING)
    ResolvePollableInputStreamMethod        ,
#endif

-- ** canPoll #method:canPoll#

#if defined(ENABLE_OVERLOADING)
    PollableInputStreamCanPollMethodInfo    ,
#endif
    pollableInputStreamCanPoll              ,


-- ** createSource #method:createSource#

#if defined(ENABLE_OVERLOADING)
    PollableInputStreamCreateSourceMethodInfo,
#endif
    pollableInputStreamCreateSource         ,


-- ** isReadable #method:isReadable#

#if defined(ENABLE_OVERLOADING)
    PollableInputStreamIsReadableMethodInfo ,
#endif
    pollableInputStreamIsReadable           ,


-- ** readNonblocking #method:readNonblocking#

#if defined(ENABLE_OVERLOADING)
    PollableInputStreamReadNonblockingMethodInfo,
#endif
    pollableInputStreamReadNonblocking      ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.BasicTypes as B.Types
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GArray as B.GArray
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GHashTable as B.GHT
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
import qualified Data.GI.Base.Signals as B.Signals
import qualified Control.Monad.IO.Class as MIO
import qualified Data.Coerce as Coerce
import qualified Data.Text as T
import qualified Data.Kind as DK
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP
import qualified GHC.OverloadedLabels as OL
import qualified GHC.Records as R

import qualified GI.GLib.Structs.Source as GLib.Source
import qualified GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.Gio.Objects.Cancellable as Gio.Cancellable
import {-# SOURCE #-} qualified GI.Gio.Objects.InputStream as Gio.InputStream

-- interface PollableInputStream 
-- | Memory-managed wrapper type.
newtype PollableInputStream = PollableInputStream (SP.ManagedPtr PollableInputStream)
    deriving (PollableInputStream -> PollableInputStream -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PollableInputStream -> PollableInputStream -> Bool
$c/= :: PollableInputStream -> PollableInputStream -> Bool
== :: PollableInputStream -> PollableInputStream -> Bool
$c== :: PollableInputStream -> PollableInputStream -> Bool
Eq)

instance SP.ManagedPtrNewtype PollableInputStream where
    toManagedPtr :: PollableInputStream -> ManagedPtr PollableInputStream
toManagedPtr (PollableInputStream ManagedPtr PollableInputStream
p) = ManagedPtr PollableInputStream
p

foreign import ccall "g_pollable_input_stream_get_type"
    c_g_pollable_input_stream_get_type :: IO B.Types.GType

instance B.Types.TypedObject PollableInputStream where
    glibType :: IO GType
glibType = IO GType
c_g_pollable_input_stream_get_type

instance B.Types.GObject PollableInputStream

-- | Type class for types which can be safely cast to `PollableInputStream`, for instance with `toPollableInputStream`.
class (SP.GObject o, O.IsDescendantOf PollableInputStream o) => IsPollableInputStream o
instance (SP.GObject o, O.IsDescendantOf PollableInputStream o) => IsPollableInputStream o

instance O.HasParentTypes PollableInputStream
type instance O.ParentTypes PollableInputStream = '[Gio.InputStream.InputStream, GObject.Object.Object]

-- | Cast to `PollableInputStream`, for types for which this is known to be safe. For general casts, use `Data.GI.Base.ManagedPtr.castTo`.
toPollableInputStream :: (MIO.MonadIO m, IsPollableInputStream o) => o -> m PollableInputStream
toPollableInputStream :: forall (m :: * -> *) o.
(MonadIO m, IsPollableInputStream o) =>
o -> m PollableInputStream
toPollableInputStream = forall (m :: * -> *) a. MonadIO m => IO a -> m a
MIO.liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall o o'.
(HasCallStack, ManagedPtrNewtype o, TypedObject o,
 ManagedPtrNewtype o', TypedObject o') =>
(ManagedPtr o' -> o') -> o -> IO o'
B.ManagedPtr.unsafeCastTo ManagedPtr PollableInputStream -> PollableInputStream
PollableInputStream

-- | Convert 'PollableInputStream' to and from 'Data.GI.Base.GValue.GValue'. See 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue (Maybe PollableInputStream) where
    gvalueGType_ :: IO GType
gvalueGType_ = IO GType
c_g_pollable_input_stream_get_type
    gvalueSet_ :: Ptr GValue -> Maybe PollableInputStream -> IO ()
gvalueSet_ Ptr GValue
gv Maybe PollableInputStream
P.Nothing = forall a. GObject a => Ptr GValue -> Ptr a -> IO ()
B.GValue.set_object Ptr GValue
gv (forall a. Ptr a
FP.nullPtr :: FP.Ptr PollableInputStream)
    gvalueSet_ Ptr GValue
gv (P.Just PollableInputStream
obj) = forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
B.ManagedPtr.withManagedPtr PollableInputStream
obj (forall a. GObject a => Ptr GValue -> Ptr a -> IO ()
B.GValue.set_object Ptr GValue
gv)
    gvalueGet_ :: Ptr GValue -> IO (Maybe PollableInputStream)
gvalueGet_ Ptr GValue
gv = do
        Ptr PollableInputStream
ptr <- forall a. GObject a => Ptr GValue -> IO (Ptr a)
B.GValue.get_object Ptr GValue
gv :: IO (FP.Ptr PollableInputStream)
        if Ptr PollableInputStream
ptr forall a. Eq a => a -> a -> Bool
/= forall a. Ptr a
FP.nullPtr
        then forall a. a -> Maybe a
P.Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a b.
(HasCallStack, GObject a, GObject b) =>
(ManagedPtr a -> a) -> Ptr b -> IO a
B.ManagedPtr.newObject ManagedPtr PollableInputStream -> PollableInputStream
PollableInputStream Ptr PollableInputStream
ptr
        else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
P.Nothing
        
    

#if defined(ENABLE_OVERLOADING)
instance O.HasAttributeList PollableInputStream
type instance O.AttributeList PollableInputStream = PollableInputStreamAttributeList
type PollableInputStreamAttributeList = ('[ ] :: [(Symbol, DK.Type)])
#endif

#if defined(ENABLE_OVERLOADING)
#endif

#if defined(ENABLE_OVERLOADING)
type family ResolvePollableInputStreamMethod (t :: Symbol) (o :: DK.Type) :: DK.Type where
    ResolvePollableInputStreamMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolvePollableInputStreamMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolvePollableInputStreamMethod "canPoll" o = PollableInputStreamCanPollMethodInfo
    ResolvePollableInputStreamMethod "clearPending" o = Gio.InputStream.InputStreamClearPendingMethodInfo
    ResolvePollableInputStreamMethod "close" o = Gio.InputStream.InputStreamCloseMethodInfo
    ResolvePollableInputStreamMethod "closeAsync" o = Gio.InputStream.InputStreamCloseAsyncMethodInfo
    ResolvePollableInputStreamMethod "closeFinish" o = Gio.InputStream.InputStreamCloseFinishMethodInfo
    ResolvePollableInputStreamMethod "createSource" o = PollableInputStreamCreateSourceMethodInfo
    ResolvePollableInputStreamMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolvePollableInputStreamMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolvePollableInputStreamMethod "getv" o = GObject.Object.ObjectGetvMethodInfo
    ResolvePollableInputStreamMethod "hasPending" o = Gio.InputStream.InputStreamHasPendingMethodInfo
    ResolvePollableInputStreamMethod "isClosed" o = Gio.InputStream.InputStreamIsClosedMethodInfo
    ResolvePollableInputStreamMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolvePollableInputStreamMethod "isReadable" o = PollableInputStreamIsReadableMethodInfo
    ResolvePollableInputStreamMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolvePollableInputStreamMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolvePollableInputStreamMethod "read" o = Gio.InputStream.InputStreamReadMethodInfo
    ResolvePollableInputStreamMethod "readAll" o = Gio.InputStream.InputStreamReadAllMethodInfo
    ResolvePollableInputStreamMethod "readAllAsync" o = Gio.InputStream.InputStreamReadAllAsyncMethodInfo
    ResolvePollableInputStreamMethod "readAllFinish" o = Gio.InputStream.InputStreamReadAllFinishMethodInfo
    ResolvePollableInputStreamMethod "readAsync" o = Gio.InputStream.InputStreamReadAsyncMethodInfo
    ResolvePollableInputStreamMethod "readBytes" o = Gio.InputStream.InputStreamReadBytesMethodInfo
    ResolvePollableInputStreamMethod "readBytesAsync" o = Gio.InputStream.InputStreamReadBytesAsyncMethodInfo
    ResolvePollableInputStreamMethod "readBytesFinish" o = Gio.InputStream.InputStreamReadBytesFinishMethodInfo
    ResolvePollableInputStreamMethod "readFinish" o = Gio.InputStream.InputStreamReadFinishMethodInfo
    ResolvePollableInputStreamMethod "readNonblocking" o = PollableInputStreamReadNonblockingMethodInfo
    ResolvePollableInputStreamMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolvePollableInputStreamMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolvePollableInputStreamMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolvePollableInputStreamMethod "skip" o = Gio.InputStream.InputStreamSkipMethodInfo
    ResolvePollableInputStreamMethod "skipAsync" o = Gio.InputStream.InputStreamSkipAsyncMethodInfo
    ResolvePollableInputStreamMethod "skipFinish" o = Gio.InputStream.InputStreamSkipFinishMethodInfo
    ResolvePollableInputStreamMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolvePollableInputStreamMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolvePollableInputStreamMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolvePollableInputStreamMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolvePollableInputStreamMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolvePollableInputStreamMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolvePollableInputStreamMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolvePollableInputStreamMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolvePollableInputStreamMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolvePollableInputStreamMethod "setDataFull" o = GObject.Object.ObjectSetDataFullMethodInfo
    ResolvePollableInputStreamMethod "setPending" o = Gio.InputStream.InputStreamSetPendingMethodInfo
    ResolvePollableInputStreamMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolvePollableInputStreamMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolvePollableInputStreamMethod t PollableInputStream, O.OverloadedMethod info PollableInputStream p) => OL.IsLabel t (PollableInputStream -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod @info
#else
    fromLabel _ = O.overloadedMethod @info
#endif

#if MIN_VERSION_base(4,13,0)
instance (info ~ ResolvePollableInputStreamMethod t PollableInputStream, O.OverloadedMethod info PollableInputStream p, R.HasField t PollableInputStream p) => R.HasField t PollableInputStream p where
    getField = O.overloadedMethod @info

#endif

instance (info ~ ResolvePollableInputStreamMethod t PollableInputStream, O.OverloadedMethodInfo info PollableInputStream) => OL.IsLabel t (O.MethodProxy info PollableInputStream) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.MethodProxy
#else
    fromLabel _ = O.MethodProxy
#endif

#endif

-- method PollableInputStream::can_poll
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "stream"
--           , argType =
--               TInterface
--                 Name { namespace = "Gio" , name = "PollableInputStream" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "a #GPollableInputStream."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_input_stream_can_poll" g_pollable_input_stream_can_poll :: 
    Ptr PollableInputStream ->              -- stream : TInterface (Name {namespace = "Gio", name = "PollableInputStream"})
    IO CInt

-- | Checks if /@stream@/ is actually pollable. Some classes may implement
-- t'GI.Gio.Interfaces.PollableInputStream.PollableInputStream' but have only certain instances of that class
-- be pollable. If this method returns 'P.False', then the behavior of
-- other t'GI.Gio.Interfaces.PollableInputStream.PollableInputStream' methods is undefined.
-- 
-- For any given stream, the value returned by this method is constant;
-- a stream cannot switch from pollable to non-pollable or vice versa.
-- 
-- /Since: 2.28/
pollableInputStreamCanPoll ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableInputStream a) =>
    a
    -- ^ /@stream@/: a t'GI.Gio.Interfaces.PollableInputStream.PollableInputStream'.
    -> m Bool
    -- ^ __Returns:__ 'P.True' if /@stream@/ is pollable, 'P.False' if not.
pollableInputStreamCanPoll :: forall (m :: * -> *) a.
(HasCallStack, MonadIO m, IsPollableInputStream a) =>
a -> m Bool
pollableInputStreamCanPoll a
stream = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
    Ptr PollableInputStream
stream' <- forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
unsafeManagedPtrCastPtr a
stream
    CInt
result <- Ptr PollableInputStream -> IO CInt
g_pollable_input_stream_can_poll Ptr PollableInputStream
stream'
    let result' :: Bool
result' = (forall a. Eq a => a -> a -> Bool
/= CInt
0) CInt
result
    forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr a
stream
    forall (m :: * -> *) a. Monad m => a -> m a
return Bool
result'

#if defined(ENABLE_OVERLOADING)
data PollableInputStreamCanPollMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsPollableInputStream a) => O.OverloadedMethod PollableInputStreamCanPollMethodInfo a signature where
    overloadedMethod = pollableInputStreamCanPoll

instance O.OverloadedMethodInfo PollableInputStreamCanPollMethodInfo a where
    overloadedMethodInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.Gio.Interfaces.PollableInputStream.pollableInputStreamCanPoll",
        O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-gio-2.0.30/docs/GI-Gio-Interfaces-PollableInputStream.html#v:pollableInputStreamCanPoll"
        })


#endif

-- method PollableInputStream::create_source
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "stream"
--           , argType =
--               TInterface
--                 Name { namespace = "Gio" , name = "PollableInputStream" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "a #GPollableInputStream."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "cancellable"
--           , argType =
--               TInterface Name { namespace = "Gio" , name = "Cancellable" }
--           , direction = DirectionIn
--           , mayBeNull = True
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "a #GCancellable, or %NULL"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just (TInterface Name { namespace = "GLib" , name = "Source" })
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_input_stream_create_source" g_pollable_input_stream_create_source :: 
    Ptr PollableInputStream ->              -- stream : TInterface (Name {namespace = "Gio", name = "PollableInputStream"})
    Ptr Gio.Cancellable.Cancellable ->      -- cancellable : TInterface (Name {namespace = "Gio", name = "Cancellable"})
    IO (Ptr GLib.Source.Source)

-- | Creates a t'GI.GLib.Structs.Source.Source' that triggers when /@stream@/ can be read, or
-- /@cancellable@/ is triggered or an error occurs. The callback on the
-- source is of the t'GI.Gio.Callbacks.PollableSourceFunc' type.
-- 
-- As with 'GI.Gio.Interfaces.PollableInputStream.pollableInputStreamIsReadable', it is possible that
-- the stream may not actually be readable even after the source
-- triggers, so you should use 'GI.Gio.Interfaces.PollableInputStream.pollableInputStreamReadNonblocking'
-- rather than 'GI.Gio.Objects.InputStream.inputStreamRead' from the callback.
-- 
-- /Since: 2.28/
pollableInputStreamCreateSource ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableInputStream a, Gio.Cancellable.IsCancellable b) =>
    a
    -- ^ /@stream@/: a t'GI.Gio.Interfaces.PollableInputStream.PollableInputStream'.
    -> Maybe (b)
    -- ^ /@cancellable@/: a t'GI.Gio.Objects.Cancellable.Cancellable', or 'P.Nothing'
    -> m GLib.Source.Source
    -- ^ __Returns:__ a new t'GI.GLib.Structs.Source.Source'
pollableInputStreamCreateSource :: forall (m :: * -> *) a b.
(HasCallStack, MonadIO m, IsPollableInputStream a,
 IsCancellable b) =>
a -> Maybe b -> m Source
pollableInputStreamCreateSource a
stream Maybe b
cancellable = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
    Ptr PollableInputStream
stream' <- forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
unsafeManagedPtrCastPtr a
stream
    Ptr Cancellable
maybeCancellable <- case Maybe b
cancellable of
        Maybe b
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Ptr a
nullPtr
        Just b
jCancellable -> do
            Ptr Cancellable
jCancellable' <- forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
unsafeManagedPtrCastPtr b
jCancellable
            forall (m :: * -> *) a. Monad m => a -> m a
return Ptr Cancellable
jCancellable'
    Ptr Source
result <- Ptr PollableInputStream -> Ptr Cancellable -> IO (Ptr Source)
g_pollable_input_stream_create_source Ptr PollableInputStream
stream' Ptr Cancellable
maybeCancellable
    forall a. HasCallStack => Text -> Ptr a -> IO ()
checkUnexpectedReturnNULL Text
"pollableInputStreamCreateSource" Ptr Source
result
    Source
result' <- (forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapBoxed ManagedPtr Source -> Source
GLib.Source.Source) Ptr Source
result
    forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr a
stream
    forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust Maybe b
cancellable forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr
    forall (m :: * -> *) a. Monad m => a -> m a
return Source
result'

#if defined(ENABLE_OVERLOADING)
data PollableInputStreamCreateSourceMethodInfo
instance (signature ~ (Maybe (b) -> m GLib.Source.Source), MonadIO m, IsPollableInputStream a, Gio.Cancellable.IsCancellable b) => O.OverloadedMethod PollableInputStreamCreateSourceMethodInfo a signature where
    overloadedMethod = pollableInputStreamCreateSource

instance O.OverloadedMethodInfo PollableInputStreamCreateSourceMethodInfo a where
    overloadedMethodInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.Gio.Interfaces.PollableInputStream.pollableInputStreamCreateSource",
        O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-gio-2.0.30/docs/GI-Gio-Interfaces-PollableInputStream.html#v:pollableInputStreamCreateSource"
        })


#endif

-- method PollableInputStream::is_readable
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "stream"
--           , argType =
--               TInterface
--                 Name { namespace = "Gio" , name = "PollableInputStream" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "a #GPollableInputStream."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_input_stream_is_readable" g_pollable_input_stream_is_readable :: 
    Ptr PollableInputStream ->              -- stream : TInterface (Name {namespace = "Gio", name = "PollableInputStream"})
    IO CInt

-- | Checks if /@stream@/ can be read.
-- 
-- Note that some stream types may not be able to implement this 100%
-- reliably, and it is possible that a call to 'GI.Gio.Objects.InputStream.inputStreamRead'
-- after this returns 'P.True' would still block. To guarantee
-- non-blocking behavior, you should always use
-- 'GI.Gio.Interfaces.PollableInputStream.pollableInputStreamReadNonblocking', which will return a
-- 'GI.Gio.Enums.IOErrorEnumWouldBlock' error rather than blocking.
-- 
-- /Since: 2.28/
pollableInputStreamIsReadable ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableInputStream a) =>
    a
    -- ^ /@stream@/: a t'GI.Gio.Interfaces.PollableInputStream.PollableInputStream'.
    -> m Bool
    -- ^ __Returns:__ 'P.True' if /@stream@/ is readable, 'P.False' if not. If an error
    --   has occurred on /@stream@/, this will result in
    --   'GI.Gio.Interfaces.PollableInputStream.pollableInputStreamIsReadable' returning 'P.True', and the
    --   next attempt to read will return the error.
pollableInputStreamIsReadable :: forall (m :: * -> *) a.
(HasCallStack, MonadIO m, IsPollableInputStream a) =>
a -> m Bool
pollableInputStreamIsReadable a
stream = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
    Ptr PollableInputStream
stream' <- forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
unsafeManagedPtrCastPtr a
stream
    CInt
result <- Ptr PollableInputStream -> IO CInt
g_pollable_input_stream_is_readable Ptr PollableInputStream
stream'
    let result' :: Bool
result' = (forall a. Eq a => a -> a -> Bool
/= CInt
0) CInt
result
    forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr a
stream
    forall (m :: * -> *) a. Monad m => a -> m a
return Bool
result'

#if defined(ENABLE_OVERLOADING)
data PollableInputStreamIsReadableMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsPollableInputStream a) => O.OverloadedMethod PollableInputStreamIsReadableMethodInfo a signature where
    overloadedMethod = pollableInputStreamIsReadable

instance O.OverloadedMethodInfo PollableInputStreamIsReadableMethodInfo a where
    overloadedMethodInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.Gio.Interfaces.PollableInputStream.pollableInputStreamIsReadable",
        O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-gio-2.0.30/docs/GI-Gio-Interfaces-PollableInputStream.html#v:pollableInputStreamIsReadable"
        })


#endif

-- method PollableInputStream::read_nonblocking
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "stream"
--           , argType =
--               TInterface
--                 Name { namespace = "Gio" , name = "PollableInputStream" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "a #GPollableInputStream"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "buffer"
--           , argType = TCArray False (-1) 2 (TBasicType TUInt8)
--           , direction = DirectionOut
--           , mayBeNull = True
--           , argDoc =
--               Documentation
--                 { rawDocText =
--                     Just
--                       "a\n    buffer to read data into (which should be at least @count bytes long)."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = True
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "count"
--           , argType = TBasicType TUInt32
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "the number of bytes you want to read"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "cancellable"
--           , argType =
--               TInterface Name { namespace = "Gio" , name = "Cancellable" }
--           , direction = DirectionIn
--           , mayBeNull = True
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "a #GCancellable, or %NULL"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: [ Arg
--              { argCName = "count"
--              , argType = TBasicType TUInt32
--              , direction = DirectionIn
--              , mayBeNull = False
--              , argDoc =
--                  Documentation
--                    { rawDocText = Just "the number of bytes you want to read"
--                    , sinceVersion = Nothing
--                    }
--              , argScope = ScopeTypeInvalid
--              , argClosure = -1
--              , argDestroy = -1
--              , argCallerAllocates = False
--              , transfer = TransferNothing
--              }
--          ]
-- returnType: Just (TBasicType TInt32)
-- throws : True
-- Skip return : False

foreign import ccall "g_pollable_input_stream_read_nonblocking" g_pollable_input_stream_read_nonblocking :: 
    Ptr PollableInputStream ->              -- stream : TInterface (Name {namespace = "Gio", name = "PollableInputStream"})
    Ptr Word8 ->                            -- buffer : TCArray False (-1) 2 (TBasicType TUInt8)
    Word32 ->                               -- count : TBasicType TUInt32
    Ptr Gio.Cancellable.Cancellable ->      -- cancellable : TInterface (Name {namespace = "Gio", name = "Cancellable"})
    Ptr (Ptr GError) ->                     -- error
    IO Int32

-- | Attempts to read up to /@count@/ bytes from /@stream@/ into /@buffer@/, as
-- with 'GI.Gio.Objects.InputStream.inputStreamRead'. If /@stream@/ is not currently readable,
-- this will immediately return 'GI.Gio.Enums.IOErrorEnumWouldBlock', and you can
-- use 'GI.Gio.Interfaces.PollableInputStream.pollableInputStreamCreateSource' to create a t'GI.GLib.Structs.Source.Source'
-- that will be triggered when /@stream@/ is readable.
-- 
-- Note that since this method never blocks, you cannot actually
-- use /@cancellable@/ to cancel it. However, it will return an error
-- if /@cancellable@/ has already been cancelled when you call, which
-- may happen if you call this method after a source triggers due
-- to having been cancelled.
pollableInputStreamReadNonblocking ::
    (B.CallStack.HasCallStack, MonadIO m, IsPollableInputStream a, Gio.Cancellable.IsCancellable b) =>
    a
    -- ^ /@stream@/: a t'GI.Gio.Interfaces.PollableInputStream.PollableInputStream'
    -> Maybe (ByteString)
    -- ^ /@buffer@/: a
    --     buffer to read data into (which should be at least /@count@/ bytes long).
    -> Maybe (b)
    -- ^ /@cancellable@/: a t'GI.Gio.Objects.Cancellable.Cancellable', or 'P.Nothing'
    -> m ((Int32, Maybe ByteString))
    -- ^ __Returns:__ the number of bytes read, or -1 on error (including
    --   'GI.Gio.Enums.IOErrorEnumWouldBlock'). /(Can throw 'Data.GI.Base.GError.GError')/
pollableInputStreamReadNonblocking :: forall (m :: * -> *) a b.
(HasCallStack, MonadIO m, IsPollableInputStream a,
 IsCancellable b) =>
a -> Maybe ByteString -> Maybe b -> m (Int32, Maybe ByteString)
pollableInputStreamReadNonblocking a
stream Maybe ByteString
buffer Maybe b
cancellable = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
    let count :: Word32
count = case Maybe ByteString
buffer of
            Maybe ByteString
Nothing -> Word32
0
            Just ByteString
jBuffer -> forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int
B.length ByteString
jBuffer
    Ptr PollableInputStream
stream' <- forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
unsafeManagedPtrCastPtr a
stream
    Ptr Word8
maybeBuffer <- case Maybe ByteString
buffer of
        Maybe ByteString
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Ptr a
nullPtr
        Just ByteString
jBuffer -> do
            Ptr Word8
jBuffer' <- ByteString -> IO (Ptr Word8)
packByteString ByteString
jBuffer
            forall (m :: * -> *) a. Monad m => a -> m a
return Ptr Word8
jBuffer'
    Ptr Cancellable
maybeCancellable <- case Maybe b
cancellable of
        Maybe b
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Ptr a
nullPtr
        Just b
jCancellable -> do
            Ptr Cancellable
jCancellable' <- forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
unsafeManagedPtrCastPtr b
jCancellable
            forall (m :: * -> *) a. Monad m => a -> m a
return Ptr Cancellable
jCancellable'
    forall a b. IO a -> IO b -> IO a
onException (do
        Int32
result <- forall a. (Ptr (Ptr GError) -> IO a) -> IO a
propagateGError forall a b. (a -> b) -> a -> b
$ Ptr PollableInputStream
-> Ptr Word8
-> Word32
-> Ptr Cancellable
-> Ptr (Ptr GError)
-> IO Int32
g_pollable_input_stream_read_nonblocking Ptr PollableInputStream
stream' Ptr Word8
maybeBuffer Word32
count Ptr Cancellable
maybeCancellable
        Maybe ByteString
maybeMaybeBuffer <- forall a b. Ptr a -> (Ptr a -> IO b) -> IO (Maybe b)
convertIfNonNull Ptr Word8
maybeBuffer forall a b. (a -> b) -> a -> b
$ \Ptr Word8
maybeBuffer' -> do
            ByteString
maybeBuffer'' <- (forall a. Integral a => a -> Ptr Word8 -> IO ByteString
unpackByteStringWithLength Word32
count) Ptr Word8
maybeBuffer'
            forall a. Ptr a -> IO ()
freeMem Ptr Word8
maybeBuffer'
            forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
maybeBuffer''
        forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr a
stream
        forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust Maybe b
cancellable forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr
        forall (m :: * -> *) a. Monad m => a -> m a
return (Int32
result, Maybe ByteString
maybeMaybeBuffer)
     ) (do
        forall a. Ptr a -> IO ()
freeMem Ptr Word8
maybeBuffer
     )

#if defined(ENABLE_OVERLOADING)
data PollableInputStreamReadNonblockingMethodInfo
instance (signature ~ (Maybe (ByteString) -> Maybe (b) -> m ((Int32, Maybe ByteString))), MonadIO m, IsPollableInputStream a, Gio.Cancellable.IsCancellable b) => O.OverloadedMethod PollableInputStreamReadNonblockingMethodInfo a signature where
    overloadedMethod = pollableInputStreamReadNonblocking

instance O.OverloadedMethodInfo PollableInputStreamReadNonblockingMethodInfo a where
    overloadedMethodInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.Gio.Interfaces.PollableInputStream.pollableInputStreamReadNonblocking",
        O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-gio-2.0.30/docs/GI-Gio-Interfaces-PollableInputStream.html#v:pollableInputStreamReadNonblocking"
        })


#endif

#if defined(ENABLE_OVERLOADING)
type instance O.SignalList PollableInputStream = PollableInputStreamSignalList
type PollableInputStreamSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, DK.Type)])

#endif