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"
35 * @Short_description: Functions as first-class objects
38 * A #GClosure represents a callback supplied by the programmer. It
39 * will generally comprise a function of some kind and a marshaller
40 * used to call it. It is the reponsibility of the marshaller to
41 * convert the arguments for the invocation from #GValue<!-- -->s into
42 * a suitable form, perform the callback on the converted arguments,
43 * and transform the return value back into a #GValue.
45 * In the case of C programs, a closure usually just holds a pointer
46 * to a function and maybe a data argument, and the marshaller
47 * converts between #GValue<!-- --> and native C types. The GObject
48 * library provides the #GCClosure type for this purpose. Bindings for
49 * other languages need marshallers which convert between #GValue<!--
50 * -->s and suitable representations in the runtime of the language in
51 * order to use functions written in that languages as callbacks.
53 * Within GObject, closures play an important role in the
54 * implementation of signals. When a signal is registered, the
55 * @c_marshaller argument to g_signal_new() specifies the default C
56 * marshaller for any closure which is connected to this
57 * signal. GObject provides a number of C marshallers for this
58 * purpose, see the g_cclosure_marshal_*() functions. Additional C
59 * marshallers can be generated with the <link
60 * linkend="glib-genmarshal">glib-genmarshal</link> utility. Closures
61 * can be explicitly connected to signals with
62 * g_signal_connect_closure(), but it usually more convenient to let
63 * GObject create a closure automatically by using one of the
64 * g_signal_connect_*() functions which take a callback function/user
67 * Using closures has a number of important advantages over a simple
68 * callback function/data pointer combination:
71 * Closures allow the callee to get the types of the callback parameters,
72 * which means that language bindings don't have to write individual glue
73 * for each callback type.
76 * The reference counting of #GClosure makes it easy to handle reentrancy
77 * right; if a callback is removed while it is being invoked, the closure
78 * and it's parameters won't be freed until the invocation finishes.
81 * g_closure_invalidate() and invalidation notifiers allow callbacks to be
82 * automatically removed when the objects they point to go away.
87 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
88 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
89 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
90 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
91 #define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \
92 ((cl)->n_guards << 1L))
93 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
94 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
95 (cl)->n_fnotifiers + \
103 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
105 ClosureInt *cunion = (ClosureInt*) _closure; \
106 gint new_int, old_int, success; \
110 tmp.vint = old_int = cunion->vint; \
111 _SET_OLD tmp.closure._field; \
112 tmp.closure._field _OP _value; \
113 _SET_NEW tmp.closure._field; \
114 new_int = tmp.vint; \
115 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
117 while (!success && _must_set); \
120 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
121 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
122 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
123 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
124 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
125 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
127 #if 0 /* for non-thread-safe closures */
128 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
129 #define SET(cl,f,v) (void) (cl->f = v)
130 #define INC(cl,f) (void) (cl->f += 1)
131 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
132 #define DEC(cl,f) (void) (cl->f -= 1)
133 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
144 /* --- functions --- */
146 * g_closure_new_simple:
147 * @sizeof_closure: the size of the structure to allocate, must be at least
148 * <literal>sizeof (GClosure)</literal>
149 * @data: data to store in the @data field of the newly allocated #GClosure
151 * Allocates a struct of the given size and initializes the initial part
152 * as a #GClosure. This function is mainly useful when implementing new types
156 * typedef struct _MyClosure MyClosure;
160 * // extra data goes here
164 * my_closure_finalize (gpointer notify_data,
167 * MyClosure *my_closure = (MyClosure *)closure;
169 * // free extra data here
172 * MyClosure *my_closure_new (gpointer data)
175 * MyClosure *my_closure;
177 * closure = g_closure_new_simple (sizeof (MyClosure), data);
178 * my_closure = (MyClosure *) closure;
180 * // initialize extra data here
182 * g_closure_add_finalize_notifier (closure, notify_data,
183 * my_closure_finalize);
188 * Returns: a newly allocated #GClosure
191 g_closure_new_simple (guint sizeof_closure,
196 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
198 closure = g_malloc0 (sizeof_closure);
199 SET (closure, ref_count, 1);
200 SET (closure, meta_marshal, 0);
201 SET (closure, n_guards, 0);
202 SET (closure, n_fnotifiers, 0);
203 SET (closure, n_inotifiers, 0);
204 SET (closure, in_inotify, FALSE);
205 SET (closure, floating, TRUE);
206 SET (closure, derivative_flag, 0);
207 SET (closure, in_marshal, FALSE);
208 SET (closure, is_invalid, FALSE);
209 closure->marshal = NULL;
210 closure->data = data;
211 closure->notifiers = NULL;
212 memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
218 closure_invoke_notifiers (GClosure *closure,
222 * meta_marshal n_guards n_guards n_fnotif. n_inotifiers
223 * ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]]
225 * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards;
226 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
228 * constrains/catches:
229 * - closure->notifiers may be reloacted during callback
230 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
231 * - i.e. callbacks can be removed/added during invocation
232 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
233 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
234 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
235 * + closure->meta_marshal is const for all cases
236 * + none of the callbacks can cause recursion
237 * + closure->n_inotifiers is const 0 during FNOTIFY
241 GClosureNotifyData *ndata;
244 while (closure->n_fnotifiers)
247 DEC_ASSIGN (closure, n_fnotifiers, &n);
249 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
250 closure->marshal = (GClosureMarshal) ndata->notify;
251 closure->data = ndata->data;
252 ndata->notify (ndata->data, closure);
254 closure->marshal = NULL;
255 closure->data = NULL;
258 SET (closure, in_inotify, TRUE);
259 while (closure->n_inotifiers)
262 DEC_ASSIGN (closure, n_inotifiers, &n);
264 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
265 closure->marshal = (GClosureMarshal) ndata->notify;
266 closure->data = ndata->data;
267 ndata->notify (ndata->data, closure);
269 closure->marshal = NULL;
270 closure->data = NULL;
271 SET (closure, in_inotify, FALSE);
274 i = closure->n_guards;
275 offs = closure->meta_marshal;
278 ndata = closure->notifiers + offs + i;
279 ndata->notify (ndata->data, closure);
283 i = closure->n_guards;
284 offs = closure->meta_marshal + i;
287 ndata = closure->notifiers + offs + i;
288 ndata->notify (ndata->data, closure);
295 * g_closure_set_meta_marshal:
296 * @closure: a #GClosure
297 * @marshal_data: context-dependent data to pass to @meta_marshal
298 * @meta_marshal: a #GClosureMarshal function
300 * Sets the meta marshaller of @closure.
301 * A meta marshaller wraps @closure->marshal and modifies the way it is called
302 * in some fashion. The most common use of this facility is for C callbacks.
303 * The same marshallers (generated by
304 * <link linkend="glib-genmarshal">glib-genmarshal</link>) are used everywhere,
305 * but the way that we get the callback function differs. In most cases we want
306 * to use @closure->callback, but in other cases we want to use some
307 * different technique to retrieve the callback function.
309 * For example, class closures for signals (see g_signal_type_cclosure_new())
310 * retrieve the callback function from a fixed offset in the class structure.
311 * The meta marshaller retrieves the right callback and passes it to the
312 * marshaller as the @marshal_data argument.
315 g_closure_set_meta_marshal (GClosure *closure,
316 gpointer marshal_data,
317 GClosureMarshal meta_marshal)
319 GClosureNotifyData *notifiers;
321 g_return_if_fail (closure != NULL);
322 g_return_if_fail (meta_marshal != NULL);
323 g_return_if_fail (closure->is_invalid == FALSE);
324 g_return_if_fail (closure->in_marshal == FALSE);
325 g_return_if_fail (closure->meta_marshal == 0);
327 notifiers = closure->notifiers;
328 closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
331 /* usually the meta marshal will be setup right after creation, so the
332 * g_memmove() should be rare-case scenario
334 g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
337 closure->notifiers[0].data = marshal_data;
338 closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
339 SET (closure, meta_marshal, 1);
343 * g_closure_add_marshal_guards:
344 * @closure: a #GClosure
345 * @pre_marshal_data: data to pass to @pre_marshal_notify
346 * @pre_marshal_notify: a function to call before the closure callback
347 * @post_marshal_data: data to pass to @post_marshal_notify
348 * @post_marshal_notify: a function to call after the closure callback
350 * Adds a pair of notifiers which get invoked before and after the closure
351 * callback, respectively. This is typically used to protect the extra arguments
352 * for the duration of the callback. See g_object_watch_closure() for an
353 * example of marshal guards.
356 g_closure_add_marshal_guards (GClosure *closure,
357 gpointer pre_marshal_data,
358 GClosureNotify pre_marshal_notify,
359 gpointer post_marshal_data,
360 GClosureNotify post_marshal_notify)
364 g_return_if_fail (closure != NULL);
365 g_return_if_fail (pre_marshal_notify != NULL);
366 g_return_if_fail (post_marshal_notify != NULL);
367 g_return_if_fail (closure->is_invalid == FALSE);
368 g_return_if_fail (closure->in_marshal == FALSE);
369 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
371 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
372 if (closure->n_inotifiers)
373 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
374 closure->n_fnotifiers +
375 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
376 closure->n_fnotifiers + 0)];
377 if (closure->n_inotifiers > 1)
378 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
379 closure->n_fnotifiers +
380 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
381 closure->n_fnotifiers + 1)];
382 if (closure->n_fnotifiers)
383 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
384 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
385 if (closure->n_fnotifiers > 1)
386 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
387 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
388 if (closure->n_guards)
389 closure->notifiers[(closure->meta_marshal +
391 closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
392 i = closure->n_guards;
393 closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
394 closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
395 closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
396 closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
397 INC (closure, n_guards);
401 * g_closure_add_finalize_notifier:
402 * @closure: a #GClosure
403 * @notify_data: data to pass to @notify_func
404 * @notify_func: the callback function to register
406 * Registers a finalization notifier which will be called when the reference
407 * count of @closure goes down to 0. Multiple finalization notifiers on a
408 * single closure are invoked in unspecified order. If a single call to
409 * g_closure_unref() results in the closure being both invalidated and
410 * finalized, then the invalidate notifiers will be run before the finalize
414 g_closure_add_finalize_notifier (GClosure *closure,
415 gpointer notify_data,
416 GClosureNotify notify_func)
420 g_return_if_fail (closure != NULL);
421 g_return_if_fail (notify_func != NULL);
422 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
424 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
425 if (closure->n_inotifiers)
426 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
427 closure->n_fnotifiers +
428 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
429 closure->n_fnotifiers + 0)];
430 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
431 closure->notifiers[i].data = notify_data;
432 closure->notifiers[i].notify = notify_func;
433 INC (closure, n_fnotifiers);
437 * g_closure_add_invalidate_notifier:
438 * @closure: a #GClosure
439 * @notify_data: data to pass to @notify_func
440 * @notify_func: the callback function to register
442 * Registers an invalidation notifier which will be called when the @closure
443 * is invalidated with g_closure_invalidate(). Invalidation notifiers are
444 * invoked before finalization notifiers, in an unspecified order.
447 g_closure_add_invalidate_notifier (GClosure *closure,
448 gpointer notify_data,
449 GClosureNotify notify_func)
453 g_return_if_fail (closure != NULL);
454 g_return_if_fail (notify_func != NULL);
455 g_return_if_fail (closure->is_invalid == FALSE);
456 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
458 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
459 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
460 closure->notifiers[i].data = notify_data;
461 closure->notifiers[i].notify = notify_func;
462 INC (closure, n_inotifiers);
465 static inline gboolean
466 closure_try_remove_inotify (GClosure *closure,
467 gpointer notify_data,
468 GClosureNotify notify_func)
470 GClosureNotifyData *ndata, *nlast;
472 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
473 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
474 if (ndata->notify == notify_func && ndata->data == notify_data)
476 DEC (closure, n_inotifiers);
485 static inline gboolean
486 closure_try_remove_fnotify (GClosure *closure,
487 gpointer notify_data,
488 GClosureNotify notify_func)
490 GClosureNotifyData *ndata, *nlast;
492 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
493 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
494 if (ndata->notify == notify_func && ndata->data == notify_data)
496 DEC (closure, n_fnotifiers);
499 if (closure->n_inotifiers)
500 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
501 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
502 closure->n_fnotifiers +
503 closure->n_inotifiers)];
511 * @closure: #GClosure to increment the reference count on
513 * Increments the reference count on a closure to force it staying
514 * alive while the caller holds a pointer to it.
516 * Returns: The @closure passed in, for convenience
519 g_closure_ref (GClosure *closure)
522 g_return_val_if_fail (closure != NULL, NULL);
523 g_return_val_if_fail (closure->ref_count > 0, NULL);
524 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
526 INC_ASSIGN (closure, ref_count, &new_ref_count);
527 g_return_val_if_fail (new_ref_count > 1, NULL);
533 * g_closure_invalidate:
534 * @closure: GClosure to invalidate
536 * Sets a flag on the closure to indicate that it's calling environment has
537 * become invalid, and thus causes any future invocations of g_closure_invoke()
538 * on this @closure to be ignored. Also, invalidation notifiers installed on
539 * the closure will be called at this point. Note that unless you are holding
540 * a reference to the closure yourself, the invalidation notifiers may unref
541 * the closure and cause it to be destroyed, so if you need to access the
542 * closure after calling g_closure_invalidate(), make sure that you've
543 * previously called g_closure_ref().
545 * Note that g_closure_invalidate() will also be called when the reference count
546 * of a closure drops to zero (unless it has already been invalidated before).
549 g_closure_invalidate (GClosure *closure)
551 g_return_if_fail (closure != NULL);
553 if (!closure->is_invalid)
555 gboolean was_invalid;
556 g_closure_ref (closure); /* preserve floating flag */
557 SWAP (closure, is_invalid, TRUE, &was_invalid);
558 /* invalidate only once */
560 closure_invoke_notifiers (closure, INOTIFY);
561 g_closure_unref (closure);
567 * @closure: #GClosure to decrement the reference count on
569 * Decrements the reference count of a closure after it was previously
570 * incremented by the same caller. If no other callers are using the closure,
571 * then the closure will be destroyed and freed.
574 g_closure_unref (GClosure *closure)
578 g_return_if_fail (closure != NULL);
579 g_return_if_fail (closure->ref_count > 0);
581 if (closure->ref_count == 1) /* last unref, invalidate first */
582 g_closure_invalidate (closure);
584 DEC_ASSIGN (closure, ref_count, &new_ref_count);
586 if (new_ref_count == 0)
588 closure_invoke_notifiers (closure, FNOTIFY);
589 g_free (closure->notifiers);
596 * @closure: #GClosure to decrement the initial reference count on, if it's
599 * Takes over the initial ownership of a closure.
600 * Each closure is initially created in a <firstterm>floating</firstterm> state,
601 * which means that the initial reference count is not owned by any caller.
602 * g_closure_sink() checks to see if the object is still floating, and if so,
603 * unsets the floating state and decreases the reference count. If the closure
604 * is not floating, g_closure_sink() does nothing. The reason for the existance
605 * of the floating state is to prevent cumbersome code sequences like:
607 * closure = g_cclosure_new (cb_func, cb_data);
608 * g_source_set_closure (source, closure);
609 * g_closure_unref (closure); // XXX GObject doesn't really need this
611 * Because g_source_set_closure() (and similar functions) take ownership of the
612 * initial reference count, if it is unowned, we instead can write:
614 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
617 * Generally, this function is used together with g_closure_ref(). Ane example
618 * of storing a closure for later notification looks like:
620 * static GClosure *notify_closure = NULL;
622 * foo_notify_set_closure (GClosure *closure)
624 * if (notify_closure)
625 * g_closure_unref (notify_closure);
626 * notify_closure = closure;
627 * if (notify_closure)
629 * g_closure_ref (notify_closure);
630 * g_closure_sink (notify_closure);
635 * Because g_closure_sink() may decrement the reference count of a closure
636 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
637 * g_closure_ref() should be called prior to this function.
640 g_closure_sink (GClosure *closure)
642 g_return_if_fail (closure != NULL);
643 g_return_if_fail (closure->ref_count > 0);
645 /* floating is basically a kludge to avoid creating closures
646 * with a ref_count of 0. so the intial ref_count a closure has
647 * is unowned. with invoking g_closure_sink() code may
648 * indicate that it takes over that intiial ref_count.
650 if (closure->floating)
652 gboolean was_floating;
653 SWAP (closure, floating, FALSE, &was_floating);
654 /* unref floating flag only once */
656 g_closure_unref (closure);
661 * g_closure_remove_invalidate_notifier:
662 * @closure: a #GClosure
663 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
664 * when registering @notify_func
665 * @notify_func: the callback function to remove
667 * Removes an invalidation notifier.
670 * Notice that notifiers are automatically removed after they are run.
673 g_closure_remove_invalidate_notifier (GClosure *closure,
674 gpointer notify_data,
675 GClosureNotify notify_func)
677 g_return_if_fail (closure != NULL);
678 g_return_if_fail (notify_func != NULL);
680 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
681 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
682 closure->data == notify_data)
683 closure->marshal = NULL;
684 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
685 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
686 notify_func, notify_data);
690 * g_closure_remove_finalize_notifier:
691 * @closure: a #GClosure
692 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
693 * when registering @notify_func
694 * @notify_func: the callback function to remove
696 * Removes a finalization notifier.
698 * Notice that notifiers are automatically removed after they are run.
701 g_closure_remove_finalize_notifier (GClosure *closure,
702 gpointer notify_data,
703 GClosureNotify notify_func)
705 g_return_if_fail (closure != NULL);
706 g_return_if_fail (notify_func != NULL);
708 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
709 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
710 closure->data == notify_data)
711 closure->marshal = NULL;
712 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
713 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
714 notify_func, notify_data);
719 * @closure: a #GClosure
720 * @return_value: a #GValue to store the return value. May be %NULL if the
721 * callback of @closure doesn't return a value.
722 * @n_param_values: the length of the @param_values array
723 * @param_values: an array of #GValue<!-- -->s holding the arguments on
724 * which to invoke the callback of @closure
725 * @invocation_hint: a context-dependent invocation hint
727 * Invokes the closure, i.e. executes the callback represented by the @closure.
730 g_closure_invoke (GClosure *closure,
731 GValue /*out*/ *return_value,
732 guint n_param_values,
733 const GValue *param_values,
734 gpointer invocation_hint)
736 g_return_if_fail (closure != NULL);
738 g_closure_ref (closure); /* preserve floating flag */
739 if (!closure->is_invalid)
741 GClosureMarshal marshal;
742 gpointer marshal_data;
743 gboolean in_marshal = closure->in_marshal;
745 g_return_if_fail (closure->marshal || closure->meta_marshal);
747 SET (closure, in_marshal, TRUE);
748 if (closure->meta_marshal)
750 marshal_data = closure->notifiers[0].data;
751 marshal = (GClosureMarshal) closure->notifiers[0].notify;
756 marshal = closure->marshal;
759 closure_invoke_notifiers (closure, PRE_NOTIFY);
762 n_param_values, param_values,
766 closure_invoke_notifiers (closure, POST_NOTIFY);
767 SET (closure, in_marshal, in_marshal);
769 g_closure_unref (closure);
773 * g_closure_set_marshal:
774 * @closure: a #GClosure
775 * @marshal: a #GClosureMarshal function
777 * Sets the marshaller of @closure. The <literal>marshal_data</literal>
778 * of @marshal provides a way for a meta marshaller to provide additional
779 * information to the marshaller. (See g_closure_set_meta_marshal().) For
780 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
781 * functions), what it provides is a callback function to use instead of
782 * @closure->callback.
785 g_closure_set_marshal (GClosure *closure,
786 GClosureMarshal marshal)
788 g_return_if_fail (closure != NULL);
789 g_return_if_fail (marshal != NULL);
791 if (closure->marshal && closure->marshal != marshal)
792 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
793 closure->marshal, marshal);
795 closure->marshal = marshal;
800 * @callback_func: the function to invoke
801 * @user_data: user data to pass to @callback_func
802 * @destroy_data: destroy notify to be called when @user_data is no longer used
804 * Creates a new closure which invokes @callback_func with @user_data as
805 * the last parameter.
807 * Returns: a new #GCClosure
810 g_cclosure_new (GCallback callback_func,
812 GClosureNotify destroy_data)
816 g_return_val_if_fail (callback_func != NULL, NULL);
818 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
820 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
821 ((GCClosure*) closure)->callback = (gpointer) callback_func;
827 * g_cclosure_new_swap:
828 * @callback_func: the function to invoke
829 * @user_data: user data to pass to @callback_func
830 * @destroy_data: destroy notify to be called when @user_data is no longer used
832 * Creates a new closure which invokes @callback_func with @user_data as
833 * the first parameter.
835 * Returns: a new #GCClosure
838 g_cclosure_new_swap (GCallback callback_func,
840 GClosureNotify destroy_data)
844 g_return_val_if_fail (callback_func != NULL, NULL);
846 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
848 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
849 ((GCClosure*) closure)->callback = (gpointer) callback_func;
850 SET (closure, derivative_flag, TRUE);
856 g_type_class_meta_marshal (GClosure *closure,
857 GValue /*out*/ *return_value,
858 guint n_param_values,
859 const GValue *param_values,
860 gpointer invocation_hint,
861 gpointer marshal_data)
865 /* GType itype = (GType) closure->data; */
866 guint offset = GPOINTER_TO_UINT (marshal_data);
868 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
869 callback = G_STRUCT_MEMBER (gpointer, class, offset);
871 closure->marshal (closure,
873 n_param_values, param_values,
879 g_type_iface_meta_marshal (GClosure *closure,
880 GValue /*out*/ *return_value,
881 guint n_param_values,
882 const GValue *param_values,
883 gpointer invocation_hint,
884 gpointer marshal_data)
888 GType itype = (GType) closure->data;
889 guint offset = GPOINTER_TO_UINT (marshal_data);
891 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
892 callback = G_STRUCT_MEMBER (gpointer, class, offset);
894 closure->marshal (closure,
896 n_param_values, param_values,
902 * g_signal_type_cclosure_new:
903 * @itype: the #GType identifier of an interface or classed type
904 * @struct_offset: the offset of the member function of @itype's class
905 * structure which is to be invoked by the new closure
907 * Creates a new closure which invokes the function found at the offset
908 * @struct_offset in the class structure of the interface or classed type
909 * identified by @itype.
911 * Returns: a new #GCClosure
914 g_signal_type_cclosure_new (GType itype,
919 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
920 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
922 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
923 if (G_TYPE_IS_INTERFACE (itype))
924 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
926 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
933 * g_cclosure_marshal_VOID__VOID:
934 * @closure: the #GClosure to which the marshaller belongs
935 * @return_value: ignored
937 * @param_values: a #GValue array holding only the instance
938 * @invocation_hint: the invocation hint given as the last argument
939 * to g_closure_invoke()
940 * @marshal_data: additional data specified when registering the marshaller
942 * A marshaller for a #GCClosure with a callback of type
943 * <literal>void (*callback) (gpointer instance, gpointer user_data)</literal>.
946 * g_cclosure_marshal_VOID__BOOLEAN:
947 * @closure: the #GClosure to which the marshaller belongs
948 * @return_value: ignored
950 * @param_values: a #GValue array holding the instance and the #gboolean parameter
951 * @invocation_hint: the invocation hint given as the last argument
952 * to g_closure_invoke()
953 * @marshal_data: additional data specified when registering the marshaller
955 * A marshaller for a #GCClosure with a callback of type
956 * <literal>void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)</literal>.
959 * g_cclosure_marshal_VOID__CHAR:
960 * @closure: the #GClosure to which the marshaller belongs
961 * @return_value: ignored
963 * @param_values: a #GValue array holding the instance and the #gchar parameter
964 * @invocation_hint: the invocation hint given as the last argument
965 * to g_closure_invoke()
966 * @marshal_data: additional data specified when registering the marshaller
968 * A marshaller for a #GCClosure with a callback of type
969 * <literal>void (*callback) (gpointer instance, gchar arg1, gpointer user_data)</literal>.
972 * g_cclosure_marshal_VOID__UCHAR:
973 * @closure: the #GClosure to which the marshaller belongs
974 * @return_value: ignored
976 * @param_values: a #GValue array holding the instance and the #guchar parameter
977 * @invocation_hint: the invocation hint given as the last argument
978 * to g_closure_invoke()
979 * @marshal_data: additional data specified when registering the marshaller
981 * A marshaller for a #GCClosure with a callback of type
982 * <literal>void (*callback) (gpointer instance, guchar arg1, gpointer user_data)</literal>.
985 * g_cclosure_marshal_VOID__INT:
986 * @closure: the #GClosure to which the marshaller belongs
987 * @return_value: ignored
989 * @param_values: a #GValue array holding the instance and the #gint parameter
990 * @invocation_hint: the invocation hint given as the last argument
991 * to g_closure_invoke()
992 * @marshal_data: additional data specified when registering the marshaller
994 * A marshaller for a #GCClosure with a callback of type
995 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal>.
998 * g_cclosure_marshal_VOID__UINT:
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 #guint 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, guint arg1, gpointer user_data)</literal>.
1011 * g_cclosure_marshal_VOID__LONG:
1012 * @closure: the #GClosure to which the marshaller belongs
1013 * @return_value: ignored
1014 * @n_param_values: 2
1015 * @param_values: a #GValue array holding the instance and the #glong parameter
1016 * @invocation_hint: the invocation hint given as the last argument
1017 * to g_closure_invoke()
1018 * @marshal_data: additional data specified when registering the marshaller
1020 * A marshaller for a #GCClosure with a callback of type
1021 * <literal>void (*callback) (gpointer instance, glong arg1, gpointer user_data)</literal>.
1024 * g_cclosure_marshal_VOID__ULONG:
1025 * @closure: the #GClosure to which the marshaller belongs
1026 * @return_value: ignored
1027 * @n_param_values: 2
1028 * @param_values: a #GValue array holding the instance and the #gulong parameter
1029 * @invocation_hint: the invocation hint given as the last argument
1030 * to g_closure_invoke()
1031 * @marshal_data: additional data specified when registering the marshaller
1033 * A marshaller for a #GCClosure with a callback of type
1034 * <literal>void (*callback) (gpointer instance, gulong arg1, gpointer user_data)</literal>.
1037 * g_cclosure_marshal_VOID__ENUM:
1038 * @closure: the #GClosure to which the marshaller belongs
1039 * @return_value: ignored
1040 * @n_param_values: 2
1041 * @param_values: a #GValue array holding the instance and the enumeration parameter
1042 * @invocation_hint: the invocation hint given as the last argument
1043 * to g_closure_invoke()
1044 * @marshal_data: additional data specified when registering the marshaller
1046 * A marshaller for a #GCClosure with a callback of type
1047 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes an enumeration type..
1050 * g_cclosure_marshal_VOID__FLAGS:
1051 * @closure: the #GClosure to which the marshaller belongs
1052 * @return_value: ignored
1053 * @n_param_values: 2
1054 * @param_values: a #GValue array holding the instance and the flags parameter
1055 * @invocation_hint: the invocation hint given as the last argument
1056 * to g_closure_invoke()
1057 * @marshal_data: additional data specified when registering the marshaller
1059 * A marshaller for a #GCClosure with a callback of type
1060 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes a flags type.
1063 * g_cclosure_marshal_VOID__FLOAT:
1064 * @closure: the #GClosure to which the marshaller belongs
1065 * @return_value: ignored
1066 * @n_param_values: 2
1067 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1068 * @invocation_hint: the invocation hint given as the last argument
1069 * to g_closure_invoke()
1070 * @marshal_data: additional data specified when registering the marshaller
1072 * A marshaller for a #GCClosure with a callback of type
1073 * <literal>void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)</literal>.
1076 * g_cclosure_marshal_VOID__DOUBLE:
1077 * @closure: the #GClosure to which the marshaller belongs
1078 * @return_value: ignored
1079 * @n_param_values: 2
1080 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1081 * @invocation_hint: the invocation hint given as the last argument
1082 * to g_closure_invoke()
1083 * @marshal_data: additional data specified when registering the marshaller
1085 * A marshaller for a #GCClosure with a callback of type
1086 * <literal>void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)</literal>.
1089 * g_cclosure_marshal_VOID__STRING:
1090 * @closure: the #GClosure to which the marshaller belongs
1091 * @return_value: ignored
1092 * @n_param_values: 2
1093 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1094 * @invocation_hint: the invocation hint given as the last argument
1095 * to g_closure_invoke()
1096 * @marshal_data: additional data specified when registering the marshaller
1098 * A marshaller for a #GCClosure with a callback of type
1099 * <literal>void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)</literal>.
1102 * g_cclosure_marshal_VOID__PARAM:
1103 * @closure: the #GClosure to which the marshaller belongs
1104 * @return_value: ignored
1105 * @n_param_values: 2
1106 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1107 * @invocation_hint: the invocation hint given as the last argument
1108 * to g_closure_invoke()
1109 * @marshal_data: additional data specified when registering the marshaller
1111 * A marshaller for a #GCClosure with a callback of type
1112 * <literal>void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)</literal>.
1115 * g_cclosure_marshal_VOID__BOXED:
1116 * @closure: the #GClosure to which the marshaller belongs
1117 * @return_value: ignored
1118 * @n_param_values: 2
1119 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1120 * @invocation_hint: the invocation hint given as the last argument
1121 * to g_closure_invoke()
1122 * @marshal_data: additional data specified when registering the marshaller
1124 * A marshaller for a #GCClosure with a callback of type
1125 * <literal>void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)</literal>.
1128 * g_cclosure_marshal_VOID__POINTER:
1129 * @closure: the #GClosure to which the marshaller belongs
1130 * @return_value: ignored
1131 * @n_param_values: 2
1132 * @param_values: a #GValue array holding the instance and the #gpointer parameter
1133 * @invocation_hint: the invocation hint given as the last argument
1134 * to g_closure_invoke()
1135 * @marshal_data: additional data specified when registering the marshaller
1137 * A marshaller for a #GCClosure with a callback of type
1138 * <literal>void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)</literal>.
1141 * g_cclosure_marshal_VOID__OBJECT:
1142 * @closure: the #GClosure to which the marshaller belongs
1143 * @return_value: ignored
1144 * @n_param_values: 2
1145 * @param_values: a #GValue array holding the instance and the #GObject* parameter
1146 * @invocation_hint: the invocation hint given as the last argument
1147 * to g_closure_invoke()
1148 * @marshal_data: additional data specified when registering the marshaller
1150 * A marshaller for a #GCClosure with a callback of type
1151 * <literal>void (*callback) (gpointer instance, GOBject *arg1, gpointer user_data)</literal>.
1154 * g_cclosure_marshal_VOID__UINT_POINTER:
1155 * @closure: the #GClosure to which the marshaller belongs
1156 * @return_value: ignored
1157 * @n_param_values: 3
1158 * @param_values: a #GValue array holding instance, arg1 and arg2
1159 * @invocation_hint: the invocation hint given as the last argument
1160 * to g_closure_invoke()
1161 * @marshal_data: additional data specified when registering the marshaller
1163 * A marshaller for a #GCClosure with a callback of type
1164 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)</literal>.
1167 * g_cclosure_marshal_BOOLEAN__FLAGS:
1168 * @closure: the #GClosure to which the marshaller belongs
1169 * @return_value: a #GValue which can store the returned #gboolean
1170 * @n_param_values: 2
1171 * @param_values: a #GValue array holding instance and arg1
1172 * @invocation_hint: the invocation hint given as the last argument
1173 * to g_closure_invoke()
1174 * @marshal_data: additional data specified when registering the marshaller
1176 * A marshaller for a #GCClosure with a callback of type
1177 * <literal>gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter
1178 * denotes a flags type.
1181 * g_cclosure_marshal_BOOL__FLAGS:
1183 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1186 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1187 * @closure: the #GClosure to which the marshaller belongs
1188 * @return_value: a #GValue, which can store the returned string
1189 * @n_param_values: 3
1190 * @param_values: a #GValue array holding instance, arg1 and arg2
1191 * @invocation_hint: the invocation hint given as the last argument
1192 * to g_closure_invoke()
1193 * @marshal_data: additional data specified when registering the marshaller
1195 * A marshaller for a #GCClosure with a callback of type
1196 * <literal>gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)</literal>.
1199 #define __G_CLOSURE_C__
1200 #include "gobjectaliasdef.c"