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.1 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
20#if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
21#error "Only <glib-object.h> can be included directly."
22#endif
23
24#include <gobject/gobject.h>
25#include <gobject/genums.h>
26
27G_BEGIN_DECLS
28
29typedef struct _GTypeModule GTypeModule;
30typedef struct _GTypeModuleClass GTypeModuleClass;
31
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))
38
39G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTypeModule, g_object_unref)
40
41/**
42 * GTypeModule:
43 * @name: the name of the module
44 *
45 * The members of the GTypeModule structure should not
46 * be accessed directly, except for the @name field.
47 */
48struct _GTypeModule
49{
50 GObject parent_instance;
51
52 guint use_count;
53 GSList *type_infos;
54 GSList *interface_infos;
55
56 /*< public >*/
57 gchar *name;
58};
59
60/**
61 * GTypeModuleClass:
62 * @parent_class: the parent class
63 * @load: loads the module and registers one or more types using
64 * g_type_module_register_type().
65 * @unload: unloads the module
66 *
67 * In order to implement dynamic loading of types based on #GTypeModule,
68 * the @load and @unload functions in #GTypeModuleClass must be implemented.
69 */
70struct _GTypeModuleClass
71{
72 GObjectClass parent_class;
73
74 /*< public >*/
75 gboolean (* load) (GTypeModule *module);
76 void (* unload) (GTypeModule *module);
77
78 /*< private >*/
79 /* Padding for future expansion */
80 void (*reserved1) (void);
81 void (*reserved2) (void);
82 void (*reserved3) (void);
83 void (*reserved4) (void);
84};
85
86/**
87 * G_DEFINE_DYNAMIC_TYPE:
88 * @TN: The name of the new type, in Camel case.
89 * @t_n: The name of the new type, in lowercase, with words
90 * separated by '_'.
91 * @T_P: The #GType of the parent type.
92 *
93 * A convenience macro for dynamic type implementations, which declares a
94 * class initialization function, an instance initialization function (see
95 * #GTypeInfo for information about these) and a static variable named
96 * `t_n`_parent_class pointing to the parent class. Furthermore,
97 * it defines a `*_get_type()` and a static `*_register_type()` functions
98 * for use in your `module_init()`.
99 *
100 * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
101 *
102 * Since: 2.14
103 */
104#define G_DEFINE_DYNAMIC_TYPE(TN, t_n, T_P) G_DEFINE_DYNAMIC_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
105/**
106 * G_DEFINE_DYNAMIC_TYPE_EXTENDED:
107 * @TypeName: The name of the new type, in Camel case.
108 * @type_name: The name of the new type, in lowercase, with words
109 * separated by '_'.
110 * @TYPE_PARENT: The #GType of the parent type.
111 * @flags: #GTypeFlags to pass to g_type_module_register_type()
112 * @CODE: Custom code that gets inserted in the *_get_type() function.
113 *
114 * A more general version of G_DEFINE_DYNAMIC_TYPE() which
115 * allows to specify #GTypeFlags and custom code.
116 *
117 * |[
118 * G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtkGadget,
119 * gtk_gadget,
120 * GTK_TYPE_THING,
121 * 0,
122 * G_IMPLEMENT_INTERFACE_DYNAMIC (TYPE_GIZMO,
123 * gtk_gadget_gizmo_init));
124 * ]|
125 * expands to
126 * |[
127 * static void gtk_gadget_init (GtkGadget *self);
128 * static void gtk_gadget_class_init (GtkGadgetClass *klass);
129 * static void gtk_gadget_class_finalize (GtkGadgetClass *klass);
130 *
131 * static gpointer gtk_gadget_parent_class = NULL;
132 * static GType gtk_gadget_type_id = 0;
133 *
134 * static void gtk_gadget_class_intern_init (gpointer klass)
135 * {
136 * gtk_gadget_parent_class = g_type_class_peek_parent (klass);
137 * gtk_gadget_class_init ((GtkGadgetClass*) klass);
138 * }
139 *
140 * GType
141 * gtk_gadget_get_type (void)
142 * {
143 * return gtk_gadget_type_id;
144 * }
145 *
146 * static void
147 * gtk_gadget_register_type (GTypeModule *type_module)
148 * {
149 * const GTypeInfo g_define_type_info = {
150 * sizeof (GtkGadgetClass),
151 * (GBaseInitFunc) NULL,
152 * (GBaseFinalizeFunc) NULL,
153 * (GClassInitFunc) gtk_gadget_class_intern_init,
154 * (GClassFinalizeFunc) gtk_gadget_class_finalize,
155 * NULL, // class_data
156 * sizeof (GtkGadget),
157 * 0, // n_preallocs
158 * (GInstanceInitFunc) gtk_gadget_init,
159 * NULL // value_table
160 * };
161 * gtk_gadget_type_id = g_type_module_register_type (type_module,
162 * GTK_TYPE_THING,
163 * "GtkGadget",
164 * &g_define_type_info,
165 * (GTypeFlags) flags);
166 * {
167 * const GInterfaceInfo g_implement_interface_info = {
168 * (GInterfaceInitFunc) gtk_gadget_gizmo_init
169 * };
170 * g_type_module_add_interface (type_module, g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
171 * }
172 * }
173 * ]|
174 *
175 * Since: 2.14
176 */
177#define G_DEFINE_DYNAMIC_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
178static void type_name##_init (TypeName *self); \
179static void type_name##_class_init (TypeName##Class *klass); \
180static void type_name##_class_finalize (TypeName##Class *klass); \
181static gpointer type_name##_parent_class = NULL; \
182static GType type_name##_type_id = 0; \
183static gint TypeName##_private_offset; \
184\
185_G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
186\
187G_GNUC_UNUSED \
188static inline gpointer \
189type_name##_get_instance_private (TypeName *self) \
190{ \
191 return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
192} \
193\
194GType \
195type_name##_get_type (void) \
196{ \
197 return type_name##_type_id; \
198} \
199static void \
200type_name##_register_type (GTypeModule *type_module) \
201{ \
202 GType g_define_type_id G_GNUC_UNUSED; \
203 const GTypeInfo g_define_type_info = { \
204 sizeof (TypeName##Class), \
205 (GBaseInitFunc) NULL, \
206 (GBaseFinalizeFunc) NULL, \
207 (GClassInitFunc) type_name##_class_intern_init, \
208 (GClassFinalizeFunc) type_name##_class_finalize, \
209 NULL, /* class_data */ \
210 sizeof (TypeName), \
211 0, /* n_preallocs */ \
212 (GInstanceInitFunc) type_name##_init, \
213 NULL /* value_table */ \
214 }; \
215 type_name##_type_id = g_type_module_register_type (type_module, \
216 TYPE_PARENT, \
217 #TypeName, \
218 &g_define_type_info, \
219 (GTypeFlags) flags); \
220 g_define_type_id = type_name##_type_id; \
221 { CODE ; } \
222}
223
224/**
225 * G_IMPLEMENT_INTERFACE_DYNAMIC:
226 * @TYPE_IFACE: The #GType of the interface to add
227 * @iface_init: The interface init function
228 *
229 * A convenience macro to ease interface addition in the @_C_ section
230 * of G_DEFINE_DYNAMIC_TYPE_EXTENDED(). See G_DEFINE_DYNAMIC_TYPE_EXTENDED()
231 * for an example.
232 *
233 * Note that this macro can only be used together with the
234 * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
235 * names from that macro.
236 *
237 * Since: 2.24
238 */
239#define G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init) { \
240 const GInterfaceInfo g_implement_interface_info = { \
241 (GInterfaceInitFunc) iface_init, NULL, NULL \
242 }; \
243 g_type_module_add_interface (type_module, g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
244}
245
246/**
247 * G_ADD_PRIVATE_DYNAMIC:
248 * @TypeName: the name of the type in CamelCase
249 *
250 * A convenience macro to ease adding private data to instances of a new dynamic
251 * type in the @_C_ section of G_DEFINE_DYNAMIC_TYPE_EXTENDED(). See
252 * G_ADD_PRIVATE() for details, it is similar but for static types.
253 *
254 * Note that this macro can only be used together with the
255 * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
256 * names from that macro.
257 *
258 * Since: 2.38
259 */
260#define G_ADD_PRIVATE_DYNAMIC(TypeName) { \
261 TypeName##_private_offset = sizeof (TypeName##Private); \
262}
263
264GLIB_AVAILABLE_IN_ALL
265GType g_type_module_get_type (void) G_GNUC_CONST;
266GLIB_AVAILABLE_IN_ALL
267gboolean g_type_module_use (GTypeModule *module);
268GLIB_AVAILABLE_IN_ALL
269void g_type_module_unuse (GTypeModule *module);
270GLIB_AVAILABLE_IN_ALL
271void g_type_module_set_name (GTypeModule *module,
272 const gchar *name);
273GLIB_AVAILABLE_IN_ALL
274GType g_type_module_register_type (GTypeModule *module,
275 GType parent_type,
276 const gchar *type_name,
277 const GTypeInfo *type_info,
278 GTypeFlags flags);
279GLIB_AVAILABLE_IN_ALL
280void g_type_module_add_interface (GTypeModule *module,
281 GType instance_type,
282 GType interface_type,
283 const GInterfaceInfo *interface_info);
284GLIB_AVAILABLE_IN_ALL
285GType g_type_module_register_enum (GTypeModule *module,
286 const gchar *name,
287 const GEnumValue *const_static_values);
288GLIB_AVAILABLE_IN_ALL
289GType g_type_module_register_flags (GTypeModule *module,
290 const gchar *name,
291 const GFlagsValue *const_static_values);
292
293G_END_DECLS
294
295#endif /* __G_TYPE_MODULE_H__ */
296