{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- Contains the public fields of a pointer array.

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

module GI.GLib.Structs.PtrArray
    ( 

-- * Exported types
    PtrArray(..)                            ,
    newZeroPtrArray                         ,
    noPtrArray                              ,


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

#if defined(ENABLE_OVERLOADING)
    ResolvePtrArrayMethod                   ,
#endif




 -- * Properties
-- ** len #attr:len#
-- | number of pointers in the array

    getPtrArrayLen                          ,
#if defined(ENABLE_OVERLOADING)
    ptrArray_len                            ,
#endif
    setPtrArrayLen                          ,


-- ** pdata #attr:pdata#
-- | points to the array of pointers, which may be moved when the
--     array grows

    clearPtrArrayPdata                      ,
    getPtrArrayPdata                        ,
#if defined(ENABLE_OVERLOADING)
    ptrArray_pdata                          ,
#endif
    setPtrArrayPdata                        ,




    ) 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 PtrArray = PtrArray (ManagedPtr PtrArray)
    deriving (PtrArray -> PtrArray -> Bool
(PtrArray -> PtrArray -> Bool)
-> (PtrArray -> PtrArray -> Bool) -> Eq PtrArray
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PtrArray -> PtrArray -> Bool
$c/= :: PtrArray -> PtrArray -> Bool
== :: PtrArray -> PtrArray -> Bool
$c== :: PtrArray -> PtrArray -> Bool
Eq)
foreign import ccall "g_ptr_array_get_type" c_g_ptr_array_get_type :: 
    IO GType

instance BoxedObject PtrArray where
    boxedType :: PtrArray -> IO GType
boxedType _ = IO GType
c_g_ptr_array_get_type

-- | Convert 'PtrArray' to and from 'Data.GI.Base.GValue.GValue' with 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue PtrArray where
    toGValue :: PtrArray -> IO GValue
toGValue o :: PtrArray
o = do
        GType
gtype <- IO GType
c_g_ptr_array_get_type
        PtrArray -> (Ptr PtrArray -> IO GValue) -> IO GValue
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
B.ManagedPtr.withManagedPtr PtrArray
o (GType
-> (GValue -> Ptr PtrArray -> IO ()) -> Ptr PtrArray -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
B.GValue.buildGValue GType
gtype GValue -> Ptr PtrArray -> IO ()
forall a. GValue -> Ptr a -> IO ()
B.GValue.set_boxed)
        
    fromGValue :: GValue -> IO PtrArray
fromGValue gv :: GValue
gv = do
        Ptr PtrArray
ptr <- GValue -> IO (Ptr PtrArray)
forall b. GValue -> IO (Ptr b)
B.GValue.get_boxed GValue
gv :: IO (Ptr PtrArray)
        (ManagedPtr PtrArray -> PtrArray) -> Ptr PtrArray -> IO PtrArray
forall a.
(HasCallStack, BoxedObject a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
B.ManagedPtr.newBoxed ManagedPtr PtrArray -> PtrArray
PtrArray Ptr PtrArray
ptr
        
    

-- | Construct a `PtrArray` struct initialized to zero.
newZeroPtrArray :: MonadIO m => m PtrArray
newZeroPtrArray :: m PtrArray
newZeroPtrArray = IO PtrArray -> m PtrArray
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO PtrArray -> m PtrArray) -> IO PtrArray -> m PtrArray
forall a b. (a -> b) -> a -> b
$ Int -> IO (Ptr PtrArray)
forall a. BoxedObject a => Int -> IO (Ptr a)
callocBoxedBytes 16 IO (Ptr PtrArray) -> (Ptr PtrArray -> IO PtrArray) -> IO PtrArray
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ManagedPtr PtrArray -> PtrArray) -> Ptr PtrArray -> IO PtrArray
forall a.
(HasCallStack, BoxedObject a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapBoxed ManagedPtr PtrArray -> PtrArray
PtrArray

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


-- | A convenience alias for `Nothing` :: `Maybe` `PtrArray`.
noPtrArray :: Maybe PtrArray
noPtrArray :: Maybe PtrArray
noPtrArray = Maybe PtrArray
forall a. Maybe a
Nothing

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

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

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

#if defined(ENABLE_OVERLOADING)
data PtrArrayPdataFieldInfo
instance AttrInfo PtrArrayPdataFieldInfo where
    type AttrBaseTypeConstraint PtrArrayPdataFieldInfo = (~) PtrArray
    type AttrAllowedOps PtrArrayPdataFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint PtrArrayPdataFieldInfo = (~) (Ptr ())
    type AttrTransferTypeConstraint PtrArrayPdataFieldInfo = (~)(Ptr ())
    type AttrTransferType PtrArrayPdataFieldInfo = (Ptr ())
    type AttrGetType PtrArrayPdataFieldInfo = Ptr ()
    type AttrLabel PtrArrayPdataFieldInfo = "pdata"
    type AttrOrigin PtrArrayPdataFieldInfo = PtrArray
    attrGet = getPtrArrayPdata
    attrSet = setPtrArrayPdata
    attrConstruct = undefined
    attrClear = clearPtrArrayPdata
    attrTransfer _ v = do
        return v

ptrArray_pdata :: AttrLabelProxy "pdata"
ptrArray_pdata = AttrLabelProxy

#endif


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

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

#if defined(ENABLE_OVERLOADING)
data PtrArrayLenFieldInfo
instance AttrInfo PtrArrayLenFieldInfo where
    type AttrBaseTypeConstraint PtrArrayLenFieldInfo = (~) PtrArray
    type AttrAllowedOps PtrArrayLenFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PtrArrayLenFieldInfo = (~) Word32
    type AttrTransferTypeConstraint PtrArrayLenFieldInfo = (~)Word32
    type AttrTransferType PtrArrayLenFieldInfo = Word32
    type AttrGetType PtrArrayLenFieldInfo = Word32
    type AttrLabel PtrArrayLenFieldInfo = "len"
    type AttrOrigin PtrArrayLenFieldInfo = PtrArray
    attrGet = getPtrArrayLen
    attrSet = setPtrArrayLen
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v

ptrArray_len :: AttrLabelProxy "len"
ptrArray_len = AttrLabelProxy

#endif



#if defined(ENABLE_OVERLOADING)
instance O.HasAttributeList PtrArray
type instance O.AttributeList PtrArray = PtrArrayAttributeList
type PtrArrayAttributeList = ('[ '("pdata", PtrArrayPdataFieldInfo), '("len", PtrArrayLenFieldInfo)] :: [(Symbol, *)])
#endif

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

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

#endif