{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds, TypeFamilies #-}
module Data.GI.Base.GValue
    (
    -- * Constructing GValues
      GValue(..)
    , IsGValue(..)
    , GValueConstruct(..)

    , newGValue
    , buildGValue
    , disownGValue
    , noGValue
    , newGValueFromPtr
    , wrapGValuePtr
    , unsetGValue
    , gvalueType

    -- * Packing GValues into arrays
    , packGValueArray
    , unpackGValueArrayWithLength
    , mapGValueArrayWithLength

    -- * Setters and getters
    , set_string
    , get_string
    , set_pointer
    , get_pointer
    , set_int
    , get_int
    , set_uint
    , get_uint
    , set_long
    , get_long
    , set_ulong
    , get_ulong
    , set_int32
    , get_int32
    , set_uint32
    , get_uint32
    , set_int64
    , get_int64
    , set_uint64
    , get_uint64
    , set_float
    , get_float
    , set_double
    , get_double
    , set_boolean
    , get_boolean
    , set_gtype
    , get_gtype
    , set_object
    , get_object
    , set_boxed
    , get_boxed
    , set_variant
    , get_variant
    , set_enum
    , get_enum
    , set_flags
    , get_flags
    , set_stablePtr
    , get_stablePtr
    , take_stablePtr
    ) where

import Data.Coerce (coerce)
import Data.Word
import Data.Int
import Data.Text (Text, pack, unpack)

import Foreign.C.Types (CInt(..), CUInt(..), CFloat(..), CDouble(..),
                        CLong(..), CULong(..))
import Foreign.C.String (CString)
import Foreign.Ptr (Ptr, nullPtr, plusPtr)
import Foreign.StablePtr (StablePtr, castStablePtrToPtr, castPtrToStablePtr)

import Data.GI.Base.BasicTypes
import Data.GI.Base.BasicConversions (cstringToText, textToCString)
import Data.GI.Base.GType
import Data.GI.Base.ManagedPtr
import Data.GI.Base.Overloading (HasParentTypes, ParentTypes)
import Data.GI.Base.Utils (callocBytes, freeMem)
import Data.GI.Base.Internal.CTypes (cgvalueSize)

-- | Haskell-side representation of a @GValue@.
newtype GValue = GValue (ManagedPtr GValue)

-- | A convenience alias for @`Nothing` :: `Maybe` `GValue`@.
noGValue :: Maybe GValue
noGValue :: Maybe GValue
noGValue = Maybe GValue
forall a. Maybe a
Nothing

foreign import ccall unsafe "g_value_get_type" c_g_value_get_type ::
    IO GType

-- | There are no types in the bindings that a `GValue` can be safely
-- cast to.
type instance ParentTypes GValue = '[]
instance HasParentTypes GValue

-- | Find the associated `GType` for `GValue`.
instance TypedObject GValue where
  glibType :: IO GType
glibType = IO GType
c_g_value_get_type

-- | `GValue`s are registered as boxed in the GLib type system.
instance GBoxed GValue

foreign import ccall "g_value_init" g_value_init ::
    Ptr GValue -> CGType -> IO (Ptr GValue)

-- | A type holding a `GValue` with an associated label. It is
-- parameterized by a phantom type encoding the target type for the
-- `GValue` (useful when constructing properties).
data GValueConstruct o = GValueConstruct String GValue

-- | Build a new, empty, `GValue` of the given type.
newGValue :: GType -> IO GValue
newGValue :: GType -> IO GValue
newGValue (GType CGType
gtype) = do
  Ptr GValue
gvptr <- Int -> IO (Ptr GValue)
forall a. Int -> IO (Ptr a)
callocBytes Int
cgvalueSize
  Ptr GValue
_ <- Ptr GValue -> CGType -> IO (Ptr GValue)
g_value_init Ptr GValue
gvptr CGType
gtype
  GValue
gv <- (ManagedPtr GValue -> GValue) -> Ptr GValue -> IO GValue
forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapBoxed ManagedPtr GValue -> GValue
GValue Ptr GValue
gvptr
  GValue -> IO GValue
forall (m :: * -> *) a. Monad m => a -> m a
return (GValue -> IO GValue) -> GValue -> IO GValue
forall a b. (a -> b) -> a -> b
$! GValue
gv

-- | Take ownership of a passed in 'Ptr'.
wrapGValuePtr :: Ptr GValue -> IO GValue
wrapGValuePtr :: Ptr GValue -> IO GValue
wrapGValuePtr Ptr GValue
ptr = (ManagedPtr GValue -> GValue) -> Ptr GValue -> IO GValue
forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapBoxed ManagedPtr GValue -> GValue
GValue Ptr GValue
ptr

-- | Construct a Haskell wrapper for the given 'GValue', making a
-- copy.
newGValueFromPtr :: Ptr GValue -> IO GValue
newGValueFromPtr :: Ptr GValue -> IO GValue
newGValueFromPtr Ptr GValue
ptr = (ManagedPtr GValue -> GValue) -> Ptr GValue -> IO GValue
forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
newBoxed ManagedPtr GValue -> GValue
GValue Ptr GValue
ptr

-- | A convenience function for building a new GValue and setting the
-- initial value.
buildGValue :: GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue :: GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtype GValue -> a -> IO ()
setter a
val = do
  GValue
gv <- GType -> IO GValue
newGValue GType
gtype
  GValue -> a -> IO ()
setter GValue
gv a
val
  GValue -> IO GValue
forall (m :: * -> *) a. Monad m => a -> m a
return GValue
gv

-- | Disown a `GValue`, i.e. do not unref the underlying object when
-- the Haskell object is garbage collected.
disownGValue :: GValue -> IO (Ptr GValue)
disownGValue :: GValue -> IO (Ptr GValue)
disownGValue = GValue -> IO (Ptr GValue)
forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
disownManagedPtr

foreign import ccall "_haskell_gi_g_value_get_type" g_value_get_type :: Ptr GValue -> IO CGType

-- | Return the `GType` contained by a `GValue`.
gvalueType :: GValue -> IO GType
gvalueType :: GValue -> IO GType
gvalueType GValue
gv = GValue -> (Ptr GValue -> IO GType) -> IO GType
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO GType) -> IO GType)
-> (Ptr GValue -> IO GType) -> IO GType
forall a b. (a -> b) -> a -> b
$ \Ptr GValue
gvptr -> do
  CGType
