1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
3 * Copyright (C) 2005 Imendio AB
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef __G_CLOSURE_H__
21 #define __G_CLOSURE_H__
23 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
24 #error "Only <glib-object.h> can be included directly."
27 #include <gobject/gtype.h>
33 * G_CLOSURE_NEEDS_MARSHAL:
34 * @closure: a #GClosure
36 * Check if the closure still needs a marshaller. See g_closure_set_marshal().
38 * Returns: %TRUE if a #GClosureMarshal marshaller has not yet been set on
41 #define G_CLOSURE_NEEDS_MARSHAL(closure) (((GClosure*) (closure))->marshal == NULL)
43 * G_CLOSURE_N_NOTIFIERS:
46 * Get the total number of notifiers connected with the closure @cl.
48 * The count includes the meta marshaller, the finalize and invalidate notifiers
49 * and the marshal guards. Note that each guard counts as two notifiers.
50 * See g_closure_set_meta_marshal(), g_closure_add_finalize_notifier(),
51 * g_closure_add_invalidate_notifier() and g_closure_add_marshal_guards().
53 * Returns: number of notifiers
55 #define G_CLOSURE_N_NOTIFIERS(cl) (((cl)->n_guards << 1L) + \
56 (cl)->n_fnotifiers + (cl)->n_inotifiers)
58 * G_CCLOSURE_SWAP_DATA:
59 * @cclosure: a #GCClosure
61 * Checks whether the user data of the #GCClosure should be passed as the
62 * first parameter to the callback. See g_cclosure_new_swap().
64 * Returns: %TRUE if data has to be swapped.
66 #define G_CCLOSURE_SWAP_DATA(cclosure) (((GClosure*) (cclosure))->derivative_flag)
69 * @f: a function pointer.
71 * Cast a function pointer to a #GCallback.
73 #define G_CALLBACK(f) ((GCallback) (f))
77 typedef struct _GClosure GClosure;
78 typedef struct _GClosureNotifyData GClosureNotifyData;
83 * The type used for callback functions in structure definitions and function
86 * This doesn't mean that all callback functions must take no parameters and
87 * return void. The required signature of a callback function is determined by
88 * the context in which is used (e.g. the signal to which it is connected).
90 * Use G_CALLBACK() to cast the callback function to a #GCallback.
92 typedef void (*GCallback) (void);
95 * @data: data specified when registering the notification callback
96 * @closure: the #GClosure on which the notification is emitted
98 * The type used for the various notification callbacks which can be registered
101 typedef void (*GClosureNotify) (gpointer data,
105 * @closure: the #GClosure to which the marshaller belongs
106 * @return_value: (nullable): a #GValue to store the return
107 * value. May be %NULL if the callback of @closure doesn't return a
109 * @n_param_values: the length of the @param_values array
110 * @param_values: (array length=n_param_values): an array of
111 * #GValues holding the arguments on which to invoke the
112 * callback of @closure
113 * @invocation_hint: (nullable): the invocation hint given as the
114 * last argument to g_closure_invoke()
115 * @marshal_data: (nullable): additional data specified when
116 * registering the marshaller, see g_closure_set_marshal() and
117 * g_closure_set_meta_marshal()
119 * The type used for marshaller functions.
121 typedef void (*GClosureMarshal) (GClosure *closure,
122 GValue *return_value,
123 guint n_param_values,
124 const GValue *param_values,
125 gpointer invocation_hint,
126 gpointer marshal_data);
130 * @closure: the #GClosure to which the marshaller belongs
131 * @return_value: (nullable): a #GValue to store the return
132 * value. May be %NULL if the callback of @closure doesn't return a
134 * @instance: (type GObject.TypeInstance): the instance on which the closure is
136 * @args: va_list of arguments to be passed to the closure.
137 * @marshal_data: (nullable): additional data specified when
138 * registering the marshaller, see g_closure_set_marshal() and
139 * g_closure_set_meta_marshal()
140 * @n_params: the length of the @param_types array
141 * @param_types: (array length=n_params): the #GType of each argument from
144 * This is the signature of va_list marshaller functions, an optional
145 * marshaller that can be used in some situations to avoid
146 * marshalling the signal argument into GValues.
148 typedef void (* GVaClosureMarshal) (GClosure *closure,
149 GValue *return_value,
152 gpointer marshal_data,
158 * @closure: the #GClosure
159 * @callback: the callback function
161 * A #GCClosure is a specialization of #GClosure for C function callbacks.
163 typedef struct _GCClosure GCClosure;
166 /* --- structures --- */
167 struct _GClosureNotifyData
170 GClosureNotify notify;
174 * @in_marshal: Indicates whether the closure is currently being invoked with
176 * @is_invalid: Indicates whether the closure has been invalidated by
177 * g_closure_invalidate()
179 * A #GClosure represents a callback supplied by the programmer.
184 guint ref_count : 15; /* (atomic) */
185 /* meta_marshal is not used anymore but must be zero for historical reasons
186 as it was exposed in the G_CLOSURE_N_NOTIFIERS macro */
187 guint meta_marshal_nouse : 1; /* (atomic) */
188 guint n_guards : 1; /* (atomic) */
189 guint n_fnotifiers : 2; /* finalization notifiers (atomic) */
190 guint n_inotifiers : 8; /* invalidation notifiers (atomic) */
191 guint in_inotify : 1; /* (atomic) */
192 guint floating : 1; /* (atomic) */
194 guint derivative_flag : 1; /* (atomic) */
196 guint in_marshal : 1; /* (atomic) */
197 guint is_invalid : 1; /* (atomic) */
199 /*< private >*/ void (*marshal) (GClosure *closure,
200 GValue /*out*/ *return_value,
201 guint n_param_values,
202 const GValue *param_values,
203 gpointer invocation_hint,
204 gpointer marshal_data);
205 /*< protected >*/ gpointer data;
207 /*< private >*/ GClosureNotifyData *notifiers;
209 /* invariants/constraints:
210 * - ->marshal and ->data are _invalid_ as soon as ->is_invalid==TRUE
211 * - invocation of all inotifiers occurs prior to fnotifiers
212 * - order of inotifiers is random
213 * inotifiers may _not_ free/invalidate parameter values (e.g. ->data)
214 * - order of fnotifiers is random
215 * - each notifier may only be removed before or during its invocation
216 * - reference counting may only happen prior to fnotify invocation
217 * (in that sense, fnotifiers are really finalization handlers)
220 /* closure for C function calls, callback() is the user function
229 /* --- prototypes --- */
230 GOBJECT_AVAILABLE_IN_ALL
231 GClosure* g_cclosure_new (GCallback callback_func,
233 GClosureNotify destroy_data);
234 GOBJECT_AVAILABLE_IN_ALL
235 GClosure* g_cclosure_new_swap (GCallback callback_func,
237 GClosureNotify destroy_data);
238 GOBJECT_AVAILABLE_IN_ALL
239 GClosure* g_signal_type_cclosure_new (GType itype,
240 guint struct_offset);
243 /* --- prototypes --- */
244 GOBJECT_AVAILABLE_IN_ALL
245 GClosure* g_closure_ref (GClosure *closure);
246 GOBJECT_AVAILABLE_IN_ALL
247 void g_closure_sink (GClosure *closure);
248 GOBJECT_AVAILABLE_IN_ALL
249 void g_closure_unref (GClosure *closure);
251 GOBJECT_AVAILABLE_IN_ALL
252 GClosure* g_closure_new_simple (guint sizeof_closure,
254 GOBJECT_AVAILABLE_IN_ALL
255 void g_closure_add_finalize_notifier (GClosure *closure,
256 gpointer notify_data,
257 GClosureNotify notify_func);
258 GOBJECT_AVAILABLE_IN_ALL
259 void g_closure_remove_finalize_notifier (GClosure *closure,
260 gpointer notify_data,
261 GClosureNotify notify_func);
262 GOBJECT_AVAILABLE_IN_ALL
263 void g_closure_add_invalidate_notifier (GClosure *closure,
264 gpointer notify_data,
265 GClosureNotify notify_func);
266 GOBJECT_AVAILABLE_IN_ALL
267 void g_closure_remove_invalidate_notifier (GClosure *closure,
268 gpointer notify_data,
269 GClosureNotify notify_func);
270 GOBJECT_AVAILABLE_IN_ALL
271 void g_closure_add_marshal_guards (GClosure *closure,
272 gpointer pre_marshal_data,
273 GClosureNotify pre_marshal_notify,
274 gpointer post_marshal_data,
275 GClosureNotify post_marshal_notify);
276 GOBJECT_AVAILABLE_IN_ALL
277 void g_closure_set_marshal (GClosure *closure,
278 GClosureMarshal marshal);
279 GOBJECT_AVAILABLE_IN_ALL
280 void g_closure_set_meta_marshal (GClosure *closure,
281 gpointer marshal_data,
282 GClosureMarshal meta_marshal);
283 GOBJECT_AVAILABLE_IN_ALL
284 void g_closure_invalidate (GClosure *closure);
285 GOBJECT_AVAILABLE_IN_ALL
286 void g_closure_invoke (GClosure *closure,
287 GValue /*out*/ *return_value,
288 guint n_param_values,
289 const GValue *param_values,
290 gpointer invocation_hint);
293 OK: data_object::destroy -> closure_invalidate();
294 MIS: closure_invalidate() -> disconnect(closure);
295 MIS: disconnect(closure) -> (unlink) closure_unref();
296 OK: closure_finalize() -> g_free (data_string);
299 - need marshaller repo with decent aliasing to base types
300 - provide marshaller collection, virtually covering anything out there
303 GOBJECT_AVAILABLE_IN_ALL
304 void g_cclosure_marshal_generic (GClosure *closure,
305 GValue *return_gvalue,
306 guint n_param_values,
307 const GValue *param_values,
308 gpointer invocation_hint,
309 gpointer marshal_data);
311 GOBJECT_AVAILABLE_IN_ALL
312 void g_cclosure_marshal_generic_va (GClosure *closure,
313 GValue *return_value,
316 gpointer marshal_data,
323 #endif /* __G_CLOSURE_H__ */