mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-05-06 14:06:52 +02:00
Improvements from Owen's feedback.
* gobject/tmpl/gclosure.sgml: Improvements from Owen's feedback.
This commit is contained in:
parent
9d641fd17a
commit
4dd7b60fb9
@ -1,3 +1,7 @@
|
|||||||
|
Wed Oct 15 00:56:30 2003 Matthias Clasen <maclas@gmx.de>
|
||||||
|
|
||||||
|
* gobject/tmpl/gclosure.sgml: Improvements from Owen's feedback.
|
||||||
|
|
||||||
Tue Oct 14 02:35:45 2003 Matthias Clasen <maclas@gmx.de>
|
Tue Oct 14 02:35:45 2003 Matthias Clasen <maclas@gmx.de>
|
||||||
|
|
||||||
* gobject/gobject-sections.txt: Move the g_cclosure_marshal_*
|
* gobject/gobject-sections.txt: Move the g_cclosure_marshal_*
|
||||||
|
@ -21,6 +21,40 @@ convert between #GValue<!-- -->s and suitable representations in the runtime
|
|||||||
of the language in order to use functions written in that languages as
|
of the language in order to use functions written in that languages as
|
||||||
callbacks.
|
callbacks.
|
||||||
</para>
|
</para>
|
||||||
|
<para>
|
||||||
|
Within GObject, closures play an important role in the implementation of
|
||||||
|
signals. When a signal is registered, the @c_marshaller argument to
|
||||||
|
g_signal_new() specifies the default C marshaller for any closure which is
|
||||||
|
connected to this signal. GObject provides a number of C marshallers
|
||||||
|
for this purpose, see the g_cclosure_marshal_*() functions. Additional
|
||||||
|
C marshallers can be generated with the <link linkend="glib-genmarshal"
|
||||||
|
>glib-genmarshal</link> utility.
|
||||||
|
Closures can be explicitly connected to signals with
|
||||||
|
g_signal_connect_closure(), but it usually more convenient to let GObject
|
||||||
|
create a closure automatically by using one of the g_signal_connect_*()
|
||||||
|
functions which take a callback function/user data pair.
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Using closures has a number of important advantages over a simple
|
||||||
|
callback function/data pointer combination:
|
||||||
|
<itemizedlist>
|
||||||
|
<listitem><para>
|
||||||
|
Closures allow the callee to get the types of the callback parameters,
|
||||||
|
which means that language bindings don't have to write individual glue
|
||||||
|
for each callback type.
|
||||||
|
</para></listitem>
|
||||||
|
<listitem><para>
|
||||||
|
The reference counting of #GClosure makes it easy to handle reentrancy
|
||||||
|
right; if a callback is removed while it is being invoked, the closure
|
||||||
|
and it's parameters won't be freed until the invocation finishes.
|
||||||
|
</para></listitem>
|
||||||
|
<listitem><para>
|
||||||
|
g_closure_invalidate() and invalidation notifiers allow callbacks to be
|
||||||
|
automatically removed when the objects they point to go away.
|
||||||
|
</para></listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### SECTION See_Also ##### -->
|
<!-- ##### SECTION See_Also ##### -->
|
||||||
<para>
|
<para>
|
||||||
@ -130,34 +164,34 @@ on closures.
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_cclosure_new ##### -->
|
<!-- ##### FUNCTION g_cclosure_new ##### -->
|
||||||
<para>
|
<para>
|
||||||
Creates a new closure which invokes @callback_func with @user_data as last
|
Creates a new closure which invokes @callback_func with @user_data as
|
||||||
parameter.
|
the last parameter.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@callback_func: the function to invoke
|
@callback_func: the function to invoke
|
||||||
@user_data: user data to pass to @callback_func
|
@user_data: user data to pass to @callback_func
|
||||||
@destroy_data: destroy notify to be called when @user_data is destroyed
|
@destroy_data: destroy notify to be called when @user_data is no longer used
|
||||||
@Returns: a new #GCClosure
|
@Returns: a new #GCClosure
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_cclosure_new_swap ##### -->
|
<!-- ##### FUNCTION g_cclosure_new_swap ##### -->
|
||||||
<para>
|
<para>
|
||||||
Creates a new closure which invokes @callback_func with @user_data as first
|
Creates a new closure which invokes @callback_func with @user_data as
|
||||||
parameter.
|
the first parameter.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@callback_func: the function to invoke
|
@callback_func: the function to invoke
|
||||||
@user_data: user data to pass to @callback_func
|
@user_data: user data to pass to @callback_func
|
||||||
@destroy_data: destroy notify to be called when @user_data is destroyed
|
@destroy_data: destroy notify to be called when @user_data is no longer used
|
||||||
@Returns: a new #GCClosure
|
@Returns: a new #GCClosure
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_cclosure_new_object ##### -->
|
<!-- ##### FUNCTION g_cclosure_new_object ##### -->
|
||||||
<para>
|
<para>
|
||||||
A variant of g_cclosure_new() which uses @object as @user_data
|
A variant of g_cclosure_new() which uses @object as @user_data and calls
|
||||||
and calls g_object_watch_closure() on @object and the
|
g_object_watch_closure() on @object and the created closure. This function
|
||||||
created closure. This function is mainly useful when implementing new types
|
is useful when you have a callback closely associated with a #GObject,
|
||||||
of closures.
|
and want the callback to no longer run after the object is is freed.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@callback_func: the function to invoke
|
@callback_func: the function to invoke
|
||||||
@ -167,10 +201,10 @@ of closures.
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_cclosure_new_object_swap ##### -->
|
<!-- ##### FUNCTION g_cclosure_new_object_swap ##### -->
|
||||||
<para>
|
<para>
|
||||||
A variant of g_cclosure_new_swap() which uses @object as @user_data
|
A variant of g_cclosure_new_swap() which uses @object as @user_data and calls
|
||||||
and calls g_object_watch_closure() on @object and the
|
g_object_watch_closure() on @object and the created closure. This function
|
||||||
created closure. This function is mainly useful when implementing new types
|
is useful when you have a callback closely associated with a #GObject,
|
||||||
of closures.
|
and want the callback to no longer run after the object is is freed.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@callback_func: the function to invoke
|
@callback_func: the function to invoke
|
||||||
@ -206,10 +240,26 @@ alive while the caller holds a pointer to it.
|
|||||||
<!-- ##### FUNCTION g_closure_sink ##### -->
|
<!-- ##### FUNCTION g_closure_sink ##### -->
|
||||||
<para>
|
<para>
|
||||||
Takes over the initial ownership of a closure.
|
Takes over the initial ownership of a closure.
|
||||||
When closures are newly created, they get an initial reference count
|
Each closure is initially created in a<firstterm>floating</firstterm> state,
|
||||||
of 1, eventhough no caller has yet invoked g_closure_ref() on the @closure.
|
which means that the initial reference count is not owned by any caller.
|
||||||
Code entities that store closures for notification purposes are supposed
|
g_closure_sink() checks to see if the object is still floating, and if so,
|
||||||
to call this function, for example like this:
|
unsets the floating state and decreases the reference count. If the closure
|
||||||
|
is not floating, g_closure_sink() does nothing. The reason for the existance
|
||||||
|
of the floating state is to prevent cumbersome code sequences like:
|
||||||
|
<programlisting>
|
||||||
|
closure = g_cclosure_new (cb_func, cb_data);
|
||||||
|
g_source_set_closure (source, closure);
|
||||||
|
g_closure_unref (closure); /* XXX GObject doesn't really need this */
|
||||||
|
</programlisting>
|
||||||
|
Because g_source_set_closure() (and similar functions) take ownership of the
|
||||||
|
initial reference count, if it is unowned, we instead can write:
|
||||||
|
<programlisting>
|
||||||
|
g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
|
||||||
|
</programlisting>
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Generally, this function is used together with g_closure_ref(). Ane example
|
||||||
|
of storing a closure for later notification looks like:
|
||||||
<informalexample><programlisting>
|
<informalexample><programlisting>
|
||||||
static GClosure *notify_closure = NULL;
|
static GClosure *notify_closure = NULL;
|
||||||
void
|
void
|
||||||
@ -225,6 +275,8 @@ foo_notify_set_closure (GClosure *closure)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
</programlisting></informalexample>
|
</programlisting></informalexample>
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
Because g_closure_sink() may decrement the reference count of a closure
|
Because g_closure_sink() may decrement the reference count of a closure
|
||||||
(if it hasn't been called on @closure yet) just like g_closure_unref(),
|
(if it hasn't been called on @closure yet) just like g_closure_unref(),
|
||||||
g_closure_ref() should be called prior to this function.
|
g_closure_ref() should be called prior to this function.
|
||||||
@ -236,10 +288,9 @@ g_closure_ref() should be called prior to this function.
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_closure_unref ##### -->
|
<!-- ##### FUNCTION g_closure_unref ##### -->
|
||||||
<para>
|
<para>
|
||||||
Decrements the reference count of a closure after it was
|
Decrements the reference count of a closure after it was previously
|
||||||
previously incremented by the same caller. The closure
|
incremented by the same caller. If no other callers are using the closure,
|
||||||
will most likely be destroyed and freed after this function
|
then the closure will be destroyed and freed.
|
||||||
returns.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: #GClosure to decrement the reference count on
|
@closure: #GClosure to decrement the reference count on
|
||||||
@ -247,7 +298,7 @@ returns.
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_closure_invoke ##### -->
|
<!-- ##### FUNCTION g_closure_invoke ##### -->
|
||||||
<para>
|
<para>
|
||||||
Invokes the closure.
|
Invokes the closure, i.e. executes the callback represented by the @closure.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: a #GClosure
|
@closure: a #GClosure
|
||||||
@ -263,11 +314,16 @@ Invokes the closure.
|
|||||||
<para>
|
<para>
|
||||||
Sets a flag on the closure to indicate that it's calling environment has
|
Sets a flag on the closure to indicate that it's calling environment has
|
||||||
become invalid, and thus causes any future invocations of g_closure_invoke()
|
become invalid, and thus causes any future invocations of g_closure_invoke()
|
||||||
on this @closure to be ignored.
|
on this @closure to be ignored. Also, invalidation notifiers installed on
|
||||||
Also, invalidation notifiers installed on the closure will be called
|
the closure will be called at this point. Note that unless you are holding
|
||||||
at this point, and since invalidation notifiers may unreference
|
a reference to the closure yourself, the invalidation notifiers may unref
|
||||||
the closure, @closure should be considered an invalidated pointer
|
the closure and cause it to be destroyed, so if you need to access the
|
||||||
after this function, unless g_closure_ref() was called beforehand.
|
closure after calling g_closure_invalidate(), make sure that you've
|
||||||
|
previously called g_closure_ref().
|
||||||
|
</para>
|
||||||
|
<para>
|
||||||
|
Note that g_closure_invalidate() will also be called when the reference count
|
||||||
|
of a closure drops to zero (unless it has already been invalidated before).
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: GClosure to invalidate
|
@closure: GClosure to invalidate
|
||||||
@ -276,8 +332,11 @@ after this function, unless g_closure_ref() was called beforehand.
|
|||||||
<!-- ##### FUNCTION g_closure_add_finalize_notifier ##### -->
|
<!-- ##### FUNCTION g_closure_add_finalize_notifier ##### -->
|
||||||
<para>
|
<para>
|
||||||
Registers a finalization notifier which will be called when the reference
|
Registers a finalization notifier which will be called when the reference
|
||||||
count of @closure goes down to 0. Finalization notifiers are invoked after
|
count of @closure goes down to 0. Multiple finalization notifiers on a
|
||||||
invalidation notifiers, in an unspecified order.
|
single closure are invoked in unspecified order. If a single call to
|
||||||
|
g_closure_unref() results in the closure being both invalidated and
|
||||||
|
finalized, then the invalidate notifiers will be run before the finalize
|
||||||
|
notifiers.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: a #GClosure
|
@closure: a #GClosure
|
||||||
@ -299,8 +358,8 @@ invoked before finalization notifiers, in an unspecified order.
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_closure_remove_finalize_notifier ##### -->
|
<!-- ##### FUNCTION g_closure_remove_finalize_notifier ##### -->
|
||||||
<para>
|
<para>
|
||||||
Removes a finalization notifier. Notifiers may only be removed before or
|
Removes a finalization notifier. Notifiers are automatically removed after
|
||||||
during their invocation.
|
they are run.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: a #GClosure
|
@closure: a #GClosure
|
||||||
@ -311,8 +370,8 @@ during their invocation.
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_closure_remove_invalidate_notifier ##### -->
|
<!-- ##### FUNCTION g_closure_remove_invalidate_notifier ##### -->
|
||||||
<para>
|
<para>
|
||||||
Removes a invalidation notifier. Notifiers may only be removed before or
|
Removes a invalidation notifier. Notifiers are automatically removed after
|
||||||
during their invocation.
|
they are run.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: a #GClosure
|
@closure: a #GClosure
|
||||||
@ -371,10 +430,11 @@ MyClosure *my_closure_new (gpointer data)
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_closure_set_marshal ##### -->
|
<!-- ##### FUNCTION g_closure_set_marshal ##### -->
|
||||||
<para>
|
<para>
|
||||||
Sets the marshaller of @closure. A marshaller set with g_closure_set_marshal()
|
Sets the marshaller of @closure. The @marshal_data provides a way for a
|
||||||
should interpret its @marshal_data argument as a callback function to be
|
meta marshaller to provide additional information to the marshaller.
|
||||||
invoked instead of @closure->callback, provided it is not %NULL. GObject provides a number of predefined marshallers for use with #GCClosure<!-- -->s,
|
(See g_closure_set_meta_marshal().) For GObject's C predefined marshallers
|
||||||
see the g_cclosure_marshal_*() functions.
|
(the g_cclosure_marshal_*() functions), what it provides is a callback
|
||||||
|
function to use instead of @closure->callback.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: a #GClosure
|
@closure: a #GClosure
|
||||||
@ -384,8 +444,9 @@ see the g_cclosure_marshal_*() functions.
|
|||||||
<!-- ##### FUNCTION g_closure_add_marshal_guards ##### -->
|
<!-- ##### FUNCTION g_closure_add_marshal_guards ##### -->
|
||||||
<para>
|
<para>
|
||||||
Adds a pair of notifiers which get invoked before and after the closure
|
Adds a pair of notifiers which get invoked before and after the closure
|
||||||
callback, respectively. See g_object_watch_closure() for a use of marshal
|
callback, respectively. This is typically used to protect the extra arguments
|
||||||
guards.
|
for the duration of the callback. See g_object_watch_closure() for an
|
||||||
|
example of marshal guards.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: a #GClosure
|
@closure: a #GClosure
|
||||||
@ -397,17 +458,20 @@ guards.
|
|||||||
|
|
||||||
<!-- ##### FUNCTION g_closure_set_meta_marshal ##### -->
|
<!-- ##### FUNCTION g_closure_set_meta_marshal ##### -->
|
||||||
<para>
|
<para>
|
||||||
Sets the meta marshaller of @closure. A meta marshaller
|
Sets the meta marshaller of @closure.
|
||||||
should use its @marshal_data argument together with @closure->data
|
A meta marshaller wraps @closure->marshal and modifies the way it is called
|
||||||
to determine the callback function to use, and pass it as @marshal_data
|
in some fashion. The most common use of this facility is for C callbacks.
|
||||||
to @closure->marshal.
|
The same marshallers (generated by
|
||||||
|
<link linkend="glib-genmarshal">glib-genmarshal</link>) are used everywhere,
|
||||||
|
but the way that we get the callback function differs. In most cases we want
|
||||||
|
to use @closure->callback, but in other cases we want to use use some
|
||||||
|
different technique to retrieve the callbakc function.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
As an example for the use of a meta marshaller, consider a closure whose
|
For example, class closures for signals (see g_signal_type_cclosure_new())
|
||||||
@data member contains a pointer to a class structure. The meta marshaller
|
retrieve the callback function from a fixed offset in the class structure.
|
||||||
could then be given an offset into this struct in @marshal_data, and use
|
The meta marshaller retrieves the right callback and passes it to the
|
||||||
it to determine the class member function to use as callback. This is
|
marshaller as the @marshal_data argument.
|
||||||
how class closures for signals are actually implemented in GObject.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@closure: a #GClosure
|
@closure: a #GClosure
|
||||||
|
Loading…
x
Reference in New Issue
Block a user