Dist ref-add.sin, ref-del.sin. (#63092, Dan Winship)
[glib.git] / gobject / gtypemodule.c
blobdbb66bd74e9343e473147f715eff79871c56096c
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 <stdlib.h>
22 #include "gtypeplugin.h"
23 #include "gtypemodule.h"
25 typedef struct _ModuleTypeInfo ModuleTypeInfo;
26 typedef struct _ModuleInterfaceInfo ModuleInterfaceInfo;
28 struct _ModuleTypeInfo
30 gboolean loaded;
31 GType type;
32 GType parent_type;
33 GTypeInfo info;
36 struct _ModuleInterfaceInfo
38 gboolean loaded;
39 GType instance_type;
40 GType interface_type;
41 GInterfaceInfo info;
44 static void g_type_module_use_plugin (GTypePlugin *plugin);
45 static void g_type_module_complete_type_info (GTypePlugin *plugin,
46 GType g_type,
47 GTypeInfo *info,
48 GTypeValueTable *value_table);
49 static void g_type_module_complete_interface_info (GTypePlugin *plugin,
50 GType instance_type,
51 GType interface_type,
52 GInterfaceInfo *info);
54 static gpointer parent_class = NULL;
56 static void
57 g_type_module_dispose (GObject *object)
59 GTypeModule *module = G_TYPE_MODULE (object);
61 if (module->type_infos || module->interface_infos)
63 g_warning (G_STRLOC ": unsolicitated invocation of g_object_dispose() on GTypeModule");
65 g_object_ref (object);
68 G_OBJECT_CLASS (parent_class)->dispose (object);
71 static void
72 g_type_module_finalize (GObject *object)
74 GTypeModule *module = G_TYPE_MODULE (object);
76 g_free (module->name);
78 G_OBJECT_CLASS (parent_class)->finalize (object);
81 static void
82 g_type_module_class_init (GTypeModuleClass *class)
84 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
86 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
88 gobject_class->dispose = g_type_module_dispose;
89 gobject_class->finalize = g_type_module_finalize;
92 static void
93 g_type_module_iface_init (GTypePluginClass *iface)
95 iface->use_plugin = g_type_module_use_plugin;
96 iface->unuse_plugin = (void (*) (GTypePlugin *))g_type_module_unuse;
97 iface->complete_type_info = g_type_module_complete_type_info;
98 iface->complete_interface_info = g_type_module_complete_interface_info;
101 GType
102 g_type_module_get_type (void)
104 static GType type_module_type = 0;
106 if (!type_module_type)
108 static const GTypeInfo type_module_info = {
109 sizeof (GTypeModuleClass),
110 NULL, /* base_init */
111 NULL, /* base_finalize */
112 (GClassInitFunc) g_type_module_class_init,
113 NULL, /* class_finalize */
114 NULL, /* class_data */
115 sizeof (GTypeModule),
116 0, /* n_preallocs */
117 NULL, /* instance_init */
119 static const GInterfaceInfo iface_info = {
120 (GInterfaceInitFunc) g_type_module_iface_init,
121 NULL, /* interface_finalize */
122 NULL, /* interface_data */
125 type_module_type = g_type_register_static (G_TYPE_OBJECT, "GTypeModule", &type_module_info, G_TYPE_FLAG_ABSTRACT);
127 g_type_add_interface_static (type_module_type, G_TYPE_TYPE_PLUGIN, &iface_info);
130 return type_module_type;
134 * g_type_module_set_name
135 * @module: a #GTypeModule.
136 * @name: a human-readable name to use in error messages.
138 * Sets the name for a #GTypeModule
140 void
141 g_type_module_set_name (GTypeModule *module,
142 const gchar *name)
144 g_return_if_fail (G_IS_TYPE_MODULE (module));
146 g_free (module->name);
147 module->name = g_strdup (name);
150 static ModuleTypeInfo *
151 g_type_module_find_type_info (GTypeModule *module,
152 GType type)
154 GSList *tmp_list = module->type_infos;
155 while (tmp_list)
157 ModuleTypeInfo *type_info = tmp_list->data;
158 if (type_info->type == type)
159 return type_info;
161 tmp_list = tmp_list->next;
164 return NULL;
167 static ModuleInterfaceInfo *
168 g_type_module_find_interface_info (GTypeModule *module,
169 GType instance_type,
170 GType interface_type)
172 GSList *tmp_list = module->interface_infos;
173 while (tmp_list)
175 ModuleInterfaceInfo *interface_info = tmp_list->data;
176 if (interface_info->instance_type == instance_type &&
177 interface_info->interface_type == interface_type)
178 return interface_info;
180 tmp_list = tmp_list->next;
183 return NULL;
187 * g_type_module_use:
188 * @module: a #GTypeModule
190 * Increases the use count of a #GTypeModule by one. If the
191 * use count was zero before, the plugin will be loaded.
193 * Return Value: %FALSE if the plugin needed to be loaded and
194 * loading the plugin failed.
196 gboolean
197 g_type_module_use (GTypeModule *module)
199 g_return_val_if_fail (G_IS_TYPE_MODULE (module), FALSE);
201 module->use_count++;
202 if (module->use_count == 1)
204 GSList *tmp_list;
206 if (!G_TYPE_MODULE_GET_CLASS (module)->load (module))
208 module->use_count--;
209 return FALSE;
212 tmp_list = module->type_infos;
213 while (tmp_list)
215 ModuleTypeInfo *type_info = tmp_list->data;
216 if (!type_info->loaded)
218 g_warning ("plugin '%s' failed to register type '%s'\n",
219 module->name ? module->name : "(unknown)",
220 g_type_name (type_info->type));
221 return FALSE;
224 tmp_list = tmp_list->next;
228 return TRUE;
232 * g_type_module_unuse:
233 * @module: a #GTypeModule
235 * Decreases the use count of a #GTypeModule by one. If the
236 * result is zero, the module will be unloaded. (However, the
237 * #GTypeModule will not be freed, and types associated with the
238 * #GTypeModule are not unregistered. Once a #GTypeModule is
239 * initialized, it must exist forever.)
241 void
242 g_type_module_unuse (GTypeModule *module)
244 g_return_if_fail (G_IS_TYPE_MODULE (module));
245 g_return_if_fail (module->use_count > 0);
247 module->use_count--;
249 if (module->use_count == 0)
251 GSList *tmp_list;
253 G_TYPE_MODULE_GET_CLASS (module)->unload (module);
255 tmp_list = module->type_infos;
256 while (tmp_list)
258 ModuleTypeInfo *type_info = tmp_list->data;
259 type_info->loaded = FALSE;
261 tmp_list = tmp_list->next;
266 static void
267 g_type_module_use_plugin (GTypePlugin *plugin)
269 GTypeModule *module = G_TYPE_MODULE (plugin);
271 if (!g_type_module_use (module))
273 g_warning ("Fatal error - Could not reload previously loaded plugin '%s'\n",
274 module->name ? module->name : "(unknown)");
275 exit (1);
279 static void
280 g_type_module_complete_type_info (GTypePlugin *plugin,
281 GType g_type,
282 GTypeInfo *info,
283 GTypeValueTable *value_table)
285 GTypeModule *module = G_TYPE_MODULE (plugin);
286 ModuleTypeInfo *module_type_info = g_type_module_find_type_info (module, g_type);
288 *info = module_type_info->info;
290 if (module_type_info->info.value_table)
291 *value_table = *module_type_info->info.value_table;
294 static void
295 g_type_module_complete_interface_info (GTypePlugin *plugin,
296 GType instance_type,
297 GType interface_type,
298 GInterfaceInfo *info)
300 GTypeModule *module = G_TYPE_MODULE (plugin);
301 ModuleInterfaceInfo *module_interface_info = g_type_module_find_interface_info (module, interface_type, instance_type);
303 *info = module_interface_info->info;
307 * g_type_module_register_type:
308 * @module: a #GTypeModule
309 * @parent_type: the type for the parent class
310 * @type_name: name for the type
311 * @type_info: type information structure
312 * @flags: flags field providing details about the type
314 * Looks up or registers a type that is implemented with a particular
315 * type plugin. If a type with name @type_name is already registered,
316 * the #GType identifier for the type is returned, otherwise the type
317 * is newly registered, and the resulting #GType identifier returned.
319 * As long as any instances of the type exist, the type plugin will
320 * not be unloaded.
322 * Return value: the type ID for the class.
324 GType
325 g_type_module_register_type (GTypeModule *module,
326 GType parent_type,
327 const gchar *type_name,
328 const GTypeInfo *type_info,
329 GTypeFlags flags)
331 ModuleTypeInfo *module_type_info = NULL;
332 GType type;
334 g_return_val_if_fail (module != NULL, 0);
335 g_return_val_if_fail (type_name != NULL, 0);
336 g_return_val_if_fail (type_info != NULL, 0);
338 type = g_type_from_name (type_name);
339 if (type)
341 GTypePlugin *old_plugin = g_type_get_plugin (type);
343 if (old_plugin != G_TYPE_PLUGIN (module))
345 g_warning ("Two different plugins tried to register '%s'.", type_name);
346 return 0;
350 if (type)
352 module_type_info = g_type_module_find_type_info (module, type);
354 if (module_type_info->parent_type != parent_type)
356 const gchar *parent_type_name = g_type_name (parent_type);
358 g_warning ("Type '%s' recreated with different parent type.\n"
359 "(was '%s', now '%s')", type_name,
360 g_type_name (module_type_info->parent_type),
361 parent_type_name ? parent_type_name : "(unknown)");
362 return 0;
365 else
367 module_type_info = g_new (ModuleTypeInfo, 1);
369 module_type_info->parent_type = parent_type;
370 module_type_info->type = g_type_register_dynamic (parent_type, type_name, G_TYPE_PLUGIN (module), flags);
372 module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
375 module_type_info->loaded = TRUE;
376 module_type_info->info = *type_info;
377 if (type_info->value_table)
378 module_type_info->info.value_table = g_memdup (type_info->value_table,
379 sizeof (type_info->value_table));
381 return module_type_info->type;
385 * g_type_module_add_interface:
386 * @module: a #GTypeModule
387 * @instance_type: type to which to add the interface.
388 * @interface_type: interface type to add
389 * @interface_info: type information structure
391 * Registers an additional interface for a type, whose interface
392 * lives in the given type plugin. If the interface was already registered
393 * for the type in this plugin, nothing will be done.
395 * As long as any instances of the type exist, the type plugin will
396 * not be unloaded.
398 void
399 g_type_module_add_interface (GTypeModule *module,
400 GType instance_type,
401 GType interface_type,
402 GInterfaceInfo *interface_info)
404 ModuleInterfaceInfo *module_interface_info = NULL;
406 g_return_if_fail (module != NULL);
407 g_return_if_fail (interface_info != NULL);
409 if (g_type_is_a (instance_type, interface_type))
411 GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
412 interface_type);
414 if (!old_plugin)
416 g_warning ("Interface '%s' for '%s' was previously registered statically or for a parent type.",
417 g_type_name (interface_type), g_type_name (instance_type));
418 return;
420 else if (old_plugin != G_TYPE_PLUGIN (module))
422 g_warning ("Two different plugins tried to register interface '%s' for '%s'.",
423 g_type_name (interface_type), g_type_name (instance_type));
424 return;
427 module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
429 g_assert (module_interface_info);
431 else
433 module_interface_info = g_new (ModuleInterfaceInfo, 1);
435 module_interface_info->instance_type = instance_type;
436 module_interface_info->interface_type = interface_type;
438 g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
440 module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
443 module_interface_info->loaded = TRUE;
444 module_interface_info->info = *interface_info;