gclosure.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * #GValue<!-- -->s 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. typedef void (* GVaClosureMarshal) (GClosure *closure,
  115. GValue *return_value,
  116. gpointer instance,
  117. va_list args,
  118. gpointer marshal_data,
  119. int n_params,
  120. GType *param_types);
  121. /**
  122. * GCClosure:
  123. * @closure: the #GClosure
  124. * @callback: the callback function
  125. *
  126. * A #GCClosure is a specialization of #GClosure for C function callbacks.
  127. */
  128. typedef struct _GCClosure GCClosure;
  129. /* --- structures --- */
  130. struct _GClosureNotifyData
  131. {
  132. gpointer data;
  133. GClosureNotify notify;
  134. };
  135. /**
  136. * GClosure:
  137. * @in_marshal: Indicates whether the closure is currently being invoked with
  138. * g_closure_invoke()
  139. * @is_invalid: Indicates whether the closure has been invalidated by
  140. * g_closure_invalidate()
  141. *
  142. * A #GClosure represents a callback supplied by the programmer.
  143. */
  144. struct _GClosure
  145. {
  146. /*< private >*/
  147. volatile guint ref_count : 15;
  148. /* meta_marshal is not used anymore but must be zero for historical reasons
  149. as it was exposed in the G_CLOSURE_N_NOTIFIERS macro */
  150. volatile guint meta_marshal_nouse : 1;
  151. volatile guint n_guards : 1;
  152. volatile guint n_fnotifiers : 2; /* finalization notifiers */
  153. volatile guint n_inotifiers : 8; /* invalidation notifiers */
  154. volatile guint in_inotify : 1;
  155. volatile guint floating : 1;
  156. /*< protected >*/
  157. volatile guint derivative_flag : 1;
  158. /*< public >*/
  159. volatile guint in_marshal : 1;
  160. volatile guint is_invalid : 1;
  161. /*< private >*/ void (*marshal) (GClosure *closure,
  162. GValue /*out*/ *return_value,
  163. guint n_param_values,
  164. const GValue *param_values,
  165. gpointer invocation_hint,
  166. gpointer marshal_data);
  167. /*< protected >*/ gpointer data;
  168. /*< private >*/ GClosureNotifyData *notifiers;
  169. /* invariants/constrains:
  170. * - ->marshal and ->data are _invalid_ as soon as ->is_invalid==TRUE
  171. * - invocation of all inotifiers occours prior to fnotifiers
  172. * - order of inotifiers is random
  173. * inotifiers may _not_ free/invalidate parameter values (e.g. ->data)
  174. * - order of fnotifiers is random
  175. * - each notifier may only be removed before or during its invocation
  176. * - reference counting may only happen prior to fnotify invocation
  177. * (in that sense, fnotifiers are really finalization handlers)
  178. */
  179. };
  180. /* closure for C function calls, callback() is the user function
  181. */
  182. struct _GCClosure
  183. {
  184. GClosure closure;
  185. gpointer callback;
  186. };
  187. /* --- prototypes --- */
  188. GLIB_AVAILABLE_IN_ALL
  189. GClosure* g_cclosure_new (GCallback callback_func,
  190. gpointer user_data,
  191. GClosureNotify destroy_data);
  192. GLIB_AVAILABLE_IN_ALL
  193. GClosure* g_cclosure_new_swap (GCallback callback_func,
  194. gpointer user_data,
  195. GClosureNotify destroy_data);
  196. GLIB_AVAILABLE_IN_ALL
  197. GClosure* g_signal_type_cclosure_new (GType itype,
  198. guint struct_offset);
  199. /* --- prototypes --- */
  200. GLIB_AVAILABLE_IN_ALL
  201. GClosure* g_closure_ref (GClosure *closure);
  202. GLIB_AVAILABLE_IN_ALL
  203. void g_closure_sink (GClosure *closure);
  204. GLIB_AVAILABLE_IN_ALL
  205. void g_closure_unref (GClosure *closure);
  206. /* intimidating */
  207. GLIB_AVAILABLE_IN_ALL
  208. GClosure* g_closure_new_simple (guint sizeof_closure,
  209. gpointer data);
  210. GLIB_AVAILABLE_IN_ALL
  211. void g_closure_add_finalize_notifier (GClosure *closure,
  212. gpointer notify_data,
  213. GClosureNotify notify_func);
  214. GLIB_AVAILABLE_IN_ALL
  215. void g_closure_remove_finalize_notifier (GClosure *closure,
  216. gpointer notify_data,
  217. GClosureNotify notify_func);
  218. GLIB_AVAILABLE_IN_ALL
  219. void g_closure_add_invalidate_notifier (GClosure *closure,
  220. gpointer notify_data,
  221. GClosureNotify notify_func);
  222. GLIB_AVAILABLE_IN_ALL
  223. void g_closure_remove_invalidate_notifier (GClosure *closure,
  224. gpointer notify_data,
  225. GClosureNotify notify_func);
  226. GLIB_AVAILABLE_IN_ALL
  227. void g_closure_add_marshal_guards (GClosure *closure,
  228. gpointer pre_marshal_data,
  229. GClosureNotify pre_marshal_notify,
  230. gpointer post_marshal_data,
  231. GClosureNotify post_marshal_notify);
  232. GLIB_AVAILABLE_IN_ALL
  233. void g_closure_set_marshal (GClosure *closure,
  234. GClosureMarshal marshal);
  235. GLIB_AVAILABLE_IN_ALL
  236. void g_closure_set_meta_marshal (GClosure *closure,
  237. gpointer marshal_data,
  238. GClosureMarshal meta_marshal);
  239. GLIB_AVAILABLE_IN_ALL
  240. void g_closure_invalidate (GClosure *closure);
  241. GLIB_AVAILABLE_IN_ALL
  242. void g_closure_invoke (GClosure *closure,
  243. GValue /*out*/ *return_value,
  244. guint n_param_values,
  245. const GValue *param_values,
  246. gpointer invocation_hint);
  247. /* FIXME:
  248. OK: data_object::destroy -> closure_invalidate();
  249. MIS: closure_invalidate() -> disconnect(closure);
  250. MIS: disconnect(closure) -> (unlink) closure_unref();
  251. OK: closure_finalize() -> g_free (data_string);
  252. random remarks:
  253. - need marshaller repo with decent aliasing to base types
  254. - provide marshaller collection, virtually covering anything out there
  255. */
  256. GLIB_AVAILABLE_IN_ALL
  257. void g_cclosure_marshal_generic (GClosure *closure,
  258. GValue *return_gvalue,
  259. guint n_param_values,
  260. const GValue *param_values,
  261. gpointer invocation_hint,
  262. gpointer marshal_data);
  263. GLIB_AVAILABLE_IN_ALL
  264. void g_cclosure_marshal_generic_va (GClosure *closure,
  265. GValue *return_value,
  266. gpointer instance,
  267. va_list args_list,
  268. gpointer marshal_data,
  269. int n_params,
  270. GType *param_types);
  271. G_END_DECLS
  272. #endif /* __G_CLOSURE_H__ */