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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * @Short_description: Type loading modules
22 * @See_also:<variablelist>
24 * <term>#GTypePlugin</term>
25 * <listitem><para>The abstract type loader interface.</para></listitem>
28 * <term>#GModule</term>
29 * <listitem><para>Portable mechanism for dynamically loaded modules.</para></listitem>
34 * #GTypeModule provides a simple implementation of the #GTypePlugin
35 * interface. The model of #GTypeModule is a dynamically loaded module
36 * which implements some number of types and interface
37 * implementations. When the module is loaded, it registers its types
38 * and interfaces using g_type_module_register_type() and
39 * g_type_module_add_interface(). As long as any instances of these
40 * types and interface implementations are in use, the module is kept
41 * loaded. When the types and interfaces are gone, the module may be
42 * unloaded. If the types and interfaces become used again, the module
43 * will be reloaded. Note that the last unref can not happen in module
44 * code, since that would lead to the caller's code being unloaded before
45 * g_object_unref() returns to it.
47 * Keeping track of whether the module should be loaded or not is done by
48 * using a use count - it starts at zero, and whenever it is greater than
49 * zero, the module is loaded. The use count is maintained internally by
50 * the type system, but also can be explicitly controlled by
51 * g_type_module_use() and g_type_module_unuse(). Typically, when loading
52 * a module for the first type, g_type_module_use() will be used to load
53 * it so that it can initialize its types. At some later point, when the
54 * module no longer needs to be loaded except for the type
55 * implementations it contains, g_type_module_unuse() is called.
57 * #GTypeModule does not actually provide any implementation of module
58 * loading and unloading. To create a particular module type you must
59 * derive from #GTypeModule and implement the load and unload functions
60 * in #GTypeModuleClass.
65 #include "gtypeplugin.h"
66 #include "gtypemodule.h"
67 #include "gobjectalias.h"
69 typedef struct _ModuleTypeInfo ModuleTypeInfo;
70 typedef struct _ModuleInterfaceInfo ModuleInterfaceInfo;
72 struct _ModuleTypeInfo
80 struct _ModuleInterfaceInfo
88 static void g_type_module_use_plugin (GTypePlugin *plugin);
89 static void g_type_module_complete_type_info (GTypePlugin *plugin,
92 GTypeValueTable *value_table);
93 static void g_type_module_complete_interface_info (GTypePlugin *plugin,
96 GInterfaceInfo *info);
98 static gpointer parent_class = NULL;
101 g_type_module_dispose (GObject *object)
103 GTypeModule *module = G_TYPE_MODULE (object);
105 if (module->type_infos || module->interface_infos)
107 g_warning (G_STRLOC ": unsolicitated invocation of g_object_dispose() on GTypeModule");
109 g_object_ref (object);
112 G_OBJECT_CLASS (parent_class)->dispose (object);
116 g_type_module_finalize (GObject *object)
118 GTypeModule *module = G_TYPE_MODULE (object);
120 g_free (module->name);
122 G_OBJECT_CLASS (parent_class)->finalize (object);
126 g_type_module_class_init (GTypeModuleClass *class)
128 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
130 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
132 gobject_class->dispose = g_type_module_dispose;
133 gobject_class->finalize = g_type_module_finalize;
137 g_type_module_iface_init (GTypePluginClass *iface)
139 iface->use_plugin = g_type_module_use_plugin;
140 iface->unuse_plugin = (void (*) (GTypePlugin *))g_type_module_unuse;
141 iface->complete_type_info = g_type_module_complete_type_info;
142 iface->complete_interface_info = g_type_module_complete_interface_info;
146 g_type_module_get_type (void)
148 static GType type_module_type = 0;
150 if (!type_module_type)
152 static const GTypeInfo type_module_info = {
153 sizeof (GTypeModuleClass),
154 NULL, /* base_init */
155 NULL, /* base_finalize */
156 (GClassInitFunc) g_type_module_class_init,
157 NULL, /* class_finalize */
158 NULL, /* class_data */
159 sizeof (GTypeModule),
161 NULL, /* instance_init */
163 static const GInterfaceInfo iface_info = {
164 (GInterfaceInitFunc) g_type_module_iface_init,
165 NULL, /* interface_finalize */
166 NULL, /* interface_data */
169 type_module_type = g_type_register_static (G_TYPE_OBJECT, g_intern_static_string ("GTypeModule"), &type_module_info, G_TYPE_FLAG_ABSTRACT);
171 g_type_add_interface_static (type_module_type, G_TYPE_TYPE_PLUGIN, &iface_info);
174 return type_module_type;
178 * g_type_module_set_name:
179 * @module: a #GTypeModule.
180 * @name: a human-readable name to use in error messages.
182 * Sets the name for a #GTypeModule
185 g_type_module_set_name (GTypeModule *module,
188 g_return_if_fail (G_IS_TYPE_MODULE (module));
190 g_free (module->name);
191 module->name = g_strdup (name);
194 static ModuleTypeInfo *
195 g_type_module_find_type_info (GTypeModule *module,
198 GSList *tmp_list = module->type_infos;
201 ModuleTypeInfo *type_info = tmp_list->data;
202 if (type_info->type == type)
205 tmp_list = tmp_list->next;
211 static ModuleInterfaceInfo *
212 g_type_module_find_interface_info (GTypeModule *module,
214 GType interface_type)
216 GSList *tmp_list = module->interface_infos;
219 ModuleInterfaceInfo *interface_info = tmp_list->data;
220 if (interface_info->instance_type == instance_type &&
221 interface_info->interface_type == interface_type)
222 return interface_info;
224 tmp_list = tmp_list->next;
232 * @module: a #GTypeModule
234 * Increases the use count of a #GTypeModule by one. If the
235 * use count was zero before, the plugin will be loaded.
237 * Returns: %FALSE if the plugin needed to be loaded and
238 * loading the plugin failed.
241 g_type_module_use (GTypeModule *module)
243 g_return_val_if_fail (G_IS_TYPE_MODULE (module), FALSE);
246 if (module->use_count == 1)
250 if (!G_TYPE_MODULE_GET_CLASS (module)->load (module))
256 tmp_list = module->type_infos;
259 ModuleTypeInfo *type_info = tmp_list->data;
260 if (!type_info->loaded)
262 g_warning ("plugin '%s' failed to register type '%s'\n",
263 module->name ? module->name : "(unknown)",
264 g_type_name (type_info->type));
268 tmp_list = tmp_list->next;
276 * g_type_module_unuse:
277 * @module: a #GTypeModule
279 * Decreases the use count of a #GTypeModule by one. If the
280 * result is zero, the module will be unloaded. (However, the
281 * #GTypeModule will not be freed, and types associated with the
282 * #GTypeModule are not unregistered. Once a #GTypeModule is
283 * initialized, it must exist forever.)
286 g_type_module_unuse (GTypeModule *module)
288 g_return_if_fail (G_IS_TYPE_MODULE (module));
289 g_return_if_fail (module->use_count > 0);
293 if (module->use_count == 0)
297 G_TYPE_MODULE_GET_CLASS (module)->unload (module);
299 tmp_list = module->type_infos;
302 ModuleTypeInfo *type_info = tmp_list->data;
303 type_info->loaded = FALSE;
305 tmp_list = tmp_list->next;
311 g_type_module_use_plugin (GTypePlugin *plugin)
313 GTypeModule *module = G_TYPE_MODULE (plugin);
315 if (!g_type_module_use (module))
317 g_warning ("Fatal error - Could not reload previously loaded plugin '%s'\n",
318 module->name ? module->name : "(unknown)");
324 g_type_module_complete_type_info (GTypePlugin *plugin,
327 GTypeValueTable *value_table)
329 GTypeModule *module = G_TYPE_MODULE (plugin);
330 ModuleTypeInfo *module_type_info = g_type_module_find_type_info (module, g_type);
332 *info = module_type_info->info;
334 if (module_type_info->info.value_table)
335 *value_table = *module_type_info->info.value_table;
339 g_type_module_complete_interface_info (GTypePlugin *plugin,
341 GType interface_type,
342 GInterfaceInfo *info)
344 GTypeModule *module = G_TYPE_MODULE (plugin);
345 ModuleInterfaceInfo *module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
347 *info = module_interface_info->info;
351 * g_type_module_register_type:
352 * @module: a #GTypeModule
353 * @parent_type: the type for the parent class
354 * @type_name: name for the type
355 * @type_info: type information structure
356 * @flags: flags field providing details about the type
358 * Looks up or registers a type that is implemented with a particular
359 * type plugin. If a type with name @type_name was previously registered,
360 * the #GType identifier for the type is returned, otherwise the type
361 * is newly registered, and the resulting #GType identifier returned.
363 * When reregistering a type (typically because a module is unloaded
364 * then reloaded, and reinitialized), @module and @parent_type must
365 * be the same as they were previously.
367 * As long as any instances of the type exist, the type plugin will
370 * Returns: the new or existing type ID
373 g_type_module_register_type (GTypeModule *module,
375 const gchar *type_name,
376 const GTypeInfo *type_info,
379 ModuleTypeInfo *module_type_info = NULL;
382 g_return_val_if_fail (module != NULL, 0);
383 g_return_val_if_fail (type_name != NULL, 0);
384 g_return_val_if_fail (type_info != NULL, 0);
386 type = g_type_from_name (type_name);
389 GTypePlugin *old_plugin = g_type_get_plugin (type);
391 if (old_plugin != G_TYPE_PLUGIN (module))
393 g_warning ("Two different plugins tried to register '%s'.", type_name);
400 module_type_info = g_type_module_find_type_info (module, type);
402 if (module_type_info->parent_type != parent_type)
404 const gchar *parent_type_name = g_type_name (parent_type);
406 g_warning ("Type '%s' recreated with different parent type.\n"
407 "(was '%s', now '%s')", type_name,
408 g_type_name (module_type_info->parent_type),
409 parent_type_name ? parent_type_name : "(unknown)");
413 if (module_type_info->info.value_table)
414 g_free ((GTypeValueTable *) module_type_info->info.value_table);
418 module_type_info = g_new (ModuleTypeInfo, 1);
420 module_type_info->parent_type = parent_type;
421 module_type_info->type = g_type_register_dynamic (parent_type, type_name, G_TYPE_PLUGIN (module), flags);
423 module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
426 module_type_info->loaded = TRUE;
427 module_type_info->info = *type_info;
428 if (type_info->value_table)
429 module_type_info->info.value_table = g_memdup (type_info->value_table,
430 sizeof (GTypeValueTable));
432 return module_type_info->type;
436 * g_type_module_add_interface:
437 * @module: a #GTypeModule
438 * @instance_type: type to which to add the interface.
439 * @interface_type: interface type to add
440 * @interface_info: type information structure
442 * Registers an additional interface for a type, whose interface
443 * lives in the given type plugin. If the interface was already registered
444 * for the type in this plugin, nothing will be done.
446 * As long as any instances of the type exist, the type plugin will
450 g_type_module_add_interface (GTypeModule *module,
452 GType interface_type,
453 const GInterfaceInfo *interface_info)
455 ModuleInterfaceInfo *module_interface_info = NULL;
457 g_return_if_fail (module != NULL);
458 g_return_if_fail (interface_info != NULL);
460 if (g_type_is_a (instance_type, interface_type))
462 GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
467 g_warning ("Interface '%s' for '%s' was previously registered statically or for a parent type.",
468 g_type_name (interface_type), g_type_name (instance_type));
471 else if (old_plugin != G_TYPE_PLUGIN (module))
473 g_warning ("Two different plugins tried to register interface '%s' for '%s'.",
474 g_type_name (interface_type), g_type_name (instance_type));
478 module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
480 g_assert (module_interface_info);
484 module_interface_info = g_new (ModuleInterfaceInfo, 1);
486 module_interface_info->instance_type = instance_type;
487 module_interface_info->interface_type = interface_type;
489 g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
491 module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
494 module_interface_info->loaded = TRUE;
495 module_interface_info->info = *interface_info;
499 * g_type_module_register_enum:
500 * @module: a #GTypeModule
501 * @name: name for the type
502 * @const_static_values: an array of #GEnumValue structs for the possible
503 * enumeration values. The array is terminated by a struct with all
506 * Looks up or registers an enumeration that is implemented with a particular
507 * type plugin. If a type with name @type_name was previously registered,
508 * the #GType identifier for the type is returned, otherwise the type
509 * is newly registered, and the resulting #GType identifier returned.
511 * As long as any instances of the type exist, the type plugin will
515 * Returns: the new or existing type ID
518 g_type_module_register_enum (GTypeModule *module,
520 const GEnumValue *const_static_values)
522 GTypeInfo enum_type_info = { 0, };
524 g_return_val_if_fail (G_IS_TYPE_MODULE (module), 0);
525 g_return_val_if_fail (name != NULL, 0);
526 g_return_val_if_fail (const_static_values != NULL, 0);
528 g_enum_complete_type_info (G_TYPE_ENUM,
529 &enum_type_info, const_static_values);
531 return g_type_module_register_type (G_TYPE_MODULE (module),
532 G_TYPE_ENUM, name, &enum_type_info, 0);
536 * g_type_module_register_flags:
537 * @module: a #GTypeModule
538 * @name: name for the type
539 * @const_static_values: an array of #GFlagsValue structs for the possible
540 * flags values. The array is terminated by a struct with all
543 * Looks up or registers a flags type that is implemented with a particular
544 * type plugin. If a type with name @type_name was previously registered,
545 * the #GType identifier for the type is returned, otherwise the type
546 * is newly registered, and the resulting #GType identifier returned.
548 * As long as any instances of the type exist, the type plugin will
552 * Returns: the new or existing type ID
555 g_type_module_register_flags (GTypeModule *module,
557 const GFlagsValue *const_static_values)
559 GTypeInfo flags_type_info = { 0, };
561 g_return_val_if_fail (G_IS_TYPE_MODULE (module), 0);
562 g_return_val_if_fail (name != NULL, 0);
563 g_return_val_if_fail (const_static_values != NULL, 0);
565 g_flags_complete_type_info (G_TYPE_FLAGS,
566 &flags_type_info, const_static_values);
568 return g_type_module_register_type (G_TYPE_MODULE (module),
569 G_TYPE_FLAGS, name, &flags_type_info, 0);
573 #define __G_TYPE_MODULE_C__
574 #include "gobjectaliasdef.c"