cgtype <- Ptr GValue -> IO CGType
g_value_get_type Ptr GValue
gvptr
  GType -> IO GType
forall (m :: * -> *) a. Monad m => a -> m a
return (CGType -> GType
GType CGType
cgtype)

foreign import ccall "g_value_unset" g_value_unset :: Ptr GValue -> IO ()

-- | Unset the `GValue`, freeing all resources associated to it.
unsetGValue :: Ptr GValue -> IO ()
unsetGValue :: Ptr GValue -> IO ()
unsetGValue = Ptr GValue -> IO ()
g_value_unset

-- | A convenience class for marshaling back and forth between Haskell
-- values and `GValue`s.
class IsGValue a where
    toGValue :: a -> IO GValue
    fromGValue :: GValue -> IO a

instance IsGValue (Maybe String) where
    toGValue :: Maybe String -> IO GValue
toGValue = GType -> (GValue -> Maybe Text -> IO ()) -> Maybe Text -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeString GValue -> Maybe Text -> IO ()
set_string (Maybe Text -> IO GValue)
-> (Maybe String -> Maybe Text) -> Maybe String -> IO GValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Text) -> Maybe String -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Text
pack
    fromGValue :: GValue -> IO (Maybe String)
fromGValue GValue
v = ((Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> String
unpack) (Maybe Text -> Maybe String)
-> IO (Maybe Text) -> IO (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GValue -> IO (Maybe Text)
get_string GValue
v

instance IsGValue (Maybe Text) where
    toGValue :: Maybe Text -> IO GValue
toGValue = GType -> (GValue -> Maybe Text -> IO ()) -> Maybe Text -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeString GValue -> Maybe Text -> IO ()
set_string
    fromGValue :: GValue -> IO (Maybe Text)
fromGValue = GValue -> IO (Maybe Text)
get_string

instance IsGValue (Ptr a) where
    toGValue :: Ptr a -> IO GValue
toGValue = GType -> (GValue -> Ptr a -> IO ()) -> Ptr a -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypePointer GValue -> Ptr a -> IO ()
forall a. GValue -> Ptr a -> IO ()
set_pointer
    fromGValue :: GValue -> IO (Ptr a)
fromGValue = GValue -> IO (Ptr a)
forall a. GValue -> IO (Ptr a)
get_pointer

instance IsGValue Int32 where
    toGValue :: Int32 -> IO GValue
toGValue = GType -> (GValue -> Int32 -> IO ()) -> Int32 -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeInt GValue -> Int32 -> IO ()
set_int32
    fromGValue :: GValue -> IO Int32
fromGValue = GValue -> IO Int32
get_int32

instance IsGValue Word32 where
    toGValue :: Word32 -> IO GValue
toGValue = GType -> (GValue -> Word32 -> IO ()) -> Word32 -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeUInt GValue -> Word32 -> IO ()
set_uint32
    fromGValue :: GValue -> IO Word32
fromGValue = GValue -> IO Word32
get_uint32

instance IsGValue CInt where
    toGValue :: CInt -> IO GValue
toGValue = GType -> (GValue -> CInt -> IO ()) -> CInt -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeInt GValue -> CInt -> IO ()
set_int
    fromGValue :: GValue -> IO CInt
fromGValue = GValue -> IO CInt
get_int

instance IsGValue CUInt where
    toGValue :: CUInt -> IO GValue
toGValue = GType -> (GValue -> CUInt -> IO ()) -> CUInt -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeUInt GValue -> CUInt -> IO ()
set_uint
    fromGValue :: GValue -> IO CUInt
fromGValue = GValue -> IO CUInt
get_uint

instance IsGValue CLong where
    toGValue :: CLong -> IO GValue
toGValue = GType -> (GValue -> CLong -> IO ()) -> CLong -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeLong GValue -> CLong -> IO ()
set_long
    fromGValue :: GValue -> IO CLong
fromGValue = GValue -> IO CLong
get_long

instance IsGValue CULong where
    toGValue :: CULong -> IO GValue
toGValue = GType -> (GValue -> CULong -> IO ()) -> CULong -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeULong GValue -> CULong -> IO ()
set_ulong
    fromGValue :: GValue -> IO CULong
fromGValue = GValue -> IO CULong
get_ulong

instance IsGValue Int64 where
    toGValue :: Int64 -> IO GValue
toGValue = GType -> (GValue -> Int64 -> IO ()) -> Int64 -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeInt64 GValue -> Int64 -> IO ()
set_int64
    fromGValue :: GValue -> IO Int64
fromGValue = GValue -> IO Int64
get_int64

instance IsGValue Word64 where
    toGValue :: CGType -> IO GValue
toGValue = GType -> (GValue -> CGType -> IO ()) -> CGType -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeUInt64 GValue -> CGType -> IO ()
set_uint64
    fromGValue :: GValue -> IO CGType
fromGValue = GValue -> IO CGType
get_uint64

instance IsGValue Float where
    toGValue :: Float -> IO GValue
toGValue = GType -> (GValue -> Float -> IO ()) -> Float -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeFloat GValue -> Float -> IO ()
set_float
    fromGValue :: GValue -> IO Float
fromGValue = GValue -> IO Float
get_float

instance IsGValue Double where
    toGValue :: Double -> IO GValue
toGValue = GType -> (GValue -> Double -> IO ()) -> Double -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeDouble GValue -> Double -> IO ()
set_double
    fromGValue :: GValue -> IO Double
fromGValue = GValue -> IO Double
get_double

instance IsGValue Bool where
    toGValue :: Bool -> IO GValue
toGValue = GType -> (GValue -> Bool -> IO ()) -> Bool -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeBoolean GValue -> Bool -> IO ()
set_boolean
    fromGValue :: GValue -> IO Bool
fromGValue = GValue -> IO Bool
get_boolean

instance IsGValue GType where
    toGValue :: GType -> IO GValue
toGValue = GType -> (GValue -> GType -> IO ()) -> GType -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeGType GValue -> GType -> IO ()
set_gtype
    fromGValue :: GValue -> IO GType
fromGValue = GValue -> IO GType
get_gtype

instance IsGValue (StablePtr a) where
    toGValue :: StablePtr a -> IO GValue
toGValue = GType
-> (GValue -> StablePtr a -> IO ()) -> StablePtr a -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
buildGValue GType
gtypeStablePtr GValue -> StablePtr a -> IO ()
forall a. GValue -> StablePtr a -> IO ()
set_stablePtr
    fromGValue :: GValue -> IO (StablePtr a)
fromGValue = GValue -> IO (StablePtr a)
forall a. GValue -> IO (StablePtr a)
get_stablePtr

foreign import ccall "g_value_set_string" _set_string ::
    Ptr GValue -> CString -> IO ()
foreign import ccall "g_value_get_string" _get_string ::
    Ptr GValue -> IO CString

set_string :: GValue -> Maybe Text -> IO ()
set_string :: GValue -> Maybe Text -> IO ()
set_string GValue
gv Maybe Text
maybeStr =
    GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr GValue
ptr -> do
      CString
cstr <- case Maybe Text
maybeStr of
                Just Text
str -> Text -> IO CString
textToCString Text
str
                Maybe Text
Nothing -> CString -> IO CString
forall (m :: * -> *) a. Monad m => a -> m a
return CString
forall a. Ptr a
nullPtr
      Ptr GValue -> CString -> IO ()
_set_string Ptr GValue
ptr CString
cstr
      CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
cstr

get_string :: GValue -> IO (Maybe Text)
get_string :: GValue -> IO (Maybe Text)
get_string GValue
gv = GValue -> (Ptr GValue -> IO (Maybe Text)) -> IO (Maybe Text)
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO (Maybe Text)) -> IO (Maybe Text))
-> (Ptr GValue -> IO (Maybe Text)) -> IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ \Ptr GValue
gvptr -> do
                  CString
