{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson and Iñaki García Etxebarria
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- A @GtkExpression@ that tries to evaluate each of its expressions until it succeeds.
-- 
-- If all expressions fail to evaluate, the @GtkTryExpression@\'s evaluation fails as well.
-- 
-- /Since: 4.22/

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

module GI.Gtk.Objects.TryExpression
    ( 

-- * Exported types
    TryExpression(..)                       ,
    IsTryExpression                         ,
    toTryExpression                         ,


 -- * Methods
-- | 
-- 
--  === __Click to display all available methods, including inherited ones__
-- ==== Methods
-- [bind]("GI.Gtk.Objects.Expression#g:method:bind"), [evaluate]("GI.Gtk.Objects.Expression#g:method:evaluate"), [isStatic]("GI.Gtk.Objects.Expression#g:method:isStatic"), [ref]("GI.Gtk.Objects.Expression#g:method:ref"), [unref]("GI.Gtk.Objects.Expression#g:method:unref"), [watch]("GI.Gtk.Objects.Expression#g:method:watch").
-- 
-- ==== Getters
-- [getValueType]("GI.Gtk.Objects.Expression#g:method:getValueType").
-- 
-- ==== Setters
-- /None/.

#if defined(ENABLE_OVERLOADING)
    ResolveTryExpressionMethod              ,
#endif

-- ** new #method:new#

    tryExpressionNew                        ,




    ) 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 Data.Word as DW
import qualified Data.Int as DI
import qualified System.Posix.Types as SPT
import qualified Foreign.C.Types as FCT

-- Workaround for https://gitlab.haskell.org/ghc/ghc/-/issues/23392
#if MIN_VERSION_base(4,18,0)
import qualified GI.GLib.Callbacks as GLib.Callbacks
import qualified GI.GObject.Objects.Object as GObject.Object
import qualified GI.Gtk.Callbacks as Gtk.Callbacks
import {-# SOURCE #-} qualified GI.Gtk.Objects.Expression as Gtk.Expression
import {-# SOURCE #-} qualified GI.Gtk.Structs.ExpressionWatch as Gtk.ExpressionWatch

#else
import {-# SOURCE #-} qualified GI.Gtk.Objects.Expression as Gtk.Expression

#endif

-- | Memory-managed wrapper type.
newtype TryExpression = TryExpression (SP.ManagedPtr TryExpression)
    deriving (Eq)

instance SP.ManagedPtrNewtype TryExpression where
    toManagedPtr (TryExpression p) = p

foreign import ccall "gtk_try_expression_get_type"
    c_gtk_try_expression_get_type :: IO B.Types.GType

instance B.Types.TypedObject TryExpression where
    glibType = c_gtk_try_expression_get_type

-- | Type class for types which can be safely cast to t'TryExpression', for instance with `toTryExpression`.
class (SP.BoxedPtr o, SP.TypedObject o, O.IsDescendantOf TryExpression o) => IsTryExpression o
instance (SP.BoxedPtr o, SP.TypedObject o, O.IsDescendantOf TryExpression o) => IsTryExpression o

instance O.HasParentTypes TryExpression
type instance O.ParentTypes TryExpression = '[Gtk.Expression.Expression]

-- | Cast to t'TryExpression', for types for which this is known to be safe. For general casts, use 'Data.GI.Base.ManagedPtr.castTo'.
toTryExpression :: (MIO.MonadIO m, IsTryExpression o) => o -> m TryExpression
toTryExpression = MIO.liftIO . B.ManagedPtr.unsafeCastTo TryExpression

--- XXX Missing getter and/or setter, so no GValue instance could be generated.
#if defined(ENABLE_OVERLOADING)
type family ResolveTryExpressionMethod (t :: Symbol) (o :: DK.Type) :: DK.Type where
    ResolveTryExpressionMethod "bind" o = Gtk.Expression.ExpressionBindMethodInfo
    ResolveTryExpressionMethod "evaluate" o = Gtk.Expression.ExpressionEvaluateMethodInfo
    ResolveTryExpressionMethod "isStatic" o = Gtk.Expression.ExpressionIsStaticMethodInfo
    ResolveTryExpressionMethod "ref" o = Gtk.Expression.ExpressionRefMethodInfo
    ResolveTryExpressionMethod "unref" o = Gtk.Expression.ExpressionUnrefMethodInfo
    ResolveTryExpressionMethod "watch" o = Gtk.Expression.ExpressionWatchMethodInfo
    ResolveTryExpressionMethod "getValueType" o = Gtk.Expression.ExpressionGetValueTypeMethodInfo
    ResolveTryExpressionMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveTryExpressionMethod t TryExpression, O.OverloadedMethod info TryExpression p) => OL.IsLabel t (TryExpression -> 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 ~ ResolveTryExpressionMethod t TryExpression, O.OverloadedMethod info TryExpression p, R.HasField t TryExpression p) => R.HasField t TryExpression p where
    getField = O.overloadedMethod @info

#endif

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

#endif

-- XXX Wrapping a foreign struct/union with no known destructor or size, leak?
instance BoxedPtr TryExpression where
    boxedPtrCopy = return
    boxedPtrFree = \_x -> return ()


-- method TryExpression::new
-- method type : Constructor
-- Args: [ Arg
--           { argCName = "n_expressions"
--           , argType = TBasicType TUInt
--           , argCType = Just "guint"
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "The number of expressions"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "expressions"
--           , argType =
--               TCArray
--                 False
--                 (-1)
--                 0
--                 (TInterface Name { namespace = "Gtk" , name = "Expression" })
--           , argCType = Just "GtkExpression**"
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "The array of expressions"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferEverything
--           }
--       ]
-- Lengths: [ Arg
--              { argCName = "n_expressions"
--              , argType = TBasicType TUInt
--              , argCType = Just "guint"
--              , direction = DirectionIn
--              , mayBeNull = False
--              , argDoc =
--                  Documentation
--                    { rawDocText = Just "The number of expressions"
--                    , sinceVersion = Nothing
--                    }
--              , argScope = ScopeTypeInvalid
--              , argClosure = -1
--              , argDestroy = -1
--              , argCallerAllocates = False
--              , argCallbackUserData = False
--              , transfer = TransferNothing
--              }
--          ]
-- returnType: Just
--               (TInterface Name { namespace = "Gtk" , name = "TryExpression" })
-- throws : False
-- Skip return : False

foreign import ccall "gtk_try_expression_new" gtk_try_expression_new :: 
    Word32 ->                               -- n_expressions : TBasicType TUInt
    Ptr (Ptr Gtk.Expression.Expression) ->  -- expressions : TCArray False (-1) 0 (TInterface (Name {namespace = "Gtk", name = "Expression"}))
    IO (Ptr TryExpression)

-- | Creates a @GtkExpression@ with an array of expressions.
-- 
-- When evaluated, the @GtkTryExpression@ tries to evaluate each of its expressions until it succeeds.
-- If all expressions fail to evaluate, the @GtkTryExpression@\'s evaluation fails as well.
-- 
-- The value type of the expressions in the array must match.
-- 
-- /Since: 4.22/
tryExpressionNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    [Gtk.Expression.Expression]
    -- ^ /@expressions@/: The array of expressions
    -> m TryExpression
    -- ^ __Returns:__ a new @GtkExpression@
tryExpressionNew expressions = liftIO $ do
    let nExpressions = fromIntegral $ P.length expressions
    expressions' <- mapM B.ManagedPtr.disownManagedPtr expressions
    expressions'' <- packPtrArray expressions'
    result <- gtk_try_expression_new nExpressions expressions''
    checkUnexpectedReturnNULL "tryExpressionNew" result
    result' <- (wrapPtr TryExpression) result
    mapM_ touchManagedPtr expressions
    return result'

#if defined(ENABLE_OVERLOADING)
#endif


