Update Hungarian translation
[glib.git] / gobject / gtypemodule.c
blob35d13e21470b9851a35a5ea726ff10836c328599
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, 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'\n",
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'\n",
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: 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 * Returns: the new or existing type ID
367 GType
368 g_type_module_register_type (GTypeModule *module,
369 GType parent_type,
370 const gchar *type_name,
371 const GTypeInfo *type_info,
372 GTypeFlags flags)
374 ModuleTypeInfo *module_type_info = NULL;
375 GType type;
377 g_return_val_if_fail (module != NULL, 0);
378 g_return_val_if_fail (type_name != NULL, 0);
379 g_return_val_if_fail (type_info != NULL, 0);
381 type = g_type_from_name (type_name);
382 if (type)
384 GTypePlugin *old_plugin = g_type_get_plugin (type);
386 if (old_plugin != G_TYPE_PLUGIN (module))
388 g_warning ("Two different plugins tried to register '%s'.", type_name);
389 return 0;
393 if (type)
395 module_type_info = g_type_module_find_type_info (module, type);
397 if (module_type_info->parent_type != parent_type)
399 const gchar *parent_type_name = g_type_name (parent_type);
401 g_warning ("Type '%s' recreated with different parent type.\n"
402 "(was '%s', now '%s')", type_name,
403 g_type_name (module_type_info->parent_type),
404 parent_type_name ? parent_type_name : "(unknown)");
405 return 0;
408 if (module_type_info->info.value_table)
409 g_free ((GTypeValueTable *) module_type_info->info.value_table);
411 else
413 module_type_info = g_new (ModuleTypeInfo, 1);
415 module_type_info->parent_type = parent_type;
416 module_type_info->type = g_type_register_dynamic (parent_type, type_name, G_TYPE_PLUGIN (module), flags);
418 module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
421 module_type_info->loaded = TRUE;
422 module_type_info->info = *type_info;
423 if (type_info->value_table)
424 module_type_info->info.value_table = g_memdup (type_info->value_table,
425 sizeof (GTypeValueTable));
427 return module_type_info->type;
431 * g_type_module_add_interface:
432 * @module: a #GTypeModule
433 * @instance_type: type to which to add the interface.
434 * @interface_type: interface type to add
435 * @interface_info: type information structure
437 * Registers an additional interface for a type, whose interface lives
438 * in the given type plugin. If the interface was already registered
439 * for the type in this plugin, nothing will be done.
441 * As long as any instances of the type exist, the type plugin will
442 * not be unloaded.
444 void
445 g_type_module_add_interface (GTypeModule *module,
446 GType instance_type,
447 GType interface_type,
448 const GInterfaceInfo *interface_info)
450 ModuleInterfaceInfo *module_interface_info = NULL;
452 g_return_if_fail (module != NULL);
453 g_return_if_fail (interface_info != NULL);
455 if (g_type_is_a (instance_type, interface_type))
457 GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
458 interface_type);
460 if (!old_plugin)
462 g_warning ("Interface '%s' for '%s' was previously registered statically or for a parent type.",
463 g_type_name (interface_type), g_type_name (instance_type));
464 return;
466 else if (old_plugin != G_TYPE_PLUGIN (module))
468 g_warning ("Two different plugins tried to register interface '%s' for '%s'.",
469 g_type_name (interface_type), g_type_name (instance_type));
470 return;
473 module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
475 g_assert (module_interface_info);
477 else
479 module_interface_info = g_new (ModuleInterfaceInfo, 1);
481 module_interface_info->instance_type = instance_type;
482 module_interface_info->interface_type = interface_type;
484 g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
486 module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
489 module_interface_info->loaded = TRUE;
490 module_interface_info->info = *interface_info;
494 * g_type_module_register_enum:
495 * @module: a #GTypeModule
496 * @name: name for the type
497 * @const_static_values: an array of #GEnumValue structs for the
498 * possible enumeration values. The array is
499 * terminated by a struct with all members being
500 * 0.
502 * Looks up or registers an enumeration that is implemented with a particular
503 * type plugin. If a type with name @type_name was previously registered,
504 * the #GType identifier for the type is returned, otherwise the type
505 * is newly registered, and the resulting #GType identifier returned.
507 * As long as any instances of the type exist, the type plugin will
508 * not be unloaded.
510 * Since: 2.6
512 * Returns: the new or existing type ID
514 GType
515 g_type_module_register_enum (GTypeModule *module,
516 const gchar *name,
517 const GEnumValue *const_static_values)
519 GTypeInfo enum_type_info = { 0, };
521 g_return_val_if_fail (G_IS_TYPE_MODULE (module), 0);
522 g_return_val_if_fail (name != NULL, 0);
523 g_return_val_if_fail (const_static_values != NULL, 0);
525 g_enum_complete_type_info (G_TYPE_ENUM,
526 &enum_type_info, const_static_values);
528 return g_type_module_register_type (G_TYPE_MODULE (module),
529 G_TYPE_ENUM, name, &enum_type_info, 0);
533 * g_type_module_register_flags:
534 * @module: a #GTypeModule
535 * @name: name for the type
536 * @const_static_values: an array of #GFlagsValue structs for the
537 * possible flags values. The array is
538 * terminated by a struct with all members being
539 * 0.
541 * Looks up or registers a flags type that is implemented with a particular
542 * type plugin. If a type with name @type_name was previously registered,
543 * the #GType identifier for the type is returned, otherwise the type
544 * is newly registered, and the resulting #GType identifier returned.
546 * As long as any instances of the type exist, the type plugin will
547 * not be unloaded.
549 * Since: 2.6
551 * Returns: the new or existing type ID
553 GType
554 g_type_module_register_flags (GTypeModule *module,
555 const gchar *name,
556 const GFlagsValue *const_static_values)
558 GTypeInfo flags_type_info = { 0, };
560 g_return_val_if_fail (G_IS_TYPE_MODULE (module), 0);
561 g_return_val_if_fail (name != NULL, 0);
562 g_return_val_if_fail (const_static_values != NULL, 0);
564 g_flags_complete_type_info (G_TYPE_FLAGS,
565 &flags_type_info, const_static_values);
567 return g_type_module_register_type (G_TYPE_MODULE (module),
568 G_TYPE_FLAGS, name, &flags_type_info, 0);