gclosure.h 11 KB

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