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

Datastructure to initialize 'GI.Gst.Structs.Caps.Caps' from a string description usually
used in conjunction with @/GST_STATIC_CAPS()/@ and 'GI.Gst.Structs.StaticCaps.staticCapsGet' to
instantiate a 'GI.Gst.Structs.Caps.Caps'.
-}

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

module GI.Gst.Structs.StaticCaps
    (

-- * Exported types
    StaticCaps(..)                          ,
    newZeroStaticCaps                       ,
    noStaticCaps                            ,


 -- * Methods
-- ** cleanup #method:cleanup#

#if ENABLE_OVERLOADING
    StaticCapsCleanupMethodInfo             ,
#endif
    staticCapsCleanup                       ,


-- ** get #method:get#

#if ENABLE_OVERLOADING
    StaticCapsGetMethodInfo                 ,
#endif
    staticCapsGet                           ,




 -- * Properties
-- ** caps #attr:caps#
{- | the cached 'GI.Gst.Structs.Caps.Caps'
-}
    clearStaticCapsCaps                     ,
    getStaticCapsCaps                       ,
    setStaticCapsCaps                       ,
#if ENABLE_OVERLOADING
    staticCaps_caps                         ,
#endif


-- ** string #attr:string#
{- | a string describing a caps
-}
    clearStaticCapsString                   ,
    getStaticCapsString                     ,
    setStaticCapsString                     ,
#if ENABLE_OVERLOADING
    staticCaps_string                       ,
#endif




    ) 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.Gst.Structs.Caps as Gst.Caps

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

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

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


-- | A convenience alias for `Nothing` :: `Maybe` `StaticCaps`.
noStaticCaps :: Maybe StaticCaps
noStaticCaps = Nothing

{- |
Get the value of the “@caps@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' staticCaps #caps
@
-}
getStaticCapsCaps :: MonadIO m => StaticCaps -> m (Maybe Gst.Caps.Caps)
getStaticCapsCaps s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO (Ptr Gst.Caps.Caps)
    result <- SP.convertIfNonNull val $ \val' -> do
        val'' <- (newBoxed Gst.Caps.Caps) val'
        return val''
    return result

{- |
Set the value of the “@caps@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' staticCaps [ #caps 'Data.GI.Base.Attributes.:=' value ]
@
-}
setStaticCapsCaps :: MonadIO m => StaticCaps -> Ptr Gst.Caps.Caps -> m ()
setStaticCapsCaps s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (val :: Ptr Gst.Caps.Caps)

{- |
Set the value of the “@caps@” field to `Nothing`.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.clear' #caps
@
-}
clearStaticCapsCaps :: MonadIO m => StaticCaps -> m ()
clearStaticCapsCaps s = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (FP.nullPtr :: Ptr Gst.Caps.Caps)

#if ENABLE_OVERLOADING
data StaticCapsCapsFieldInfo
instance AttrInfo StaticCapsCapsFieldInfo where
    type AttrAllowedOps StaticCapsCapsFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint StaticCapsCapsFieldInfo = (~) (Ptr Gst.Caps.Caps)
    type AttrBaseTypeConstraint StaticCapsCapsFieldInfo = (~) StaticCaps
    type AttrGetType StaticCapsCapsFieldInfo = Maybe Gst.Caps.Caps
    type AttrLabel StaticCapsCapsFieldInfo = "caps"
    type AttrOrigin StaticCapsCapsFieldInfo = StaticCaps
    attrGet _ = getStaticCapsCaps
    attrSet _ = setStaticCapsCaps
    attrConstruct = undefined
    attrClear _ = clearStaticCapsCaps

staticCaps_caps :: AttrLabelProxy "caps"
staticCaps_caps = AttrLabelProxy

#endif


{- |
Get the value of the “@string@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' staticCaps #string
@
-}
getStaticCapsString :: MonadIO m => StaticCaps -> m (Maybe T.Text)
getStaticCapsString s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 8) :: IO CString
    result <- SP.convertIfNonNull val $ \val' -> do
        val'' <- cstringToText val'
        return val''
    return result

{- |
Set the value of the “@string@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' staticCaps [ #string 'Data.GI.Base.Attributes.:=' value ]
@
-}
setStaticCapsString :: MonadIO m => StaticCaps -> CString -> m ()
setStaticCapsString s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 8) (val :: CString)

{- |
Set the value of the “@string@” field to `Nothing`.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.clear' #string
@
-}
clearStaticCapsString :: MonadIO m => StaticCaps -> m ()
clearStaticCapsString s = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 8) (FP.nullPtr :: CString)

