1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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/>.
17 #ifndef __G_TYPE_MODULE_H__
18 #define __G_TYPE_MODULE_H__
20 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
21 #error "Only <glib-object.h> can be included directly."
24 #include <gobject/gobject.h>
25 #include <gobject/genums.h>
29 typedef struct _GTypeModule GTypeModule;
30 typedef struct _GTypeModuleClass GTypeModuleClass;
32 #define G_TYPE_TYPE_MODULE (g_type_module_get_type ())
33 #define G_TYPE_MODULE(module) (G_TYPE_CHECK_INSTANCE_CAST ((module), G_TYPE_TYPE_MODULE, GTypeModule))
34 #define G_TYPE_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_TYPE_MODULE, GTypeModuleClass))
35 #define G_IS_TYPE_MODULE(module) (G_TYPE_CHECK_INSTANCE_TYPE ((module), G_TYPE_TYPE_MODULE))
36 #define G_IS_TYPE_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_TYPE_MODULE))
37 #define G_TYPE_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), G_TYPE_TYPE_MODULE, GTypeModuleClass))
41 * @name: the name of the module
43 * The members of the GTypeModule structure should not
44 * be accessed directly, except for the @name field.
48 GObject parent_instance;
52 GSList *interface_infos;
60 * @parent_class: the parent class
61 * @load: loads the module and registers one or more types using
62 * g_type_module_register_type().
63 * @unload: unloads the module
65 * In order to implement dynamic loading of types based on #GTypeModule,
66 * the @load and @unload functions in #GTypeModuleClass must be implemented.
68 struct _GTypeModuleClass
70 GObjectClass parent_class;
73 gboolean (* load) (GTypeModule *module);
74 void (* unload) (GTypeModule *module);
77 /* Padding for future expansion */
78 void (*reserved1) (void);
79 void (*reserved2) (void);
80 void (*reserved3) (void);
81 void (*reserved4) (void);
85 * G_DEFINE_DYNAMIC_TYPE:
86 * @TN: The name of the new type, in Camel case.
87 * @t_n: The name of the new type, in lowercase, with words
89 * @T_P: The #GType of the parent type.
91 * A convenience macro for dynamic type implementations, which declares a
92 * class initialization function, an instance initialization function (see
93 * #GTypeInfo for information about these) and a static variable named
94 * @t_n<!-- -->_parent_class pointing to the parent class. Furthermore,
95 * it defines a `*_get_type()` and a static `*_register_type()` functions
96 * for use in your `module_init()`.
98 * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
102 #define G_DEFINE_DYNAMIC_TYPE(TN, t_n, T_P) G_DEFINE_DYNAMIC_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
104 * G_DEFINE_DYNAMIC_TYPE_EXTENDED:
105 * @TypeName: The name of the new type, in Camel case.
106 * @type_name: The name of the new type, in lowercase, with words
108 * @TYPE_PARENT: The #GType of the parent type.
109 * @flags: #GTypeFlags to pass to g_type_module_register_type()
110 * @CODE: Custom code that gets inserted in the *_get_type() function.
112 * A more general version of G_DEFINE_DYNAMIC_TYPE() which
113 * allows to specify #GTypeFlags and custom code.
116 * G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtkGadget,
120 * G_IMPLEMENT_INTERFACE_DYNAMIC (TYPE_GIZMO,
121 * gtk_gadget_gizmo_init));
125 * static void gtk_gadget_init (GtkGadget *self);
126 * static void gtk_gadget_class_init (GtkGadgetClass *klass);
127 * static void gtk_gadget_class_finalize (GtkGadgetClass *klass);
129 * static gpointer gtk_gadget_parent_class = NULL;
130 * static GType gtk_gadget_type_id = 0;
132 * static void gtk_gadget_class_intern_init (gpointer klass)
134 * gtk_gadget_parent_class = g_type_class_peek_parent (klass);
135 * gtk_gadget_class_init ((GtkGadgetClass*) klass);
139 * gtk_gadget_get_type (void)
141 * return gtk_gadget_type_id;
145 * gtk_gadget_register_type (GTypeModule *type_module)
147 * const GTypeInfo g_define_type_info = {
148 * sizeof (GtkGadgetClass),
149 * (GBaseInitFunc) NULL,
150 * (GBaseFinalizeFunc) NULL,
151 * (GClassInitFunc) gtk_gadget_class_intern_init,
152 * (GClassFinalizeFunc) gtk_gadget_class_finalize,
153 * NULL, // class_data
154 * sizeof (GtkGadget),
156 * (GInstanceInitFunc) gtk_gadget_init,
157 * NULL // value_table
159 * gtk_gadget_type_id = g_type_module_register_type (type_module,
162 * &g_define_type_info,
163 * (GTypeFlags) flags);
165 * const GInterfaceInfo g_implement_interface_info = {
166 * (GInterfaceInitFunc) gtk_gadget_gizmo_init
168 * g_type_module_add_interface (type_module, g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
175 #define G_DEFINE_DYNAMIC_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
176 static void type_name##_init (TypeName *self); \
177 static void type_name##_class_init (TypeName##Class *klass); \
178 static void type_name##_class_finalize (TypeName##Class *klass); \
179 static gpointer type_name##_parent_class = NULL; \
180 static GType type_name##_type_id = 0; \
181 static gint TypeName##_private_offset; \
183 _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
186 static inline gpointer \
187 type_name##_get_instance_private (TypeName *self) \
189 return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
193 type_name##_get_type (void) \
195 return type_name##_type_id; \
198 type_name##_register_type (GTypeModule *type_module) \
200 GType g_define_type_id G_GNUC_UNUSED; \
201 const GTypeInfo g_define_type_info = { \
202 sizeof (TypeName##Class), \
203 (GBaseInitFunc) NULL, \
204 (GBaseFinalizeFunc) NULL, \
205 (GClassInitFunc) type_name##_class_intern_init, \
206 (GClassFinalizeFunc) type_name##_class_finalize, \
207 NULL, /* class_data */ \
209 0, /* n_preallocs */ \
210 (GInstanceInitFunc) type_name##_init, \
211 NULL /* value_table */ \
213 type_name##_type_id = g_type_module_register_type (type_module, \
216 &g_define_type_info, \
217 (GTypeFlags) flags); \
218 g_define_type_id = type_name##_type_id; \
223 * G_IMPLEMENT_INTERFACE_DYNAMIC:
224 * @TYPE_IFACE: The #GType of the interface to add
225 * @iface_init: The interface init function
227 * A convenience macro to ease interface addition in the @_C_ section
228 * of G_DEFINE_DYNAMIC_TYPE_EXTENDED(). See G_DEFINE_DYNAMIC_TYPE_EXTENDED()
231 * Note that this macro can only be used together with the
232 * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
233 * names from that macro.
237 #define G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init) { \
238 const GInterfaceInfo g_implement_interface_info = { \
239 (GInterfaceInitFunc) iface_init, NULL, NULL \
241 g_type_module_add_interface (type_module, g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
245 * G_ADD_PRIVATE_DYNAMIC:
246 * @TypeName: the name of the type in CamelCase
248 * A convenience macro to ease adding private data to instances of a new dynamic
249 * type in the @_C_ section of G_DEFINE_DYNAMIC_TYPE_EXTENDED(). See
250 * G_ADD_PRIVATE() for details, it is similar but for static types.
252 * Note that this macro can only be used together with the
253 * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
254 * names from that macro.
258 #define G_ADD_PRIVATE_DYNAMIC(TypeName) { \
259 TypeName##_private_offset = sizeof (TypeName##Private); \
262 GLIB_AVAILABLE_IN_ALL
263 GType g_type_module_get_type (void) G_GNUC_CONST;
264 GLIB_AVAILABLE_IN_ALL
265 gboolean g_type_module_use (GTypeModule *module);
266 GLIB_AVAILABLE_IN_ALL
267 void g_type_module_unuse (GTypeModule *module);
268 GLIB_AVAILABLE_IN_ALL
269 void g_type_module_set_name (GTypeModule *module,
271 GLIB_AVAILABLE_IN_ALL
272 GType g_type_module_register_type (GTypeModule *module,
274 const gchar *type_name,
275 const GTypeInfo *type_info,
277 GLIB_AVAILABLE_IN_ALL
278 void g_type_module_add_interface (GTypeModule *module,
280 GType interface_type,
281 const GInterfaceInfo *interface_info);
282 GLIB_AVAILABLE_IN_ALL
283 GType g_type_module_register_enum (GTypeModule *module,
285 const GEnumValue *const_static_values);
286 GLIB_AVAILABLE_IN_ALL
287 GType g_type_module_register_flags (GTypeModule *module,
289 const GFlagsValue *const_static_values);
293 #endif /* __G_TYPE_MODULE_H__ */