gtypemodule.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* GObject - GLib Type, Object, Parameter and Signal Library
  2. * Copyright (C) 2000 Red Hat, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __G_TYPE_MODULE_H__
  18. #define __G_TYPE_MODULE_H__
  19. #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
  20. #error "Only <glib-object.h> can be included directly."
  21. #endif
  22. #include <gobject/gobject.h>
  23. #include <gobject/genums.h>
  24. G_BEGIN_DECLS
  25. typedef struct _GTypeModule GTypeModule;
  26. typedef struct _GTypeModuleClass GTypeModuleClass;
  27. #define G_TYPE_TYPE_MODULE (g_type_module_get_type ())
  28. #define G_TYPE_MODULE(module) (G_TYPE_CHECK_INSTANCE_CAST ((module), G_TYPE_TYPE_MODULE, GTypeModule))
  29. #define G_TYPE_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_TYPE_MODULE, GTypeModuleClass))
  30. #define G_IS_TYPE_MODULE(module) (G_TYPE_CHECK_INSTANCE_TYPE ((module), G_TYPE_TYPE_MODULE))
  31. #define G_IS_TYPE_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_TYPE_MODULE))
  32. #define G_TYPE_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), G_TYPE_TYPE_MODULE, GTypeModuleClass))
  33. /**
  34. * GTypeModule:
  35. * @name: the name of the module
  36. *
  37. * The members of the GTypeModule structure should not
  38. * be accessed directly, except for the @name field.
  39. */
  40. struct _GTypeModule
  41. {
  42. GObject parent_instance;
  43. guint use_count;
  44. GSList *type_infos;
  45. GSList *interface_infos;
  46. /*< public >*/
  47. gchar *name;
  48. };
  49. /**
  50. * GTypeModuleClass:
  51. * @parent_class: the parent class
  52. * @load: loads the module and registers one or more types using
  53. * g_type_module_register_type().
  54. * @unload: unloads the module
  55. *
  56. * In order to implement dynamic loading of types based on #GTypeModule,
  57. * the @load and @unload functions in #GTypeModuleClass must be implemented.
  58. */
  59. struct _GTypeModuleClass
  60. {
  61. GObjectClass parent_class;
  62. /*< public >*/
  63. gboolean (* load) (GTypeModule *module);
  64. void (* unload) (GTypeModule *module);
  65. /*< private >*/
  66. /* Padding for future expansion */
  67. void (*reserved1) (void);
  68. void (*reserved2) (void);
  69. void (*reserved3) (void);
  70. void (*reserved4) (void);
  71. };
  72. /**
  73. * G_DEFINE_DYNAMIC_TYPE:
  74. * @TN: The name of the new type, in Camel case.
  75. * @t_n: The name of the new type, in lowercase, with words
  76. * separated by '_'.
  77. * @T_P: The #GType of the parent type.
  78. *
  79. * A convenience macro for dynamic type implementations, which declares a
  80. * class initialization function, an instance initialization function (see
  81. * #GTypeInfo for information about these) and a static variable named
  82. * @t_n<!-- -->_parent_class pointing to the parent class. Furthermore,
  83. * it defines a <function>*_get_type()</function> and a static
  84. * <function>*_register_type()</function> function for use in your
  85. * <function>module_init()</function>.
  86. * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
  87. *
  88. * Since: 2.14
  89. */
  90. #define G_DEFINE_DYNAMIC_TYPE(TN, t_n, T_P) G_DEFINE_DYNAMIC_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
  91. /**
  92. * G_DEFINE_DYNAMIC_TYPE_EXTENDED:
  93. * @TypeName: The name of the new type, in Camel case.
  94. * @type_name: The name of the new type, in lowercase, with words
  95. * separated by '_'.
  96. * @TYPE_PARENT: The #GType of the parent type.
  97. * @flags: #GTypeFlags to pass to g_type_module_register_type()
  98. * @CODE: Custom code that gets inserted in the *_get_type() function.
  99. *
  100. * A more general version of G_DEFINE_DYNAMIC_TYPE() which
  101. * allows to specify #GTypeFlags and custom code.
  102. *
  103. * |[
  104. * G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtkGadget,
  105. * gtk_gadget,
  106. * GTK_TYPE_THING,
  107. * 0,
  108. * G_IMPLEMENT_INTERFACE_DYNAMIC (TYPE_GIZMO,
  109. * gtk_gadget_gizmo_init));
  110. * ]|
  111. * expands to
  112. * |[
  113. * static void gtk_gadget_init (GtkGadget *self);
  114. * static void gtk_gadget_class_init (GtkGadgetClass *klass);
  115. * static void gtk_gadget_class_finalize (GtkGadgetClass *klass);
  116. *
  117. * static gpointer gtk_gadget_parent_class = NULL;
  118. * static GType gtk_gadget_type_id = 0;
  119. *
  120. * static void gtk_gadget_class_intern_init (gpointer klass)
  121. * {
  122. * gtk_gadget_parent_class = g_type_class_peek_parent (klass);
  123. * gtk_gadget_class_init ((GtkGadgetClass*) klass);
  124. * }
  125. *
  126. * GType
  127. * gtk_gadget_get_type (void)
  128. * {
  129. * return gtk_gadget_type_id;
  130. * }
  131. *
  132. * static void
  133. * gtk_gadget_register_type (GTypeModule *type_module)
  134. * {
  135. * const GTypeInfo g_define_type_info = {
  136. * sizeof (GtkGadgetClass),
  137. * (GBaseInitFunc) NULL,
  138. * (GBaseFinalizeFunc) NULL,
  139. * (GClassInitFunc) gtk_gadget_class_intern_init,
  140. * (GClassFinalizeFunc) gtk_gadget_class_finalize,
  141. * NULL, // class_data
  142. * sizeof (GtkGadget),
  143. * 0, // n_preallocs
  144. * (GInstanceInitFunc) gtk_gadget_init,
  145. * NULL // value_table
  146. * };
  147. * gtk_gadget_type_id = g_type_module_register_type (type_module,
  148. * GTK_TYPE_THING,
  149. * "GtkGadget",
  150. * &g_define_type_info,
  151. * (GTypeFlags) flags);
  152. * {
  153. * const GInterfaceInfo g_implement_interface_info = {
  154. * (GInterfaceInitFunc) gtk_gadget_gizmo_init
  155. * };
  156. * g_type_module_add_interface (type_module, g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
  157. * }
  158. * }
  159. * ]|
  160. *
  161. * Since: 2.14
  162. */
  163. #define G_DEFINE_DYNAMIC_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
  164. static void type_name##_init (TypeName *self); \
  165. static void type_name##_class_init (TypeName##Class *klass); \
  166. static void type_name##_class_finalize (TypeName##Class *klass); \
  167. static gpointer type_name##_parent_class = NULL; \
  168. static GType type_name##_type_id = 0; \
  169. static gint TypeName##_private_offset; \
  170. \
  171. _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
  172. \
  173. static inline gpointer \
  174. type_name##_get_instance_private (TypeName *self) \
  175. { \
  176. return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
  177. } \
  178. \
  179. GType \
  180. type_name##_get_type (void) \
  181. { \
  182. return type_name##_type_id; \
  183. } \
  184. static void \
  185. type_name##_register_type (GTypeModule *type_module) \
  186. { \
  187. GType g_define_type_id G_GNUC_UNUSED; \
  188. const GTypeInfo g_define_type_info = { \
  189. sizeof (TypeName##Class), \
  190. (GBaseInitFunc) NULL, \
  191. (GBaseFinalizeFunc) NULL, \
  192. (GClassInitFunc) type_name##_class_intern_init, \
  193. (GClassFinalizeFunc) type_name##_class_finalize, \
  194. NULL, /* class_data */ \
  195. sizeof (TypeName), \
  196. 0, /* n_preallocs */ \
  197. (GInstanceInitFunc) type_name##_init, \
  198. NULL /* value_table */ \
  199. }; \
  200. type_name##_type_id = g_type_module_register_type (type_module, \
  201. TYPE_PARENT, \
  202. #TypeName, \
  203. &g_define_type_info, \
  204. (GTypeFlags) flags); \
  205. g_define_type_id = type_name##_type_id; \
  206. { CODE ; } \
  207. }
  208. /**
  209. * G_IMPLEMENT_INTERFACE_DYNAMIC:
  210. * @TYPE_IFACE: The #GType of the interface to add
  211. * @iface_init: The interface init function
  212. *
  213. * A convenience macro to ease interface addition in the @_C_ section
  214. * of G_DEFINE_DYNAMIC_TYPE_EXTENDED(). See G_DEFINE_DYNAMIC_TYPE_EXTENDED()
  215. * for an example.
  216. *
  217. * Note that this macro can only be used together with the
  218. * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
  219. * names from that macro.
  220. *
  221. * Since: 2.24
  222. */
  223. #define G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init) { \
  224. const GInterfaceInfo g_implement_interface_info = { \
  225. (GInterfaceInitFunc) iface_init, NULL, NULL \
  226. }; \
  227. g_type_module_add_interface (type_module, g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
  228. }
  229. #define G_ADD_PRIVATE_DYNAMIC(TypeName) { \
  230. TypeName##_private_offset = sizeof (TypeName##Private); \
  231. }
  232. GLIB_AVAILABLE_IN_ALL
  233. GType g_type_module_get_type (void) G_GNUC_CONST;
  234. GLIB_AVAILABLE_IN_ALL
  235. gboolean g_type_module_use (GTypeModule *module);
  236. GLIB_AVAILABLE_IN_ALL
  237. void g_type_module_unuse (GTypeModule *module);
  238. GLIB_AVAILABLE_IN_ALL
  239. void g_type_module_set_name (GTypeModule *module,
  240. const gchar *name);
  241. GLIB_AVAILABLE_IN_ALL
  242. GType g_type_module_register_type (GTypeModule *module,
  243. GType parent_type,
  244. const gchar *type_name,
  245. const GTypeInfo *type_info,
  246. GTypeFlags flags);
  247. GLIB_AVAILABLE_IN_ALL
  248. void g_type_module_add_interface (GTypeModule *module,
  249. GType instance_type,
  250. GType interface_type,
  251. const GInterfaceInfo *interface_info);
  252. GLIB_AVAILABLE_IN_ALL
  253. GType g_type_module_register_enum (GTypeModule *module,
  254. const gchar *name,
  255. const GEnumValue *const_static_values);
  256. GLIB_AVAILABLE_IN_ALL
  257. GType g_type_module_register_flags (GTypeModule *module,
  258. const gchar *name,
  259. const GFlagsValue *const_static_values);
  260. G_END_DECLS
  261. #endif /* __G_TYPE_MODULE_H__ */