cstr <- Ptr GValue -> IO CString
_get_string Ptr GValue
gvptr
                  if CString
cstr CString -> CString -> Bool
forall a. Eq a => a -> a -> Bool
/= CString
forall a. Ptr a
nullPtr
                  then Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> IO Text -> IO (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HasCallStack => CString -> IO Text
CString -> IO Text
cstringToText CString
cstr
                  else Maybe Text -> IO (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Text
forall a. Maybe a
Nothing

foreign import ccall unsafe "g_value_set_pointer" _set_pointer ::
    Ptr GValue -> Ptr a -> IO ()
foreign import ccall unsafe "g_value_get_pointer" _get_pointer ::
    Ptr GValue -> IO (Ptr b)

set_pointer :: GValue -> Ptr a -> IO ()
set_pointer :: GValue -> Ptr a -> IO ()
set_pointer GValue
gv Ptr a
ptr = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> Ptr a -> IO ()) -> Ptr a -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> Ptr a -> IO ()
forall a. Ptr GValue -> Ptr a -> IO ()
_set_pointer Ptr a
ptr

get_pointer :: GValue -> IO (Ptr b)
get_pointer :: GValue -> IO (Ptr b)
get_pointer GValue
gv = GValue -> (Ptr GValue -> IO (Ptr b)) -> IO (Ptr b)
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO (Ptr b)
forall b. Ptr GValue -> IO (Ptr b)
_get_pointer

foreign import ccall unsafe "g_value_set_int" _set_int ::
    Ptr GValue -> CInt -> IO ()
foreign import ccall unsafe "g_value_get_int" _get_int ::
    Ptr GValue -> IO CInt

set_int32 :: GValue -> Int32 -> IO ()
set_int32 :: GValue -> Int32 -> IO ()
set_int32 GValue
gv Int32
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CInt -> IO ()) -> CInt -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CInt -> IO ()
_set_int (Int32 -> CInt
coerce Int32
n)

