{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (garetxe@gmail.com)

This is the asynchronous version of 'GI.Gio.Interfaces.Initable.Initable'; it behaves the same
in all ways except that initialization is asynchronous. For more details
see the descriptions on 'GI.Gio.Interfaces.Initable.Initable'.

A class may implement both the 'GI.Gio.Interfaces.Initable.Initable' and 'GI.Gio.Interfaces.AsyncInitable.AsyncInitable' interfaces.

Users of objects implementing this are not intended to use the interface
method directly; instead it will be used automatically in various ways.
For C applications you generally just call @/g_async_initable_new_async()/@
directly, or indirectly via a @/foo_thing_new_async()/@ wrapper. This will call
'GI.Gio.Interfaces.AsyncInitable.asyncInitableInitAsync' under the cover, calling back with 'Nothing' and
a set @/GError/@ on failure.

A typical implementation might look something like this:


=== /C code/
>
>enum {
>   NOT_INITIALIZED,
>   INITIALIZING,
>   INITIALIZED
>};
>
>static void
>_foo_ready_cb (Foo *self)
>{
>  GList *l;
>
>  self->priv->state = INITIALIZED;
>
>  for (l = self->priv->init_results; l != NULL; l = l->next)
>    {
>      GTask *task = l->data;
>
>      if (self->priv->success)
>        g_task_return_boolean (task, TRUE);
>      else
>        g_task_return_new_error (task, ...);
>      g_object_unref (task);
>    }
>
>  g_list_free (self->priv->init_results);
>  self->priv->init_results = NULL;
>}
>
>static void
>foo_init_async (GAsyncInitable       *initable,
>                int                   io_priority,
>                GCancellable         *cancellable,
>                GAsyncReadyCallback   callback,
>                gpointer              user_data)
>{
>  Foo *self = FOO (initable);
>  GTask *task;
>
>  task = g_task_new (initable, cancellable, callback, user_data);
>
>  switch (self->priv->state)
>    {
>      case NOT_INITIALIZED:
>        _foo_get_ready (self);
>        self->priv->init_results = g_list_append (self->priv->init_results,
>                                                  task);
>        self->priv->state = INITIALIZING;
>        break;
>      case INITIALIZING:
>        self->priv->init_results = g_list_append (self->priv->init_results,
>                                                  task);
>        break;
>      case INITIALIZED:
>        if (!self->priv->success)
>          g_task_return_new_error (task, ...);
>        else
>          g_task_return_boolean (task, TRUE);
>        g_object_unref (task);
>        break;
>    }
>}
>
>static gboolean
>foo_init_finish (GAsyncInitable       *initable,
>                 GAsyncResult         *result,
>                 GError              **error)
>{
>  g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
>
>  return g_task_propagate_boolean (G_TASK (result), error);
>}
>
>static void
>foo_async_initable_iface_init (gpointer g_iface,
>                               gpointer data)
>{
>  GAsyncInitableIface *iface = g_iface;
>
>  iface->init_async = foo_init_async;
>  iface->init_finish = foo_init_finish;
>}

-}

module GI.Gio.Interfaces.AsyncInitable
    ( 

-- * Exported types
    AsyncInitable(..)                       ,
    noAsyncInitable                         ,
    IsAsyncInitable                         ,
    toAsyncInitable                         ,


 -- * Methods
-- ** initAsync #method:initAsync#
    AsyncInitableInitAsyncMethodInfo        ,
    asyncInitableInitAsync                  ,


-- ** initFinish #method:initFinish#
    AsyncInitableInitFinishMethodInfo       ,
    asyncInitableInitFinish                 ,


-- ** newFinish #method:newFinish#
    AsyncInitableNewFinishMethodInfo        ,
    asyncInitableNewFinish                  ,


-- ** newvAsync #method:newvAsync#
    asyncInitableNewvAsync                  ,




    ) 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.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP

