ghook.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #ifndef __G_HOOK_H__
  26. #define __G_HOOK_H__
  27. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  30. #include <glib/gmem.h>
  31. G_BEGIN_DECLS
  32. /* --- typedefs --- */
  33. typedef struct _GHook GHook;
  34. typedef struct _GHookList GHookList;
  35. typedef gint (*GHookCompareFunc) (GHook *new_hook,
  36. GHook *sibling);
  37. typedef gboolean (*GHookFindFunc) (GHook *hook,
  38. gpointer data);
  39. typedef void (*GHookMarshaller) (GHook *hook,
  40. gpointer marshal_data);
  41. typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
  42. gpointer marshal_data);
  43. typedef void (*GHookFunc) (gpointer data);
  44. typedef gboolean (*GHookCheckFunc) (gpointer data);
  45. typedef void (*GHookFinalizeFunc) (GHookList *hook_list,
  46. GHook *hook);
  47. typedef enum
  48. {
  49. G_HOOK_FLAG_ACTIVE = 1 << 0,
  50. G_HOOK_FLAG_IN_CALL = 1 << 1,
  51. G_HOOK_FLAG_MASK = 0x0f
  52. } GHookFlagMask;
  53. #define G_HOOK_FLAG_USER_SHIFT (4)
  54. /* --- structures --- */
  55. struct _GHookList
  56. {
  57. gulong seq_id;
  58. guint hook_size : 16;
  59. guint is_setup : 1;
  60. GHook *hooks;
  61. gpointer dummy3;
  62. GHookFinalizeFunc finalize_hook;
  63. gpointer dummy[2];
  64. };
  65. struct _GHook
  66. {
  67. gpointer data;
  68. GHook *next;
  69. GHook *prev;
  70. guint ref_count;
  71. gulong hook_id;
  72. guint flags;
  73. gpointer func;
  74. GDestroyNotify destroy;
  75. };
  76. /* --- macros --- */
  77. #define G_HOOK(hook) ((GHook*) (hook))
  78. #define G_HOOK_FLAGS(hook) (G_HOOK (hook)->flags)
  79. #define G_HOOK_ACTIVE(hook) ((G_HOOK_FLAGS (hook) & \
  80. G_HOOK_FLAG_ACTIVE) != 0)
  81. #define G_HOOK_IN_CALL(hook) ((G_HOOK_FLAGS (hook) & \
  82. G_HOOK_FLAG_IN_CALL) != 0)
  83. #define G_HOOK_IS_VALID(hook) (G_HOOK (hook)->hook_id != 0 && \
  84. (G_HOOK_FLAGS (hook) & \
  85. G_HOOK_FLAG_ACTIVE))
  86. #define G_HOOK_IS_UNLINKED(hook) (G_HOOK (hook)->next == NULL && \
  87. G_HOOK (hook)->prev == NULL && \
  88. G_HOOK (hook)->hook_id == 0 && \
  89. G_HOOK (hook)->ref_count == 0)
  90. /* --- prototypes --- */
  91. /* callback maintenance functions */
  92. GLIB_AVAILABLE_IN_ALL
  93. void g_hook_list_init (GHookList *hook_list,
  94. guint hook_size);
  95. GLIB_AVAILABLE_IN_ALL
  96. void g_hook_list_clear (GHookList *hook_list);
  97. GLIB_AVAILABLE_IN_ALL
  98. GHook* g_hook_alloc (GHookList *hook_list);
  99. GLIB_AVAILABLE_IN_ALL
  100. void g_hook_free (GHookList *hook_list,
  101. GHook *hook);
  102. GLIB_AVAILABLE_IN_ALL
  103. GHook * g_hook_ref (GHookList *hook_list,
  104. GHook *hook);
  105. GLIB_AVAILABLE_IN_ALL
  106. void g_hook_unref (GHookList *hook_list,
  107. GHook *hook);
  108. GLIB_AVAILABLE_IN_ALL
  109. gboolean g_hook_destroy (GHookList *hook_list,
  110. gulong hook_id);
  111. GLIB_AVAILABLE_IN_ALL
  112. void g_hook_destroy_link (GHookList *hook_list,
  113. GHook *hook);
  114. GLIB_AVAILABLE_IN_ALL
  115. void g_hook_prepend (GHookList *hook_list,
  116. GHook *hook);
  117. GLIB_AVAILABLE_IN_ALL
  118. void g_hook_insert_before (GHookList *hook_list,
  119. GHook *sibling,
  120. GHook *hook);
  121. GLIB_AVAILABLE_IN_ALL
  122. void g_hook_insert_sorted (GHookList *hook_list,
  123. GHook *hook,
  124. GHookCompareFunc func);
  125. GLIB_AVAILABLE_IN_ALL
  126. GHook* g_hook_get (GHookList *hook_list,
  127. gulong hook_id);
  128. GLIB_AVAILABLE_IN_ALL
  129. GHook* g_hook_find (GHookList *hook_list,
  130. gboolean need_valids,
  131. GHookFindFunc func,
  132. gpointer data);
  133. GLIB_AVAILABLE_IN_ALL
  134. GHook* g_hook_find_data (GHookList *hook_list,
  135. gboolean need_valids,
  136. gpointer data);
  137. GLIB_AVAILABLE_IN_ALL
  138. GHook* g_hook_find_func (GHookList *hook_list,
  139. gboolean need_valids,
  140. gpointer func);
  141. GLIB_AVAILABLE_IN_ALL
  142. GHook* g_hook_find_func_data (GHookList *hook_list,
  143. gboolean need_valids,
  144. gpointer func,
  145. gpointer data);
  146. /* return the first valid hook, and increment its reference count */
  147. GLIB_AVAILABLE_IN_ALL
  148. GHook* g_hook_first_valid (GHookList *hook_list,
  149. gboolean may_be_in_call);
  150. /* return the next valid hook with incremented reference count, and
  151. * decrement the reference count of the original hook
  152. */
  153. GLIB_AVAILABLE_IN_ALL
  154. GHook* g_hook_next_valid (GHookList *hook_list,
  155. GHook *hook,
  156. gboolean may_be_in_call);
  157. /* GHookCompareFunc implementation to insert hooks sorted by their id */
  158. GLIB_AVAILABLE_IN_ALL
  159. gint g_hook_compare_ids (GHook *new_hook,
  160. GHook *sibling);
  161. /* convenience macros */
  162. #define g_hook_append( hook_list, hook ) \
  163. g_hook_insert_before ((hook_list), NULL, (hook))
  164. /* invoke all valid hooks with the (*GHookFunc) signature.
  165. */
  166. GLIB_AVAILABLE_IN_ALL
  167. void g_hook_list_invoke (GHookList *hook_list,
  168. gboolean may_recurse);
  169. /* invoke all valid hooks with the (*GHookCheckFunc) signature,
  170. * and destroy the hook if FALSE is returned.
  171. */
  172. GLIB_AVAILABLE_IN_ALL
  173. void g_hook_list_invoke_check (GHookList *hook_list,
  174. gboolean may_recurse);
  175. /* invoke a marshaller on all valid hooks.
  176. */
  177. GLIB_AVAILABLE_IN_ALL
  178. void g_hook_list_marshal (GHookList *hook_list,
  179. gboolean may_recurse,
  180. GHookMarshaller marshaller,
  181. gpointer marshal_data);
  182. GLIB_AVAILABLE_IN_ALL
  183. void g_hook_list_marshal_check (GHookList *hook_list,
  184. gboolean may_recurse,
  185. GHookCheckMarshaller marshaller,
  186. gpointer marshal_data);
  187. G_END_DECLS
  188. #endif /* __G_HOOK_H__ */