get_int32 :: GValue -> IO Int32
get_int32 :: GValue -> IO Int32
get_int32 GValue
gv = CInt -> Int32
coerce (CInt -> Int32) -> IO CInt -> IO Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GValue -> (Ptr GValue -> IO CInt) -> IO CInt
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CInt
_get_int

set_int :: GValue -> CInt -> IO ()
set_int :: GValue -> CInt -> IO ()
set_int GValue
gv CInt
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CInt -> IO ()) -> CInt -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CInt -> IO ()
_set_int CInt
n

get_int :: GValue -> IO CInt
get_int :: GValue -> IO CInt
get_int GValue
gv = GValue -> (Ptr GValue -> IO CInt) -> IO CInt
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CInt
_get_int

foreign import ccall unsafe "g_value_set_uint" _set_uint ::
    Ptr GValue -> CUInt -> IO ()
foreign import ccall unsafe "g_value_get_uint" _get_uint ::
    Ptr GValue -> IO CUInt

set_uint32 :: GValue -> Word32 -> IO ()
set_uint32 :: GValue -> Word32 -> IO ()
set_uint32 GValue
gv Word32
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CUInt -> IO ()) -> CUInt -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CUInt -> IO ()
_set_uint (Word32 -> CUInt
coerce Word32
n)

get_uint32 :: GValue -> IO Word32
get_uint32 :: GValue -> IO Word32
get_uint32 GValue
gv = CUInt -> Word32
coerce (CUInt -> Word32) -> IO CUInt -> IO Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GValue -> (Ptr GValue -> IO CUInt) -> IO CUInt
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CUInt
_get_uint

set_uint :: GValue -> CUInt -> IO ()
set_uint :: GValue -> CUInt -> IO ()
set_uint GValue
gv CUInt
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CUInt -> IO ()) -> CUInt -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CUInt -> IO ()
_set_uint CUInt
n

