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