gtypemodule: use G_GNUC_UNUSED in G_DEFINE_DYNAMIC_TYPE_EXTENDED
[glib.git] / gobject / gtypemodule.h
blob3ef0e84a4231b91a96ecc23e1877949619d1c47c
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."
22 #endif
24 #include <gobject/gobject.h>
25 #include <gobject/genums.h>
27 G_BEGIN_DECLS
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))
39 /**
40 * GTypeModule:
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.
46 struct _GTypeModule
48 GObject parent_instance;
50 guint use_count;
51 GSList *type_infos;
52 GSList *interface_infos;
54 /*< public >*/
55 gchar *name;
58 /**
59 * GTypeModuleClass:
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;
72 /*< public >*/
73 gboolean (* load) (GTypeModule *module);
74 void (* unload) (GTypeModule *module);
76 /*< private >*/
77 /* Padding for future expansion */
78 void (*reserved1) (void);
79 void (*reserved2) (void);
80 void (*reserved3) (void);
81 void (*reserved4) (void);
84 /**
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
88 * separated by '_'.
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.
100 * Since: 2.14
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
107 * separated by '_'.
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.
115 * |[
116 * G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtkGadget,
117 * gtk_gadget,
118 * GTK_TYPE_THING,
119 * 0,
120 * G_IMPLEMENT_INTERFACE_DYNAMIC (TYPE_GIZMO,
121 * gtk_gadget_gizmo_init));
122 * ]|
123 * expands to
124 * |[
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);
138 * GType
139 * gtk_gadget_get_type (void)
141 * return gtk_gadget_type_id;
144 * static void
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),
155 * 0, // n_preallocs
156 * (GInstanceInitFunc) gtk_gadget_init,
157 * NULL // value_table
158 * };
159 * gtk_gadget_type_id = g_type_module_register_type (type_module,
160 * GTK_TYPE_THING,
161 * "GtkGadget",
162 * &g_define_type_info,
163 * (GTypeFlags) flags);
165 * const GInterfaceInfo g_implement_interface_info = {
166 * (GInterfaceInitFunc) gtk_gadget_gizmo_init
167 * };
168 * g_type_module_add_interface (type_module, g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
171 * ]|
173 * Since: 2.14
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) \
185 G_GNUC_UNUSED \
186 static inline gpointer \
187 type_name##_get_instance_private (TypeName *self) \
189 return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
192 GType \
193 type_name##_get_type (void) \
195 return type_name##_type_id; \
197 static void \
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 */ \
208 sizeof (TypeName), \
209 0, /* n_preallocs */ \
210 (GInstanceInitFunc) type_name##_init, \
211 NULL /* value_table */ \
212 }; \
213 type_name##_type_id = g_type_module_register_type (type_module, \
214 TYPE_PARENT, \
215 #TypeName, \
216 &g_define_type_info, \
217 (GTypeFlags) flags); \
218 g_define_type_id = type_name##_type_id; \
219 { CODE ; } \
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()
229 * for an example.
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.
235 * Since: 2.24
237 #define G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init) { \
238 const GInterfaceInfo g_implement_interface_info = { \
239 (GInterfaceInitFunc) iface_init, NULL, NULL \
240 }; \
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.
256 * Since: 2.38
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,
270 const gchar *name);
271 GLIB_AVAILABLE_IN_ALL
272 GType g_type_module_register_type (GTypeModule *module,
273 GType parent_type,
274 const gchar *type_name,
275 const GTypeInfo *type_info,
276 GTypeFlags flags);
277 GLIB_AVAILABLE_IN_ALL
278 void g_type_module_add_interface (GTypeModule *module,
279 GType instance_type,
280 GType interface_type,
281 const GInterfaceInfo *interface_info);
282 GLIB_AVAILABLE_IN_ALL
283 GType g_type_module_register_enum (GTypeModule *module,
284 const gchar *name,
285 const GEnumValue *const_static_values);
286 GLIB_AVAILABLE_IN_ALL
287 GType g_type_module_register_flags (GTypeModule *module,
288 const gchar *name,
289 const GFlagsValue *const_static_values);
291 G_END_DECLS
293 #endif /* __G_TYPE_MODULE_H__ */