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

An opaque structure used as the base of all interface types.
-}

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

module GI.GObject.Structs.TypeInterface
    (

-- * Exported types
    TypeInterface(..)                       ,
    newZeroTypeInterface                    ,
    noTypeInterface                         ,


 -- * Methods
-- ** addPrerequisite #method:addPrerequisite#

    typeInterfaceAddPrerequisite            ,


-- ** getPlugin #method:getPlugin#

    typeInterfaceGetPlugin                  ,


-- ** peek #method:peek#

    typeInterfacePeek                       ,


-- ** peekParent #method:peekParent#

#if ENABLE_OVERLOADING
    TypeInterfacePeekParentMethodInfo       ,
#endif
    typeInterfacePeekParent                 ,


-- ** prerequisites #method:prerequisites#

    typeInterfacePrerequisites              ,




    ) 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.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
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.Text as T
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 {-# SOURCE #-} qualified GI.GObject.Interfaces.TypePlugin as GObject.TypePlugin
import {-# SOURCE #-} qualified GI.GObject.Structs.TypeClass as GObject.TypeClass

-- | Memory-managed wrapper type.
newtype TypeInterface = TypeInterface (ManagedPtr TypeInterface)
instance WrappedPtr TypeInterface where
    wrappedPtrCalloc = callocBytes 16
    wrappedPtrCopy = \p -> withManagedPtr p (copyBytes 16 >=> wrapPtr TypeInterface)
    wrappedPtrFree = Just ptr_to_g_free

-- | Construct a `TypeInterface` struct initialized to zero.
newZeroTypeInterface :: MonadIO m => m TypeInterface
newZeroTypeInterface = liftIO $ wrappedPtrCalloc >>= wrapPtr TypeInterface

instance tag ~ 'AttrSet => Constructible TypeInterface tag where
    new _ attrs = do
        o <- newZeroTypeInterface
        GI.Attributes.set o attrs
        return o


-- | A convenience alias for `Nothing` :: `Maybe` `TypeInterface`.
noTypeInterface :: Maybe TypeInterface
noTypeInterface = Nothing


#if ENABLE_OVERLOADING
instance O.HasAttributeList TypeInterface
type instance O.AttributeList TypeInterface = TypeInterfaceAttributeList
type TypeInterfaceAttributeList = ('[ ] :: [(Symbol, *)])
#endif

-- method TypeInterface::peek_parent
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "g_iface", argType = TInterface (Name {namespace = "GObject", name = "TypeInterface"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GTypeInterface structure", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GObject", name = "TypeInterface"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_type_interface_peek_parent" g_type_interface_peek_parent ::
    Ptr TypeInterface ->                    -- g_iface : TInterface (Name {namespace = "GObject", name = "TypeInterface"})
    IO (Ptr TypeInterface)

{- |
Returns the corresponding 'GI.GObject.Structs.TypeInterface.TypeInterface' structure of the parent type
of the instance type to which /@gIface@/ belongs. This is useful when
deriving the implementation of an interface from the parent type and
then possibly overriding some methods.
-}
typeInterfacePeekParent ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    TypeInterface
    {- ^ /@gIface@/: a 'GI.GObject.Structs.TypeInterface.TypeInterface' structure -}
    -> m TypeInterface
    {- ^ __Returns:__ the
    corresponding 'GI.GObject.Structs.TypeInterface.TypeInterface' structure of the parent type of the
    instance type to which /@gIface@/ belongs, or 'Nothing' if the parent
    type doesn\'t conform to the interface -}
typeInterfacePeekParent gIface = liftIO $ do
    gIface' <- unsafeManagedPtrGetPtr gIface
    result <- g_type_interface_peek_parent gIface'
    checkUnexpectedReturnNULL "typeInterfacePeekParent" result
    result' <- (newPtr TypeInterface) result
    touchManagedPtr gIface
    return result'

#if ENABLE_OVERLOADING
data TypeInterfacePeekParentMethodInfo
instance (signature ~ (m TypeInterface), MonadIO m) => O.MethodInfo TypeInterfacePeekParentMethodInfo TypeInterface signature where
    overloadedMethod _ = typeInterfacePeekParent

#endif

-- method TypeInterface::add_prerequisite
-- method type : MemberFunction
-- Args : [Arg {argCName = "interface_type", argType = TBasicType TGType, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "#GType value of an interface type", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "prerequisite_type", argType = TBasicType TGType, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "#GType value of an interface or instantiatable type", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_type_interface_add_prerequisite" g_type_interface_add_prerequisite ::
    CGType ->                               -- interface_type : TBasicType TGType
    CGType ->                               -- prerequisite_type : TBasicType TGType
    IO ()

{- |
Adds /@prerequisiteType@/ to the list of prerequisites of /@interfaceType@/.
This means that any type implementing /@interfaceType@/ must also implement
/@prerequisiteType@/. Prerequisites can be thought of as an alternative to
interface derivation (which GType doesn\'t support). An interface can have
at most one instantiatable prerequisite type.
-}
typeInterfaceAddPrerequisite ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GType
    {- ^ /@interfaceType@/: 'GType' value of an interface type -}
    -> GType
    {- ^ /@prerequisiteType@/: 'GType' value of an interface or instantiatable type -}
    -> m ()
typeInterfaceAddPrerequisite interfaceType prerequisiteType = liftIO $ do
    let interfaceType' = gtypeToCGType interfaceType
    let prerequisiteType' = gtypeToCGType prerequisiteType
    g_type_interface_add_prerequisite interfaceType' prerequisiteType'
    return ()

#if ENABLE_OVERLOADING
#endif

-- method TypeInterface::get_plugin
-- method type : MemberFunction
-- Args : [Arg {argCName = "instance_type", argType = TBasicType TGType, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "#GType of an instantiatable type", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "interface_type", argType = TBasicType TGType, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "#GType of an interface type", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GObject", name = "TypePlugin"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_type_interface_get_plugin" g_type_interface_get_plugin ::
    CGType ->                               -- instance_type : TBasicType TGType
    CGType ->                               -- interface_type : TBasicType TGType
    IO (Ptr GObject.TypePlugin.TypePlugin)

{- |
Returns the 'GI.GObject.Interfaces.TypePlugin.TypePlugin' structure for the dynamic interface
/@interfaceType@/ which has been added to /@instanceType@/, or 'Nothing'
if /@interfaceType@/ has not been added to /@instanceType@/ or does
not have a 'GI.GObject.Interfaces.TypePlugin.TypePlugin' structure. See 'GI.GObject.Functions.typeAddInterfaceDynamic'.
-}
typeInterfaceGetPlugin ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GType
    {- ^ /@instanceType@/: 'GType' of an instantiatable type -}
    -> GType
    {- ^ /@interfaceType@/: 'GType' of an interface type -}
    -> m GObject.TypePlugin.TypePlugin
    {- ^ __Returns:__ the 'GI.GObject.Interfaces.TypePlugin.TypePlugin' for the dynamic
    interface /@interfaceType@/ of /@instanceType@/ -}
typeInterfaceGetPlugin instanceType interfaceType = liftIO $ do
    let instanceType' = gtypeToCGType instanceType
    let interfaceType' = gtypeToCGType interfaceType
    result <- g_type_interface_get_plugin instanceType' interfaceType'
    checkUnexpectedReturnNULL "typeInterfaceGetPlugin" result
    result' <- (newPtr GObject.TypePlugin.TypePlugin) result
    return result'

#if ENABLE_OVERLOADING
#endif

-- method TypeInterface::peek
-- method type : MemberFunction
-- Args : [Arg {argCName = "instance_class", argType = TInterface (Name {namespace = "GObject", name = "TypeClass"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GTypeClass structure", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "iface_type", argType = TBasicType TGType, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an interface ID which this class conforms to", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GObject", name = "TypeInterface"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_type_interface_peek" g_type_interface_peek ::
    Ptr GObject.TypeClass.TypeClass ->      -- instance_class : TInterface (Name {namespace = "GObject", name = "TypeClass"})
    CGType ->                               -- iface_type : TBasicType TGType
    IO (Ptr TypeInterface)

{- |
Returns the 'GI.GObject.Structs.TypeInterface.TypeInterface' structure of an interface to which the
passed in class conforms.
-}
typeInterfacePeek ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GObject.TypeClass.TypeClass
    {- ^ /@instanceClass@/: a 'GI.GObject.Structs.TypeClass.TypeClass' structure -}
    -> GType
    {- ^ /@ifaceType@/: an interface ID which this class conforms to -}
    -> m TypeInterface
    {- ^ __Returns:__ the 'GI.GObject.Structs.TypeInterface.TypeInterface'
    structure of /@ifaceType@/ if implemented by /@instanceClass@/, 'Nothing'
    otherwise -}
typeInterfacePeek instanceClass ifaceType = liftIO $ do
    instanceClass' <- unsafeManagedPtrGetPtr instanceClass
    let ifaceType' = gtypeToCGType ifaceType
    result <- g_type_interface_peek instanceClass' ifaceType'
    checkUnexpectedReturnNULL "typeInterfacePeek" result
    result' <- (newPtr TypeInterface) result
    touchManagedPtr instanceClass
    return result'

#if ENABLE_OVERLOADING
#endif

-- method TypeInterface::prerequisites
-- method type : MemberFunction
-- Args : [Arg {argCName = "interface_type", argType = TBasicType TGType, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an interface type", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "n_prerequisites", argType = TBasicType TUInt, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to return the number\n    of prerequisites, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : [Arg {argCName = "n_prerequisites", argType = TBasicType TUInt, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "location to return the number\n    of prerequisites, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- returnType : Just (TCArray False (-1) 1 (TBasicType TGType))
-- throws : False
-- Skip return : False

foreign import ccall "g_type_interface_prerequisites" g_type_interface_prerequisites ::
    CGType ->                               -- interface_type : TBasicType TGType
    Ptr Word32 ->                           -- n_prerequisites : TBasicType TUInt
    IO (Ptr CGType)

{- |
Returns the prerequisites of an interfaces type.

/Since: 2.2/
-}
typeInterfacePrerequisites ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GType
    {- ^ /@interfaceType@/: an interface type -}
    -> m [GType]
    {- ^ __Returns:__ a
    newly-allocated zero-terminated array of 'GType' containing
    the prerequisites of /@interfaceType@/ -}
typeInterfacePrerequisites interfaceType = liftIO $ do
    let interfaceType' = gtypeToCGType interfaceType
    nPrerequisites <- allocMem :: IO (Ptr Word32)
    result <- g_type_interface_prerequisites interfaceType' nPrerequisites
    nPrerequisites' <- peek nPrerequisites
    checkUnexpectedReturnNULL "typeInterfacePrerequisites" result
    result' <- (unpackMapStorableArrayWithLength GType nPrerequisites') result
    freeMem result
    freeMem nPrerequisites
    return result'

#if ENABLE_OVERLOADING
#endif

#if ENABLE_OVERLOADING
type family ResolveTypeInterfaceMethod (t :: Symbol) (o :: *) :: * where
    ResolveTypeInterfaceMethod "peekParent" o = TypeInterfacePeekParentMethodInfo
    ResolveTypeInterfaceMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveTypeInterfaceMethod t TypeInterface, O.MethodInfo info TypeInterface p) => OL.IsLabel t (TypeInterface -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#else
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

#endif