get_uint :: GValue -> IO CUInt
get_uint :: GValue -> IO CUInt
get_uint GValue
gv = GValue -> (Ptr GValue -> IO CUInt) -> IO CUInt
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CUInt
_get_uint

foreign import ccall unsafe "g_value_set_long" _set_long ::
    Ptr GValue -> CLong -> IO ()
foreign import ccall unsafe "g_value_get_long" _get_long ::
    Ptr GValue -> IO CLong

set_long :: GValue -> CLong -> IO ()
set_long :: GValue -> CLong -> IO ()
set_long GValue
gv CLong
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CLong -> IO ()) -> CLong -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CLong -> IO ()
_set_long CLong
n

get_long :: GValue -> IO CLong
get_long :: GValue -> IO CLong
get_long GValue
gv = GValue -> (Ptr GValue -> IO CLong) -> IO CLong
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CLong
_get_long

foreign import ccall unsafe "g_value_set_ulong" _set_ulong ::
    Ptr GValue -> CULong -> IO ()
foreign import ccall unsafe "g_value_get_ulong" _get_ulong ::
    Ptr GValue -> IO CULong

set_ulong :: GValue -> CULong -> IO ()
set_ulong :: GValue -> CULong -> IO ()
set_ulong GValue
gv CULong
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CULong -> IO ()) -> CULong -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CULong -> IO ()
_set_ulong CULong
n

get_ulong :: GValue -> IO CULong
get_ulong :: GValue -> IO CULong
get_ulong GValue
gv = GValue -> (Ptr GValue -> IO CULong) -> IO CULong
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CULong
_get_ulong

foreign import ccall unsafe "g_value_set_int64" _set_int64 ::
    Ptr GValue -> Int64 -> IO ()
foreign import ccall unsafe "g_value_get_int64" _get_int64 ::
    Ptr GValue -> IO Int64

set_int64 :: GValue -> Int64 -> IO ()
set_int64 :: GValue -> Int64 -> IO ()
set_int64 GValue
gv Int64
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> Int64 -> IO ()) -> Int64 -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> Int64 -> IO ()
_set_int64 Int64
n

get_int64 :: GValue -> IO Int64
get_int64 :: GValue -> IO Int64
get_int64 GValue
gv = GValue -> (Ptr GValue -> IO Int64) -> IO Int64
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO Int64
_get_int64

foreign import ccall unsafe "g_value_set_uint64" _set_uint64 ::
    Ptr GValue -> Word64 -> IO ()
foreign import ccall unsafe "g_value_get_uint64" _get_uint64 ::
    Ptr GValue -> IO Word64

set_uint64 :: GValue -> Word64 -> IO ()
set_uint64 :: GValue -> CGType -> IO ()
set_uint64 GValue
gv CGType
n = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CGType -> IO ()) -> CGType -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CGType -> IO ()
_set_uint64 CGType
n

get_uint64 :: GValue -> IO Word64
get_uint64 :: GValue -> IO CGType
get_uint64 GValue
gv = GValue -> (Ptr GValue -> IO CGType) -> IO CGType
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CGType
_get_uint64

foreign import ccall unsafe "g_value_set_float" _set_float ::
    Ptr GValue -> CFloat -> IO ()
foreign import ccall unsafe "g_value_get_float" _get_float ::
    Ptr GValue -> IO CFloat

set_float :: GValue -> Float -> IO ()
set_float :: GValue -> Float -> IO ()
set_float GValue
gv Float
f = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CFloat -> IO ()) -> CFloat -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CFloat -> IO ()
_set_float (Float -> CFloat
forall a b. (Real a, Fractional b) => a -> b
realToFrac Float
f)

get_float :: GValue -> IO Float
get_float :: GValue -> IO Float
get_float GValue
gv = CFloat -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac (CFloat -> Float) -> IO CFloat -> IO Float
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GValue -> (Ptr GValue -> IO CFloat) -> IO CFloat
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CFloat
_get_float

foreign import ccall unsafe "g_value_set_double" _set_double ::
    Ptr GValue -> CDouble -> IO ()
foreign import ccall unsafe "g_value_get_double" _get_double ::
    Ptr GValue -> IO CDouble

set_double :: GValue -> Double -> IO ()
set_double :: GValue -> Double -> IO ()
set_double GValue
gv Double
d = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CDouble -> IO ()) -> CDouble -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CDouble -> IO ()
_set_double (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
d)

