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
24 * A #GClosure represents a callback supplied by the programmer. It will generally
25 * comprise a function of some kind and a marshaller used to call it. It is the
26 * reponsibility of the marshaller to convert the arguments for the invocation
27 * from #GValue<!-- -->s into a suitable form, perform the callback on the
28 * converted arguments, and transform the return value back into a #GValue.
30 * In the case of C programs, a closure usually just holds a pointer to a function
31 * and maybe a data argument, and the marshaller converts between #GValue<!-- -->
32 * and native C types. The GObject library provides the #GCClosure type for this
33 * purpose. Bindings for other languages need marshallers which
34 * convert between #GValue<!-- -->s and suitable representations in the runtime
35 * of the language in order to use functions written in that languages as
38 * Within GObject, closures play an important role in the implementation of
39 * signals. When a signal is registered, the @c_marshaller argument to
40 * g_signal_new() specifies the default C marshaller for any closure which is
41 * connected to this signal. GObject provides a number of C marshallers
42 * for this purpose, see the g_cclosure_marshal_*() functions. Additional
43 * C marshallers can be generated with the <link linkend="glib-genmarshal"
44 * >glib-genmarshal</link> utility.
45 * Closures can be explicitly connected to signals with
46 * g_signal_connect_closure(), but it usually more convenient to let GObject
47 * create a closure automatically by using one of the g_signal_connect_*()
48 * functions which take a callback function/user data pair.
50 * Using closures has a number of important advantages over a simple
51 * callback function/data pointer combination:
54 * Closures allow the callee to get the types of the callback parameters,
55 * which means that language bindings don't have to write individual glue
56 * for each callback type.
59 * The reference counting of #GClosure makes it easy to handle reentrancy
60 * right; if a callback is removed while it is being invoked, the closure
61 * and it's parameters won't be freed until the invocation finishes.
64 * g_closure_invalidate() and invalidation notifiers allow callbacks to be
65 * automatically removed when the objects they point to go away.
73 * MT safe with regards to reference counting.
77 #include "gobjectalias.h"
81 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
82 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
83 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
84 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
85 #define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \
86 ((cl)->n_guards << 1L))
87 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
88 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
89 (cl)->n_fnotifiers + \
97 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
99 ClosureInt *cunion = (ClosureInt*) _closure; \
100 gint new_int, old_int, success; \
104 tmp.vint = old_int = cunion->vint; \
105 _SET_OLD tmp.closure._field; \
106 tmp.closure._field _OP _value; \
107 _SET_NEW tmp.closure._field; \
108 new_int = tmp.vint; \
109 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
111 while (!success && _must_set); \
114 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
115 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
116 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
117 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
118 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
119 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
121 #if 0 /* for non-thread-safe closures */
122 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
123 #define SET(cl,f,v) (void) (cl->f = v)
124 #define INC(cl,f) (void) (cl->f += 1)
125 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
126 #define DEC(cl,f) (void) (cl->f -= 1)
127 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
138 /* --- functions --- */
140 * g_closure_new_simple:
141 * @sizeof_closure: the size of the structure to allocate, must be at least
142 * <literal>sizeof (GClosure)</literal>
143 * @data: data to store in the @data field of the newly allocated #GClosure
145 * Allocates a struct of the given size and initializes the initial part
146 * as a #GClosure. This function is mainly useful when implementing new types
150 * typedef struct _MyClosure MyClosure;
154 * // extra data goes here
158 * my_closure_finalize (gpointer notify_data,
161 * MyClosure *my_closure = (MyClosure *)closure;
163 * // free extra data here
166 * MyClosure *my_closure_new (gpointer data)
169 * MyClosure *my_closure;
171 * closure = g_closure_new_simple (sizeof (MyClosure), data);
172 * my_closure = (MyClosure *) closure;
174 * // initialize extra data here
176 * g_closure_add_finalize_notifier (closure, notify_data,
177 * my_closure_finalize);
182 * Returns: a newly allocated #GClosure
185 g_closure_new_simple (guint sizeof_closure,
190 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
192 closure = g_malloc0 (sizeof_closure);
193 SET (closure, ref_count, 1);
194 SET (closure, meta_marshal, 0);
195 SET (closure, n_guards, 0);
196 SET (closure, n_fnotifiers, 0);
197 SET (closure, n_inotifiers, 0);
198 SET (closure, in_inotify, FALSE);
199 SET (closure, floating, TRUE);
200 SET (closure, derivative_flag, 0);
201 SET (closure, in_marshal, FALSE);
202 SET (closure, is_invalid, FALSE);
203 closure->marshal = NULL;
204 closure->data = data;
205 closure->notifiers = NULL;
206 memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
212 closure_invoke_notifiers (GClosure *closure,
216 * meta_marshal n_guards n_guards n_fnotif. n_inotifiers
217 * ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]]
219 * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards;
220 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
222 * constrains/catches:
223 * - closure->notifiers may be reloacted during callback
224 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
225 * - i.e. callbacks can be removed/added during invocation
226 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
227 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
228 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
229 * + closure->meta_marshal is const for all cases
230 * + none of the callbacks can cause recursion
231 * + closure->n_inotifiers is const 0 during FNOTIFY
235 GClosureNotifyData *ndata;
238 while (closure->n_fnotifiers)
241 DEC_ASSIGN (closure, n_fnotifiers, &n);
243 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
244 closure->marshal = (GClosureMarshal) ndata->notify;
245 closure->data = ndata->data;
246 ndata->notify (ndata->data, closure);
248 closure->marshal = NULL;
249 closure->data = NULL;
252 SET (closure, in_inotify, TRUE);
253 while (closure->n_inotifiers)
256 DEC_ASSIGN (closure, n_inotifiers, &n);
258 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
259 closure->marshal = (GClosureMarshal) ndata->notify;
260 closure->data = ndata->data;
261 ndata->notify (ndata->data, closure);
263 closure->marshal = NULL;
264 closure->data = NULL;
265 SET (closure, in_inotify, FALSE);
268 i = closure->n_guards;
269 offs = closure->meta_marshal;
272 ndata = closure->notifiers + offs + i;
273 ndata->notify (ndata->data, closure);
277 i = closure->n_guards;
278 offs = closure->meta_marshal + i;
281 ndata = closure->notifiers + offs + i;
282 ndata->notify (ndata->data, closure);
289 * g_closure_set_meta_marshal:
290 * @closure: a #GClosure
291 * @marshal_data: context-dependent data to pass to @meta_marshal
292 * @meta_marshal: a #GClosureMarshal function
294 * Sets the meta marshaller of @closure.
295 * A meta marshaller wraps @closure->marshal and modifies the way it is called
296 * in some fashion. The most common use of this facility is for C callbacks.
297 * The same marshallers (generated by
298 * <link linkend="glib-genmarshal">glib-genmarshal</link>) are used everywhere,
299 * but the way that we get the callback function differs. In most cases we want
300 * to use @closure->callback, but in other cases we want to use some
301 * different technique to retrieve the callback function.
303 * For example, class closures for signals (see g_signal_type_cclosure_new())
304 * retrieve the callback function from a fixed offset in the class structure.
305 * The meta marshaller retrieves the right callback and passes it to the
306 * marshaller as the @marshal_data argument.
309 g_closure_set_meta_marshal (GClosure *closure,
310 gpointer marshal_data,
311 GClosureMarshal meta_marshal)
313 GClosureNotifyData *notifiers;
315 g_return_if_fail (closure != NULL);
316 g_return_if_fail (meta_marshal != NULL);
317 g_return_if_fail (closure->is_invalid == FALSE);
318 g_return_if_fail (closure->in_marshal == FALSE);
319 g_return_if_fail (closure->meta_marshal == 0);
321 notifiers = closure->notifiers;
322 closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
325 /* usually the meta marshal will be setup right after creation, so the
326 * g_memmove() should be rare-case scenario
328 g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
331 closure->notifiers[0].data = marshal_data;
332 closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
333 SET (closure, meta_marshal, 1);
337 * g_closure_add_marshal_guards:
338 * @closure: a #GClosure
339 * @pre_marshal_data: data to pass to @pre_marshal_notify
340 * @pre_marshal_notify: a function to call before the closure callback
341 * @post_marshal_data: data to pass to @post_marshal_notify
342 * @post_marshal_notify: a function to call after the closure callback
344 * Adds a pair of notifiers which get invoked before and after the closure
345 * callback, respectively. This is typically used to protect the extra arguments
346 * for the duration of the callback. See g_object_watch_closure() for an
347 * example of marshal guards.
350 g_closure_add_marshal_guards (GClosure *closure,
351 gpointer pre_marshal_data,
352 GClosureNotify pre_marshal_notify,
353 gpointer post_marshal_data,
354 GClosureNotify post_marshal_notify)
358 g_return_if_fail (closure != NULL);
359 g_return_if_fail (pre_marshal_notify != NULL);
360 g_return_if_fail (post_marshal_notify != NULL);
361 g_return_if_fail (closure->is_invalid == FALSE);
362 g_return_if_fail (closure->in_marshal == FALSE);
363 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
365 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
366 if (closure->n_inotifiers)
367 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
368 closure->n_fnotifiers +
369 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
370 closure->n_fnotifiers + 0)];
371 if (closure->n_inotifiers > 1)
372 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
373 closure->n_fnotifiers +
374 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
375 closure->n_fnotifiers + 1)];
376 if (closure->n_fnotifiers)
377 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
378 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
379 if (closure->n_fnotifiers > 1)
380 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
381 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
382 if (closure->n_guards)
383 closure->notifiers[(closure->meta_marshal +
385 closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
386 i = closure->n_guards;
387 closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
388 closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
389 closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
390 closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
391 INC (closure, n_guards);
395 * g_closure_add_finalize_notifier:
396 * @closure: a #GClosure
397 * @notify_data: data to pass to @notify_func
398 * @notify_func: the callback function to register
400 * Registers a finalization notifier which will be called when the reference
401 * count of @closure goes down to 0. Multiple finalization notifiers on a
402 * single closure are invoked in unspecified order. If a single call to
403 * g_closure_unref() results in the closure being both invalidated and
404 * finalized, then the invalidate notifiers will be run before the finalize
408 g_closure_add_finalize_notifier (GClosure *closure,
409 gpointer notify_data,
410 GClosureNotify notify_func)
414 g_return_if_fail (closure != NULL);
415 g_return_if_fail (notify_func != NULL);
416 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
418 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
419 if (closure->n_inotifiers)
420 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
421 closure->n_fnotifiers +
422 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
423 closure->n_fnotifiers + 0)];
424 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
425 closure->notifiers[i].data = notify_data;
426 closure->notifiers[i].notify = notify_func;
427 INC (closure, n_fnotifiers);
431 * g_closure_add_invalidate_notifier:
432 * @closure: a #GClosure
433 * @notify_data: data to pass to @notify_func
434 * @notify_func: the callback function to register
436 * Registers an invalidation notifier which will be called when the @closure
437 * is invalidated with g_closure_invalidate(). Invalidation notifiers are
438 * invoked before finalization notifiers, in an unspecified order.
441 g_closure_add_invalidate_notifier (GClosure *closure,
442 gpointer notify_data,
443 GClosureNotify notify_func)
447 g_return_if_fail (closure != NULL);
448 g_return_if_fail (notify_func != NULL);
449 g_return_if_fail (closure->is_invalid == FALSE);
450 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
452 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
453 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
454 closure->notifiers[i].data = notify_data;
455 closure->notifiers[i].notify = notify_func;
456 INC (closure, n_inotifiers);
459 static inline gboolean
460 closure_try_remove_inotify (GClosure *closure,
461 gpointer notify_data,
462 GClosureNotify notify_func)
464 GClosureNotifyData *ndata, *nlast;
466 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
467 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
468 if (ndata->notify == notify_func && ndata->data == notify_data)
470 DEC (closure, n_inotifiers);
479 static inline gboolean
480 closure_try_remove_fnotify (GClosure *closure,
481 gpointer notify_data,
482 GClosureNotify notify_func)
484 GClosureNotifyData *ndata, *nlast;
486 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
487 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
488 if (ndata->notify == notify_func && ndata->data == notify_data)
490 DEC (closure, n_fnotifiers);
493 if (closure->n_inotifiers)
494 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
495 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
496 closure->n_fnotifiers +
497 closure->n_inotifiers)];
505 * @closure: #GClosure to increment the reference count on
507 * Increments the reference count on a closure to force it staying
508 * alive while the caller holds a pointer to it.
510 * Returns: The @closure passed in, for convenience
513 g_closure_ref (GClosure *closure)
516 g_return_val_if_fail (closure != NULL, NULL);
517 g_return_val_if_fail (closure->ref_count > 0, NULL);
518 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
520 INC_ASSIGN (closure, ref_count, &new_ref_count);
521 g_return_val_if_fail (new_ref_count > 1, NULL);
527 * g_closure_invalidate:
528 * @closure: GClosure to invalidate
530 * Sets a flag on the closure to indicate that it's calling environment has
531 * become invalid, and thus causes any future invocations of g_closure_invoke()
532 * on this @closure to be ignored. Also, invalidation notifiers installed on
533 * the closure will be called at this point. Note that unless you are holding
534 * a reference to the closure yourself, the invalidation notifiers may unref
535 * the closure and cause it to be destroyed, so if you need to access the
536 * closure after calling g_closure_invalidate(), make sure that you've
537 * previously called g_closure_ref().
539 * Note that g_closure_invalidate() will also be called when the reference count
540 * of a closure drops to zero (unless it has already been invalidated before).
543 g_closure_invalidate (GClosure *closure)
545 g_return_if_fail (closure != NULL);
547 if (!closure->is_invalid)
549 gboolean was_invalid;
550 g_closure_ref (closure); /* preserve floating flag */
551 SWAP (closure, is_invalid, TRUE, &was_invalid);
552 /* invalidate only once */
554 closure_invoke_notifiers (closure, INOTIFY);
555 g_closure_unref (closure);
561 * @closure: #GClosure to decrement the reference count on
563 * Decrements the reference count of a closure after it was previously
564 * incremented by the same caller. If no other callers are using the closure,
565 * then the closure will be destroyed and freed.
568 g_closure_unref (GClosure *closure)
572 g_return_if_fail (closure != NULL);
573 g_return_if_fail (closure->ref_count > 0);
575 if (closure->ref_count == 1) /* last unref, invalidate first */
576 g_closure_invalidate (closure);
578 DEC_ASSIGN (closure, ref_count, &new_ref_count);
580 if (new_ref_count == 0)
582 closure_invoke_notifiers (closure, FNOTIFY);
583 g_free (closure->notifiers);
590 * @closure: #GClosure to decrement the initial reference count on, if it's
593 * Takes over the initial ownership of a closure.
594 * Each closure is initially created in a <firstterm>floating</firstterm> state,
595 * which means that the initial reference count is not owned by any caller.
596 * g_closure_sink() checks to see if the object is still floating, and if so,
597 * unsets the floating state and decreases the reference count. If the closure
598 * is not floating, g_closure_sink() does nothing. The reason for the existance
599 * of the floating state is to prevent cumbersome code sequences like:
601 * closure = g_cclosure_new (cb_func, cb_data);
602 * g_source_set_closure (source, closure);
603 * g_closure_unref (closure); // XXX GObject doesn't really need this
605 * Because g_source_set_closure() (and similar functions) take ownership of the
606 * initial reference count, if it is unowned, we instead can write:
608 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
611 * Generally, this function is used together with g_closure_ref(). Ane example
612 * of storing a closure for later notification looks like:
614 * static GClosure *notify_closure = NULL;
616 * foo_notify_set_closure (GClosure *closure)
618 * if (notify_closure)
619 * g_closure_unref (notify_closure);
620 * notify_closure = closure;
621 * if (notify_closure)
623 * g_closure_ref (notify_closure);
624 * g_closure_sink (notify_closure);
629 * Because g_closure_sink() may decrement the reference count of a closure
630 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
631 * g_closure_ref() should be called prior to this function.
634 g_closure_sink (GClosure *closure)
636 g_return_if_fail (closure != NULL);
637 g_return_if_fail (closure->ref_count > 0);
639 /* floating is basically a kludge to avoid creating closures
640 * with a ref_count of 0. so the intial ref_count a closure has
641 * is unowned. with invoking g_closure_sink() code may
642 * indicate that it takes over that intiial ref_count.
644 if (closure->floating)
646 gboolean was_floating;
647 SWAP (closure, floating, FALSE, &was_floating);
648 /* unref floating flag only once */
650 g_closure_unref (closure);
655 * g_closure_remove_invalidate_notifier:
656 * @closure: a #GClosure
657 * @notify_data: data which was passed to g_closure_add_invalidate_notifier()
658 * when registering @notify_func
659 * @notify_func: the callback function to remove
661 * Removes an invalidation notifier.
664 * Notice that notifiers are automatically removed after they are run.
667 g_closure_remove_invalidate_notifier (GClosure *closure,
668 gpointer notify_data,
669 GClosureNotify notify_func)
671 g_return_if_fail (closure != NULL);
672 g_return_if_fail (notify_func != NULL);
674 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
675 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
676 closure->data == notify_data)
677 closure->marshal = NULL;
678 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
679 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
680 notify_func, notify_data);
684 * g_closure_remove_finalize_notifier:
685 * @closure: a #GClosure
686 * @notify_data: data which was passed to g_closure_add_finalize_notifier()
687 * when registering @notify_func
688 * @notify_func: the callback function to remove
690 * Removes a finalization notifier.
692 * Notice that notifiers are automatically removed after they are run.
695 g_closure_remove_finalize_notifier (GClosure *closure,
696 gpointer notify_data,
697 GClosureNotify notify_func)
699 g_return_if_fail (closure != NULL);
700 g_return_if_fail (notify_func != NULL);
702 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
703 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
704 closure->data == notify_data)
705 closure->marshal = NULL;
706 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
707 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
708 notify_func, notify_data);
713 * @closure: a #GClosure
714 * @return_value: a #GValue to store the return value. May be %NULL if the
715 * callback of @closure doesn't return a value.
716 * @n_param_values: the length of the @param_values array
717 * @param_values: an array of #GValue<!-- -->s holding the arguments on
718 * which to invoke the callback of @closure
719 * @invocation_hint: a context-dependent invocation hint
721 * Invokes the closure, i.e. executes the callback represented by the @closure.
724 g_closure_invoke (GClosure *closure,
725 GValue /*out*/ *return_value,
726 guint n_param_values,
727 const GValue *param_values,
728 gpointer invocation_hint)
730 g_return_if_fail (closure != NULL);
732 g_closure_ref (closure); /* preserve floating flag */
733 if (!closure->is_invalid)
735 GClosureMarshal marshal;
736 gpointer marshal_data;
737 gboolean in_marshal = closure->in_marshal;
739 g_return_if_fail (closure->marshal || closure->meta_marshal);
741 SET (closure, in_marshal, TRUE);
742 if (closure->meta_marshal)
744 marshal_data = closure->notifiers[0].data;
745 marshal = (GClosureMarshal) closure->notifiers[0].notify;
750 marshal = closure->marshal;
753 closure_invoke_notifiers (closure, PRE_NOTIFY);
756 n_param_values, param_values,
760 closure_invoke_notifiers (closure, POST_NOTIFY);
761 SET (closure, in_marshal, in_marshal);
763 g_closure_unref (closure);
767 * g_closure_set_marshal:
768 * @closure: a #GClosure
769 * @marshal: a #GClosureMarshal function
771 * Sets the marshaller of @closure. The <literal>marshal_data</literal>
772 * of @marshal provides a way for a meta marshaller to provide additional
773 * information to the marshaller. (See g_closure_set_meta_marshal().) For
774 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
775 * functions), what it provides is a callback function to use instead of
776 * @closure->callback.
779 g_closure_set_marshal (GClosure *closure,
780 GClosureMarshal marshal)
782 g_return_if_fail (closure != NULL);
783 g_return_if_fail (marshal != NULL);
785 if (closure->marshal && closure->marshal != marshal)
786 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
787 closure->marshal, marshal);
789 closure->marshal = marshal;
794 * @callback_func: the function to invoke
795 * @user_data: user data to pass to @callback_func
796 * @destroy_data: destroy notify to be called when @user_data is no longer used
798 * Creates a new closure which invokes @callback_func with @user_data as
799 * the last parameter.
801 * Returns: a new #GCClosure
804 g_cclosure_new (GCallback callback_func,
806 GClosureNotify destroy_data)
810 g_return_val_if_fail (callback_func != NULL, NULL);
812 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
814 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
815 ((GCClosure*) closure)->callback = (gpointer) callback_func;
821 * g_cclosure_new_swap:
822 * @callback_func: the function to invoke
823 * @user_data: user data to pass to @callback_func
824 * @destroy_data: destroy notify to be called when @user_data is no longer used
826 * Creates a new closure which invokes @callback_func with @user_data as
827 * the first parameter.
829 * Returns: a new #GCClosure
832 g_cclosure_new_swap (GCallback callback_func,
834 GClosureNotify destroy_data)
838 g_return_val_if_fail (callback_func != NULL, NULL);
840 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
842 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
843 ((GCClosure*) closure)->callback = (gpointer) callback_func;
844 SET (closure, derivative_flag, TRUE);
850 g_type_class_meta_marshal (GClosure *closure,
851 GValue /*out*/ *return_value,
852 guint n_param_values,
853 const GValue *param_values,
854 gpointer invocation_hint,
855 gpointer marshal_data)
859 /* GType itype = (GType) closure->data; */
860 guint offset = GPOINTER_TO_UINT (marshal_data);
862 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
863 callback = G_STRUCT_MEMBER (gpointer, class, offset);
865 closure->marshal (closure,
867 n_param_values, param_values,
873 g_type_iface_meta_marshal (GClosure *closure,
874 GValue /*out*/ *return_value,
875 guint n_param_values,
876 const GValue *param_values,
877 gpointer invocation_hint,
878 gpointer marshal_data)
882 GType itype = (GType) closure->data;
883 guint offset = GPOINTER_TO_UINT (marshal_data);
885 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
886 callback = G_STRUCT_MEMBER (gpointer, class, offset);
888 closure->marshal (closure,
890 n_param_values, param_values,
896 g_signal_type_cclosure_new (GType itype,
901 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
902 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
904 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
905 if (G_TYPE_IS_INTERFACE (itype))
906 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
908 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
915 * g_cclosure_marshal_VOID__VOID:
916 * @closure: the #GClosure to which the marshaller belongs
917 * @return_value: ignored
919 * @param_values: a #GValue array holding only the instance
920 * @invocation_hint: the invocation hint given as the last argument
921 * to g_closure_invoke()
922 * @marshal_data: additional data specified when registering the marshaller
924 * A marshaller for a #GCClosure with a callback of type
925 * <literal>void (*callback) (gpointer instance, gpointer user_data)</literal>.
928 * g_cclosure_marshal_VOID__BOOLEAN:
929 * @closure: the #GClosure to which the marshaller belongs
930 * @return_value: ignored
932 * @param_values: a #GValue array holding the instance and the #gboolean parameter
933 * @invocation_hint: the invocation hint given as the last argument
934 * to g_closure_invoke()
935 * @marshal_data: additional data specified when registering the marshaller
937 * A marshaller for a #GCClosure with a callback of type
938 * <literal>void (*callback) (gpointer instance, gboolean arg1, gpointer user_data)</literal>.
941 * g_cclosure_marshal_VOID__CHAR:
942 * @closure: the #GClosure to which the marshaller belongs
943 * @return_value: ignored
945 * @param_values: a #GValue array holding the instance and the #gchar parameter
946 * @invocation_hint: the invocation hint given as the last argument
947 * to g_closure_invoke()
948 * @marshal_data: additional data specified when registering the marshaller
950 * A marshaller for a #GCClosure with a callback of type
951 * <literal>void (*callback) (gpointer instance, gchar arg1, gpointer user_data)</literal>.
954 * g_cclosure_marshal_VOID__UCHAR:
955 * @closure: the #GClosure to which the marshaller belongs
956 * @return_value: ignored
958 * @param_values: a #GValue array holding the instance and the #guchar parameter
959 * @invocation_hint: the invocation hint given as the last argument
960 * to g_closure_invoke()
961 * @marshal_data: additional data specified when registering the marshaller
963 * A marshaller for a #GCClosure with a callback of type
964 * <literal>void (*callback) (gpointer instance, guchar arg1, gpointer user_data)</literal>.
967 * g_cclosure_marshal_VOID__INT:
968 * @closure: the #GClosure to which the marshaller belongs
969 * @return_value: ignored
971 * @param_values: a #GValue array holding the instance and the #gint parameter
972 * @invocation_hint: the invocation hint given as the last argument
973 * to g_closure_invoke()
974 * @marshal_data: additional data specified when registering the marshaller
976 * A marshaller for a #GCClosure with a callback of type
977 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal>.
980 * g_cclosure_marshal_VOID__UINT:
981 * @closure: the #GClosure to which the marshaller belongs
982 * @return_value: ignored
984 * @param_values: a #GValue array holding the instance and the #guint parameter
985 * @invocation_hint: the invocation hint given as the last argument
986 * to g_closure_invoke()
987 * @marshal_data: additional data specified when registering the marshaller
989 * A marshaller for a #GCClosure with a callback of type
990 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer user_data)</literal>.
993 * g_cclosure_marshal_VOID__LONG:
994 * @closure: the #GClosure to which the marshaller belongs
995 * @return_value: ignored
997 * @param_values: a #GValue array holding the instance and the #glong parameter
998 * @invocation_hint: the invocation hint given as the last argument
999 * to g_closure_invoke()
1000 * @marshal_data: additional data specified when registering the marshaller
1002 * A marshaller for a #GCClosure with a callback of type
1003 * <literal>void (*callback) (gpointer instance, glong arg1, gpointer user_data)</literal>.
1006 * g_cclosure_marshal_VOID__ULONG:
1007 * @closure: the #GClosure to which the marshaller belongs
1008 * @return_value: ignored
1009 * @n_param_values: 2
1010 * @param_values: a #GValue array holding the instance and the #gulong parameter
1011 * @invocation_hint: the invocation hint given as the last argument
1012 * to g_closure_invoke()
1013 * @marshal_data: additional data specified when registering the marshaller
1015 * A marshaller for a #GCClosure with a callback of type
1016 * <literal>void (*callback) (gpointer instance, gulong arg1, gpointer user_data)</literal>.
1019 * g_cclosure_marshal_VOID__ENUM:
1020 * @closure: the #GClosure to which the marshaller belongs
1021 * @return_value: ignored
1022 * @n_param_values: 2
1023 * @param_values: a #GValue array holding the instance and the enumeration parameter
1024 * @invocation_hint: the invocation hint given as the last argument
1025 * to g_closure_invoke()
1026 * @marshal_data: additional data specified when registering the marshaller
1028 * A marshaller for a #GCClosure with a callback of type
1029 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes an enumeration type..
1032 * g_cclosure_marshal_VOID__FLAGS:
1033 * @closure: the #GClosure to which the marshaller belongs
1034 * @return_value: ignored
1035 * @n_param_values: 2
1036 * @param_values: a #GValue array holding the instance and the flags parameter
1037 * @invocation_hint: the invocation hint given as the last argument
1038 * to g_closure_invoke()
1039 * @marshal_data: additional data specified when registering the marshaller
1041 * A marshaller for a #GCClosure with a callback of type
1042 * <literal>void (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter denotes a flags type.
1045 * g_cclosure_marshal_VOID__FLOAT:
1046 * @closure: the #GClosure to which the marshaller belongs
1047 * @return_value: ignored
1048 * @n_param_values: 2
1049 * @param_values: a #GValue array holding the instance and the #gfloat parameter
1050 * @invocation_hint: the invocation hint given as the last argument
1051 * to g_closure_invoke()
1052 * @marshal_data: additional data specified when registering the marshaller
1054 * A marshaller for a #GCClosure with a callback of type
1055 * <literal>void (*callback) (gpointer instance, gfloat arg1, gpointer user_data)</literal>.
1058 * g_cclosure_marshal_VOID__DOUBLE:
1059 * @closure: the #GClosure to which the marshaller belongs
1060 * @return_value: ignored
1061 * @n_param_values: 2
1062 * @param_values: a #GValue array holding the instance and the #gdouble parameter
1063 * @invocation_hint: the invocation hint given as the last argument
1064 * to g_closure_invoke()
1065 * @marshal_data: additional data specified when registering the marshaller
1067 * A marshaller for a #GCClosure with a callback of type
1068 * <literal>void (*callback) (gpointer instance, gdouble arg1, gpointer user_data)</literal>.
1071 * g_cclosure_marshal_VOID__STRING:
1072 * @closure: the #GClosure to which the marshaller belongs
1073 * @return_value: ignored
1074 * @n_param_values: 2
1075 * @param_values: a #GValue array holding the instance and the #gchar* parameter
1076 * @invocation_hint: the invocation hint given as the last argument
1077 * to g_closure_invoke()
1078 * @marshal_data: additional data specified when registering the marshaller
1080 * A marshaller for a #GCClosure with a callback of type
1081 * <literal>void (*callback) (gpointer instance, const gchar *arg1, gpointer user_data)</literal>.
1084 * g_cclosure_marshal_VOID__PARAM:
1085 * @closure: the #GClosure to which the marshaller belongs
1086 * @return_value: ignored
1087 * @n_param_values: 2
1088 * @param_values: a #GValue array holding the instance and the #GParamSpec* parameter
1089 * @invocation_hint: the invocation hint given as the last argument
1090 * to g_closure_invoke()
1091 * @marshal_data: additional data specified when registering the marshaller
1093 * A marshaller for a #GCClosure with a callback of type
1094 * <literal>void (*callback) (gpointer instance, GParamSpec *arg1, gpointer user_data)</literal>.
1097 * g_cclosure_marshal_VOID__BOXED:
1098 * @closure: the #GClosure to which the marshaller belongs
1099 * @return_value: ignored
1100 * @n_param_values: 2
1101 * @param_values: a #GValue array holding the instance and the #GBoxed* parameter
1102 * @invocation_hint: the invocation hint given as the last argument
1103 * to g_closure_invoke()
1104 * @marshal_data: additional data specified when registering the marshaller
1106 * A marshaller for a #GCClosure with a callback of type
1107 * <literal>void (*callback) (gpointer instance, GBoxed *arg1, gpointer user_data)</literal>.
1110 * g_cclosure_marshal_VOID__POINTER:
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 #gpointer 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, gpointer arg1, gpointer user_data)</literal>.
1123 * g_cclosure_marshal_VOID__OBJECT:
1124 * @closure: the #GClosure to which the marshaller belongs
1125 * @return_value: ignored
1126 * @n_param_values: 2
1127 * @param_values: a #GValue array holding the instance and the #GObject* parameter
1128 * @invocation_hint: the invocation hint given as the last argument
1129 * to g_closure_invoke()
1130 * @marshal_data: additional data specified when registering the marshaller
1132 * A marshaller for a #GCClosure with a callback of type
1133 * <literal>void (*callback) (gpointer instance, GOBject *arg1, gpointer user_data)</literal>.
1136 * g_cclosure_marshal_VOID__UINT_POINTER:
1137 * @closure: the #GClosure to which the marshaller belongs
1138 * @return_value: ignored
1139 * @n_param_values: 3
1140 * @param_values: a #GValue array holding instance, arg1 and arg2
1141 * @invocation_hint: the invocation hint given as the last argument
1142 * to g_closure_invoke()
1143 * @marshal_data: additional data specified when registering the marshaller
1145 * A marshaller for a #GCClosure with a callback of type
1146 * <literal>void (*callback) (gpointer instance, guint arg1, gpointer arg2, gpointer user_data)</literal>.
1149 * g_cclosure_marshal_BOOLEAN__FLAGS:
1150 * @closure: the #GClosure to which the marshaller belongs
1151 * @return_value: a #GValue which can store the returned #gboolean
1152 * @n_param_values: 2
1153 * @param_values: a #GValue array holding instance and arg1
1154 * @invocation_hint: the invocation hint given as the last argument
1155 * to g_closure_invoke()
1156 * @marshal_data: additional data specified when registering the marshaller
1158 * A marshaller for a #GCClosure with a callback of type
1159 * <literal>gboolean (*callback) (gpointer instance, gint arg1, gpointer user_data)</literal> where the #gint parameter
1160 * denotes a flags type.
1163 * g_cclosure_marshal_BOOL__FLAGS:
1165 * Another name for g_cclosure_marshal_BOOLEAN__FLAGS().
1168 * g_cclosure_marshal_STRING__OBJECT_POINTER:
1169 * @closure: the #GClosure to which the marshaller belongs
1170 * @return_value: a #GValue, which can store the returned string
1171 * @n_param_values: 3
1172 * @param_values: a #GValue array holding instance, arg1 and arg2
1173 * @invocation_hint: the invocation hint given as the last argument
1174 * to g_closure_invoke()
1175 * @marshal_data: additional data specified when registering the marshaller
1177 * A marshaller for a #GCClosure with a callback of type
1178 * <literal>gchar* (*callback) (gpointer instance, GObject *arg1, gpointer arg2, gpointer user_data)</literal>.
1181 #define __G_CLOSURE_C__
1182 #include "gobjectaliasdef.c"