Change LGPL-2.1+ to LGPL-2.1-or-later
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
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.
11  *
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.
16  *
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/>.
19  */
20 #ifndef __G_CLOSURE_H__
21 #define __G_CLOSURE_H__
22
23 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
24 #error "Only <glib-object.h> can be included directly."
25 #endif
26
27 #include        <gobject/gtype.h>
28
29 G_BEGIN_DECLS
30
31 /* --- defines --- */
32 /**
33  * G_CLOSURE_NEEDS_MARSHAL:
34  * @closure: a #GClosure
35  * 
36  * Check if the closure still needs a marshaller. See g_closure_set_marshal().
37  *
38  * Returns: %TRUE if a #GClosureMarshal marshaller has not yet been set on 
39  * @closure.
40  */
41 #define G_CLOSURE_NEEDS_MARSHAL(closure) (((GClosure*) (closure))->marshal == NULL)
42 /**
43  * G_CLOSURE_N_NOTIFIERS:
44  * @cl: a #GClosure
45  * 
46  * Get the total number of notifiers connected with the closure @cl. 
47  *
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().
52  *
53  * Returns: number of notifiers
54  */
55 #define G_CLOSURE_N_NOTIFIERS(cl)        (((cl)->n_guards << 1L) + \
56                                           (cl)->n_fnotifiers + (cl)->n_inotifiers)
57 /**
58  * G_CCLOSURE_SWAP_DATA:
59  * @cclosure: a #GCClosure
60  * 
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().
63  *
64  * Returns: %TRUE if data has to be swapped.
65  */
66 #define G_CCLOSURE_SWAP_DATA(cclosure)   (((GClosure*) (cclosure))->derivative_flag)
67 /**
68  * G_CALLBACK:
69  * @f: a function pointer.
70  * 
71  * Cast a function pointer to a #GCallback.
72  */
73 #define G_CALLBACK(f)                    ((GCallback) (f))
74
75
76 /* -- typedefs --- */
77 typedef struct _GClosure                 GClosure;
78 typedef struct _GClosureNotifyData       GClosureNotifyData;
79
80 /**
81  * GCallback:
82  * 
83  * The type used for callback functions in structure definitions and function 
84  * signatures.
85  *
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).
89  *
90  * Use G_CALLBACK() to cast the callback function to a #GCallback. 
91  */
92 typedef void  (*GCallback)              (void);
93 /**
94  * GClosureNotify:
95  * @data: data specified when registering the notification callback
96  * @closure: the #GClosure on which the notification is emitted
97  * 
98  * The type used for the various notification callbacks which can be registered
99  * on closures.
100  */
101 typedef void  (*GClosureNotify)         (gpointer        data,
102                                          GClosure       *closure);
103 /**
104  * GClosureMarshal:
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
108  *  value.
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()
118  * 
119  * The type used for marshaller functions.
120  */
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);
127
128 /**
129  * GVaClosureMarshal:
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
133  *  value.
134  * @instance: (type GObject.TypeInstance): the instance on which the closure is
135  *  invoked.
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
142  *  @args.
143  *
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.
147  */
148 typedef void (* GVaClosureMarshal) (GClosure *closure,
149                                     GValue   *return_value,
150                                     gpointer  instance,
151                                     va_list   args,
152                                     gpointer  marshal_data,
153                                     int       n_params,
154                                     GType    *param_types);
155
156 /**
157  * GCClosure:
158  * @closure: the #GClosure
159  * @callback: the callback function
160  * 
161  * A #GCClosure is a specialization of #GClosure for C function callbacks.
162  */
163 typedef struct _GCClosure                GCClosure;
164
165
166 /* --- structures --- */
167 struct _GClosureNotifyData
168 {
169   gpointer       data;
170   GClosureNotify notify;
171 };
172 /**
173  * GClosure:
174  * @in_marshal: Indicates whether the closure is currently being invoked with 
175  *  g_closure_invoke()
176  * @is_invalid: Indicates whether the closure has been invalidated by 
177  *  g_closure_invalidate()
178  * 
179  * A #GClosure represents a callback supplied by the programmer.
180  */
181 struct _GClosure
182 {
183   /*< private >*/
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) */
193   /*< protected >*/
194   guint derivative_flag : 1;  /* (atomic) */
195   /*< public >*/
196   guint in_marshal : 1;  /* (atomic) */
197   guint is_invalid : 1;  /* (atomic) */
198
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;
206
207   /*< private >*/       GClosureNotifyData *notifiers;
208
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)
218    */
219 };
220 /* closure for C function calls, callback() is the user function
221  */
222 struct _GCClosure
223 {
224   GClosure      closure;
225   gpointer      callback;
226 };
227
228
229 /* --- prototypes --- */
230 GOBJECT_AVAILABLE_IN_ALL
231 GClosure* g_cclosure_new                        (GCallback      callback_func,
232                                                  gpointer       user_data,
233                                                  GClosureNotify destroy_data);
234 GOBJECT_AVAILABLE_IN_ALL
235 GClosure* g_cclosure_new_swap                   (GCallback      callback_func,
236                                                  gpointer       user_data,
237                                                  GClosureNotify destroy_data);
238 GOBJECT_AVAILABLE_IN_ALL
239 GClosure* g_signal_type_cclosure_new            (GType          itype,
240                                                  guint          struct_offset);
241
242
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);
250 /* intimidating */
251 GOBJECT_AVAILABLE_IN_ALL
252 GClosure* g_closure_new_simple                  (guint           sizeof_closure,
253                                                  gpointer        data);
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);
291
292 /* FIXME:
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);
297
298    random remarks:
299    - need marshaller repo with decent aliasing to base types
300    - provide marshaller collection, virtually covering anything out there
301 */
302
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);
310
311 GOBJECT_AVAILABLE_IN_ALL
312 void g_cclosure_marshal_generic_va (GClosure *closure,
313                                     GValue   *return_value,
314                                     gpointer  instance,
315                                     va_list   args_list,
316                                     gpointer  marshal_data,
317                                     int       n_params,
318                                     GType    *param_types);
319
320
321 G_END_DECLS
322
323 #endif /* __G_CLOSURE_H__ */