#if ENABLE_OVERLOADING
data StaticCapsStringFieldInfo
instance AttrInfo StaticCapsStringFieldInfo where
    type AttrAllowedOps StaticCapsStringFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint StaticCapsStringFieldInfo = (~) CString
    type AttrBaseTypeConstraint StaticCapsStringFieldInfo = (~) StaticCaps
    type AttrGetType StaticCapsStringFieldInfo = Maybe T.Text
    type AttrLabel StaticCapsStringFieldInfo = "string"
    type AttrOrigin StaticCapsStringFieldInfo = StaticCaps
    attrGet _ = getStaticCapsString
    attrSet _ = setStaticCapsString
    attrConstruct = undefined
    attrClear _ = clearStaticCapsString

staticCaps_string :: AttrLabelProxy "string"
staticCaps_string = AttrLabelProxy

#endif



#if ENABLE_OVERLOADING
instance O.HasAttributeList StaticCaps
type instance O.AttributeList StaticCaps = StaticCapsAttributeList
type StaticCapsAttributeList = ('[ '("caps", StaticCapsCapsFieldInfo), '("string", StaticCapsStringFieldInfo)] :: [(Symbol, *)])
#endif

-- method StaticCaps::cleanup
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "static_caps", argType = TInterface (Name {namespace = "Gst", name = "StaticCaps"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #GstStaticCaps to clean", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "gst_static_caps_cleanup" gst_static_caps_cleanup ::
    Ptr StaticCaps ->                       -- static_caps : TInterface (Name {namespace = "Gst", name = "StaticCaps"})
    IO ()

{- |
Clean up the cached caps contained in /@staticCaps@/.
-}
staticCapsCleanup ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    StaticCaps
    {- ^ /@staticCaps@/: the 'GI.Gst.Structs.StaticCaps.StaticCaps' to clean -}
    -> m ()
staticCapsCleanup staticCaps = liftIO $ do
    staticCaps' <- unsafeManagedPtrGetPtr staticCaps
    gst_static_caps_cleanup staticCaps'
    touchManagedPtr staticCaps
    return ()

#if ENABLE_OVERLOADING
data StaticCapsCleanupMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo StaticCapsCleanupMethodInfo StaticCaps signature where
    overloadedMethod _ = staticCapsCleanup

#endif

-- method StaticCaps::get
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "static_caps", argType = TInterface (Name {namespace = "Gst", name = "StaticCaps"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #GstStaticCaps to convert", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gst", name = "Caps"}))
-- throws : False
-- Skip return : False

foreign import ccall "gst_static_caps_get" gst_static_caps_get ::
    Ptr StaticCaps ->                       -- static_caps : TInterface (Name {namespace = "Gst", name = "StaticCaps"})
    IO (Ptr Gst.Caps.Caps)

{- |
Converts a 'GI.Gst.Structs.StaticCaps.StaticCaps' to a 'GI.Gst.Structs.Caps.Caps'.
-}
staticCapsGet ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    StaticCaps
    {- ^ /@staticCaps@/: the 'GI.Gst.Structs.StaticCaps.StaticCaps' to convert -}
    -> m (Maybe Gst.Caps.Caps)
    {- ^ __Returns:__ a pointer to the 'GI.Gst.Structs.Caps.Caps'. Unref
    after usage. Since the core holds an additional ref to the
    returned caps, use @/gst_caps_make_writable()/@ on the returned caps
    to modify it. -}
staticCapsGet staticCaps = liftIO $ do
    staticCaps' <- unsafeManagedPtrGetPtr staticCaps
    result <- gst_static_caps_get staticCaps'
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gst.Caps.Caps) result'
        return result''
    touchManagedPtr staticCaps
    return maybeResult

#if ENABLE_OVERLOADING
data StaticCapsGetMethodInfo
instance (signature ~ (m (Maybe Gst.Caps.Caps)), MonadIO m) => O.MethodInfo StaticCapsGetMethodInfo StaticCaps signature where
    overloadedMethod _ = staticCapsGet

#endif

#if ENABLE_OVERLOADING
type family ResolveStaticCapsMethod (t :: Symbol) (o :: *) :: * where
    ResolveStaticCapsMethod "cleanup" o = StaticCapsCleanupMethodInfo
    ResolveStaticCapsMethod "get" o = StaticCapsGetMethodInfo
    ResolveStaticCapsMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveStaticCapsMethod t StaticCaps, O.MethodInfo info StaticCaps p) => OL.IsLabel t (StaticCaps -> 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