get_double :: GValue -> IO Double
get_double :: GValue -> IO Double
get_double GValue
gv = CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (CDouble -> Double) -> IO CDouble -> IO Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GValue -> (Ptr GValue -> IO CDouble) -> IO CDouble
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CDouble
_get_double

foreign import ccall unsafe "g_value_set_boolean" _set_boolean ::
    Ptr GValue -> CInt -> IO ()
foreign import ccall unsafe "g_value_get_boolean" _get_boolean ::
    Ptr GValue -> IO CInt

set_boolean :: GValue -> Bool -> IO ()
set_boolean :: GValue -> Bool -> IO ()
set_boolean GValue
gv Bool
b = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr GValue
ptr ->
                   Ptr GValue -> CInt -> IO ()
_set_boolean Ptr GValue
ptr (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> Int -> CInt
forall a b. (a -> b) -> a -> b
$ Bool -> Int
forall a. Enum a => a -> Int
fromEnum Bool
b)

get_boolean :: GValue -> IO Bool
get_boolean :: GValue -> IO Bool
get_boolean GValue
gv = GValue -> (Ptr GValue -> IO Bool) -> IO Bool
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO Bool) -> IO Bool)
-> (Ptr GValue -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr GValue
ptr -> (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0) (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr GValue -> IO CInt
_get_boolean Ptr GValue
ptr

foreign import ccall unsafe "g_value_set_gtype" _set_gtype ::
    Ptr GValue -> CGType -> IO ()
foreign import ccall unsafe "g_value_get_gtype" _get_gtype ::
    Ptr GValue -> IO CGType

set_gtype :: GValue -> GType -> IO ()
set_gtype :: GValue -> GType -> IO ()
set_gtype GValue
gv (GType CGType
g) = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr GValue
ptr -> Ptr GValue -> CGType -> IO ()
_set_gtype Ptr GValue
ptr CGType
g

get_gtype :: GValue -> IO GType
get_gtype :: GValue -> IO GType
get_gtype GValue
gv = CGType -> GType
GType (CGType -> GType) -> IO CGType -> IO GType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GValue -> (Ptr GValue -> IO CGType) -> IO CGType
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CGType
_get_gtype

foreign import ccall "g_value_set_object" _set_object ::
    Ptr GValue -> Ptr a -> IO ()
foreign import ccall "g_value_get_object" _get_object ::
    Ptr GValue -> IO (Ptr a)

set_object :: GObject a => GValue -> Ptr a -> IO ()
set_object :: GValue -> Ptr a -> IO ()
set_object GValue
gv Ptr a
o = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> Ptr a -> IO ()) -> Ptr a -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> Ptr a -> IO ()
forall a. Ptr GValue -> Ptr a -> IO ()
_set_object Ptr a
o

get_object :: GObject b => GValue -> IO (Ptr b)
get_object :: GValue -> IO (Ptr b)
get_object GValue
gv = GValue -> (Ptr GValue -> IO (Ptr b)) -> IO (Ptr b)
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO (Ptr b)
forall b. Ptr GValue -> IO (Ptr b)
_get_object

foreign import ccall "g_value_set_boxed" _set_boxed ::
    Ptr GValue -> Ptr a -> IO ()
foreign import ccall "g_value_get_boxed" _get_boxed ::
    Ptr GValue -> IO (Ptr b)

set_boxed :: GValue -> Ptr a -> IO ()
set_boxed :: GValue -> Ptr a -> IO ()
set_boxed GValue
gv Ptr a
b = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> Ptr a -> IO ()) -> Ptr a -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> Ptr a -> IO ()
forall a. Ptr GValue -> Ptr a -> IO ()
_set_boxed Ptr a
b

get_boxed :: GValue -> IO (Ptr b)
get_boxed :: GValue -> IO (Ptr b)
get_boxed GValue
gv = GValue -> (Ptr GValue -> IO (Ptr b)) -> IO (Ptr b)
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO (Ptr b)
forall b. Ptr GValue -> IO (Ptr b)
_get_boxed

foreign import ccall "g_value_set_variant" _set_variant ::
    Ptr GValue -> Ptr GVariant -> IO ()
foreign import ccall "g_value_get_variant" _get_variant ::
    Ptr GValue -> IO (Ptr GVariant)

set_variant :: GValue -> Ptr GVariant -> IO ()
set_variant :: GValue -> Ptr GVariant -> IO ()
set_variant GValue
gv Ptr GVariant
v = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> Ptr GVariant -> IO ())
-> Ptr GVariant -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> Ptr GVariant -> IO ()
_set_variant Ptr GVariant
v

