1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
19 #ifndef __G_TYPE_MODULE_H__
20 #define __G_TYPE_MODULE_H__
22 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
23 #error "Only <glib-object.h> can be included directly."
26 #include <gobject/gobject.h>
27 #include <gobject/genums.h>
31 typedef struct _GTypeModule GTypeModule;
32 typedef struct _GTypeModuleClass GTypeModuleClass;
34 #define G_TYPE_TYPE_MODULE (g_type_module_get_type ())
35 #define G_TYPE_MODULE(module) (G_TYPE_CHECK_INSTANCE_CAST ((module), G_TYPE_TYPE_MODULE, GTypeModule))
36 #define G_TYPE_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_TYPE_MODULE, GTypeModuleClass))
37 #define G_IS_TYPE_MODULE(module) (G_TYPE_CHECK_INSTANCE_TYPE ((module), G_TYPE_TYPE_MODULE))
38 #define G_IS_TYPE_MODULE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_TYPE_MODULE))
39 #define G_TYPE_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), G_TYPE_TYPE_MODULE, GTypeModuleClass))
41 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTypeModule, g_object_unref)
45 * @name: the name of the module
47 * The members of the GTypeModule structure should not
48 * be accessed directly, except for the @name field.
52 GObject parent_instance;
56 GSList *interface_infos;
64 * @parent_class: the parent class
65 * @load: loads the module and registers one or more types using
66 * g_type_module_register_type().
67 * @unload: unloads the module
69 * In order to implement dynamic loading of types based on #GTypeModule,
70 * the @load and @unload functions in #GTypeModuleClass must be implemented.
72 struct _GTypeModuleClass
74 GObjectClass parent_class;
77 gboolean (* load) (GTypeModule *module);
78 void (* unload) (GTypeModule *module);
81 /* Padding for future expansion */
82 void (*reserved1) (void);
83 void (*reserved2) (void);
84 void (*reserved3) (void);
85 void (*reserved4) (void);
89 * G_DEFINE_DYNAMIC_TYPE:
90 * @TN: The name of the new type, in Camel case.
91 * @t_n: The name of the new type, in lowercase, with words
93 * @T_P: The #GType of the parent type.
95 * A convenience macro for dynamic type implementations, which declares a
96 * class initialization function, an instance initialization function (see
97 * #GTypeInfo for information about these) and a static variable named
98 * `t_n`_parent_class pointing to the parent class.
100 * Furthermore, it defines a `*_get_type()` and a static `*_register_type()`
101 * functions for use in your `module_init()`.
103 * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
107 #define G_DEFINE_DYNAMIC_TYPE(TN, t_n, T_P) G_DEFINE_DYNAMIC_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
109 * G_DEFINE_DYNAMIC_TYPE_EXTENDED:
110 * @TypeName: The name of the new type, in Camel case.
111 * @type_name: The name of the new type, in lowercase, with words
113 * @TYPE_PARENT: The #GType of the parent type.
114 * @flags: #GTypeFlags to pass to g_type_module_register_type()
115 * @CODE: Custom code that gets inserted in the *_get_type() function.
117 * A more general version of G_DEFINE_DYNAMIC_TYPE() which
118 * allows to specify #GTypeFlags and custom code.
120 * |[<!-- language="C" -->
121 * G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtkGadget,
125 * G_IMPLEMENT_INTERFACE_DYNAMIC (TYPE_GIZMO,
126 * gtk_gadget_gizmo_init));
131 * |[<!-- language="C" -->
132 * static void gtk_gadget_init (GtkGadget *self);
133 * static void gtk_gadget_class_init (GtkGadgetClass *klass);
134 * static void gtk_gadget_class_finalize (GtkGadgetClass *klass);
136 * static gpointer gtk_gadget_parent_class = NULL;
137 * static GType gtk_gadget_type_id = 0;
139 * static void gtk_gadget_class_intern_init (gpointer klass)
141 * gtk_gadget_parent_class = g_type_class_peek_parent (klass);
142 * gtk_gadget_class_init ((GtkGadgetClass*) klass);
146 * gtk_gadget_get_type (void)
148 * return gtk_gadget_type_id;
152 * gtk_gadget_register_type (GTypeModule *type_module)
154 * const GTypeInfo g_define_type_info = {
155 * sizeof (GtkGadgetClass),
156 * (GBaseInitFunc) NULL,
157 * (GBaseFinalizeFunc) NULL,
158 * (GClassInitFunc) gtk_gadget_class_intern_init,
159 * (GClassFinalizeFunc) gtk_gadget_class_finalize,
160 * NULL, // class_data
161 * sizeof (GtkGadget),
163 * (GInstanceInitFunc) gtk_gadget_init,
164 * NULL // value_table
166 * gtk_gadget_type_id = g_type_module_register_type (type_module,
169 * &g_define_type_info,
170 * (GTypeFlags) flags);
172 * const GInterfaceInfo g_implement_interface_info = {
173 * (GInterfaceInitFunc) gtk_gadget_gizmo_init
175 * g_type_module_add_interface (type_module, g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
182 #define G_DEFINE_DYNAMIC_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
183 static void type_name##_init (TypeName *self); \
184 static void type_name##_class_init (TypeName##Class *klass); \
185 static void type_name##_class_finalize (TypeName##Class *klass); \
186 static gpointer type_name##_parent_class = NULL; \
187 static GType type_name##_type_id = 0; \
188 static gint TypeName##_private_offset; \
190 _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
193 static inline gpointer \
194 type_name##_get_instance_private (TypeName *self) \
196 return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
200 type_name##_get_type (void) \
202 return type_name##_type_id; \
205 type_name##_register_type (GTypeModule *type_module) \
207 GType g_define_type_id G_GNUC_UNUSED; \
208 const GTypeInfo g_define_type_info = { \
209 sizeof (TypeName##Class), \
210 (GBaseInitFunc) NULL, \
211 (GBaseFinalizeFunc) NULL, \
212 (GClassInitFunc)(void (*)(void)) type_name##_class_intern_init, \
213 (GClassFinalizeFunc)(void (*)(void)) type_name##_class_finalize, \
214 NULL, /* class_data */ \
216 0, /* n_preallocs */ \
217 (GInstanceInitFunc)(void (*)(void)) type_name##_init, \
218 NULL /* value_table */ \
220 type_name##_type_id = g_type_module_register_type (type_module, \
223 &g_define_type_info, \
224 (GTypeFlags) flags); \
225 g_define_type_id = type_name##_type_id; \
230 * G_IMPLEMENT_INTERFACE_DYNAMIC:
231 * @TYPE_IFACE: The #GType of the interface to add
232 * @iface_init: The interface init function
234 * A convenience macro to ease interface addition in the @_C_ section
235 * of G_DEFINE_DYNAMIC_TYPE_EXTENDED().
237 * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
239 * Note that this macro can only be used together with the
240 * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
241 * names from that macro.
245 #define G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init) { \
246 const GInterfaceInfo g_implement_interface_info = { \
247 (GInterfaceInitFunc)(void (*)(void)) iface_init, NULL, NULL \
249 g_type_module_add_interface (type_module, g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
253 * G_ADD_PRIVATE_DYNAMIC:
254 * @TypeName: the name of the type in CamelCase
256 * A convenience macro to ease adding private data to instances of a new dynamic
257 * type in the @_C_ section of G_DEFINE_DYNAMIC_TYPE_EXTENDED().
259 * See G_ADD_PRIVATE() for details, it is similar but for static types.
261 * Note that this macro can only be used together with the
262 * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
263 * names from that macro.
267 #define G_ADD_PRIVATE_DYNAMIC(TypeName) { \
268 TypeName##_private_offset = sizeof (TypeName##Private); \
271 GOBJECT_AVAILABLE_IN_ALL
272 GType g_type_module_get_type (void) G_GNUC_CONST;
273 GOBJECT_AVAILABLE_IN_ALL
274 gboolean g_type_module_use (GTypeModule *module);
275 GOBJECT_AVAILABLE_IN_ALL
276 void g_type_module_unuse (GTypeModule *module);
277 GOBJECT_AVAILABLE_IN_ALL
278 void g_type_module_set_name (GTypeModule *module,
280 GOBJECT_AVAILABLE_IN_ALL
281 GType g_type_module_register_type (GTypeModule *module,
283 const gchar *type_name,
284 const GTypeInfo *type_info,
286 GOBJECT_AVAILABLE_IN_ALL
287 void g_type_module_add_interface (GTypeModule *module,
289 GType interface_type,
290 const GInterfaceInfo *interface_info);
291 GOBJECT_AVAILABLE_IN_ALL
292 GType g_type_module_register_enum (GTypeModule *module,
294 const GEnumValue *const_static_values);
295 GOBJECT_AVAILABLE_IN_ALL
296 GType g_type_module_register_flags (GTypeModule *module,
298 const GFlagsValue *const_static_values);
302 #endif /* __G_TYPE_MODULE_H__ */