If loading the module fails, don't increment the use count.
[platform/upstream/glib.git] / gobject / gtypemodule.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdlib.h>
21
22 #include "gtypeplugin.h"
23 #include "gtypemodule.h"
24
25 typedef struct _ModuleTypeInfo ModuleTypeInfo;
26 typedef struct _ModuleInterfaceInfo ModuleInterfaceInfo;
27
28 struct _ModuleTypeInfo 
29 {
30   gboolean  loaded;
31   GType     type;
32   GType     parent_type;
33   GTypeInfo info;
34 };
35
36 struct _ModuleInterfaceInfo 
37 {
38   gboolean       loaded;
39   GType          instance_type;
40   GType          interface_type;
41   GInterfaceInfo info;
42 };
43
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            interface_type,
51                                                    GType            instance_info,
52                                                    GInterfaceInfo  *info);
53  
54 static GObjectClass *parent_class;
55
56 static void
57 g_type_module_shutdown (GObject *object)
58 {
59   GTypeModule *module = G_TYPE_MODULE (object);
60   
61   if (module->type_infos || module->interface_infos)
62     {
63       g_warning (G_STRLOC ": shutdown should never happen for static type plugins once types or interfaces have been registered");
64              
65       g_object_ref (object);
66     }
67
68   parent_class->shutdown (object);
69 }
70
71 static void
72 g_type_module_finalize (GObject *object)
73 {
74   GTypeModule *module = G_TYPE_MODULE (object);
75
76   g_free (module->name);
77
78   parent_class->finalize (object);
79 }
80
81 static void
82 g_type_module_class_init (GTypeModuleClass *class)
83 {
84   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
85
86   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
87   
88   gobject_class->shutdown = g_type_module_shutdown;
89   gobject_class->finalize = g_type_module_finalize;
90 }
91
92 static void
93 g_type_module_iface_init (GTypePluginClass *iface)
94 {
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;
99 }
100
101 GType
102 g_type_module_get_type (void)
103 {
104   static GType type_module_type = 0;
105
106   if (!type_module_type)
107     {
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 */
118       };
119       static const GInterfaceInfo iface_info = {
120         (GInterfaceInitFunc) g_type_module_iface_init,
121         NULL,               /* interface_finalize */
122         NULL,               /* interface_data */
123       };
124
125       type_module_type = g_type_register_static (G_TYPE_OBJECT, "GTypeModule", &type_module_info, G_TYPE_FLAG_ABSTRACT);
126
127       g_type_add_interface_static (type_module_type, G_TYPE_TYPE_PLUGIN, &iface_info);
128     }
129   
130   return type_module_type;
131 }
132
133 /**
134  * g_type_module_set_name
135  * @module: a #GTypeModule.
136  * @name: a human-readable name to use in error messages.
137  * 
138  * Sets the name for a #GTypeModule 
139  **/
140 void
141 g_type_module_set_name (GTypeModule  *module,
142                         const gchar  *name)
143 {
144   g_return_if_fail (G_IS_TYPE_MODULE (module));
145
146   g_free (module->name);
147   module->name = g_strdup (name);
148 }
149
150 static ModuleTypeInfo *
151 g_type_module_find_type_info (GTypeModule *module,
152                               GType        type)
153 {
154   GSList *tmp_list = module->type_infos;
155   while (tmp_list)
156     {
157       ModuleTypeInfo *type_info = tmp_list->data;
158       if (type_info->type == type)
159         return type_info;
160       
161       tmp_list = tmp_list->next;
162     }
163
164   return NULL;
165 }
166
167 static ModuleInterfaceInfo *
168 g_type_module_find_interface_info (GTypeModule *module,
169                                    GType        instance_type,
170                                    GType        interface_type)
171 {
172   GSList *tmp_list = module->interface_infos;
173   while (tmp_list)
174     {
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;
179       
180       tmp_list = tmp_list->next;
181     }
182
183   return NULL;
184 }
185
186 /**
187  * g_type_module_use:
188  * @module: a #GTypeModule
189  * 
190  * Increases the use count of a #GTypeModule by one. If the
191  * use count was zero before, the plugin will be loaded.
192  *
193  * Return Value: %FALSE if the plugin needed to be loaded and
194  *               loading the plugin failed.
195  **/
196 gboolean
197 g_type_module_use (GTypeModule *module)
198 {
199   g_return_val_if_fail (G_IS_TYPE_MODULE (module), FALSE);
200
201   module->use_count++;
202   if (module->use_count == 1)
203     {
204       GSList *tmp_list;
205       
206       if (!G_TYPE_MODULE_GET_CLASS (module)->load (module))
207         {
208           module->use_count--;
209           return FALSE;
210         }
211
212       tmp_list = module->type_infos;
213       while (tmp_list)
214         {
215           ModuleTypeInfo *type_info = tmp_list->data;
216           if (!type_info->loaded)
217             {
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;
222             }
223           
224           tmp_list = tmp_list->next;
225         }
226     }
227  
228   return TRUE;
229 }
230
231 /**
232  * g_type_module_unuse:
233  * @module: a #GTypeModule
234  * 
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.)
240  **/
241 void
242 g_type_module_unuse (GTypeModule *module)
243 {
244   g_return_if_fail (G_IS_TYPE_MODULE (module));
245   g_return_if_fail (module->use_count > 0);
246
247   module->use_count--;
248
249   if (module->use_count == 0)
250     {
251       GSList *tmp_list;
252
253       G_TYPE_MODULE_GET_CLASS (module)->unload (module);
254
255       tmp_list = module->type_infos;
256       while (tmp_list)
257         {
258           ModuleTypeInfo *type_info = tmp_list->data;
259           type_info->loaded = FALSE;
260
261           tmp_list = tmp_list->next;
262         }
263     }
264 }
265         
266 static void
267 g_type_module_use_plugin (GTypePlugin *plugin)
268 {
269   GTypeModule *module = G_TYPE_MODULE (plugin);
270
271   if (!g_type_module_use (module))
272     {
273       g_warning ("Fatal error - Could not reload previously loaded plugin '%s'\n",
274                  module->name ? module->name : "(unknown)");
275       exit (1);
276     }
277 }
278
279 static void
280 g_type_module_complete_type_info (GTypePlugin     *plugin,
281                                   GType            g_type,
282                                   GTypeInfo       *info,
283                                   GTypeValueTable *value_table)
284 {
285   GTypeModule *module = G_TYPE_MODULE (plugin);
286   ModuleTypeInfo *module_type_info = g_type_module_find_type_info (module, g_type);
287
288   *info = module_type_info->info;
289   
290   if (module_type_info->info.value_table)
291     *value_table = *module_type_info->info.value_table;
292 }
293
294 static void 
295 g_type_module_complete_interface_info (GTypePlugin    *plugin,
296                                        GType           interface_type,
297                                        GType           instance_type,
298                                        GInterfaceInfo *info)
299 {
300   GTypeModule *module = G_TYPE_MODULE (plugin);
301   ModuleInterfaceInfo *module_interface_info = g_type_module_find_interface_info (module, interface_type, instance_type);
302
303   *info = module_interface_info->info;
304 }
305
306 /**
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           
313  * 
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.
318  *
319  * As long as any instances of the type exist, the type plugin will
320  * not be unloaded.
321  *
322  * Return value: the type ID for the class.
323  **/
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)
330 {
331   ModuleTypeInfo *module_type_info = NULL;
332   GType type;
333   
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);
337
338   type = g_type_from_name (type_name);
339   if (type)
340     {
341       GTypePlugin *old_plugin = g_type_get_plugin (type);
342
343       if (old_plugin != G_TYPE_PLUGIN (module))
344         {
345           g_warning ("Two different plugins tried to register '%s'.", type_name);
346           return 0;
347         }
348     }
349
350   if (type)
351     {
352       module_type_info = g_type_module_find_type_info (module, type);
353
354       if (module_type_info->parent_type != parent_type)
355         {
356           const gchar *parent_type_name = g_type_name (parent_type);
357           
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;
363         }
364     }
365   else
366     {
367       module_type_info = g_new (ModuleTypeInfo, 1);
368       
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);
371       
372       module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
373     }
374
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));
380
381   return module_type_info->type;
382 }
383
384 /**
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
390  * 
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. 
394  *
395  * As long as any instances of the type exist, the type plugin will
396  * not be unloaded.
397  **/
398 void
399 g_type_module_add_interface (GTypeModule    *module,
400                              GType           instance_type,
401                              GType           interface_type,
402                              GInterfaceInfo *interface_info)
403 {
404   ModuleInterfaceInfo *module_interface_info = NULL;
405   
406   g_return_if_fail (module != NULL);
407   g_return_if_fail (interface_info != NULL);
408
409   if (g_type_conforms_to (instance_type, interface_type))
410     {
411       GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
412                                                              interface_type);
413
414       if (!old_plugin)
415         {
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;
419         }
420       else if (old_plugin != G_TYPE_PLUGIN (module))
421         {
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;
425         }
426       
427       module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
428
429       g_assert (module_interface_info);
430     }
431   else
432     {
433       module_interface_info = g_new (ModuleInterfaceInfo, 1);
434       
435       module_interface_info->instance_type = instance_type;
436       module_interface_info->interface_type = interface_type;
437       
438       g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
439       
440       module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
441     }
442   
443   module_interface_info->loaded = TRUE;
444   module_interface_info->info = *interface_info;
445 }