get_variant :: GValue -> IO (Ptr GVariant)
get_variant :: GValue -> IO (Ptr GVariant)
get_variant GValue
gv = GValue -> (Ptr GValue -> IO (Ptr GVariant)) -> IO (Ptr GVariant)
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO (Ptr GVariant)
_get_variant

foreign import ccall unsafe "g_value_set_enum" _set_enum ::
    Ptr GValue -> CUInt -> IO ()
foreign import ccall unsafe "g_value_get_enum" _get_enum ::
    Ptr GValue -> IO CUInt

set_enum :: GValue -> CUInt -> IO ()
set_enum :: GValue -> CUInt -> IO ()
set_enum GValue
gv CUInt
e = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CUInt -> IO ()) -> CUInt -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CUInt -> IO ()
_set_enum CUInt
e

get_enum :: GValue -> IO CUInt
get_enum :: GValue -> IO CUInt
get_enum GValue
gv = GValue -> (Ptr GValue -> IO CUInt) -> IO CUInt
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CUInt
_get_enum

foreign import ccall unsafe "g_value_set_flags" _set_flags ::
    Ptr GValue -> CUInt -> IO ()
foreign import ccall unsafe "g_value_get_flags" _get_flags ::
    Ptr GValue -> IO CUInt

set_flags :: GValue -> CUInt -> IO ()
set_flags :: GValue -> CUInt -> IO ()
set_flags GValue
gv CUInt
f = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> CUInt -> IO ()) -> CUInt -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> CUInt -> IO ()
_set_flags CUInt
f

get_flags :: GValue -> IO CUInt
get_flags :: GValue -> IO CUInt
get_flags GValue
gv = GValue -> (Ptr GValue -> IO CUInt) -> IO CUInt
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO CUInt
_get_flags

