Data.GI.Base.ManagedPtr

Description

We wrap most objects in a "managed pointer", which is simply a newtype for a ForeignPtr of the appropriate type:

newtype Foo = Foo (ForeignPtr Foo)

Notice that types of this form are instances of ForeignPtrNewtype. The newtype is useful in order to make the newtype an instance of different typeclasses. The routines in this module deal with the memory management of such managed pointers.

Synopsis

# Managed pointers

newManagedPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a) Source #

A thin wrapper over newForeignPtr.

withManagedPtr :: ForeignPtrNewtype a => a -> (Ptr a -> IO c) -> IO c Source #

Perform an IO action on the Ptr inside a managed pointer.

maybeWithManagedPtr :: ForeignPtrNewtype a => Maybe a -> (Ptr a -> IO c) -> IO c Source #

Like withManagedPtr, but accepts a Maybe type. If the passed value is Nothing the inner action will be executed with a nullPtr argument.

withManagedPtrList :: ForeignPtrNewtype a => [a] -> ([Ptr a] -> IO c) -> IO c Source #

Perform an IO action taking a list of Ptr on a list of managed pointers.

Return the Ptr in a given managed pointer. As the name says, this is potentially unsafe: the given Ptr may only be used before a call to touchManagedPtr. This function is of most interest to the autogenerated bindings, for hand-written code withManagedPtr is almost always a better choice.

unsafeManagedPtrCastPtr :: forall a b. ForeignPtrNewtype a => a -> Ptr b Source #

Same as unsafeManagedPtrGetPtr, but is polymorphic on the return type.

touchManagedPtr :: forall a. ForeignPtrNewtype a => a -> IO () Source #

Ensure that the Ptr in the given managed pointer is still alive (i.e. it has not been garbage collected by the runtime) at the point that this is called.

# Safe casting

castTo :: forall o o'. (GObject o, GObject o') => (ForeignPtr o' -> o') -> o -> IO (Maybe o') Source #

Cast to the given type, checking that the cast is valid. If it is not, we return Nothing. Usage:

maybeWidget <- castTo Widget label

unsafeCastTo :: forall o o'. (HasCallStack, GObject o, GObject o') => (ForeignPtr o' -> o') -> o -> IO o' Source #

Cast to the given type, assuming that the cast will succeed. This function will call error if the cast is illegal.

# Wrappers

newObject :: (GObject a, GObject b) => (ForeignPtr a -> a) -> Ptr b -> IO a Source #

Construct a Haskell wrapper for a GObject, increasing its reference count.

wrapObject :: forall a b. (GObject a, GObject b) => (ForeignPtr a -> a) -> Ptr b -> IO a Source #

Same as newObject, but we take ownership of the object. Newly created GObjects are typically floating, so we use g_object_ref_sink.

refObject :: (GObject a, GObject b) => a -> IO (Ptr b) Source #

Increase the reference count of the given GObject.

unrefObject :: GObject a => a -> IO () Source #

Decrease the reference count of the given GObject. The memory associated with the object may be released if the reference count reaches 0.

newBoxed :: forall a. BoxedObject a => (ForeignPtr a -> a) -> Ptr a -> IO a Source #

Construct a Haskell wrapper for the given boxed object. We make a copy of the object.

wrapBoxed :: forall a. BoxedObject a => (ForeignPtr a -> a) -> Ptr a -> IO a Source #

Like newBoxed, but we do not make a copy (we "steal" the passed object, so now it is managed by the Haskell runtime).

copyBoxed :: forall a. BoxedObject a => a -> IO (Ptr a) Source #

Make a copy of the given boxed object.

copyBoxedPtr :: forall a. BoxedObject a => Ptr a -> IO (Ptr a) Source #

Like copyBoxed, but acting directly on a pointer, instead of a managed pointer.

freeBoxed :: forall a. BoxedObject a => a -> IO () Source #

Free the memory associated with a boxed object

wrapPtr :: WrappedPtr a => (ForeignPtr a -> a) -> Ptr a -> IO a Source #

Wrap a pointer, taking ownership of it.

newPtr :: WrappedPtr a => (ForeignPtr a -> a) -> Ptr a -> IO a Source #

Wrap a pointer, making a copy of the data.

copyPtr :: WrappedPtr a => Int -> Ptr a -> IO (Ptr a) Source #

Make a copy of a wrapped pointer using memcpy into a freshly allocated memory region of the given size.