1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
3 * Copyright (C) 2005 Imendio AB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * MT safe with regards to reference counting.
31 #include "gobjectalias.h"
37 * @Short_description: Functions as first-class objects
41 * A #GClosure represents a callback supplied by the programmer. It
42 * will generally comprise a function of some kind and a marshaller
43 * used to call it. It is the reponsibility of the marshaller to
44 * convert the arguments for the invocation from #GValue<!-- -->s into
45 * a suitable form, perform the callback on the converted arguments,
46 * and transform the return value back into a #GValue.
48 * In the case of C programs, a closure usually just holds a pointer
49 * to a function and maybe a data argument, and the marshaller
50 * converts between #GValue<!-- --> and native C types. The GObject
51 * library provides the #GCClosure type for this purpose. Bindings for
52 * other languages need marshallers which convert between #GValue<!--
53 * -->s and suitable representations in the runtime of the language in
54 * order to use functions written in that languages as callbacks.
56 * Within GObject, closures play an important role in the
57 * implementation of signals. When a signal is registered, the
58 * @c_marshaller argument to g_signal_new() specifies the default C
59 * marshaller for any closure which is connected to this
60 * signal. GObject provides a number of C marshallers for this
61 * purpose, see the g_cclosure_marshal_*() functions. Additional C
62 * marshallers can be generated with the <link
63 * linkend="glib-genmarshal">glib-genmarshal</link> utility. Closures
64 * can be explicitly connected to signals with
65 * g_signal_connect_closure(), but it usually more convenient to let
66 * GObject create a closure automatically by using one of the
67 * g_signal_connect_*() functions which take a callback function/user
70 * Using closures has a number of important advantages over a simple
71 * callback function/data pointer combination:
74 * Closures allow the callee to get the types of the callback parameters,
75 * which means that language bindings don't have to write individual glue
76 * for each callback type.
79 * The reference counting of #GClosure makes it easy to handle reentrancy
80 * right; if a callback is removed while it is being invoked, the closure
81 * and it's parameters won't be freed until the invocation finishes.
84 * g_closure_invalidate() and invalidation notifiers allow callbacks to be
85 * automatically removed when the objects they point to go away.
91 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
92 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
93 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
94 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
95 #define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \
96 ((cl)->n_guards << 1L))
97 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
98 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
99 (cl)->n_fnotifiers + \
107 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
109 ClosureInt *cunion = (ClosureInt*) _closure; \
110 gint new_int, old_int, success; \
114 tmp.vint = old_int = cunion->vint; \
115 _SET_OLD tmp.closure._field; \
116 tmp.closure._field _OP _value; \
117 _SET_NEW tmp.closure._field; \
118 new_int = tmp.vint; \
119 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
121 while (!success && _must_set); \
124 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
125 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
126 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
127 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
128 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
129 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
131 #if 0 /* for non-thread-safe closures */
132 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
133 #define SET(cl,f,v) (void) (cl->f = v)
134 #define INC(cl,f) (void) (cl->f += 1)
135 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
136 #define DEC(cl,f) (void) (cl->f -= 1)
137 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
148 /* --- functions --- */
150 * g_closure_new_simple:
151 * @sizeof_closure: the size of the structure to allocate, must be at least
152 * <literal>sizeof (GClosure)</literal>
153 * @data: data to store in the @data field of the newly allocated #GClosure
155 * Allocates a struct of the given size and initializes the initial
156 * part as a #GClosure. This function is mainly useful when
157 * implementing new types of closures.
160 * typedef struct _MyClosure MyClosure;
164 * // extra data goes here
168 * my_closure_finalize (gpointer notify_data,
171 * MyClosure *my_closure = (MyClosure *)closure;
173 * // free extra data here
176 * MyClosure *my_closure_new (gpointer data)
179 * MyClosure *my_closure;
181 * closure = g_closure_new_simple (sizeof (MyClosure), data);
182 * my_closure = (MyClosure *) closure;
184 * // initialize extra data here
186 * g_closure_add_finalize_notifier (closure, notify_data,
187 * my_closure_finalize);
192 * Returns: a newly allocated #GClosure
195 g_closure_new_simple (guint sizeof_closure,
200 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
202 closure = g_malloc0 (sizeof_closure);
203 SET (closure, ref_count, 1);
204 SET (closure, meta_marshal, 0);
205 SET (closure, n_guards, 0);
206 SET (closure, n_fnotifiers, 0);
207 SET (closure, n_inotifiers, 0);
208 SET (closure, in_inotify, FALSE);
209 SET (closure, floating, TRUE);
210 SET (closure, derivative_flag, 0);
211 SET (closure, in_marshal, FALSE);
212 SET (closure, is_invalid, FALSE);
213 closure->marshal = NULL;
214 closure->data = data;
215 closure->notifiers = NULL;
216 memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
222 closure_invoke_notifiers (GClosure *closure,
226 * meta_marshal n_guards n_guards n_fnotif. n_inotifiers
227 * ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]]
229 * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards;
230 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
232 * constrains/catches:
233 * - closure->notifiers may be reloacted during callback
234 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
235 * - i.e. callbacks can be removed/added during invocation
236 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
237 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
238 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
239 * + closure->meta_marshal is const for all cases
240 * + none of the callbacks can cause recursion
241 * + closure->n_inotifiers is const 0 during FNOTIFY
245 GClosureNotifyData *ndata;
248 while (closure->n_fnotifiers)
251 DEC_ASSIGN (closure, n_fnotifiers, &n);
253 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
254 closure->marshal = (GClosureMarshal) ndata->notify;
255 closure->data = ndata->data;
256 ndata->notify (ndata->data, closure);
258 closure->marshal = NULL;
259 closure->data = NULL;
262 SET (closure, in_inotify, TRUE);
263 while (closure->n_inotifiers)
266 DEC_ASSIGN (closure, n_inotifiers, &n);
268 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
269 closure->marshal = (GClosureMarshal) ndata->notify;
270 closure->data = ndata->data;
271 ndata->notify (ndata->data, closure);
273 closure->marshal = NULL;
274 closure->data = NULL;
275 SET (closure, in_inotify, FALSE);
278 i = closure->n_guards;
279 offs = closure->meta_marshal;
282 ndata = closure->notifiers + offs + i;
283 ndata->notify (ndata->data, closure);
287 i = closure->n_guards;
288 offs = closure->meta_marshal + i;
291 ndata = closure->notifiers + offs + i;
292 ndata->notify (ndata->data, closure);
299 * g_closure_set_meta_marshal:
300 * @closure: a #GClosure
301 * @marshal_data: context-dependent data to pass to @meta_marshal
302 * @meta_marshal: a #GClosureMarshal function
304 * Sets the meta marshaller of @closure. A meta marshaller wraps
305 * @closure->marshal and modifies the way it is called in some
306 * fashion. The most common use of this facility is for C callbacks.
307 * The same marshallers (generated by <link
308 * linkend="glib-genmarshal">glib-genmarshal</link>) are used
309 * everywhere, but the way that we get the callback function
310 * differs. In most cases we want to use @closure->callback, but in
311 * other cases we want to use some different technique to retrieve the
314 * For example, class closures for signals (see
315 * g_signal_type_cclosure_new()) retrieve the callback function from a
316 * fixed offset in the class structure. The meta marshaller retrieves
317 * the right callback and passes it to the marshaller as the
318 * @marshal_data argument.
321 g_closure_set_meta_marshal (GClosure *closure,
322 gpointer marshal_data,
323 GClosureMarshal meta_marshal)
325 GClosureNotifyData *notifiers;
327 g_return_if_fail (closure != NULL);
328 g_return_if_fail (meta_marshal != NULL);
329 g_return_if_fail (closure->is_invalid == FALSE);
330 g_return_if_fail (closure->in_marshal == FALSE);
331 g_return_if_fail (closure->meta_marshal == 0);
333 notifiers = closure->notifiers;
334 closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
337 /* usually the meta marshal will be setup right after creation, so the
338 * g_memmove() should be rare-case scenario
340 g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
343 closure->notifiers[0].data = marshal_data;
344 closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
345 SET (closure, meta_marshal, 1);
349 * g_closure_add_marshal_guards:
350 * @closure: a #GClosure
351 * @pre_marshal_data: data to pass to @pre_marshal_notify
352 * @pre_marshal_notify: a function to call before the closure callback
353 * @post_marshal_data: data to pass to @post_marshal_notify
354 * @post_marshal_notify: a function to call after the closure callback
356 * Adds a pair of notifiers which get invoked before and after the
357 * closure callback, respectively. This is typically used to protect
358 * the extra arguments for the duration of the callback. See
359 * g_object_watch_closure() for an example of marshal guards.
362 g_closure_add_marshal_guards (GClosure *closure,
363 gpointer pre_marshal_data,
364 GClosureNotify pre_marshal_notify,
365 gpointer post_marshal_data,
366 GClosureNotify post_marshal_notify)
370 g_return_if_fail (closure != NULL);
371 g_return_if_fail (pre_marshal_notify != NULL);
372 g_return_if_fail (post_marshal_notify != NULL);
373 g_return_if_fail (closure->is_invalid == FALSE);
374 g_return_if_fail (closure->in_marshal == FALSE);
375 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
377 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
378 if (closure->n_inotifiers)
379 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
380 closure->n_fnotifiers +
381 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
382 closure->n_fnotifiers + 0)];
383 if (closure->n_inotifiers > 1)
384 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
385 closure->n_fnotifiers +
386 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
387 closure->n_fnotifiers + 1)];
388 if (closure->n_fnotifiers)
389 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
390 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
391 if (closure->n_fnotifiers > 1)
392 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
393 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
394 if (closure->n_guards)
395 closure->notifiers[(closure->meta_marshal +
397 closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
398 i = closure->n_guards;
399 closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
400 closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
401 closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
402 closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
403 INC (closure, n_guards);
407 * g_closure_add_finalize_notifier:
408 * @closure: a #GClosure
409 * @notify_data: data to pass to @notify_func
410 * @notify_func: the callback function to register
412 * Registers a finalization notifier which will be called when the
413 * reference count of @closure goes down to 0. Multiple finalization
414 * notifiers on a single closure are invoked in unspecified order. If
415 * a single call to g_closure_unref() results in the closure being
416 * both invalidated and finalized, then the invalidate notifiers will
417 * be run before the finalize notifiers.
420 g_closure_add_finalize_notifier (GClosure *closure,
421 gpointer notify_data,
422 GClosureNotify notify_func)
426 g_return_if_fail (closure != NULL);
427 g_return_if_fail (notify_func != NULL);
428 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
430 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
431 if (closure->n_inotifiers)
432 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
433 closure->n_fnotifiers +
434 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
435 closure->n_fnotifiers + 0)];
436 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
437 closure->notifiers[i].data = notify_data;
438 closure->notifiers[i].notify = notify_func;
439 INC (closure, n_fnotifiers);
443 * g_closure_add_invalidate_notifier:
444 * @closure: a #GClosure
445 * @notify_data: data to pass to @notify_func
446 * @notify_func: the callback function to register
448 * Registers an invalidation notifier which will be called when the
449 * @closure is invalidated with g_closure_invalidate(). Invalidation
450 * notifiers are invoked before finalization notifiers, in an
454 g_closure_add_invalidate_notifier (GClosure *closure,
455 gpointer notify_data,
456 GClosureNotify notify_func)
460 g_return_if_fail (closure != NULL);
461 g_return_if_fail (notify_func != NULL);
462 g_return_if_fail (closure->is_invalid == FALSE);
463 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
465 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
466 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
467 closure->notifiers[i].data = notify_data;
468 closure->notifiers[i].notify = notify_func;
469 INC (closure, n_inotifiers);
472 static inline gboolean
473 closure_try_remove_inotify (GClosure *closure,
474 gpointer notify_data,
475 GClosureNotify notify_func)
477 GClosureNotifyData *ndata, *nlast;
479 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
480 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
481 if (ndata->notify == notify_func && ndata->data == notify_data)
483 DEC (closure, n_inotifiers);
492 static inline gboolean
493 closure_try_remove_fnotify (GClosure *closure,
494 gpointer notify_data,
495 GClosureNotify notify_func)
497 GClosureNotifyData *ndata, *nlast;
499 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
500 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
501 if (ndata->notify == notify_func && ndata->data == notify_data)
503 DEC (closure, n_fnotifiers);
506 if (closure->n_inotifiers)
507 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
508 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
509 closure->n_fnotifiers +
510 closure->n_inotifiers)];
518 * @closure: #GClosure to increment the reference count on
520 * Increments the reference count on a closure to force it staying
521 * alive while the caller holds a pointer to it.
523 * Returns: The @closure passed in, for convenience
526 g_closure_ref (GClosure *closure)
529 g_return_val_if_fail (closure != NULL, NULL);
530 g_return_val_if_fail (closure->ref_count > 0, NULL);
531 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
533 INC_ASSIGN (closure, ref_count, &new_ref_count);
534 g_return_val_if_fail (new_ref_count > 1, NULL);
540 * g_closure_invalidate:
541 * @closure: GClosure to invalidate
543 * Sets a flag on the closure to indicate that it's calling
544 * environment has become invalid, and thus causes any future
545 * invocations of g_closure_invoke() on this @closure to be
546 * ignored. Also, invalidation notifiers installed on the closure will
547 * be called at this point. Note that unless you are holding a
548 * reference to the closure yourself, the invalidation notifiers may
549 * unref the closure and cause it to be destroyed, so if you need to
550 * access the closure after calling g_closure_invalidate(), make sure
551 * that you've previously called g_closure_ref().
553 * Note that g_closure_invalidate() will also be called when the
554 * reference count of a closure drops to zero (unless it has already
555 * been invalidated before).
558 g_closure_invalidate (GClosure *closure)
560 g_return_if_fail (closure != NULL);
562 if (!closure->is_invalid)
564 gboolean was_invalid;
565 g_closure_ref (closure); /* preserve floating flag */
566 SWAP (closure, is_invalid, TRUE, &was_invalid);
567 /* invalidate only once */
569 closure_invoke_notifiers (closure, INOTIFY);
570 g_closure_unref (closure);
576 * @closure: #GClosure to decrement the reference count on
578 * Decrements the reference count of a closure after it was previously
579 * incremented by the same caller. If no other callers are using the
580 * closure, then the closure will be destroyed and freed.
583 g_closure_unref (GClosure *closure)
587 g_return_if_fail (closure != NULL);
588 g_return_if_fail (closure->ref_count > 0);
590 if (closure->ref_count == 1) /* last unref, invalidate first */
591 g_closure_invalidate (closure);
593 DEC_ASSIGN (closure, ref_count, &new_ref_count);
595 if (new_ref_count == 0)
597 closure_invoke_notifiers (closure, FNOTIFY);
598 g_free (closure->notifiers);
605 * @closure: #GClosure to decrement the initial reference count on, if it's
608 * Takes over the initial ownership of a closure. Each closure is
609 * initially created in a <firstterm>floating</firstterm> state, which
610 * means that the initial reference count is not owned by any caller.
611 * g_closure_sink() checks to see if the object is still floating, and
612 * if so, unsets the floating state and decreases the reference
613 * count. If the closure is not floating, g_closure_sink() does
614 * nothing. The reason for the existance of the floating state is to
615 * prevent cumbersome code sequences like:
617 * closure = g_cclosure_new (cb_func, cb_data);
618 * g_source_set_closure (source, closure);
619 * g_closure_unref (closure); // XXX GObject doesn't really need this
621 * Because g_source_set_closure() (and similar functions) take ownership of the
622 * initial reference count, if it is unowned, we instead can write:
624 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
627 * Generally, this function is used together with g_closure_ref(). Ane example
628 * of storing a closure for later notification looks like:
630 * static GClosure *notify_closure = NULL;
632 * foo_notify_set_closure (GClosure *closure)
634 * if (notify_closure)
635 * g_closure_unref (notify_closure);
636 * notify_closure = closure;
637 * if (notify_closure)
639 * g_closure_ref (notify_closure);
640 * g_closure_sink (notify_closure);
645 * Because g_closure_sink() may decrement the reference count of a closure
646 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
647 * g_closure_ref() should be called prior to this function.
650 g_closure_sink (GClosure *closure)
652 g_return_if_fail (closure != NULL);
653 g_return_if_fail (closure->ref_count > 0);
655 /* floating is basically a kludge to avoid creating closures
656 * with a ref_count of 0. so the intial ref_count a closure has
657 * is unowned. with invoking g_closure_sink() code may
658 * indicate that it takes over that intiial ref_count.
660 if (closure->floating)
662 gboolean was_floating;
663 SWAP (closure, floating, FALSE, &was_floating);
664 /* unref floating flag only once */
666 g_closure_unref (closure);
671 * g_closure_remove_invalidate_notifier:
672 * @closure: a #GClosure
673 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
674 * when registering @notify_func
675 * @notify_func: the callback function to remove
677 * Removes an invalidation notifier.
679 * Notice that notifiers are automatically removed after they are run.
682 g_closure_remove_invalidate_notifier (GClosure *closure,
683 gpointer notify_data,
684 GClosureNotify notify_func)
686 g_return_if_fail (closure != NULL);
687 g_return_if_fail (notify_func != NULL);
689 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
690 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
691 closure->data == notify_data)
692 closure->marshal = NULL;
693 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
694 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
695 notify_func, notify_data);
699 * g_closure_remove_finalize_notifier:
700 * @closure: a #GClosure
701 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
702 * when registering @notify_func
703 * @notify_func: the callback function to remove
705 * Removes a finalization notifier.
707 * Notice that notifiers are automatically removed after they are run.
710 g_closure_remove_finalize_notifier (GClosure *closure,
711 gpointer notify_data,
712 GClosureNotify notify_func)
714 g_return_if_fail (closure != NULL);
715 g_return_if_fail (notify_func != NULL);
717 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
718 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
719 closure->data == notify_data)
720 closure->marshal = NULL;
721 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
722 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
723 notify_func, notify_data);
728 * @closure: a #GClosure
729 * @return_value: a #GValue to store the return value. May be %NULL if the
730 * callback of @closure doesn't return a value.
731 * @n_param_values: the length of the @param_values array
732 * @param_values: an array of #GValue<!-- -->s holding the arguments on
733 * which to invoke the callback of @closure
734 * @invocation_hint: a context-dependent invocation hint
736 * Invokes the closure, i.e. executes the callback represented by the @closure.
739 g_closure_invoke (GClosure *closure,
740 GValue /*out*/ *return_value,
741 guint n_param_values,
742 const GValue *param_values,
743 gpointer invocation_hint)
745 g_return_if_fail (closure != NULL);
747 g_closure_ref (closure); /* preserve floating flag */
748 if (!closure->is_invalid)
750 GClosureMarshal marshal;
751 gpointer marshal_data;
752 gboolean in_marshal = closure->in_marshal;
754 g_return_if_fail (closure->marshal || closure->meta_marshal);
756 SET (closure, in_marshal, TRUE);
757 if (closure->meta_marshal)
759 marshal_data = closure->notifiers[0].data;
760 marshal = (GClosureMarshal) closure->notifiers[0].notify;
765 marshal = closure->marshal;
768 closure_invoke_notifiers (closure, PRE_NOTIFY);
771 n_param_values, param_values,
775 closure_invoke_notifiers (closure, POST_NOTIFY);
776 SET (closure, in_marshal, in_marshal);
778 g_closure_unref (closure);
782 * g_closure_set_marshal:
783 * @closure: a #GClosure
784 * @marshal: a #GClosureMarshal function
786 * Sets the marshaller of @closure. The <literal>marshal_data</literal>
787 * of @marshal provides a way for a meta marshaller to provide additional
788 * information to the marshaller. (See g_closure_set_meta_marshal().) For
789 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
790 * functions), what it provides is a callback function to use instead of
791 * @closure->callback.
794 g_closure_set_marshal (GClosure *closure,
795 GClosureMarshal marshal)
797 g_return_if_fail (closure != NULL);
798 g_return_if_fail (marshal != NULL);
800 if (closure->marshal && closure->marshal != marshal)
801 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
802 closure->marshal, marshal);
804 closure->marshal = marshal;
809 * @callback_func: the function to invoke
810 * @user_data: user data to pass to @callback_func
811 * @destroy_data: destroy notify to be called when @user_data is no longer used
813 * Creates a new closure which invokes @callback_func with @user_data as
814 * the last parameter.
816 * Returns: a new #GCClosure
819 g_cclosure_new (GCallback callback_func,
821 GClosureNotify destroy_data)
825 g_return_val_if_fail (callback_func != NULL, NULL);
827 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
829 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
830 ((GCClosure*) closure)->callback = (gpointer) callback_func;
836 * g_cclosure_new_swap:
837 * @callback_func: the function to invoke
838 * @user_data: user data to pass to @callback_func
839 * @destroy_data: destroy notify to be called when @user_data is no longer used
841 * Creates a new closure which invokes @callback_func with @user_data as
842 * the first parameter.
844 * Returns: a new #GCClosure
847 g_cclosure_new_swap (GCallback callback_func,
849 GClosureNotify destroy_data)
853 g_return_val_if_fail (callback_func != NULL, NULL);
855 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
857 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
858 ((GCClosure*) closure)->callback = (gpointer) callback_func;
859 SET (closure, derivative_flag, TRUE);
865 g_type_class_meta_marshal (GClosure *closure,
866 GValue /*out*/ *return_value,
867 guint n_param_values,
868 const GValue *param_values,
869 gpointer invocation_hint,
870 gpointer marshal_data)
874 /* GType itype = (GType) closure->data; */
875 guint offset = GPOINTER_TO_UINT (marshal_data);
877 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
878 callback = G_STRUCT_MEMBER (gpointer, class, offset);
880 closure->marshal (closure,
882 n_param_values, param_values,
888 g_type_iface_meta_marshal (GClosure *closure,
889 GValue /*out*/ *return_value,
890 guint n_param_values,
891 const GValue *param_values,
892 gpointer invocation_hint,
893 gpointer marshal_data)
897 GType itype = (GType) closure->data;
898 guint offset = GPOINTER_TO_UINT (marshal_data);
900 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
901 callback = G_STRUCT_MEMBER (gpointer, class, offset);
903 closure->marshal (closure,
905 n_param_values, param_values,
911 * g_signal_type_cclosure_new:
912 * @itype: the #GType identifier of an interface or classed type
913 * @struct_offset: the offset of the member function of @itype's class
914 * structure which is to be invoked by the new closure
916 * Creates a new closure which invokes the function found at the offset
917 * @struct_offset in the class structure of the interface or classed type
918 * identified by @itype.
920 * Returns: a new #GCClosure
923 g_signal_type_cclosure_new (GType itype,
928 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
929 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
931 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
932 if (G_TYPE_IS_INTERFACE (itype))
933 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
935 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
942 * g_cclosure_marshal_VOID__VOID:
943 * @closure: the #GClosure to which the marshaller belongs
944 * @return_value: ignored
946 * @param_values: a #GValue array holding only the instance
947 * @invocation_hint: the invocation hint given as the last argument
948 * to g_closure_invoke()
949 * @marshal_data: additional data specified when registering the marshaller
951 * A marshaller for a #GCClosure with a callback of type
952 * <literal>void (*callback) (gpointer instance, gpointer user_data)</literal>.
956 * g_cclosure_marshal_VOID__BOOLEAN:
957 * @closure: the #GClosure to which the marshaller belongs
958 * @return_value: ignored
960 * @param_values: a #GValue array holding the instance and the #gboolean parameter
961 * @invocation_hint: the invocation hint given as the last argument
962 * to g_closure_invoke()
963 * @marshal_data: additional data specified when registering the marshaller
965 * A marshaller for a #GCClosure with a callback of type
966 * <literal>void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)</literal>.
970 * g_cclosure_marshal_VOID__CHAR:
971 * @closure: the #GClosure to which the marshaller belongs
972 * @return_value: ignored
974 * @param_values: a #GValue array holding the instance and the #gchar parameter
975 * @invocation_hint: the invocation hint given as the last argument
976 * to g_closure_invoke()
977 * @marshal_data: additional data specified when registering the marshaller
979 * A marshaller for a #GCClosure with a callback of type
980 * <literal>void (*callback) (gpointer instance, gchar arg1, gpointer user_data)</literal>.
984 * g_cclosure_marshal_VOID__UCHAR:
985 * @closure: the #GClosure to which the marshaller belongs
986 * @return_value: ignored
988 * @param_values: a #GValue array holding the instance and the #guchar parameter
989 * @invocation_hint: the invocation hint given as the last argument
990 * to g_closure_invoke()
991 * @marshal_data: additional data specified when registering the marshaller
993 * A marshaller for a #GCClosure with a callback of type
994 * <literal>void (*callback) (gpointer instance, guchar arg1, gpointer user_data)</literal>.
998 * g_cclosure_marshal_VOID__INT:
999 * @closure: the #GClosure to which the marshaller belongs
1000 * @return_value: ignored
1001 * @n_param_values: 2
1002 * @param_values: a #GValue array holding the instance and the #gint parameter
1003 * @invocation_hint: the invocation hint given as the last argument
1004 * to g_closure_invoke()
1005 * @marshal_data: additional data specified when registering the marshaller
1007 * A marshaller for a #GCClosure with a callback of type
1008 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal>.
1012 * g_cclosure_marshal_VOID__UINT:
1013 * @closure: the #GClosure to which the marshaller belongs
1014 * @return_value: ignored
1015 * @n_param_values: 2
1016 * @param_values: a #GValue array holding the instance and the #guint parameter
1017 * @invocation_hint: the invocation hint given as the last argument
1018 * to g_closure_invoke()
1019 * @marshal_data: additional data specified when registering the marshaller
1021 * A marshaller for a #GCClosure with a callback of type
1022 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer user_data)</literal>.
1026 * g_cclosure_marshal_VOID__LONG:
1027 * @closure: the #GClosure to which the marshaller belongs
1028 * @return_value: ignored
1029 * @n_param_values: 2
1030 * @param_values: a #GValue array holding the instance and the #glong parameter
1031 * @invocation_hint: the invocation hint given as the last argument
1032 * to g_closure_invoke()
1033 * @marshal_data: additional data specified when registering the marshaller
1035 * A marshaller for a #GCClosure with a callback of type
1036 * <literal>void (*callback) (gpointer instance, glong arg1, gpointer user_data)</literal>.
1040 * g_cclosure_marshal_VOID__ULONG:
1041 * @closure: the #GClosure to which the marshaller belongs
1042 * @return_value: ignored
1043 * @n_param_values: 2
1044 * @param_values: a #GValue array holding the instance and the #gulong parameter
1045 * @invocation_hint: the invocation hint given as the last argument
1046 * to g_closure_invoke()
1047 * @marshal_data: additional data specified when registering the marshaller
1049 * A marshaller for a #GCClosure with a callback of type
1050 * <literal>void (*callback) (gpointer instance, gulong arg1, gpointer user_data)</literal>.
1054 * g_cclosure_marshal_VOID__ENUM:
1055 * @closure: the #GClosure to which the marshaller belongs
1056 * @return_value: ignored
1057 * @n_param_values: 2
1058 * @param_values: a #GValue array holding the instance and the enumeration parameter
1059 * @invocation_hint: the invocation hint given as the last argument
1060 * to g_closure_invoke()
1061 * @marshal_data: additional data specified when registering the marshaller
1063 * A marshaller for a #GCClosure with a callback of type
1064 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes an enumeration type..
1068 * g_cclosure_marshal_VOID__FLAGS:
1069 * @closure: the #GClosure to which the marshaller belongs
1070 * @return_value: ignored
1071 * @n_param_values: 2
1072 * @param_values: a #GValue array holding the instance and the flags parameter
1073 * @invocation_hint: the invocation hint given as the last argument
1074 * to g_closure_invoke()
1075 * @marshal_data: additional data specified when registering the marshaller
1077 * A marshaller for a #GCClosure with a callback of type
1078 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes a flags type.
1082 * g_cclosure_marshal_VOID__FLOAT:
1083 * @closure: the #GClosure to which the marshaller belongs
1084 * @return_value: ignored
1085 * @n_param_values: 2
1086 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1087 * @invocation_hint: the invocation hint given as the last argument
1088 * to g_closure_invoke()
1089 * @marshal_data: additional data specified when registering the marshaller
1091 * A marshaller for a #GCClosure with a callback of type
1092 * <literal>void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)</literal>.
1096 * g_cclosure_marshal_VOID__DOUBLE:
1097 * @closure: the #GClosure to which the marshaller belongs
1098 * @return_value: ignored
1099 * @n_param_values: 2
1100 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1101 * @invocation_hint: the invocation hint given as the last argument
1102 * to g_closure_invoke()
1103 * @marshal_data: additional data specified when registering the marshaller
1105 * A marshaller for a #GCClosure with a callback of type
1106 * <literal>void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)</literal>.
1110 * g_cclosure_marshal_VOID__STRING:
1111 * @closure: the #GClosure to which the marshaller belongs
1112 * @return_value: ignored
1113 * @n_param_values: 2
1114 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1115 * @invocation_hint: the invocation hint given as the last argument
1116 * to g_closure_invoke()
1117 * @marshal_data: additional data specified when registering the marshaller
1119 * A marshaller for a #GCClosure with a callback of type
1120 * <literal>void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)</literal>.
1124 * g_cclosure_marshal_VOID__PARAM:
1125 * @closure: the #GClosure to which the marshaller belongs
1126 * @return_value: ignored
1127 * @n_param_values: 2
1128 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1129 * @invocation_hint: the invocation hint given as the last argument
1130 * to g_closure_invoke()
1131 * @marshal_data: additional data specified when registering the marshaller
1133 * A marshaller for a #GCClosure with a callback of type
1134 * <literal>void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)</literal>.
1138 * g_cclosure_marshal_VOID__BOXED:
1139 * @closure: the #GClosure to which the marshaller belongs
1140 * @return_value: ignored
1141 * @n_param_values: 2
1142 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1143 * @invocation_hint: the invocation hint given as the last argument
1144 * to g_closure_invoke()
1145 * @marshal_data: additional data specified when registering the marshaller
1147 * A marshaller for a #GCClosure with a callback of type
1148 * <literal>void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)</literal>.
1152 * g_cclosure_marshal_VOID__POINTER:
1153 * @closure: the #GClosure to which the marshaller belongs
1154 * @return_value: ignored
1155 * @n_param_values: 2
1156 * @param_values: a #GValue array holding the instance and the #gpointer parameter
1157 * @invocation_hint: the invocation hint given as the last argument
1158 * to g_closure_invoke()
1159 * @marshal_data: additional data specified when registering the marshaller
1161 * A marshaller for a #GCClosure with a callback of type
1162 * <literal>void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)</literal>.
1166 * g_cclosure_marshal_VOID__OBJECT:
1167 * @closure: the #GClosure to which the marshaller belongs
1168 * @return_value: ignored
1169 * @n_param_values: 2
1170 * @param_values: a #GValue array holding the instance and the #GObject* parameter
1171 * @invocation_hint: the invocation hint given as the last argument
1172 * to g_closure_invoke()
1173 * @marshal_data: additional data specified when registering the marshaller
1175 * A marshaller for a #GCClosure with a callback of type
1176 * <literal>void (*callback) (gpointer instance, GOBject *arg1, gpointer user_data)</literal>.
1180 * g_cclosure_marshal_VOID__UINT_POINTER:
1181 * @closure: the #GClosure to which the marshaller belongs
1182 * @return_value: ignored
1183 * @n_param_values: 3
1184 * @param_values: a #GValue array holding instance, arg1 and arg2
1185 * @invocation_hint: the invocation hint given as the last argument
1186 * to g_closure_invoke()
1187 * @marshal_data: additional data specified when registering the marshaller
1189 * A marshaller for a #GCClosure with a callback of type
1190 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)</literal>.
1194 * g_cclosure_marshal_BOOLEAN__FLAGS:
1195 * @closure: the #GClosure to which the marshaller belongs
1196 * @return_value: a #GValue which can store the returned #gboolean
1197 * @n_param_values: 2
1198 * @param_values: a #GValue array holding instance and arg1
1199 * @invocation_hint: the invocation hint given as the last argument
1200 * to g_closure_invoke()
1201 * @marshal_data: additional data specified when registering the marshaller
1203 * A marshaller for a #GCClosure with a callback of type
1204 * <literal>gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter
1205 * denotes a flags type.
1209 * g_cclosure_marshal_BOOL__FLAGS:
1211 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1214 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1215 * @closure: the #GClosure to which the marshaller belongs
1216 * @return_value: a #GValue, which can store the returned string
1217 * @n_param_values: 3
1218 * @param_values: a #GValue array holding instance, arg1 and arg2
1219 * @invocation_hint: the invocation hint given as the last argument
1220 * to g_closure_invoke()
1221 * @marshal_data: additional data specified when registering the marshaller
1223 * A marshaller for a #GCClosure with a callback of type
1224 * <literal>gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)</literal>.
1227 #define __G_CLOSURE_C__
1228 #include "gobjectaliasdef.c"