import qualified GI.GObject.Objects.Object as GObject.Object
import qualified GI.GObject.Structs.Parameter as GObject.Parameter
import qualified GI.Gio.Callbacks as Gio.Callbacks
import {-# SOURCE #-} qualified GI.Gio.Interfaces.AsyncResult as Gio.AsyncResult
import {-# SOURCE #-} qualified GI.Gio.Objects.Cancellable as Gio.Cancellable

-- interface AsyncInitable 
newtype AsyncInitable = AsyncInitable (ManagedPtr AsyncInitable)
noAsyncInitable :: Maybe AsyncInitable
noAsyncInitable = Nothing

type instance O.SignalList AsyncInitable = AsyncInitableSignalList
type AsyncInitableSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

foreign import ccall "g_async_initable_get_type"
    c_g_async_initable_get_type :: IO GType

instance GObject AsyncInitable where
    gobjectType _ = c_g_async_initable_get_type
    

class GObject o => IsAsyncInitable o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError AsyncInitable a) =>
    IsAsyncInitable a
#endif
instance IsAsyncInitable AsyncInitable
instance GObject.Object.IsObject AsyncInitable

toAsyncInitable :: IsAsyncInitable o => o -> IO AsyncInitable
toAsyncInitable = unsafeCastTo AsyncInitable

instance O.HasAttributeList AsyncInitable
type instance O.AttributeList AsyncInitable = AsyncInitableAttributeList
type AsyncInitableAttributeList = ('[ ] :: [(Symbol, *)])

type family ResolveAsyncInitableMethod (t :: Symbol) (o :: *) :: * where
    ResolveAsyncInitableMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveAsyncInitableMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveAsyncInitableMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveAsyncInitableMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveAsyncInitableMethod "initAsync" o = AsyncInitableInitAsyncMethodInfo
    ResolveAsyncInitableMethod "initFinish" o = AsyncInitableInitFinishMethodInfo
    ResolveAsyncInitableMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveAsyncInitableMethod "newFinish" o = AsyncInitableNewFinishMethodInfo
    ResolveAsyncInitableMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveAsyncInitableMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveAsyncInitableMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveAsyncInitableMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveAsyncInitableMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveAsyncInitableMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveAsyncInitableMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveAsyncInitableMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveAsyncInitableMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveAsyncInitableMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveAsyncInitableMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveAsyncInitableMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveAsyncInitableMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveAsyncInitableMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveAsyncInitableMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveAsyncInitableMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveAsyncInitableMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveAsyncInitableMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveAsyncInitableMethod t AsyncInitable, O.MethodInfo info AsyncInitable p) => O.IsLabelProxy t (AsyncInitable -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveAsyncInitableMethod t AsyncInitable, O.MethodInfo info AsyncInitable p) => O.IsLabel t (AsyncInitable -> p) where
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

-- method AsyncInitable::init_async
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "initable", argType = TInterface (Name {namespace = "Gio", name = "AsyncInitable"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GAsyncInitable.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "io_priority", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the [I/O priority][io-priority] of the operation", 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 "optional #GCancellable object, %NULL to ignore.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "callback", argType = TInterface (Name {namespace = "Gio", name = "AsyncReadyCallback"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GAsyncReadyCallback to call when the request is satisfied", sinceVersion = Nothing}, argScope = ScopeTypeAsync, argClosure = 4, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "user_data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "the data to pass to callback function", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_async_initable_init_async" g_async_initable_init_async :: 
    Ptr AsyncInitable ->                    -- initable : TInterface (Name {namespace = "Gio", name = "AsyncInitable"})
    Int32 ->                                -- io_priority : TBasicType TInt
    Ptr Gio.Cancellable.Cancellable ->      -- cancellable : TInterface (Name {namespace = "Gio", name = "Cancellable"})
    FunPtr Gio.Callbacks.C_AsyncReadyCallback -> -- callback : TInterface (Name {namespace = "Gio", name = "AsyncReadyCallback"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    IO ()

{- |
Starts asynchronous initialization of the object implementing the
interface. This must be done before any real use of the object after
initial construction. If the object also implements 'GI.Gio.Interfaces.Initable.Initable' you can
optionally call 'GI.Gio.Interfaces.Initable.initableInit' instead.

When the initialization is finished, /@callback@/ will be called. You can
then call 'GI.Gio.Interfaces.AsyncInitable.asyncInitableInitFinish' to get the result of the
initialization.

Implementations may also support cancellation. If /@cancellable@/ is not
'Nothing', then initialization can be cancelled by triggering the cancellable
object from another thread. If the operation was cancelled, the error
'GI.Gio.Enums.IOErrorEnumCancelled' will be returned. If /@cancellable@/ is not 'Nothing', and
the object doesn\'t support cancellable initialization, the error
'GI.Gio.Enums.IOErrorEnumNotSupported' will be returned.

As with 'GI.Gio.Interfaces.Initable.Initable', if the object is not initialized, or initialization
returns with an error, then all operations on the object except
'GI.GObject.Objects.Object.objectRef' and 'GI.GObject.Objects.Object.objectUnref' are considered to be invalid, and
have undefined behaviour. They will often fail with @/g_critical()/@ or
@/g_warning()/@, but this must not be relied on.

Implementations of this method must be idempotent: i.e. multiple calls
to this function with the same argument should return the same results.
Only the first call initializes the object; further calls return the result
of the first call. This is so that it\'s safe to implement the singleton
pattern in the GObject constructor function.

For classes that also support the 'GI.Gio.Interfaces.Initable.Initable' interface, the default
implementation of this method will run the 'GI.Gio.Interfaces.Initable.initableInit' function
in a thread, so if you want to support asynchronous initialization via
threads, just implement the 'GI.Gio.Interfaces.AsyncInitable.AsyncInitable' interface without overriding
any interface methods.

@since 2.22
-}
asyncInitableInitAsync ::
    (B.CallStack.HasCallStack, MonadIO m, IsAsyncInitable a, Gio.Cancellable.IsCancellable b) =>
    a
    {- ^ /@initable@/: a 'GI.Gio.Interfaces.AsyncInitable.AsyncInitable'. -}
    -> Int32
    {- ^ /@ioPriority@/: the [I\/O priority][io-priority] of the operation -}
    -> Maybe (b)
    {- ^ /@cancellable@/: optional 'GI.Gio.Objects.Cancellable.Cancellable' object, 'Nothing' to ignore. -}
    -> Maybe (Gio.Callbacks.AsyncReadyCallback)
    {- ^ /@callback@/: a 'GI.Gio.Callbacks.AsyncReadyCallback' to call when the request is satisfied -}
    -> m ()
asyncInitableInitAsync initable ioPriority cancellable callback = liftIO $ do
    initable' <- unsafeManagedPtrCastPtr initable
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            jCancellable' <- unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    ptrcallback <- callocMem :: IO (Ptr (FunPtr Gio.Callbacks.C_AsyncReadyCallback))
    maybeCallback <- case callback of
        Nothing -> return (castPtrToFunPtr nullPtr)
        Just jCallback -> do
            jCallback' <- Gio.Callbacks.mk_AsyncReadyCallback (Gio.Callbacks.wrap_AsyncReadyCallback (Just ptrcallback) (Gio.Callbacks.drop_closures_AsyncReadyCallback jCallback))
            poke ptrcallback jCallback'
            return jCallback'
    let userData = nullPtr
    g_async_initable_init_async initable' ioPriority maybeCancellable maybeCallback userData
    touchManagedPtr initable
    whenJust cancellable touchManagedPtr
    return ()

data AsyncInitableInitAsyncMethodInfo
instance (signature ~ (Int32 -> Maybe (b) -> Maybe (Gio.Callbacks.AsyncReadyCallback) -> m ()), MonadIO m, IsAsyncInitable a, Gio.Cancellable.IsCancellable b) => O.MethodInfo AsyncInitableInitAsyncMethodInfo a signature where
    overloadedMethod _ = asyncInitableInitAsync

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

foreign import ccall "g_async_initable_init_finish" g_async_initable_init_finish :: 
    Ptr AsyncInitable ->                    -- initable : TInterface (Name {namespace = "Gio", name = "AsyncInitable"})
    Ptr Gio.AsyncResult.AsyncResult ->      -- res : TInterface (Name {namespace = "Gio", name = "AsyncResult"})
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Finishes asynchronous initialization and returns the result.
See 'GI.Gio.Interfaces.AsyncInitable.asyncInitableInitAsync'.

@since 2.22
-}
asyncInitableInitFinish ::
    (B.CallStack.HasCallStack, MonadIO m, IsAsyncInitable a, Gio.AsyncResult.IsAsyncResult b) =>
    a
    {- ^ /@initable@/: a 'GI.Gio.Interfaces.AsyncInitable.AsyncInitable'. -}
    -> b
    {- ^ /@res@/: a 'GI.Gio.Interfaces.AsyncResult.AsyncResult'. -}
    -> m ()
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
asyncInitableInitFinish initable res = liftIO $ do
    initable' <- unsafeManagedPtrCastPtr initable
    res' <- unsafeManagedPtrCastPtr res
    onException (do
        _ <- propagateGError $ g_async_initable_init_finish initable' res'
        touchManagedPtr initable
        touchManagedPtr res
        return ()
     ) (do
        return ()
     )

data AsyncInitableInitFinishMethodInfo
instance (signature ~ (b -> m ()), MonadIO m, IsAsyncInitable a, Gio.AsyncResult.IsAsyncResult b) => O.MethodInfo AsyncInitableInitFinishMethodInfo a signature where
    overloadedMethod _ = asyncInitableInitFinish

-- method AsyncInitable::new_finish
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "initable", argType = TInterface (Name {namespace = "Gio", name = "AsyncInitable"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #GAsyncInitable from the callback", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "res", argType = TInterface (Name {namespace = "Gio", name = "AsyncResult"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #GAsyncResult from the callback", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GObject", name = "Object"}))
-- throws : True
-- Skip return : False

foreign import ccall "g_async_initable_new_finish" g_async_initable_new_finish :: 
    Ptr AsyncInitable ->                    -- initable : TInterface (Name {namespace = "Gio", name = "AsyncInitable"})
    Ptr Gio.AsyncResult.AsyncResult ->      -- res : TInterface (Name {namespace = "Gio", name = "AsyncResult"})
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr GObject.Object.Object)

{- |
Finishes the async construction for the various g_async_initable_new
calls, returning the created object or 'Nothing' on error.

@since 2.22
-}
asyncInitableNewFinish ::
    (B.CallStack.HasCallStack, MonadIO m, IsAsyncInitable a, Gio.AsyncResult.IsAsyncResult b) =>
    a
    {- ^ /@initable@/: the 'GI.Gio.Interfaces.AsyncInitable.AsyncInitable' from the callback -}
    -> b
    {- ^ /@res@/: the 'GI.Gio.Interfaces.AsyncResult.AsyncResult' from the callback -}
    -> m GObject.Object.Object
    {- ^ __Returns:__ a newly created 'GI.GObject.Objects.Object.Object',
     or 'Nothing' on error. Free with 'GI.GObject.Objects.Object.objectUnref'. /(Can throw 'Data.GI.Base.GError.GError')/ -}
asyncInitableNewFinish initable res = liftIO $ do
    initable' <- unsafeManagedPtrCastPtr initable
    res' <- unsafeManagedPtrCastPtr res
    onException (do
        result <- propagateGError $ g_async_initable_new_finish initable' res'
        checkUnexpectedReturnNULL "asyncInitableNewFinish" result
        result' <- (wrapObject GObject.Object.Object) result
        touchManagedPtr initable
        touchManagedPtr res
        return result'
     ) (do
        return ()
     )

data AsyncInitableNewFinishMethodInfo
instance (signature ~ (b -> m GObject.Object.Object), MonadIO m, IsAsyncInitable a, Gio.AsyncResult.IsAsyncResult b) => O.MethodInfo AsyncInitableNewFinishMethodInfo a signature where
    overloadedMethod _ = asyncInitableNewFinish

-- method AsyncInitable::newv_async
-- method type : MemberFunction
-- Args : [Arg {argCName = "object_type", argType = TBasicType TGType, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GType supporting #GAsyncInitable.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "n_parameters", argType = TBasicType TUInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the number of parameters in @parameters", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "parameters", argType = TInterface (Name {namespace = "GObject", name = "Parameter"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the parameters to use to construct the object", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "io_priority", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the [I/O priority][io-priority] of the operation", 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 "optional #GCancellable object, %NULL to ignore.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "callback", argType = TInterface (Name {namespace = "Gio", name = "AsyncReadyCallback"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GAsyncReadyCallback to call when the initialization is\n    finished", sinceVersion = Nothing}, argScope = ScopeTypeAsync, argClosure = 6, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "user_data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "the data to pass to callback function", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_async_initable_newv_async" g_async_initable_newv_async :: 
    CGType ->                               -- object_type : TBasicType TGType
    Word32 ->                               -- n_parameters : TBasicType TUInt
    Ptr GObject.Parameter.Parameter ->      -- parameters : TInterface (Name {namespace = "GObject", name = "Parameter"})
    Int32 ->                                -- io_priority : TBasicType TInt
    Ptr Gio.Cancellable.Cancellable ->      -- cancellable : TInterface (Name {namespace = "Gio", name = "Cancellable"})
    FunPtr Gio.Callbacks.C_AsyncReadyCallback -> -- callback : TInterface (Name {namespace = "Gio", name = "AsyncReadyCallback"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    IO ()

{- |
Helper function for constructing 'GI.Gio.Interfaces.AsyncInitable.AsyncInitable' object. This is
similar to 'GI.GObject.Objects.Object.objectNew' but also initializes the object asynchronously.

When the initialization is finished, /@callback@/ will be called. You can
then call 'GI.Gio.Interfaces.AsyncInitable.asyncInitableNewFinish' to get the new object and check
for any errors.

@since 2.22
-}
asyncInitableNewvAsync ::
    (B.CallStack.HasCallStack, MonadIO m, Gio.Cancellable.IsCancellable a) =>
    GType
    {- ^ /@objectType@/: a 'GType' supporting 'GI.Gio.Interfaces.AsyncInitable.AsyncInitable'. -}
    -> Word32
    {- ^ /@nParameters@/: the number of parameters in /@parameters@/ -}
    -> GObject.Parameter.Parameter
    {- ^ /@parameters@/: the parameters to use to construct the object -}
    -> Int32
    {- ^ /@ioPriority@/: the [I\/O priority][io-priority] of the operation -}
    -> Maybe (a)
    {- ^ /@cancellable@/: optional 'GI.Gio.Objects.Cancellable.Cancellable' object, 'Nothing' to ignore. -}
    -> Maybe (Gio.Callbacks.AsyncReadyCallback)
    {- ^ /@callback@/: a 'GI.Gio.Callbacks.AsyncReadyCallback' to call when the initialization is
    finished -}
    -> m ()
asyncInitableNewvAsync objectType nParameters parameters ioPriority cancellable callback = liftIO $ do
    let objectType' = gtypeToCGType objectType
    parameters' <- unsafeManagedPtrGetPtr parameters
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            jCancellable' <- unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    ptrcallback <- callocMem :: IO (Ptr (FunPtr Gio.Callbacks.C_AsyncReadyCallback))
    maybeCallback <- case callback of
        Nothing -> return (castPtrToFunPtr nullPtr)
        Just jCallback -> do
            jCallback' <- Gio.Callbacks.mk_AsyncReadyCallback (Gio.Callbacks.wrap_AsyncReadyCallback (Just ptrcallback) (Gio.Callbacks.drop_closures_AsyncReadyCallback jCallback))
            poke ptrcallback jCallback'
            return jCallback'
    let userData = nullPtr
    g_async_initable_newv_async objectType' nParameters parameters' ioPriority maybeCancellable maybeCallback userData
    touchManagedPtr parameters
    whenJust cancellable touchManagedPtr
    return ()