gclosure.h 11 KB

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