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 * @Short_description: Functions as first-class objects
25 * A #GClosure represents a callback supplied by the programmer. It will generally
26 * comprise a function of some kind and a marshaller used to call it. It is the
27 * reponsibility of the marshaller to convert the arguments for the invocation
28 * from #GValue<!-- -->s into a suitable form, perform the callback on the
29 * converted arguments, and transform the return value back into a #GValue.
31 * In the case of C programs, a closure usually just holds a pointer to a function
32 * and maybe a data argument, and the marshaller converts between #GValue<!-- -->
33 * and native C types. The GObject library provides the #GCClosure type for this
34 * purpose. Bindings for other languages need marshallers which
35 * convert between #GValue<!-- -->s and suitable representations in the runtime
36 * of the language in order to use functions written in that languages as
39 * Within GObject, closures play an important role in the implementation of
40 * signals. When a signal is registered, the @c_marshaller argument to
41 * g_signal_new() specifies the default C marshaller for any closure which is
42 * connected to this signal. GObject provides a number of C marshallers
43 * for this purpose, see the g_cclosure_marshal_*() functions. Additional
44 * C marshallers can be generated with the
45 * <link linkend="glib-genmarshal">glib-genmarshal</link> utility.
46 * Closures can be explicitly connected to signals with
47 * g_signal_connect_closure(), but it usually more convenient to let GObject
48 * create a closure automatically by using one of the g_signal_connect_*()
49 * functions which take a callback function/user data pair.
51 * Using closures has a number of important advantages over a simple
52 * callback function/data pointer combination:
55 * Closures allow the callee to get the types of the callback parameters,
56 * which means that language bindings don't have to write individual glue
57 * for each callback type.
60 * The reference counting of #GClosure makes it easy to handle reentrancy
61 * right; if a callback is removed while it is being invoked, the closure
62 * and it's parameters won't be freed until the invocation finishes.
65 * g_closure_invalidate() and invalidation notifiers allow callbacks to be
66 * automatically removed when the objects they point to go away.
74 * MT safe with regards to reference counting.
78 #include "gobjectalias.h"
82 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
83 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
84 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
85 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
86 #define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \
87 ((cl)->n_guards << 1L))
88 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
89 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
90 (cl)->n_fnotifiers + \
98 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
100 ClosureInt *cunion = (ClosureInt*) _closure; \
101 gint new_int, old_int, success; \
105 tmp.vint = old_int = cunion->vint; \
106 _SET_OLD tmp.closure._field; \
107 tmp.closure._field _OP _value; \
108 _SET_NEW tmp.closure._field; \
109 new_int = tmp.vint; \
110 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
112 while (!success && _must_set); \
115 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
116 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
117 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
118 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
119 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
120 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
122 #if 0 /* for non-thread-safe closures */
123 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
124 #define SET(cl,f,v) (void) (cl->f = v)
125 #define INC(cl,f) (void) (cl->f += 1)
126 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
127 #define DEC(cl,f) (void) (cl->f -= 1)
128 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
139 /* --- functions --- */
141 * g_closure_new_simple:
142 * @sizeof_closure: the size of the structure to allocate, must be at least
143 * <literal>sizeof (GClosure)</literal>
144 * @data: data to store in the @data field of the newly allocated #GClosure
146 * Allocates a struct of the given size and initializes the initial part
147 * as a #GClosure. This function is mainly useful when implementing new types
151 * typedef struct _MyClosure MyClosure;
155 * // extra data goes here
159 * my_closure_finalize (gpointer notify_data,
162 * MyClosure *my_closure = (MyClosure *)closure;
164 * // free extra data here
167 * MyClosure *my_closure_new (gpointer data)
170 * MyClosure *my_closure;
172 * closure = g_closure_new_simple (sizeof (MyClosure), data);
173 * my_closure = (MyClosure *) closure;
175 * // initialize extra data here
177 * g_closure_add_finalize_notifier (closure, notify_data,
178 * my_closure_finalize);
183 * Returns: a newly allocated #GClosure
186 g_closure_new_simple (guint sizeof_closure,
191 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
193 closure = g_malloc0 (sizeof_closure);
194 SET (closure, ref_count, 1);
195 SET (closure, meta_marshal, 0);
196 SET (closure, n_guards, 0);
197 SET (closure, n_fnotifiers, 0);
198 SET (closure, n_inotifiers, 0);
199 SET (closure, in_inotify, FALSE);
200 SET (closure, floating, TRUE);
201 SET (closure, derivative_flag, 0);
202 SET (closure, in_marshal, FALSE);
203 SET (closure, is_invalid, FALSE);
204 closure->marshal = NULL;
205 closure->data = data;
206 closure->notifiers = NULL;
207 memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
213 closure_invoke_notifiers (GClosure *closure,
217 * meta_marshal n_guards n_guards n_fnotif. n_inotifiers
218 * ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]]
220 * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards;
221 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
223 * constrains/catches:
224 * - closure->notifiers may be reloacted during callback
225 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
226 * - i.e. callbacks can be removed/added during invocation
227 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
228 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
229 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
230 * + closure->meta_marshal is const for all cases
231 * + none of the callbacks can cause recursion
232 * + closure->n_inotifiers is const 0 during FNOTIFY
236 GClosureNotifyData *ndata;
239 while (closure->n_fnotifiers)
242 DEC_ASSIGN (closure, n_fnotifiers, &n);
244 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
245 closure->marshal = (GClosureMarshal) ndata->notify;
246 closure->data = ndata->data;
247 ndata->notify (ndata->data, closure);
249 closure->marshal = NULL;
250 closure->data = NULL;
253 SET (closure, in_inotify, TRUE);
254 while (closure->n_inotifiers)
257 DEC_ASSIGN (closure, n_inotifiers, &n);
259 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
260 closure->marshal = (GClosureMarshal) ndata->notify;
261 closure->data = ndata->data;
262 ndata->notify (ndata->data, closure);
264 closure->marshal = NULL;
265 closure->data = NULL;
266 SET (closure, in_inotify, FALSE);
269 i = closure->n_guards;
270 offs = closure->meta_marshal;
273 ndata = closure->notifiers + offs + i;
274 ndata->notify (ndata->data, closure);
278 i = closure->n_guards;
279 offs = closure->meta_marshal + i;
282 ndata = closure->notifiers + offs + i;
283 ndata->notify (ndata->data, closure);
290 * g_closure_set_meta_marshal:
291 * @closure: a #GClosure
292 * @marshal_data: context-dependent data to pass to @meta_marshal
293 * @meta_marshal: a #GClosureMarshal function
295 * Sets the meta marshaller of @closure.
296 * A meta marshaller wraps @closure->marshal and modifies the way it is called
297 * in some fashion. The most common use of this facility is for C callbacks.
298 * The same marshallers (generated by
299 * <link linkend="glib-genmarshal">glib-genmarshal</link>) are used everywhere,
300 * but the way that we get the callback function differs. In most cases we want
301 * to use @closure->callback, but in other cases we want to use some
302 * different technique to retrieve the callback function.
304 * For example, class closures for signals (see g_signal_type_cclosure_new())
305 * retrieve the callback function from a fixed offset in the class structure.
306 * The meta marshaller retrieves the right callback and passes it to the
307 * marshaller as the @marshal_data argument.
310 g_closure_set_meta_marshal (GClosure *closure,
311 gpointer marshal_data,
312 GClosureMarshal meta_marshal)
314 GClosureNotifyData *notifiers;
316 g_return_if_fail (closure != NULL);
317 g_return_if_fail (meta_marshal != NULL);
318 g_return_if_fail (closure->is_invalid == FALSE);
319 g_return_if_fail (closure->in_marshal == FALSE);
320 g_return_if_fail (closure->meta_marshal == 0);
322 notifiers = closure->notifiers;
323 closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
326 /* usually the meta marshal will be setup right after creation, so the
327 * g_memmove() should be rare-case scenario
329 g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
332 closure->notifiers[0].data = marshal_data;
333 closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
334 SET (closure, meta_marshal, 1);
338 * g_closure_add_marshal_guards:
339 * @closure: a #GClosure
340 * @pre_marshal_data: data to pass to @pre_marshal_notify
341 * @pre_marshal_notify: a function to call before the closure callback
342 * @post_marshal_data: data to pass to @post_marshal_notify
343 * @post_marshal_notify: a function to call after the closure callback
345 * Adds a pair of notifiers which get invoked before and after the closure
346 * callback, respectively. This is typically used to protect the extra arguments
347 * for the duration of the callback. See g_object_watch_closure() for an
348 * example of marshal guards.
351 g_closure_add_marshal_guards (GClosure *closure,
352 gpointer pre_marshal_data,
353 GClosureNotify pre_marshal_notify,
354 gpointer post_marshal_data,
355 GClosureNotify post_marshal_notify)
359 g_return_if_fail (closure != NULL);
360 g_return_if_fail (pre_marshal_notify != NULL);
361 g_return_if_fail (post_marshal_notify != NULL);
362 g_return_if_fail (closure->is_invalid == FALSE);
363 g_return_if_fail (closure->in_marshal == FALSE);
364 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
366 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
367 if (closure->n_inotifiers)
368 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
369 closure->n_fnotifiers +
370 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
371 closure->n_fnotifiers + 0)];
372 if (closure->n_inotifiers > 1)
373 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
374 closure->n_fnotifiers +
375 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
376 closure->n_fnotifiers + 1)];
377 if (closure->n_fnotifiers)
378 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
379 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
380 if (closure->n_fnotifiers > 1)
381 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
382 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
383 if (closure->n_guards)
384 closure->notifiers[(closure->meta_marshal +
386 closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
387 i = closure->n_guards;
388 closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
389 closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
390 closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
391 closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
392 INC (closure, n_guards);
396 * g_closure_add_finalize_notifier:
397 * @closure: a #GClosure
398 * @notify_data: data to pass to @notify_func
399 * @notify_func: the callback function to register
401 * Registers a finalization notifier which will be called when the reference
402 * count of @closure goes down to 0. Multiple finalization notifiers on a
403 * single closure are invoked in unspecified order. If a single call to
404 * g_closure_unref() results in the closure being both invalidated and
405 * finalized, then the invalidate notifiers will be run before the finalize
409 g_closure_add_finalize_notifier (GClosure *closure,
410 gpointer notify_data,
411 GClosureNotify notify_func)
415 g_return_if_fail (closure != NULL);
416 g_return_if_fail (notify_func != NULL);
417 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
419 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
420 if (closure->n_inotifiers)
421 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
422 closure->n_fnotifiers +
423 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
424 closure->n_fnotifiers + 0)];
425 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
426 closure->notifiers[i].data = notify_data;
427 closure->notifiers[i].notify = notify_func;
428 INC (closure, n_fnotifiers);
432 * g_closure_add_invalidate_notifier:
433 * @closure: a #GClosure
434 * @notify_data: data to pass to @notify_func
435 * @notify_func: the callback function to register
437 * Registers an invalidation notifier which will be called when the @closure
438 * is invalidated with g_closure_invalidate(). Invalidation notifiers are
439 * invoked before finalization notifiers, in an unspecified order.
442 g_closure_add_invalidate_notifier (GClosure *closure,
443 gpointer notify_data,
444 GClosureNotify notify_func)
448 g_return_if_fail (closure != NULL);
449 g_return_if_fail (notify_func != NULL);
450 g_return_if_fail (closure->is_invalid == FALSE);
451 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
453 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
454 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
455 closure->notifiers[i].data = notify_data;
456 closure->notifiers[i].notify = notify_func;
457 INC (closure, n_inotifiers);
460 static inline gboolean
461 closure_try_remove_inotify (GClosure *closure,
462 gpointer notify_data,
463 GClosureNotify notify_func)
465 GClosureNotifyData *ndata, *nlast;
467 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
468 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
469 if (ndata->notify == notify_func && ndata->data == notify_data)
471 DEC (closure, n_inotifiers);
480 static inline gboolean
481 closure_try_remove_fnotify (GClosure *closure,
482 gpointer notify_data,
483 GClosureNotify notify_func)
485 GClosureNotifyData *ndata, *nlast;
487 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
488 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
489 if (ndata->notify == notify_func && ndata->data == notify_data)
491 DEC (closure, n_fnotifiers);
494 if (closure->n_inotifiers)
495 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
496 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
497 closure->n_fnotifiers +
498 closure->n_inotifiers)];
506 * @closure: #GClosure to increment the reference count on
508 * Increments the reference count on a closure to force it staying
509 * alive while the caller holds a pointer to it.
511 * Returns: The @closure passed in, for convenience
514 g_closure_ref (GClosure *closure)
517 g_return_val_if_fail (closure != NULL, NULL);
518 g_return_val_if_fail (closure->ref_count > 0, NULL);
519 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
521 INC_ASSIGN (closure, ref_count, &new_ref_count);
522 g_return_val_if_fail (new_ref_count > 1, NULL);
528 * g_closure_invalidate:
529 * @closure: GClosure to invalidate
531 * Sets a flag on the closure to indicate that it's calling environment has
532 * become invalid, and thus causes any future invocations of g_closure_invoke()
533 * on this @closure to be ignored. Also, invalidation notifiers installed on
534 * the closure will be called at this point. Note that unless you are holding
535 * a reference to the closure yourself, the invalidation notifiers may unref
536 * the closure and cause it to be destroyed, so if you need to access the
537 * closure after calling g_closure_invalidate(), make sure that you've
538 * previously called g_closure_ref().
540 * Note that g_closure_invalidate() will also be called when the reference count
541 * of a closure drops to zero (unless it has already been invalidated before).
544 g_closure_invalidate (GClosure *closure)
546 g_return_if_fail (closure != NULL);
548 if (!closure->is_invalid)
550 gboolean was_invalid;
551 g_closure_ref (closure); /* preserve floating flag */
552 SWAP (closure, is_invalid, TRUE, &was_invalid);
553 /* invalidate only once */
555 closure_invoke_notifiers (closure, INOTIFY);
556 g_closure_unref (closure);
562 * @closure: #GClosure to decrement the reference count on
564 * Decrements the reference count of a closure after it was previously
565 * incremented by the same caller. If no other callers are using the closure,
566 * then the closure will be destroyed and freed.
569 g_closure_unref (GClosure *closure)
573 g_return_if_fail (closure != NULL);
574 g_return_if_fail (closure->ref_count > 0);
576 if (closure->ref_count == 1) /* last unref, invalidate first */
577 g_closure_invalidate (closure);
579 DEC_ASSIGN (closure, ref_count, &new_ref_count);
581 if (new_ref_count == 0)
583 closure_invoke_notifiers (closure, FNOTIFY);
584 g_free (closure->notifiers);
591 * @closure: #GClosure to decrement the initial reference count on, if it's
594 * Takes over the initial ownership of a closure.
595 * Each closure is initially created in a <firstterm>floating</firstterm> state,
596 * which means that the initial reference count is not owned by any caller.
597 * g_closure_sink() checks to see if the object is still floating, and if so,
598 * unsets the floating state and decreases the reference count. If the closure
599 * is not floating, g_closure_sink() does nothing. The reason for the existance
600 * of the floating state is to prevent cumbersome code sequences like:
602 * closure = g_cclosure_new (cb_func, cb_data);
603 * g_source_set_closure (source, closure);
604 * g_closure_unref (closure); // XXX GObject doesn't really need this
606 * Because g_source_set_closure() (and similar functions) take ownership of the
607 * initial reference count, if it is unowned, we instead can write:
609 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
612 * Generally, this function is used together with g_closure_ref(). Ane example
613 * of storing a closure for later notification looks like:
615 * static GClosure *notify_closure = NULL;
617 * foo_notify_set_closure (GClosure *closure)
619 * if (notify_closure)
620 * g_closure_unref (notify_closure);
621 * notify_closure = closure;
622 * if (notify_closure)
624 * g_closure_ref (notify_closure);
625 * g_closure_sink (notify_closure);
630 * Because g_closure_sink() may decrement the reference count of a closure
631 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
632 * g_closure_ref() should be called prior to this function.
635 g_closure_sink (GClosure *closure)
637 g_return_if_fail (closure != NULL);
638 g_return_if_fail (closure->ref_count > 0);
640 /* floating is basically a kludge to avoid creating closures
641 * with a ref_count of 0. so the intial ref_count a closure has
642 * is unowned. with invoking g_closure_sink() code may
643 * indicate that it takes over that intiial ref_count.
645 if (closure->floating)
647 gboolean was_floating;
648 SWAP (closure, floating, FALSE, &was_floating);
649 /* unref floating flag only once */
651 g_closure_unref (closure);
656 * g_closure_remove_invalidate_notifier:
657 * @closure: a #GClosure
658 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
659 * when registering @notify_func
660 * @notify_func: the callback function to remove
662 * Removes an invalidation notifier.
665 * Notice that notifiers are automatically removed after they are run.
668 g_closure_remove_invalidate_notifier (GClosure *closure,
669 gpointer notify_data,
670 GClosureNotify notify_func)
672 g_return_if_fail (closure != NULL);
673 g_return_if_fail (notify_func != NULL);
675 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
676 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
677 closure->data == notify_data)
678 closure->marshal = NULL;
679 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
680 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
681 notify_func, notify_data);
685 * g_closure_remove_finalize_notifier:
686 * @closure: a #GClosure
687 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
688 * when registering @notify_func
689 * @notify_func: the callback function to remove
691 * Removes a finalization notifier.
693 * Notice that notifiers are automatically removed after they are run.
696 g_closure_remove_finalize_notifier (GClosure *closure,
697 gpointer notify_data,
698 GClosureNotify notify_func)
700 g_return_if_fail (closure != NULL);
701 g_return_if_fail (notify_func != NULL);
703 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
704 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
705 closure->data == notify_data)
706 closure->marshal = NULL;
707 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
708 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
709 notify_func, notify_data);
714 * @closure: a #GClosure
715 * @return_value: a #GValue to store the return value. May be %NULL if the
716 * callback of @closure doesn't return a value.
717 * @n_param_values: the length of the @param_values array
718 * @param_values: an array of #GValue<!-- -->s holding the arguments on
719 * which to invoke the callback of @closure
720 * @invocation_hint: a context-dependent invocation hint
722 * Invokes the closure, i.e. executes the callback represented by the @closure.
725 g_closure_invoke (GClosure *closure,
726 GValue /*out*/ *return_value,
727 guint n_param_values,
728 const GValue *param_values,
729 gpointer invocation_hint)
731 g_return_if_fail (closure != NULL);
733 g_closure_ref (closure); /* preserve floating flag */
734 if (!closure->is_invalid)
736 GClosureMarshal marshal;
737 gpointer marshal_data;
738 gboolean in_marshal = closure->in_marshal;
740 g_return_if_fail (closure->marshal || closure->meta_marshal);
742 SET (closure, in_marshal, TRUE);
743 if (closure->meta_marshal)
745 marshal_data = closure->notifiers[0].data;
746 marshal = (GClosureMarshal) closure->notifiers[0].notify;
751 marshal = closure->marshal;
754 closure_invoke_notifiers (closure, PRE_NOTIFY);
757 n_param_values, param_values,
761 closure_invoke_notifiers (closure, POST_NOTIFY);
762 SET (closure, in_marshal, in_marshal);
764 g_closure_unref (closure);
768 * g_closure_set_marshal:
769 * @closure: a #GClosure
770 * @marshal: a #GClosureMarshal function
772 * Sets the marshaller of @closure. The <literal>marshal_data</literal>
773 * of @marshal provides a way for a meta marshaller to provide additional
774 * information to the marshaller. (See g_closure_set_meta_marshal().) For
775 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
776 * functions), what it provides is a callback function to use instead of
777 * @closure->callback.
780 g_closure_set_marshal (GClosure *closure,
781 GClosureMarshal marshal)
783 g_return_if_fail (closure != NULL);
784 g_return_if_fail (marshal != NULL);
786 if (closure->marshal && closure->marshal != marshal)
787 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
788 closure->marshal, marshal);
790 closure->marshal = marshal;
795 * @callback_func: the function to invoke
796 * @user_data: user data to pass to @callback_func
797 * @destroy_data: destroy notify to be called when @user_data is no longer used
799 * Creates a new closure which invokes @callback_func with @user_data as
800 * the last parameter.
802 * Returns: a new #GCClosure
805 g_cclosure_new (GCallback callback_func,
807 GClosureNotify destroy_data)
811 g_return_val_if_fail (callback_func != NULL, NULL);
813 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
815 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
816 ((GCClosure*) closure)->callback = (gpointer) callback_func;
822 * g_cclosure_new_swap:
823 * @callback_func: the function to invoke
824 * @user_data: user data to pass to @callback_func
825 * @destroy_data: destroy notify to be called when @user_data is no longer used
827 * Creates a new closure which invokes @callback_func with @user_data as
828 * the first parameter.
830 * Returns: a new #GCClosure
833 g_cclosure_new_swap (GCallback callback_func,
835 GClosureNotify destroy_data)
839 g_return_val_if_fail (callback_func != NULL, NULL);
841 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
843 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
844 ((GCClosure*) closure)->callback = (gpointer) callback_func;
845 SET (closure, derivative_flag, TRUE);
851 g_type_class_meta_marshal (GClosure *closure,
852 GValue /*out*/ *return_value,
853 guint n_param_values,
854 const GValue *param_values,
855 gpointer invocation_hint,
856 gpointer marshal_data)
860 /* GType itype = (GType) closure->data; */
861 guint offset = GPOINTER_TO_UINT (marshal_data);
863 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
864 callback = G_STRUCT_MEMBER (gpointer, class, offset);
866 closure->marshal (closure,
868 n_param_values, param_values,
874 g_type_iface_meta_marshal (GClosure *closure,
875 GValue /*out*/ *return_value,
876 guint n_param_values,
877 const GValue *param_values,
878 gpointer invocation_hint,
879 gpointer marshal_data)
883 GType itype = (GType) closure->data;
884 guint offset = GPOINTER_TO_UINT (marshal_data);
886 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
887 callback = G_STRUCT_MEMBER (gpointer, class, offset);
889 closure->marshal (closure,
891 n_param_values, param_values,
897 g_signal_type_cclosure_new (GType itype,
902 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
903 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
905 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
906 if (G_TYPE_IS_INTERFACE (itype))
907 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
909 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
916 * g_cclosure_marshal_VOID__VOID:
917 * @closure: the #GClosure to which the marshaller belongs
918 * @return_value: ignored
920 * @param_values: a #GValue array holding only the instance
921 * @invocation_hint: the invocation hint given as the last argument
922 * to g_closure_invoke()
923 * @marshal_data: additional data specified when registering the marshaller
925 * A marshaller for a #GCClosure with a callback of type
926 * <literal>void (*callback) (gpointer instance, gpointer user_data)</literal>.
929 * g_cclosure_marshal_VOID__BOOLEAN:
930 * @closure: the #GClosure to which the marshaller belongs
931 * @return_value: ignored
933 * @param_values: a #GValue array holding the instance and the #gboolean parameter
934 * @invocation_hint: the invocation hint given as the last argument
935 * to g_closure_invoke()
936 * @marshal_data: additional data specified when registering the marshaller
938 * A marshaller for a #GCClosure with a callback of type
939 * <literal>void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)</literal>.
942 * g_cclosure_marshal_VOID__CHAR:
943 * @closure: the #GClosure to which the marshaller belongs
944 * @return_value: ignored
946 * @param_values: a #GValue array holding the instance and the #gchar parameter
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, gchar arg1, gpointer user_data)</literal>.
955 * g_cclosure_marshal_VOID__UCHAR:
956 * @closure: the #GClosure to which the marshaller belongs
957 * @return_value: ignored
959 * @param_values: a #GValue array holding the instance and the #guchar parameter
960 * @invocation_hint: the invocation hint given as the last argument
961 * to g_closure_invoke()
962 * @marshal_data: additional data specified when registering the marshaller
964 * A marshaller for a #GCClosure with a callback of type
965 * <literal>void (*callback) (gpointer instance, guchar arg1, gpointer user_data)</literal>.
968 * g_cclosure_marshal_VOID__INT:
969 * @closure: the #GClosure to which the marshaller belongs
970 * @return_value: ignored
972 * @param_values: a #GValue array holding the instance and the #gint parameter
973 * @invocation_hint: the invocation hint given as the last argument
974 * to g_closure_invoke()
975 * @marshal_data: additional data specified when registering the marshaller
977 * A marshaller for a #GCClosure with a callback of type
978 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal>.
981 * g_cclosure_marshal_VOID__UINT:
982 * @closure: the #GClosure to which the marshaller belongs
983 * @return_value: ignored
985 * @param_values: a #GValue array holding the instance and the #guint parameter
986 * @invocation_hint: the invocation hint given as the last argument
987 * to g_closure_invoke()
988 * @marshal_data: additional data specified when registering the marshaller
990 * A marshaller for a #GCClosure with a callback of type
991 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer user_data)</literal>.
994 * g_cclosure_marshal_VOID__LONG:
995 * @closure: the #GClosure to which the marshaller belongs
996 * @return_value: ignored
998 * @param_values: a #GValue array holding the instance and the #glong parameter
999 * @invocation_hint: the invocation hint given as the last argument
1000 * to g_closure_invoke()
1001 * @marshal_data: additional data specified when registering the marshaller
1003 * A marshaller for a #GCClosure with a callback of type
1004 * <literal>void (*callback) (gpointer instance, glong arg1, gpointer user_data)</literal>.
1007 * g_cclosure_marshal_VOID__ULONG:
1008 * @closure: the #GClosure to which the marshaller belongs
1009 * @return_value: ignored
1010 * @n_param_values: 2
1011 * @param_values: a #GValue array holding the instance and the #gulong parameter
1012 * @invocation_hint: the invocation hint given as the last argument
1013 * to g_closure_invoke()
1014 * @marshal_data: additional data specified when registering the marshaller
1016 * A marshaller for a #GCClosure with a callback of type
1017 * <literal>void (*callback) (gpointer instance, gulong arg1, gpointer user_data)</literal>.
1020 * g_cclosure_marshal_VOID__ENUM:
1021 * @closure: the #GClosure to which the marshaller belongs
1022 * @return_value: ignored
1023 * @n_param_values: 2
1024 * @param_values: a #GValue array holding the instance and the enumeration parameter
1025 * @invocation_hint: the invocation hint given as the last argument
1026 * to g_closure_invoke()
1027 * @marshal_data: additional data specified when registering the marshaller
1029 * A marshaller for a #GCClosure with a callback of type
1030 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes an enumeration type..
1033 * g_cclosure_marshal_VOID__FLAGS:
1034 * @closure: the #GClosure to which the marshaller belongs
1035 * @return_value: ignored
1036 * @n_param_values: 2
1037 * @param_values: a #GValue array holding the instance and the flags parameter
1038 * @invocation_hint: the invocation hint given as the last argument
1039 * to g_closure_invoke()
1040 * @marshal_data: additional data specified when registering the marshaller
1042 * A marshaller for a #GCClosure with a callback of type
1043 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes a flags type.
1046 * g_cclosure_marshal_VOID__FLOAT:
1047 * @closure: the #GClosure to which the marshaller belongs
1048 * @return_value: ignored
1049 * @n_param_values: 2
1050 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1051 * @invocation_hint: the invocation hint given as the last argument
1052 * to g_closure_invoke()
1053 * @marshal_data: additional data specified when registering the marshaller
1055 * A marshaller for a #GCClosure with a callback of type
1056 * <literal>void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)</literal>.
1059 * g_cclosure_marshal_VOID__DOUBLE:
1060 * @closure: the #GClosure to which the marshaller belongs
1061 * @return_value: ignored
1062 * @n_param_values: 2
1063 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1064 * @invocation_hint: the invocation hint given as the last argument
1065 * to g_closure_invoke()
1066 * @marshal_data: additional data specified when registering the marshaller
1068 * A marshaller for a #GCClosure with a callback of type
1069 * <literal>void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)</literal>.
1072 * g_cclosure_marshal_VOID__STRING:
1073 * @closure: the #GClosure to which the marshaller belongs
1074 * @return_value: ignored
1075 * @n_param_values: 2
1076 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1077 * @invocation_hint: the invocation hint given as the last argument
1078 * to g_closure_invoke()
1079 * @marshal_data: additional data specified when registering the marshaller
1081 * A marshaller for a #GCClosure with a callback of type
1082 * <literal>void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)</literal>.
1085 * g_cclosure_marshal_VOID__PARAM:
1086 * @closure: the #GClosure to which the marshaller belongs
1087 * @return_value: ignored
1088 * @n_param_values: 2
1089 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1090 * @invocation_hint: the invocation hint given as the last argument
1091 * to g_closure_invoke()
1092 * @marshal_data: additional data specified when registering the marshaller
1094 * A marshaller for a #GCClosure with a callback of type
1095 * <literal>void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)</literal>.
1098 * g_cclosure_marshal_VOID__BOXED:
1099 * @closure: the #GClosure to which the marshaller belongs
1100 * @return_value: ignored
1101 * @n_param_values: 2
1102 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1103 * @invocation_hint: the invocation hint given as the last argument
1104 * to g_closure_invoke()
1105 * @marshal_data: additional data specified when registering the marshaller
1107 * A marshaller for a #GCClosure with a callback of type
1108 * <literal>void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)</literal>.
1111 * g_cclosure_marshal_VOID__POINTER:
1112 * @closure: the #GClosure to which the marshaller belongs
1113 * @return_value: ignored
1114 * @n_param_values: 2
1115 * @param_values: a #GValue array holding the instance and the #gpointer parameter
1116 * @invocation_hint: the invocation hint given as the last argument
1117 * to g_closure_invoke()
1118 * @marshal_data: additional data specified when registering the marshaller
1120 * A marshaller for a #GCClosure with a callback of type
1121 * <literal>void (*callback) (gpointer instance, gpointer arg1, gpointer user_data)</literal>.
1124 * g_cclosure_marshal_VOID__OBJECT:
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 #GObject* 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, GOBject *arg1, gpointer user_data)</literal>.
1137 * g_cclosure_marshal_VOID__UINT_POINTER:
1138 * @closure: the #GClosure to which the marshaller belongs
1139 * @return_value: ignored
1140 * @n_param_values: 3
1141 * @param_values: a #GValue array holding instance, arg1 and arg2
1142 * @invocation_hint: the invocation hint given as the last argument
1143 * to g_closure_invoke()
1144 * @marshal_data: additional data specified when registering the marshaller
1146 * A marshaller for a #GCClosure with a callback of type
1147 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)</literal>.
1150 * g_cclosure_marshal_BOOLEAN__FLAGS:
1151 * @closure: the #GClosure to which the marshaller belongs
1152 * @return_value: a #GValue which can store the returned #gboolean
1153 * @n_param_values: 2
1154 * @param_values: a #GValue array holding instance and arg1
1155 * @invocation_hint: the invocation hint given as the last argument
1156 * to g_closure_invoke()
1157 * @marshal_data: additional data specified when registering the marshaller
1159 * A marshaller for a #GCClosure with a callback of type
1160 * <literal>gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter
1161 * denotes a flags type.
1164 * g_cclosure_marshal_BOOL__FLAGS:
1166 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1169 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1170 * @closure: the #GClosure to which the marshaller belongs
1171 * @return_value: a #GValue, which can store the returned string
1172 * @n_param_values: 3
1173 * @param_values: a #GValue array holding instance, arg1 and arg2
1174 * @invocation_hint: the invocation hint given as the last argument
1175 * to g_closure_invoke()
1176 * @marshal_data: additional data specified when registering the marshaller
1178 * A marshaller for a #GCClosure with a callback of type
1179 * <literal>gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)</literal>.
1182 #define __G_CLOSURE_C__
1183 #include "gobjectaliasdef.c"