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