-- | Set the value of `GValue` containing a `StablePtr`
set_stablePtr :: GValue -> StablePtr a -> IO ()
set_stablePtr :: GValue -> StablePtr a -> IO ()
set_stablePtr GValue
gv StablePtr a
ptr = GValue -> (Ptr GValue -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv ((Ptr GValue -> IO ()) -> IO ()) -> (Ptr GValue -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Ptr GValue -> Ptr () -> IO ()) -> Ptr () -> Ptr GValue -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr GValue -> Ptr () -> IO ()
forall a. Ptr GValue -> Ptr a -> IO ()
_set_boxed (StablePtr a -> Ptr ()
forall a. StablePtr a -> Ptr ()
castStablePtrToPtr StablePtr a
ptr)

foreign import ccall g_value_take_boxed :: Ptr GValue -> StablePtr a -> IO ()

-- | Like `set_stablePtr`, but the `GValue` takes ownership of the `StablePtr`
take_stablePtr :: Ptr GValue -> StablePtr a -> IO ()
take_stablePtr :: Ptr GValue -> StablePtr a -> IO ()
take_stablePtr = Ptr GValue -> StablePtr a -> IO ()
forall a. Ptr GValue -> StablePtr a -> IO ()
g_value_take_boxed

-- | Get the value of a `GValue` containing a `StablePtr`
get_stablePtr :: GValue -> IO (StablePtr a)
get_stablePtr :: GValue -> IO (StablePtr a)
get_stablePtr GValue
gv = Ptr () -> StablePtr a
forall a. Ptr () -> StablePtr a
castPtrToStablePtr (Ptr () -> StablePtr a) -> IO (Ptr ()) -> IO (StablePtr a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GValue -> (Ptr GValue -> IO (Ptr ())) -> IO (Ptr ())
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr GValue
gv Ptr GValue -> IO (Ptr ())
forall b. Ptr GValue -> IO (Ptr b)
_get_boxed

foreign import ccall g_value_copy :: Ptr GValue -> Ptr GValue -> IO ()

-- | Pack the given list of GValues contiguously into a C array
packGValueArray :: [GValue] -> IO (Ptr GValue)
packGValueArray :: [GValue] -> IO (Ptr GValue)
packGValueArray [GValue]
gvalues = [GValue] -> ([Ptr GValue] -> IO (Ptr GValue)) -> IO (Ptr GValue)
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
[a] -> ([Ptr a] -> IO c) -> IO c
withManagedPtrList [GValue]
gvalues (([Ptr GValue] -> IO (Ptr GValue)) -> IO (Ptr GValue))
-> ([Ptr GValue] -> IO (Ptr GValue)) -> IO (Ptr GValue)
forall a b. (a -> b) -> a -> b
$ \[Ptr GValue]
ptrs -> do
  let nitems :: Int
nitems = [Ptr GValue] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Ptr GValue]
ptrs
  Ptr GValue
mem <- Int -> IO (Ptr GValue)
forall a. Int -> IO (Ptr a)
callocBytes (Int -> IO (Ptr GValue)) -> Int -> IO (Ptr GValue)
forall a b. (a -> b) -> a -> b
$ Int
cgvalueSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
nitems
  Ptr GValue -> [Ptr GValue] -> IO ()
fill Ptr GValue
mem [Ptr GValue]
ptrs
  Ptr GValue -> IO (Ptr GValue)
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr GValue
mem
  where fill :: Ptr GValue -> [Ptr GValue] -> IO ()
        fill :: Ptr GValue -> [Ptr GValue] -> IO ()
fill Ptr GValue
_ [] = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        fill Ptr GValue
ptr (Ptr GValue
x:[Ptr GValue]
xs) = do
          CGType
gtype <- Ptr GValue -> IO CGType
g_value_get_type Ptr GValue
x
          Ptr GValue
_ <- Ptr GValue -> CGType -> IO (Ptr GValue)
g_value_init Ptr GValue
ptr CGType
gtype
          Ptr GValue -> Ptr GValue -> IO ()
g_value_copy Ptr GValue
x Ptr GValue
ptr
          Ptr GValue -> [Ptr GValue] -> IO ()
fill (Ptr GValue
ptr Ptr GValue -> Int -> Ptr GValue
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
cgvalueSize) [Ptr GValue]
xs

-- | Unpack an array of contiguous GValues into a list of GValues.
unpackGValueArrayWithLength :: Integral a =>
                               a -> Ptr GValue -> IO [GValue]
unpackGValueArrayWithLength :: a -> Ptr GValue -> IO [GValue]
unpackGValueArrayWithLength a
nitems Ptr GValue
gvalues = Int -> Ptr GValue -> IO [GValue]
go (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
nitems) Ptr GValue
gvalues
  where go :: Int -> Ptr GValue -> IO [GValue]
        go :: Int -> Ptr GValue -> IO [GValue]
go Int
0 Ptr GValue
_ = [GValue] -> IO [GValue]
forall (m :: * -> *) a. Monad m => a -> m a
return []
        go Int
n Ptr GValue
ptr = do
          Ptr GValue
gv <- Int -> IO (Ptr GValue)
forall a. Int -> IO (Ptr a)
callocBytes Int
cgvalueSize
          CGType
gtype <- Ptr GValue -> IO CGType
g_value_get_type Ptr GValue
ptr
          Ptr GValue
_ <- Ptr GValue -> CGType -> IO (Ptr GValue)
g_value_init Ptr GValue
gv CGType
gtype
          Ptr GValue -> Ptr GValue -> IO ()
g_value_copy Ptr GValue
ptr Ptr GValue
gv
          GValue
wrapped <- Ptr GValue -> IO GValue
wrapGValuePtr Ptr GValue
gv
          (GValue
wrapped GValue -> [GValue] -> [GValue]
forall a. a -> [a] -> [a]
:) ([GValue] -> [GValue]) -> IO [GValue] -> IO [GValue]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Ptr GValue -> IO [GValue]
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Ptr GValue
ptr Ptr GValue -> Int -> Ptr GValue
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
cgvalueSize)

-- | Map over the `GValue`s inside a C array.
mapGValueArrayWithLength :: Integral a =>
                            a -> (Ptr GValue -> IO c) -> Ptr GValue -> IO ()
mapGValueArrayWithLength :: a -> (Ptr GValue -> IO c) -> Ptr GValue -> IO ()
mapGValueArrayWithLength a
nvalues Ptr GValue -> IO c
f Ptr GValue
arrayPtr
  | (Ptr GValue
arrayPtr Ptr GValue -> Ptr GValue -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr GValue
forall a. Ptr a
nullPtr) = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  | (a
nvalues a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0) = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  | Bool
otherwise = Int -> Ptr GValue -> IO ()
go (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
nvalues) Ptr GValue
arrayPtr
  where go :: Int -> Ptr GValue -> IO ()
        go :: Int -> Ptr GValue -> IO ()
go Int
0 Ptr GValue
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        go Int
n Ptr GValue
ptr = do
          c
_ <- Ptr GValue -> IO c
f Ptr GValue
ptr
          Int -> Ptr GValue -> IO ()
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Ptr GValue
ptr Ptr GValue -> Int -> Ptr GValue
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
cgvalueSize)