Improve chance to generate unique names with less effort a bit.
[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         return FALSE;
208
209       tmp_list = module->type_infos;
210       while (tmp_list)
211         {
212           ModuleTypeInfo *type_info = tmp_list->data;
213           if (!type_info->loaded)
214             {
215               g_warning ("plugin '%s' failed to register type '%s'\n",
216                          module->name ? module->name : "(unknown)",
217                          g_type_name (type_info->type));
218               return FALSE;
219             }
220           
221           tmp_list = tmp_list->next;
222         }
223     }
224  
225   return TRUE;
226 }
227
228 /**
229  * g_type_module_unuse:
230  * @module: a #GTypeModule
231  * 
232  * Decreases the use count of a #GTypeModule by one. If the
233  * result is zero, the module will be unloaded. (However, the
234  * #GTypeModule will not be freed, and types associated with the
235  * #GTypeModule are not unregistered. Once a #GTypeModule is 
236  * initialized, it must exist forever.)
237  **/
238 void
239 g_type_module_unuse (GTypeModule *module)
240 {
241   g_return_if_fail (G_IS_TYPE_MODULE (module));
242   g_return_if_fail (module->use_count > 0);
243
244   module->use_count--;
245
246   if (module->use_count == 0)
247     {
248       GSList *tmp_list;
249
250       G_TYPE_MODULE_GET_CLASS (module)->unload (module);
251
252       tmp_list = module->type_infos;
253       while (tmp_list)
254         {
255           ModuleTypeInfo *type_info = tmp_list->data;
256           type_info->loaded = FALSE;
257
258           tmp_list = tmp_list->next;
259         }
260     }
261 }
262         
263 static void
264 g_type_module_use_plugin (GTypePlugin *plugin)
265 {
266   GTypeModule *module = G_TYPE_MODULE (plugin);
267
268   if (!g_type_module_use (module))
269     {
270       g_warning ("Fatal error - Could not reload previously loaded plugin '%s'\n",
271                  module->name ? module->name : "(unknown)");
272       exit (1);
273     }
274 }
275
276 static void
277 g_type_module_complete_type_info (GTypePlugin     *plugin,
278                                   GType            g_type,
279                                   GTypeInfo       *info,
280                                   GTypeValueTable *value_table)
281 {
282   GTypeModule *module = G_TYPE_MODULE (plugin);
283   ModuleTypeInfo *module_type_info = g_type_module_find_type_info (module, g_type);
284
285   *info = module_type_info->info;
286   
287   if (module_type_info->info.value_table)
288     *value_table = *module_type_info->info.value_table;
289 }
290
291 static void 
292 g_type_module_complete_interface_info (GTypePlugin    *plugin,
293                                        GType           interface_type,
294                                        GType           instance_type,
295                                        GInterfaceInfo *info)
296 {
297   GTypeModule *module = G_TYPE_MODULE (plugin);
298   ModuleInterfaceInfo *module_interface_info = g_type_module_find_interface_info (module, interface_type, instance_type);
299
300   *info = module_interface_info->info;
301 }
302
303 /**
304  * g_type_module_register_type:
305  * @module:  a #GTypeModule
306  * @parent_type:    the type for the parent class
307  * @type_name:      name for the type
308  * @type_info:      type information structure
309  * @flags:          flags field providing details about the type           
310  * 
311  * Looks up or registers a type that is implemented with a particular
312  * type plugin. If a type with name @type_name is already registered,
313  * the #GType identifier for the type is returned, otherwise the type
314  * is newly registered, and the resulting #GType identifier returned.
315  *
316  * As long as any instances of the type exist, the type plugin will
317  * not be unloaded.
318  *
319  * Return value: the type ID for the class.
320  **/
321 GType
322 g_type_module_register_type (GTypeModule     *module,
323                              GType            parent_type,
324                              const gchar     *type_name,
325                              const GTypeInfo *type_info,
326                              GTypeFlags       flags)
327 {
328   ModuleTypeInfo *module_type_info = NULL;
329   GType type;
330   
331   g_return_val_if_fail (module != NULL, 0);
332   g_return_val_if_fail (type_name != NULL, 0);
333   g_return_val_if_fail (type_info != NULL, 0);
334
335   type = g_type_from_name (type_name);
336   if (type)
337     {
338       GTypePlugin *old_plugin = g_type_get_plugin (type);
339
340       if (old_plugin != G_TYPE_PLUGIN (module))
341         {
342           g_warning ("Two different plugins tried to register '%s'.", type_name);
343           return 0;
344         }
345     }
346
347   if (type)
348     {
349       module_type_info = g_type_module_find_type_info (module, type);
350
351       if (module_type_info->parent_type != parent_type)
352         {
353           const gchar *parent_type_name = g_type_name (parent_type);
354           
355           g_warning ("Type '%s' recreated with different parent type.\n"
356                      "(was '%s', now '%s')", type_name,
357                      g_type_name (module_type_info->parent_type),
358                      parent_type_name ? parent_type_name : "(unknown)");
359           return 0;
360         }
361     }
362   else
363     {
364       module_type_info = g_new (ModuleTypeInfo, 1);
365       
366       module_type_info->parent_type = parent_type;
367       module_type_info->type = g_type_register_dynamic (parent_type, type_name, G_TYPE_PLUGIN (module), flags);
368       
369       module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
370     }
371
372   module_type_info->loaded = TRUE;
373   module_type_info->info = *type_info;
374   if (type_info->value_table)
375     module_type_info->info.value_table = g_memdup (type_info->value_table,
376                                                    sizeof (type_info->value_table));
377
378   return module_type_info->type;
379 }
380
381 /**
382  * g_type_module_add_interface:
383  * @module: a #GTypeModule
384  * @instance_type: type to which to add the interface.
385  * @interface_type: interface type to add
386  * @interface_info: type information structure
387  * 
388  * Registers an additional interface for a type, whose interface
389  * lives in the given type plugin. If the interface was already registered
390  * for the type in this plugin, nothing will be done. 
391  *
392  * As long as any instances of the type exist, the type plugin will
393  * not be unloaded.
394  **/
395 void
396 g_type_module_add_interface (GTypeModule    *module,
397                              GType           instance_type,
398                              GType           interface_type,
399                              GInterfaceInfo *interface_info)
400 {
401   ModuleInterfaceInfo *module_interface_info = NULL;
402   
403   g_return_if_fail (module != NULL);
404   g_return_if_fail (interface_info != NULL);
405
406   if (g_type_conforms_to (instance_type, interface_type))
407     {
408       GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
409                                                              interface_type);
410
411       if (!old_plugin)
412         {
413           g_warning ("Interface '%s' for '%s' was previously registered statically or for a parent type.",
414                      g_type_name (interface_type), g_type_name (instance_type));
415           return;
416         }
417       else if (old_plugin != G_TYPE_PLUGIN (module))
418         {
419           g_warning ("Two different plugins tried to register interface '%s' for '%s'.",
420                      g_type_name (interface_type), g_type_name (instance_type));
421           return;
422         }
423       
424       module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
425
426       g_assert (module_interface_info);
427     }
428   else
429     {
430       module_interface_info = g_new (ModuleInterfaceInfo, 1);
431       
432       module_interface_info->instance_type = instance_type;
433       module_interface_info->interface_type = interface_type;
434       
435       g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
436       
437       module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
438     }
439   
440   module_interface_info->loaded = TRUE;
441   module_interface_info->info = *interface_info;
442 }