{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- The GParameter struct is an auxiliary structure used
-- to hand parameter name\/value pairs to 'GI.GObject.Objects.Object.objectNewv'.

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

module GI.GObject.Structs.Parameter
    ( 

-- * Exported types
    Parameter(..)                           ,
    newZeroParameter                        ,
    noParameter                             ,


 -- * Methods
-- ** Overloaded methods #method:Overloaded methods#

#if defined(ENABLE_OVERLOADING)
    ResolveParameterMethod                  ,
#endif




 -- * Properties
-- ** name #attr:name#
-- | the parameter name

    clearParameterName                      ,
    getParameterName                        ,
#if defined(ENABLE_OVERLOADING)
    parameter_name                          ,
#endif
    setParameterName                        ,


-- ** value #attr:value#
-- | the parameter value

    getParameterValue                       ,
#if defined(ENABLE_OVERLOADING)
    parameter_value                         ,
#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.GI.Base.Signals as B.Signals
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


-- | Memory-managed wrapper type.
newtype Parameter = Parameter (ManagedPtr Parameter)
    deriving (Parameter -> Parameter -> Bool
(Parameter -> Parameter -> Bool)
-> (Parameter -> Parameter -> Bool) -> Eq Parameter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Parameter -> Parameter -> Bool
$c/= :: Parameter -> Parameter -> Bool
== :: Parameter -> Parameter -> Bool
$c== :: Parameter -> Parameter -> Bool
Eq)
instance WrappedPtr Parameter where
    wrappedPtrCalloc :: IO (Ptr Parameter)
wrappedPtrCalloc = Int -> IO (Ptr Parameter)
forall a. Int -> IO (Ptr a)
callocBytes 32
    wrappedPtrCopy :: Parameter -> IO Parameter
wrappedPtrCopy = \p :: Parameter
p -> Parameter -> (Ptr Parameter -> IO Parameter) -> IO Parameter
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr Parameter
p (Int -> Ptr Parameter -> IO (Ptr Parameter)
forall a. WrappedPtr a => Int -> Ptr a -> IO (Ptr a)
copyBytes 32 (Ptr Parameter -> IO (Ptr Parameter))
-> (Ptr Parameter -> IO Parameter) -> Ptr Parameter -> IO Parameter
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (ManagedPtr Parameter -> Parameter)
-> Ptr Parameter -> IO Parameter
forall a.
(HasCallStack, WrappedPtr a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapPtr ManagedPtr Parameter -> Parameter
Parameter)
    wrappedPtrFree :: Maybe (GDestroyNotify Parameter)
wrappedPtrFree = GDestroyNotify Parameter -> Maybe (GDestroyNotify Parameter)
forall a. a -> Maybe a
Just GDestroyNotify Parameter
forall a. FunPtr (Ptr a -> IO ())
ptr_to_g_free

-- | Construct a `Parameter` struct initialized to zero.
newZeroParameter :: MonadIO m => m Parameter
newZeroParameter :: m Parameter
newZeroParameter = IO Parameter -> m Parameter
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Parameter -> m Parameter) -> IO Parameter -> m Parameter
forall a b. (a -> b) -> a -> b
$ IO (Ptr Parameter)
forall a. WrappedPtr a => IO (Ptr a)
wrappedPtrCalloc IO (Ptr Parameter)
-> (Ptr Parameter -> IO Parameter) -> IO Parameter
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ManagedPtr Parameter -> Parameter)
-> Ptr Parameter -> IO Parameter
forall a.
(HasCallStack, WrappedPtr a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapPtr ManagedPtr Parameter -> Parameter
Parameter

instance tag ~ 'AttrSet => Constructible Parameter tag where
    new :: (ManagedPtr Parameter -> Parameter)
-> [AttrOp Parameter tag] -> m Parameter
new _ attrs :: [AttrOp Parameter tag]
attrs = do
        Parameter
o <- m Parameter
forall (m :: * -> *). MonadIO m => m Parameter
newZeroParameter
        Parameter -> [AttrOp Parameter 'AttrSet] -> m ()
forall o (m :: * -> *).
MonadIO m =>
o -> [AttrOp o 'AttrSet] -> m ()
GI.Attributes.set Parameter
o [AttrOp Parameter tag]
[AttrOp Parameter 'AttrSet]
attrs
        Parameter -> m Parameter
forall (m :: * -> *) a. Monad m => a -> m a
return Parameter
o


-- | A convenience alias for `Nothing` :: `Maybe` `Parameter`.
noParameter :: Maybe Parameter
noParameter :: Maybe Parameter
noParameter = Maybe Parameter
forall a. Maybe a
Nothing

-- | Get the value of the “@name@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' parameter #name
-- @
getParameterName :: MonadIO m => Parameter -> m (Maybe T.Text)
getParameterName :: Parameter -> m (Maybe Text)
getParameterName s :: Parameter
s = IO (Maybe Text) -> m (Maybe Text)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe Text) -> m (Maybe Text))
-> IO (Maybe Text) -> m (Maybe Text)
forall a b. (a -> b) -> a -> b
$ Parameter -> (Ptr Parameter -> IO (Maybe Text)) -> IO (Maybe Text)
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr Parameter
s ((Ptr Parameter -> IO (Maybe Text)) -> IO (Maybe Text))
-> (Ptr Parameter -> IO (Maybe Text)) -> IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Parameter
ptr -> do
    CString
val <- Ptr CString -> IO CString
forall a. Storable a => Ptr a -> IO a
peek (Ptr Parameter
ptr Ptr Parameter -> Int -> Ptr CString
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 0) :: IO CString
    Maybe Text
result <- CString -> (CString -> IO Text) -> IO (Maybe Text)
forall a b. Ptr a -> (Ptr a -> IO b) -> IO (Maybe b)
SP.convertIfNonNull CString
val ((CString -> IO Text) -> IO (Maybe Text))
-> (CString -> IO Text) -> IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ \val' :: CString
val' -> do
        Text
val'' <- HasCallStack => CString -> IO Text
CString -> IO Text
cstringToText CString
val'
        Text -> IO Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
val''
    Maybe Text -> IO (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Text
result

-- | Set the value of the “@name@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.set' parameter [ #name 'Data.GI.Base.Attributes.:=' value ]
-- @
setParameterName :: MonadIO m => Parameter -> CString -> m ()
setParameterName :: Parameter -> CString -> m ()
setParameterName s :: Parameter
s val :: CString
val = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Parameter -> (Ptr Parameter -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr Parameter
s ((Ptr Parameter -> IO ()) -> IO ())
-> (Ptr Parameter -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Parameter
ptr -> do
    Ptr CString -> CString -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr Parameter
ptr Ptr Parameter -> Int -> Ptr CString
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 0) (CString
val :: CString)

-- | Set the value of the “@name@” 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' #name
-- @
clearParameterName :: MonadIO m => Parameter -> m ()
clearParameterName :: Parameter -> m ()
clearParameterName s :: Parameter
s = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Parameter -> (Ptr Parameter -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr Parameter
s ((Ptr Parameter -> IO ()) -> IO ())
-> (Ptr Parameter -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Parameter
ptr -> do
    Ptr CString -> CString -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr Parameter
ptr Ptr Parameter -> Int -> Ptr CString
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 0) (CString
forall a. Ptr a
FP.nullPtr :: CString)

#if defined(ENABLE_OVERLOADING)
data ParameterNameFieldInfo
instance AttrInfo ParameterNameFieldInfo where
    type AttrBaseTypeConstraint ParameterNameFieldInfo = (~) Parameter
    type AttrAllowedOps ParameterNameFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint ParameterNameFieldInfo = (~) CString
    type AttrTransferTypeConstraint ParameterNameFieldInfo = (~)CString
    type AttrTransferType ParameterNameFieldInfo = CString
    type AttrGetType ParameterNameFieldInfo = Maybe T.Text
    type AttrLabel ParameterNameFieldInfo = "name"
    type AttrOrigin ParameterNameFieldInfo = Parameter
    attrGet = getParameterName
    attrSet = setParameterName
    attrConstruct = undefined
    attrClear = clearParameterName
    attrTransfer _ v = do
        return v

parameter_name :: AttrLabelProxy "name"
parameter_name = AttrLabelProxy

#endif


-- | Get the value of the “@value@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' parameter #value
-- @
getParameterValue :: MonadIO m => Parameter -> m GValue
getParameterValue :: Parameter -> m GValue
getParameterValue s :: Parameter
s = IO GValue -> m GValue
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO GValue -> m GValue) -> IO GValue -> m GValue
forall a b. (a -> b) -> a -> b
$ Parameter -> (Ptr Parameter -> IO GValue) -> IO GValue
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr Parameter
s ((Ptr Parameter -> IO GValue) -> IO GValue)
-> (Ptr Parameter -> IO GValue) -> IO GValue
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Parameter
ptr -> do
    let val :: Ptr GValue
val = Ptr Parameter
ptr Ptr Parameter -> Int -> Ptr GValue
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 8 :: (Ptr GValue)
    GValue
val' <- ((ManagedPtr GValue -> GValue) -> Ptr GValue -> IO GValue
forall a.
(HasCallStack, BoxedObject a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
newBoxed ManagedPtr GValue -> GValue
GValue) Ptr GValue
val
    GValue -> IO GValue
forall (m :: * -> *) a. Monad m => a -> m a
return GValue
val'

#if defined(ENABLE_OVERLOADING)
data ParameterValueFieldInfo
instance AttrInfo ParameterValueFieldInfo where
    type AttrBaseTypeConstraint ParameterValueFieldInfo = (~) Parameter
    type AttrAllowedOps ParameterValueFieldInfo = '[ 'AttrGet]
    type AttrSetTypeConstraint ParameterValueFieldInfo = (~) (Ptr GValue)
    type AttrTransferTypeConstraint ParameterValueFieldInfo = (~)(Ptr GValue)
    type AttrTransferType ParameterValueFieldInfo = (Ptr GValue)
    type AttrGetType ParameterValueFieldInfo = GValue
    type AttrLabel ParameterValueFieldInfo = "value"
    type AttrOrigin ParameterValueFieldInfo = Parameter
    attrGet = getParameterValue
    attrSet = undefined
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer = undefined

parameter_value :: AttrLabelProxy "value"
parameter_value = AttrLabelProxy

#endif



#if defined(ENABLE_OVERLOADING)
instance O.HasAttributeList Parameter
type instance O.AttributeList Parameter = ParameterAttributeList
type ParameterAttributeList = ('[ '("name", ParameterNameFieldInfo), '("value", ParameterValueFieldInfo)] :: [(Symbol, *)])
#endif

#if defined(ENABLE_OVERLOADING)
type family ResolveParameterMethod (t :: Symbol) (o :: *) :: * where
    ResolveParameterMethod l o = O.MethodResolutionFailed l o

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

#endif