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.
20 #include "gtypeplugin.h"
21 #include "gtypemodule.h"
23 typedef struct _ModuleTypeInfo ModuleTypeInfo;
24 typedef struct _ModuleInterfaceInfo ModuleInterfaceInfo;
26 struct _ModuleTypeInfo
34 struct _ModuleInterfaceInfo
42 static void g_type_module_use_plugin (GTypePlugin *plugin);
43 static void g_type_module_complete_type_info (GTypePlugin *plugin,
46 GTypeValueTable *value_table);
47 static void g_type_module_complete_interface_info (GTypePlugin *plugin,
50 GInterfaceInfo *info);
52 static GObjectClass *parent_class;
55 g_type_module_shutdown (GObject *object)
57 GTypeModule *module = G_TYPE_MODULE (object);
59 if (module->type_infos || module->interface_infos)
61 g_warning (G_STRLOC ": shutdown should never happen for static type plugins once types or interfaces have been registered");
63 g_object_ref (object);
66 parent_class->shutdown (object);
70 g_type_module_finalize (GObject *object)
72 GTypeModule *module = G_TYPE_MODULE (object);
74 g_free (module->name);
76 parent_class->finalize (object);
80 g_type_module_class_init (GTypeModuleClass *class)
82 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
84 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
86 gobject_class->shutdown = g_type_module_shutdown;
87 gobject_class->finalize = g_type_module_finalize;
91 g_type_module_iface_init (GTypePluginClass *iface)
93 iface->use_plugin = g_type_module_use_plugin;
94 iface->unuse_plugin = (void (*) (GTypePlugin *))g_type_module_unuse;
95 iface->complete_type_info = g_type_module_complete_type_info;
96 iface->complete_interface_info = g_type_module_complete_interface_info;
100 g_type_module_get_type (void)
102 static GType type_module_type = 0;
104 if (!type_module_type)
106 static const GTypeInfo type_module_info = {
107 sizeof (GTypeModuleClass),
108 NULL, /* base_init */
109 NULL, /* base_finalize */
110 (GClassInitFunc) g_type_module_class_init,
111 NULL, /* class_finalize */
112 NULL, /* class_data */
113 sizeof (GTypeModule),
115 NULL, /* instance_init */
117 static const GInterfaceInfo iface_info = {
118 (GInterfaceInitFunc) g_type_module_iface_init,
119 NULL, /* interface_finalize */
120 NULL, /* interface_data */
123 type_module_type = g_type_register_static (G_TYPE_OBJECT, "GTypeModule", &type_module_info, G_TYPE_FLAG_ABSTRACT);
125 g_type_add_interface_static (type_module_type, G_TYPE_TYPE_PLUGIN, &iface_info);
128 return type_module_type;
132 * g_type_module_set_name
133 * @module: a #GTypeModule.
134 * @name: a human-readable name to use in error messages.
136 * Sets the name for a #GTypeModule
139 g_type_module_set_name (GTypeModule *module,
142 g_return_if_fail (G_IS_TYPE_MODULE (module));
144 g_free (module->name);
145 module->name = g_strdup (name);
148 static ModuleTypeInfo *
149 g_type_module_find_type_info (GTypeModule *module,
152 GSList *tmp_list = module->type_infos;
155 ModuleTypeInfo *type_info = tmp_list->data;
156 if (type_info->type == type)
159 tmp_list = tmp_list->next;
165 static ModuleInterfaceInfo *
166 g_type_module_find_interface_info (GTypeModule *module,
168 GType interface_type)
170 GSList *tmp_list = module->interface_infos;
173 ModuleInterfaceInfo *interface_info = tmp_list->data;
174 if (interface_info->instance_type == instance_type &&
175 interface_info->interface_type == interface_type)
176 return interface_info;
178 tmp_list = tmp_list->next;
186 * @module: a #GTypeModule
188 * Increases the use count of a #GTypeModule by one. If the
189 * use count was zero before, the plugin will be loaded.
191 * Return Value: %FALSE if the plugin needed to be loaded and
192 * loading the plugin failed.
195 g_type_module_use (GTypeModule *module)
197 g_return_val_if_fail (G_IS_TYPE_MODULE (module), FALSE);
200 if (module->use_count == 1)
204 if (!G_TYPE_MODULE_GET_CLASS (module)->load (module))
207 tmp_list = module->type_infos;
210 ModuleTypeInfo *type_info = tmp_list->data;
211 if (!type_info->loaded)
213 g_warning ("plugin '%s' failed to register type '%s'\n",
214 module->name ? module->name : "(unknown)",
215 g_type_name (type_info->type));
219 tmp_list = tmp_list->next;
227 * g_type_module_unuse:
228 * @module: a #GTypeModule
230 * Decreases the use count of a #GTypeModule by one. If the
231 * result is zero, the module will be unloaded. (However, the
232 * #GTypeModule will not be freed, and types associated with the
233 * #GTypeModule are not unregistered. Once a #GTypeModule is
234 * initialized, it must exist forever.)
237 g_type_module_unuse (GTypeModule *module)
239 g_return_if_fail (G_IS_TYPE_MODULE (module));
240 g_return_if_fail (module->use_count > 0);
244 if (module->use_count == 0)
248 G_TYPE_MODULE_GET_CLASS (module)->unload (module);
250 tmp_list = module->type_infos;
253 ModuleTypeInfo *type_info = tmp_list->data;
254 type_info->loaded = FALSE;
256 tmp_list = tmp_list->next;
262 g_type_module_use_plugin (GTypePlugin *plugin)
264 GTypeModule *module = G_TYPE_MODULE (plugin);
266 if (!g_type_module_use (module))
268 g_warning ("Fatal error - Could not reload previously loaded plugin '%s'\n",
269 module->name ? module->name : "(unknown)");
275 g_type_module_complete_type_info (GTypePlugin *plugin,
278 GTypeValueTable *value_table)
280 GTypeModule *module = G_TYPE_MODULE (plugin);
281 ModuleTypeInfo *module_type_info = g_type_module_find_type_info (module, g_type);
283 *info = module_type_info->info;
285 if (module_type_info->info.value_table)
286 *value_table = *module_type_info->info.value_table;
290 g_type_module_complete_interface_info (GTypePlugin *plugin,
291 GType interface_type,
293 GInterfaceInfo *info)
295 GTypeModule *module = G_TYPE_MODULE (plugin);
296 ModuleInterfaceInfo *module_interface_info = g_type_module_find_interface_info (module, interface_type, instance_type);
298 *info = module_interface_info->info;
302 * g_type_module_register_type:
303 * @module: a #GTypeModule
304 * @parent_type: the type for the parent class
305 * @type_name: name for the type
306 * @type_info: type information structure
307 * @flags: flags field providing details about the type
309 * Looks up or registers a type that is implemented with a particular
310 * type plugin. If a type with name @type_name is already registered,
311 * the #GType identifier for the type is returned, otherwise the type
312 * is newly registered, and the resulting #GType identifier returned.
314 * As long as any instances of the type exist, the type plugin will
317 * Return value: the type ID for the class.
320 g_type_module_register_type (GTypeModule *module,
322 const gchar *type_name,
323 const GTypeInfo *type_info,
326 ModuleTypeInfo *module_type_info = NULL;
329 g_return_val_if_fail (module != NULL, 0);
330 g_return_val_if_fail (type_name != NULL, 0);
331 g_return_val_if_fail (type_info != NULL, 0);
333 type = g_type_from_name (type_name);
336 GTypePlugin *old_plugin = g_type_get_plugin (type);
338 if (old_plugin != G_TYPE_PLUGIN (module))
340 g_warning ("Two different plugins tried to register '%s'.", type_name);
347 module_type_info = g_type_module_find_type_info (module, type);
349 if (module_type_info->parent_type != parent_type)
351 const gchar *parent_type_name = g_type_name (parent_type);
353 g_warning ("Type '%s' recreated with different parent type.\n"
354 "(was '%s', now '%s')", type_name,
355 g_type_name (module_type_info->parent_type),
356 parent_type_name ? parent_type_name : "(unknown)");
362 module_type_info = g_new (ModuleTypeInfo, 1);
364 module_type_info->parent_type = parent_type;
365 module_type_info->type = g_type_register_dynamic (parent_type, type_name, G_TYPE_PLUGIN (module), flags);
367 module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
370 module_type_info->loaded = TRUE;
371 module_type_info->info = *type_info;
372 if (type_info->value_table)
373 module_type_info->info.value_table = g_memdup (type_info->value_table,
374 sizeof (type_info->value_table));
376 return module_type_info->type;
380 * g_type_module_add_interface:
381 * @module: a #GTypeModule
382 * @instance_type: type to which to add the interface.
383 * @interface_type: interface type to add
384 * @interface_info: type information structure
386 * Registers an additional interface for a type, whose interface
387 * lives in the given type plugin. If the interface was already registered
388 * for the type in this plugin, nothing will be done.
390 * As long as any instances of the type exist, the type plugin will
394 g_type_module_add_interface (GTypeModule *module,
396 GType interface_type,
397 GInterfaceInfo *interface_info)
399 ModuleInterfaceInfo *module_interface_info = NULL;
401 g_return_if_fail (module != NULL);
402 g_return_if_fail (interface_info != NULL);
404 if (g_type_conforms_to (instance_type, interface_type))
406 GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
411 g_warning ("Interface '%s' for '%s' was previously registered statically or for a parent type.",
412 g_type_name (interface_type), g_type_name (instance_type));
415 else if (old_plugin != G_TYPE_PLUGIN (module))
417 g_warning ("Two different plugins tried to register interface '%s' for '%s'.",
418 g_type_name (interface_type), g_type_name (instance_type));
422 module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
424 g_assert (module_interface_info);
428 module_interface_info = g_new (ModuleInterfaceInfo, 1);
430 module_interface_info->instance_type = instance_type;
431 module_interface_info->interface_type = interface_type;
433 g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
435 module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
438 module_interface_info->loaded = TRUE;
439 module_interface_info->info = *interface_info;