Merge branch 'wip/Jehan/fopen-modes' into 'master'
[glib.git] / gobject / gtypemodule.c
blob4ecaf8c8888bf2fba3d087ca3c54cafefdb5b000
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.1 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/>.
18 #include "config.h"
20 #include <stdlib.h>
22 #include "gtypeplugin.h"
23 #include "gtypemodule.h"
26 /**
27 * SECTION:gtypemodule
28 * @short_description: Type loading modules
29 * @see_also: #GTypePlugin, #GModule
30 * @title: GTypeModule
32 * #GTypeModule provides a simple implementation of the #GTypePlugin
33 * interface. The model of #GTypeModule is a dynamically loaded module
34 * which implements some number of types and interface implementations.
35 * When the module is loaded, it registers its types and interfaces
36 * using g_type_module_register_type() and g_type_module_add_interface().
37 * As long as any instances of these types and interface implementations
38 * are in use, the module is kept loaded. When the types and interfaces
39 * are gone, the module may be unloaded. If the types and interfaces
40 * become used again, the module will be reloaded. Note that the last
41 * unref cannot happen in module code, since that would lead to the
42 * caller's code being unloaded before g_object_unref() returns to it.
44 * Keeping track of whether the module should be loaded or not is done by
45 * using a use count - it starts at zero, and whenever it is greater than
46 * zero, the module is loaded. The use count is maintained internally by
47 * the type system, but also can be explicitly controlled by
48 * g_type_module_use() and g_type_module_unuse(). Typically, when loading
49 * a module for the first type, g_type_module_use() will be used to load
50 * it so that it can initialize its types. At some later point, when the
51 * module no longer needs to be loaded except for the type
52 * implementations it contains, g_type_module_unuse() is called.
54 * #GTypeModule does not actually provide any implementation of module
55 * loading and unloading. To create a particular module type you must
56 * derive from #GTypeModule and implement the load and unload functions
57 * in #GTypeModuleClass.
61 typedef struct _ModuleTypeInfo ModuleTypeInfo;
62 typedef struct _ModuleInterfaceInfo ModuleInterfaceInfo;
64 struct _ModuleTypeInfo
66 gboolean loaded;
67 GType type;
68 GType parent_type;
69 GTypeInfo info;
72 struct _ModuleInterfaceInfo
74 gboolean loaded;
75 GType instance_type;
76 GType interface_type;
77 GInterfaceInfo info;
80 static void g_type_module_use_plugin (GTypePlugin *plugin);
81 static void g_type_module_complete_type_info (GTypePlugin *plugin,
82 GType g_type,
83 GTypeInfo *info,
84 GTypeValueTable *value_table);
85 static void g_type_module_complete_interface_info (GTypePlugin *plugin,
86 GType instance_type,
87 GType interface_type,
88 GInterfaceInfo *info);
90 static gpointer parent_class = NULL;
92 static void
93 g_type_module_dispose (GObject *object)
95 GTypeModule *module = G_TYPE_MODULE (object);
97 if (module->type_infos || module->interface_infos)
99 g_warning (G_STRLOC ": unsolicitated invocation of g_object_run_dispose() on GTypeModule");
101 g_object_ref (object);
104 G_OBJECT_CLASS (parent_class)->dispose (object);
107 static void
108 g_type_module_finalize (GObject *object)
110 GTypeModule *module = G_TYPE_MODULE (object);
112 g_free (module->name);
114 G_OBJECT_CLASS (parent_class)->finalize (object);
117 static void
118 g_type_module_class_init (GTypeModuleClass *class)
120 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
122 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
124 gobject_class->dispose = g_type_module_dispose;
125 gobject_class->finalize = g_type_module_finalize;
128 static void
129 g_type_module_iface_init (GTypePluginClass *iface)
131 iface->use_plugin = g_type_module_use_plugin;
132 iface->unuse_plugin = (void (*) (GTypePlugin *))g_type_module_unuse;
133 iface->complete_type_info = g_type_module_complete_type_info;
134 iface->complete_interface_info = g_type_module_complete_interface_info;
137 GType
138 g_type_module_get_type (void)
140 static GType type_module_type = 0;
142 if (!type_module_type)
144 const GTypeInfo type_module_info = {
145 sizeof (GTypeModuleClass),
146 NULL, /* base_init */
147 NULL, /* base_finalize */
148 (GClassInitFunc) g_type_module_class_init,
149 NULL, /* class_finalize */
150 NULL, /* class_data */
151 sizeof (GTypeModule),
152 0, /* n_preallocs */
153 NULL, /* instance_init */
155 const GInterfaceInfo iface_info = {
156 (GInterfaceInitFunc) g_type_module_iface_init,
157 NULL, /* interface_finalize */
158 NULL, /* interface_data */
161 type_module_type = g_type_register_static (G_TYPE_OBJECT, g_intern_static_string ("GTypeModule"), &type_module_info, G_TYPE_FLAG_ABSTRACT);
163 g_type_add_interface_static (type_module_type, G_TYPE_TYPE_PLUGIN, &iface_info);
166 return type_module_type;
170 * g_type_module_set_name:
171 * @module: a #GTypeModule.
172 * @name: a human-readable name to use in error messages.
174 * Sets the name for a #GTypeModule
176 void
177 g_type_module_set_name (GTypeModule *module,
178 const gchar *name)
180 g_return_if_fail (G_IS_TYPE_MODULE (module));
182 g_free (module->name);
183 module->name = g_strdup (name);
186 static ModuleTypeInfo *
187 g_type_module_find_type_info (GTypeModule *module,
188 GType type)
190 GSList *tmp_list = module->type_infos;
191 while (tmp_list)
193 ModuleTypeInfo *type_info = tmp_list->data;
194 if (type_info->type == type)
195 return type_info;
197 tmp_list = tmp_list->next;
200 return NULL;
203 static ModuleInterfaceInfo *
204 g_type_module_find_interface_info (GTypeModule *module,
205 GType instance_type,
206 GType interface_type)
208 GSList *tmp_list = module->interface_infos;
209 while (tmp_list)
211 ModuleInterfaceInfo *interface_info = tmp_list->data;
212 if (interface_info->instance_type == instance_type &&
213 interface_info->interface_type == interface_type)
214 return interface_info;
216 tmp_list = tmp_list->next;
219 return NULL;
223 * g_type_module_use:
224 * @module: a #GTypeModule
226 * Increases the use count of a #GTypeModule by one. If the
227 * use count was zero before, the plugin will be loaded.
228 * If loading the plugin fails, the use count is reset to
229 * its prior value.
231 * Returns: %FALSE if the plugin needed to be loaded and
232 * loading the plugin failed.
234 gboolean
235 g_type_module_use (GTypeModule *module)
237 g_return_val_if_fail (G_IS_TYPE_MODULE (module), FALSE);
239 module->use_count++;
240 if (module->use_count == 1)
242 GSList *tmp_list;
244 if (!G_TYPE_MODULE_GET_CLASS (module)->load (module))
246 module->use_count--;
247 return FALSE;
250 tmp_list = module->type_infos;
251 while (tmp_list)
253 ModuleTypeInfo *type_info = tmp_list->data;
254 if (!type_info->loaded)
256 g_warning ("plugin '%s' failed to register type '%s'",
257 module->name ? module->name : "(unknown)",
258 g_type_name (type_info->type));
259 module->use_count--;
260 return FALSE;
263 tmp_list = tmp_list->next;
267 return TRUE;
271 * g_type_module_unuse:
272 * @module: a #GTypeModule
274 * Decreases the use count of a #GTypeModule by one. If the
275 * result is zero, the module will be unloaded. (However, the
276 * #GTypeModule will not be freed, and types associated with the
277 * #GTypeModule are not unregistered. Once a #GTypeModule is
278 * initialized, it must exist forever.)
280 void
281 g_type_module_unuse (GTypeModule *module)
283 g_return_if_fail (G_IS_TYPE_MODULE (module));
284 g_return_if_fail (module->use_count > 0);
286 module->use_count--;
288 if (module->use_count == 0)
290 GSList *tmp_list;
292 G_TYPE_MODULE_GET_CLASS (module)->unload (module);
294 tmp_list = module->type_infos;
295 while (tmp_list)
297 ModuleTypeInfo *type_info = tmp_list->data;
298 type_info->loaded = FALSE;
300 tmp_list = tmp_list->next;
305 static void
306 g_type_module_use_plugin (GTypePlugin *plugin)
308 GTypeModule *module = G_TYPE_MODULE (plugin);
310 if (!g_type_module_use (module))
312 g_warning ("Fatal error - Could not reload previously loaded plugin '%s'",
313 module->name ? module->name : "(unknown)");
314 exit (1);
318 static void
319 g_type_module_complete_type_info (GTypePlugin *plugin,
320 GType g_type,
321 GTypeInfo *info,
322 GTypeValueTable *value_table)
324 GTypeModule *module = G_TYPE_MODULE (plugin);
325 ModuleTypeInfo *module_type_info = g_type_module_find_type_info (module, g_type);
327 *info = module_type_info->info;
329 if (module_type_info->info.value_table)
330 *value_table = *module_type_info->info.value_table;
333 static void
334 g_type_module_complete_interface_info (GTypePlugin *plugin,
335 GType instance_type,
336 GType interface_type,
337 GInterfaceInfo *info)
339 GTypeModule *module = G_TYPE_MODULE (plugin);
340 ModuleInterfaceInfo *module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
342 *info = module_interface_info->info;
346 * g_type_module_register_type:
347 * @module: (nullable): a #GTypeModule
348 * @parent_type: the type for the parent class
349 * @type_name: name for the type
350 * @type_info: type information structure
351 * @flags: flags field providing details about the type
353 * Looks up or registers a type that is implemented with a particular
354 * type plugin. If a type with name @type_name was previously registered,
355 * the #GType identifier for the type is returned, otherwise the type
356 * is newly registered, and the resulting #GType identifier returned.
358 * When reregistering a type (typically because a module is unloaded
359 * then reloaded, and reinitialized), @module and @parent_type must
360 * be the same as they were previously.
362 * As long as any instances of the type exist, the type plugin will
363 * not be unloaded.
365 * Since 2.56 if @module is %NULL this will call g_type_register_static()
366 * instead. This can be used when making a static build of the module.
368 * Returns: the new or existing type ID
370 GType
371 g_type_module_register_type (GTypeModule *module,
372 GType parent_type,
373 const gchar *type_name,
374 const GTypeInfo *type_info,
375 GTypeFlags flags)
377 ModuleTypeInfo *module_type_info = NULL;
378 GType type;
380 g_return_val_if_fail (type_name != NULL, 0);
381 g_return_val_if_fail (type_info != NULL, 0);
383 if (module == NULL)
385 /* Cannot pass type_info directly to g_type_register_static() here because
386 * it has class_finalize != NULL and that's forbidden for static types */
387 return g_type_register_static_simple (parent_type,
388 type_name,
389 type_info->class_size,
390 type_info->class_init,
391 type_info->instance_size,
392 type_info->instance_init,
393 flags);
396 type = g_type_from_name (type_name);
397 if (type)
399 GTypePlugin *old_plugin = g_type_get_plugin (type);
401 if (old_plugin != G_TYPE_PLUGIN (module))
403 g_warning ("Two different plugins tried to register '%s'.", type_name);
404 return 0;
408 if (type)
410 module_type_info = g_type_module_find_type_info (module, type);
412 if (module_type_info->parent_type != parent_type)
414 const gchar *parent_type_name = g_type_name (parent_type);
416 g_warning ("Type '%s' recreated with different parent type."
417 "(was '%s', now '%s')", type_name,
418 g_type_name (module_type_info->parent_type),
419 parent_type_name ? parent_type_name : "(unknown)");
420 return 0;
423 if (module_type_info->info.value_table)
424 g_free ((GTypeValueTable *) module_type_info->info.value_table);
426 else
428 module_type_info = g_new (ModuleTypeInfo, 1);
430 module_type_info->parent_type = parent_type;
431 module_type_info->type = g_type_register_dynamic (parent_type, type_name, G_TYPE_PLUGIN (module), flags);
433 module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
436 module_type_info->loaded = TRUE;
437 module_type_info->info = *type_info;
438 if (type_info->value_table)
439 module_type_info->info.value_table = g_memdup (type_info->value_table,
440 sizeof (GTypeValueTable));
442 return module_type_info->type;
446 * g_type_module_add_interface:
447 * @module: (nullable): a #GTypeModule
448 * @instance_type: type to which to add the interface.
449 * @interface_type: interface type to add
450 * @interface_info: type information structure
452 * Registers an additional interface for a type, whose interface lives
453 * in the given type plugin. If the interface was already registered
454 * for the type in this plugin, nothing will be done.
456 * As long as any instances of the type exist, the type plugin will
457 * not be unloaded.
459 * Since 2.56 if @module is %NULL this will call g_type_add_interface_static()
460 * instead. This can be used when making a static build of the module.
462 void
463 g_type_module_add_interface (GTypeModule *module,
464 GType instance_type,
465 GType interface_type,
466 const GInterfaceInfo *interface_info)
468 ModuleInterfaceInfo *module_interface_info = NULL;
470 g_return_if_fail (interface_info != NULL);
472 if (module == NULL)
474 g_type_add_interface_static (instance_type, interface_type, interface_info);
475 return;
478 if (g_type_is_a (instance_type, interface_type))
480 GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
481 interface_type);
483 if (!old_plugin)
485 g_warning ("Interface '%s' for '%s' was previously registered statically or for a parent type.",
486 g_type_name (interface_type), g_type_name (instance_type));
487 return;
489 else if (old_plugin != G_TYPE_PLUGIN (module))
491 g_warning ("Two different plugins tried to register interface '%s' for '%s'.",
492 g_type_name (interface_type), g_type_name (instance_type));
493 return;
496 module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
498 g_assert (module_interface_info);
500 else
502 module_interface_info = g_new (ModuleInterfaceInfo, 1);
504 module_interface_info->instance_type = instance_type;
505 module_interface_info->interface_type = interface_type;
507 g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
509 module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
512 module_interface_info->loaded = TRUE;
513 module_interface_info->info = *interface_info;
517 * g_type_module_register_enum:
518 * @module: (nullable): a #GTypeModule
519 * @name: name for the type
520 * @const_static_values: an array of #GEnumValue structs for the
521 * possible enumeration values. The array is
522 * terminated by a struct with all members being
523 * 0.
525 * Looks up or registers an enumeration that is implemented with a particular
526 * type plugin. If a type with name @type_name was previously registered,
527 * the #GType identifier for the type is returned, otherwise the type
528 * is newly registered, and the resulting #GType identifier returned.
530 * As long as any instances of the type exist, the type plugin will
531 * not be unloaded.
533 * Since 2.56 if @module is %NULL this will call g_type_register_static()
534 * instead. This can be used when making a static build of the module.
536 * Since: 2.6
538 * Returns: the new or existing type ID
540 GType
541 g_type_module_register_enum (GTypeModule *module,
542 const gchar *name,
543 const GEnumValue *const_static_values)
545 GTypeInfo enum_type_info = { 0, };
547 g_return_val_if_fail (module == NULL || G_IS_TYPE_MODULE (module), 0);
548 g_return_val_if_fail (name != NULL, 0);
549 g_return_val_if_fail (const_static_values != NULL, 0);
551 g_enum_complete_type_info (G_TYPE_ENUM,
552 &enum_type_info, const_static_values);
554 return g_type_module_register_type (G_TYPE_MODULE (module),
555 G_TYPE_ENUM, name, &enum_type_info, 0);
559 * g_type_module_register_flags:
560 * @module: (nullable): a #GTypeModule
561 * @name: name for the type
562 * @const_static_values: an array of #GFlagsValue structs for the
563 * possible flags values. The array is
564 * terminated by a struct with all members being
565 * 0.
567 * Looks up or registers a flags type that is implemented with a particular
568 * type plugin. If a type with name @type_name was previously registered,
569 * the #GType identifier for the type is returned, otherwise the type
570 * is newly registered, and the resulting #GType identifier returned.
572 * As long as any instances of the type exist, the type plugin will
573 * not be unloaded.
575 * Since 2.56 if @module is %NULL this will call g_type_register_static()
576 * instead. This can be used when making a static build of the module.
578 * Since: 2.6
580 * Returns: the new or existing type ID
582 GType
583 g_type_module_register_flags (GTypeModule *module,
584 const gchar *name,
585 const GFlagsValue *const_static_values)
587 GTypeInfo flags_type_info = { 0, };
589 g_return_val_if_fail (module == NULL || G_IS_TYPE_MODULE (module), 0);
590 g_return_val_if_fail (name != NULL, 0);
591 g_return_val_if_fail (const_static_values != NULL, 0);
593 g_flags_complete_type_info (G_TYPE_FLAGS,
594 &flags_type_info, const_static_values);
596 return g_type_module_register_type (G_TYPE_MODULE (module),
597 G_TYPE_FLAGS, name, &flags_type_info, 0);