{-# 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 !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 (TryExpression -> TryExpression -> Bool
(TryExpression -> TryExpression -> Bool)
-> (TryExpression -> TryExpression -> Bool) -> Eq TryExpression
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TryExpression -> TryExpression -> Bool
== :: TryExpression -> TryExpression -> Bool
$c/= :: TryExpression -> TryExpression -> Bool
/= :: TryExpression -> TryExpression -> Bool
Eq)

instance SP.ManagedPtrNewtype TryExpression where
    toManagedPtr :: TryExpression -> ManagedPtr TryExpression
toManagedPtr (TryExpression ManagedPtr TryExpression
p) = ManagedPtr TryExpression
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 :: IO GType
glibType = IO GType
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 :: forall (m :: * -> *) o.
(MonadIO m, IsTryExpression o) =>
o -> m TryExpression
toTryExpression = IO TryExpression -> m TryExpression
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
MIO.liftIO (IO TryExpression -> m TryExpression)
-> (o -> IO TryExpression) -> o -> m TryExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ManagedPtr TryExpression -> TryExpression)
-> o -> IO TryExpression
forall o o'.
(HasCallStack, ManagedPtrNewtype o, TypedObject o,
 ManagedPtrNewtype o', TypedObject o') =>
(ManagedPtr o' -> o') -> o -> IO o'
B.ManagedPtr.unsafeCastTo ManagedPtr TryExpression -> TryExpression
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 :: TryExpression -> IO TryExpression
boxedPtrCopy = TryExpression -> IO TryExpression
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
    boxedPtrFree :: TryExpression -> IO ()
boxedPtrFree = \TryExpression
_x -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
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 :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
[Expression] -> m TryExpression
tryExpressionNew [Expression]
expressions = IO TryExpression -> m TryExpression
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO TryExpression -> m TryExpression)
-> IO TryExpression -> m TryExpression
forall a b. (a -> b) -> a -> b
$ do
    let nExpressions :: Word32
nExpressions = Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word32) -> Int -> Word32
forall a b. (a -> b) -> a -> b
$ [Expression] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
P.length [Expression]
expressions
    expressions' <- (Expression -> IO (Ptr Expression))
-> [Expression] -> IO [Ptr Expression]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Expression -> IO (Ptr Expression)
forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
B.ManagedPtr.disownManagedPtr [Expression]
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