2.35.6
[glib.git] / gobject / gclosure.h
blobf59aab5cecb04580249f2f064aca5ca34dcf7687
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.
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."
25 #endif
27 #include <gobject/gtype.h>
29 G_BEGIN_DECLS
31 /* --- defines --- */
32 /**
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
39 * @closure.
41 #define G_CLOSURE_NEEDS_MARSHAL(closure) (((GClosure*) (closure))->marshal == NULL)
42 /**
43 * G_CLOSURE_N_NOTIFIERS:
44 * @cl: a #GClosure
46 * Get the total number of notifiers connected with the closure @cl.
47 * The count includes the meta marshaller, the finalize and invalidate notifiers
48 * and the marshal guards. Note that each guard counts as two notifiers.
49 * See g_closure_set_meta_marshal(), g_closure_add_finalize_notifier(),
50 * g_closure_add_invalidate_notifier() and g_closure_add_marshal_guards().
52 * Returns: number of notifiers
54 #define G_CLOSURE_N_NOTIFIERS(cl) (((cl)->n_guards << 1L) + \
55 (cl)->n_fnotifiers + (cl)->n_inotifiers)
56 /**
57 * G_CCLOSURE_SWAP_DATA:
58 * @cclosure: a #GCClosure
60 * Checks whether the user data of the #GCClosure should be passed as the
61 * first parameter to the callback. See g_cclosure_new_swap().
63 * Returns: %TRUE if data has to be swapped.
65 #define G_CCLOSURE_SWAP_DATA(cclosure) (((GClosure*) (cclosure))->derivative_flag)
66 /**
67 * G_CALLBACK:
68 * @f: a function pointer.
70 * Cast a function pointer to a #GCallback.
72 #define G_CALLBACK(f) ((GCallback) (f))
75 /* -- typedefs --- */
76 typedef struct _GClosure GClosure;
77 typedef struct _GClosureNotifyData GClosureNotifyData;
79 /**
80 * GCallback:
82 * The type used for callback functions in structure definitions and function
83 * signatures. This doesn't mean that all callback functions must take no
84 * parameters and return void. The required signature of a callback function
85 * is determined by the context in which is used (e.g. the signal to which it
86 * is connected). Use G_CALLBACK() to cast the callback function to a #GCallback.
88 typedef void (*GCallback) (void);
89 /**
90 * GClosureNotify:
91 * @data: data specified when registering the notification callback
92 * @closure: the #GClosure on which the notification is emitted
94 * The type used for the various notification callbacks which can be registered
95 * on closures.
97 typedef void (*GClosureNotify) (gpointer data,
98 GClosure *closure);
99 /**
100 * GClosureMarshal:
101 * @closure: the #GClosure to which the marshaller belongs
102 * @return_value: (allow-none): a #GValue to store the return
103 * value. May be %NULL if the callback of @closure doesn't return a
104 * value.
105 * @n_param_values: the length of the @param_values array
106 * @param_values: (array length=n_param_values): an array of
107 * #GValue<!-- -->s holding the arguments on which to invoke the
108 * callback of @closure
109 * @invocation_hint: (allow-none): the invocation hint given as the
110 * last argument to g_closure_invoke()
111 * @marshal_data: (allow-none): additional data specified when
112 * registering the marshaller, see g_closure_set_marshal() and
113 * g_closure_set_meta_marshal()
115 * The type used for marshaller functions.
117 typedef void (*GClosureMarshal) (GClosure *closure,
118 GValue *return_value,
119 guint n_param_values,
120 const GValue *param_values,
121 gpointer invocation_hint,
122 gpointer marshal_data);
124 typedef void (* GVaClosureMarshal) (GClosure *closure,
125 GValue *return_value,
126 gpointer instance,
127 va_list args,
128 gpointer marshal_data,
129 int n_params,
130 GType *param_types);
133 * GCClosure:
134 * @closure: the #GClosure
135 * @callback: the callback function
137 * A #GCClosure is a specialization of #GClosure for C function callbacks.
139 typedef struct _GCClosure GCClosure;
142 /* --- structures --- */
143 struct _GClosureNotifyData
145 gpointer data;
146 GClosureNotify notify;
149 * GClosure:
150 * @in_marshal: Indicates whether the closure is currently being invoked with
151 * g_closure_invoke()
152 * @is_invalid: Indicates whether the closure has been invalidated by
153 * g_closure_invalidate()
155 * A #GClosure represents a callback supplied by the programmer.
157 struct _GClosure
159 /*< private >*/
160 volatile guint ref_count : 15;
161 /* meta_marshal is not used anymore but must be zero for historical reasons
162 as it was exposed in the G_CLOSURE_N_NOTIFIERS macro */
163 volatile guint meta_marshal_nouse : 1;
164 volatile guint n_guards : 1;
165 volatile guint n_fnotifiers : 2; /* finalization notifiers */
166 volatile guint n_inotifiers : 8; /* invalidation notifiers */
167 volatile guint in_inotify : 1;
168 volatile guint floating : 1;
169 /*< protected >*/
170 volatile guint derivative_flag : 1;
171 /*< public >*/
172 volatile guint in_marshal : 1;
173 volatile guint is_invalid : 1;
175 /*< private >*/ void (*marshal) (GClosure *closure,
176 GValue /*out*/ *return_value,
177 guint n_param_values,
178 const GValue *param_values,
179 gpointer invocation_hint,
180 gpointer marshal_data);
181 /*< protected >*/ gpointer data;
183 /*< private >*/ GClosureNotifyData *notifiers;
185 /* invariants/constrains:
186 * - ->marshal and ->data are _invalid_ as soon as ->is_invalid==TRUE
187 * - invocation of all inotifiers occours prior to fnotifiers
188 * - order of inotifiers is random
189 * inotifiers may _not_ free/invalidate parameter values (e.g. ->data)
190 * - order of fnotifiers is random
191 * - each notifier may only be removed before or during its invocation
192 * - reference counting may only happen prior to fnotify invocation
193 * (in that sense, fnotifiers are really finalization handlers)
196 /* closure for C function calls, callback() is the user function
198 struct _GCClosure
200 GClosure closure;
201 gpointer callback;
205 /* --- prototypes --- */
206 GLIB_AVAILABLE_IN_ALL
207 GClosure* g_cclosure_new (GCallback callback_func,
208 gpointer user_data,
209 GClosureNotify destroy_data);
210 GLIB_AVAILABLE_IN_ALL
211 GClosure* g_cclosure_new_swap (GCallback callback_func,
212 gpointer user_data,
213 GClosureNotify destroy_data);
214 GLIB_AVAILABLE_IN_ALL
215 GClosure* g_signal_type_cclosure_new (GType itype,
216 guint struct_offset);
219 /* --- prototypes --- */
220 GLIB_AVAILABLE_IN_ALL
221 GClosure* g_closure_ref (GClosure *closure);
222 GLIB_AVAILABLE_IN_ALL
223 void g_closure_sink (GClosure *closure);
224 GLIB_AVAILABLE_IN_ALL
225 void g_closure_unref (GClosure *closure);
226 /* intimidating */
227 GLIB_AVAILABLE_IN_ALL
228 GClosure* g_closure_new_simple (guint sizeof_closure,
229 gpointer data);
230 GLIB_AVAILABLE_IN_ALL
231 void g_closure_add_finalize_notifier (GClosure *closure,
232 gpointer notify_data,
233 GClosureNotify notify_func);
234 GLIB_AVAILABLE_IN_ALL
235 void g_closure_remove_finalize_notifier (GClosure *closure,
236 gpointer notify_data,
237 GClosureNotify notify_func);
238 GLIB_AVAILABLE_IN_ALL
239 void g_closure_add_invalidate_notifier (GClosure *closure,
240 gpointer notify_data,
241 GClosureNotify notify_func);
242 GLIB_AVAILABLE_IN_ALL
243 void g_closure_remove_invalidate_notifier (GClosure *closure,
244 gpointer notify_data,
245 GClosureNotify notify_func);
246 GLIB_AVAILABLE_IN_ALL
247 void g_closure_add_marshal_guards (GClosure *closure,
248 gpointer pre_marshal_data,
249 GClosureNotify pre_marshal_notify,
250 gpointer post_marshal_data,
251 GClosureNotify post_marshal_notify);
252 GLIB_AVAILABLE_IN_ALL
253 void g_closure_set_marshal (GClosure *closure,
254 GClosureMarshal marshal);
255 GLIB_AVAILABLE_IN_ALL
256 void g_closure_set_meta_marshal (GClosure *closure,
257 gpointer marshal_data,
258 GClosureMarshal meta_marshal);
259 GLIB_AVAILABLE_IN_ALL
260 void g_closure_invalidate (GClosure *closure);
261 GLIB_AVAILABLE_IN_ALL
262 void g_closure_invoke (GClosure *closure,
263 GValue /*out*/ *return_value,
264 guint n_param_values,
265 const GValue *param_values,
266 gpointer invocation_hint);
268 /* FIXME:
269 OK: data_object::destroy -> closure_invalidate();
270 MIS: closure_invalidate() -> disconnect(closure);
271 MIS: disconnect(closure) -> (unlink) closure_unref();
272 OK: closure_finalize() -> g_free (data_string);
274 random remarks:
275 - need marshaller repo with decent aliasing to base types
276 - provide marshaller collection, virtually covering anything out there
279 GLIB_AVAILABLE_IN_ALL
280 void g_cclosure_marshal_generic (GClosure *closure,
281 GValue *return_gvalue,
282 guint n_param_values,
283 const GValue *param_values,
284 gpointer invocation_hint,
285 gpointer marshal_data);
287 GLIB_AVAILABLE_IN_ALL
288 void g_cclosure_marshal_generic_va (GClosure *closure,
289 GValue *return_value,
290 gpointer instance,
291 va_list args_list,
292 gpointer marshal_data,
293 int n_params,
294 GType *param_types);
297 G_END_DECLS
299 #endif /* __G_CLOSURE_H__ */