*** empty log message ***
[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 "gtypeplugin.h"
21 #include "gtypemodule.h"
22
23 typedef struct _ModuleTypeInfo ModuleTypeInfo;
24 typedef struct _ModuleInterfaceInfo ModuleInterfaceInfo;
25
26 struct _ModuleTypeInfo 
27 {
28   gboolean  loaded;
29   GType     type;
30   GType     parent_type;
31   GTypeInfo info;
32 };
33
34 struct _ModuleInterfaceInfo 
35 {
36   gboolean       loaded;
37   GType          instance_type;
38   GType          interface_type;
39   GInterfaceInfo info;
40 };
41
42 static void g_type_module_use_plugin              (GTypePlugin     *plugin);
43 static void g_type_module_complete_type_info      (GTypePlugin     *plugin,
44                                                    GType            g_type,
45                                                    GTypeInfo       *info,
46                                                    GTypeValueTable *value_table);
47 static void g_type_module_complete_interface_info (GTypePlugin     *plugin,
48                                                    GType            interface_type,
49                                                    GType            instance_info,
50                                                    GInterfaceInfo  *info);
51  
52 static GObjectClass *parent_class;
53
54 static void
55 g_type_module_shutdown (GObject *object)
56 {
57   GTypeModule *module = G_TYPE_MODULE (object);
58   
59   if (module->type_infos || module->interface_infos)
60     {
61       g_warning (G_STRLOC ": shutdown should never happen for static type plugins once types or interfaces have been registered");
62              
63       g_object_ref (object);
64     }
65
66   parent_class->shutdown (object);
67 }
68
69 static void
70 g_type_module_finalize (GObject *object)
71 {
72   GTypeModule *module = G_TYPE_MODULE (object);
73
74   g_free (module->name);
75
76   parent_class->finalize (object);
77 }
78
79 static void
80 g_type_module_class_init (GTypeModuleClass *class)
81 {
82   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
83
84   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
85   
86   gobject_class->shutdown = g_type_module_shutdown;
87   gobject_class->finalize = g_type_module_finalize;
88 }
89
90 static void
91 g_type_module_iface_init (GTypePluginClass *iface)
92 {
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;
97 }
98
99 GType
100 g_type_module_get_type (void)
101 {
102   static GType type_module_type = 0;
103
104   if (!type_module_type)
105     {
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),
114         0,              /* n_preallocs */
115         NULL,           /* instance_init */
116       };
117       static const GInterfaceInfo iface_info = {
118         (GInterfaceInitFunc) g_type_module_iface_init,
119         NULL,               /* interface_finalize */
120         NULL,               /* interface_data */
121       };
122
123       type_module_type = g_type_register_static (G_TYPE_OBJECT, "GTypeModule", &type_module_info, G_TYPE_FLAG_ABSTRACT);
124
125       g_type_add_interface_static (type_module_type, G_TYPE_TYPE_PLUGIN, &iface_info);
126     }
127   
128   return type_module_type;
129 }
130
131 /**
132  * g_type_module_set_name
133  * @module: a #GTypeModule.
134  * @name: a human-readable name to use in error messages.
135  * 
136  * Sets the name for a #GTypeModule 
137  **/
138 void
139 g_type_module_set_name (GTypeModule  *module,
140                         const gchar  *name)
141 {
142   g_return_if_fail (G_IS_TYPE_MODULE (module));
143
144   g_free (module->name);
145   module->name = g_strdup (name);
146 }
147
148 static ModuleTypeInfo *
149 g_type_module_find_type_info (GTypeModule *module,
150                               GType        type)
151 {
152   GSList *tmp_list = module->type_infos;
153   while (tmp_list)
154     {
155       ModuleTypeInfo *type_info = tmp_list->data;
156       if (type_info->type == type)
157         return type_info;
158       
159       tmp_list = tmp_list->next;
160     }
161
162   return NULL;
163 }
164
165 static ModuleInterfaceInfo *
166 g_type_module_find_interface_info (GTypeModule *module,
167                                    GType        instance_type,
168                                    GType        interface_type)
169 {
170   GSList *tmp_list = module->interface_infos;
171   while (tmp_list)
172     {
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;
177       
178       tmp_list = tmp_list->next;
179     }
180
181   return NULL;
182 }
183
184 /**
185  * g_type_module_use:
186  * @module: a #GTypeModule
187  * 
188  * Increases the use count of a #GTypeModule by one. If the
189  * use count was zero before, the plugin will be loaded.
190  *
191  * Return Value: %FALSE if the plugin needed to be loaded and
192  *               loading the plugin failed.
193  **/
194 gboolean
195 g_type_module_use (GTypeModule *module)
196 {
197   g_return_val_if_fail (G_IS_TYPE_MODULE (module), FALSE);
198
199   module->use_count++;
200   if (module->use_count == 1)
201     {
202       GSList *tmp_list;
203       
204       if (!G_TYPE_MODULE_GET_CLASS (module)->load (module))
205         return FALSE;
206
207       tmp_list = module->type_infos;
208       while (tmp_list)
209         {
210           ModuleTypeInfo *type_info = tmp_list->data;
211           if (!type_info->loaded)
212             {
213               g_warning ("plugin '%s' failed to register type '%s'\n",
214                          module->name ? module->name : "(unknown)",
215                          g_type_name (type_info->type));
216               return FALSE;
217             }
218           
219           tmp_list = tmp_list->next;
220         }
221     }
222  
223   return TRUE;
224 }
225
226 /**
227  * g_type_module_unuse:
228  * @module: a #GTypeModule
229  * 
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.)
235  **/
236 void
237 g_type_module_unuse (GTypeModule *module)
238 {
239   g_return_if_fail (G_IS_TYPE_MODULE (module));
240   g_return_if_fail (module->use_count > 0);
241
242   module->use_count--;
243
244   if (module->use_count == 0)
245     {
246       GSList *tmp_list;
247
248       G_TYPE_MODULE_GET_CLASS (module)->unload (module);
249
250       tmp_list = module->type_infos;
251       while (tmp_list)
252         {
253           ModuleTypeInfo *type_info = tmp_list->data;
254           type_info->loaded = FALSE;
255
256           tmp_list = tmp_list->next;
257         }
258     }
259 }
260         
261 static void
262 g_type_module_use_plugin (GTypePlugin *plugin)
263 {
264   GTypeModule *module = G_TYPE_MODULE (plugin);
265
266   if (!g_type_module_use (module))
267     {
268       g_warning ("Fatal error - Could not reload previously loaded plugin '%s'\n",
269                  module->name ? module->name : "(unknown)");
270       exit (1);
271     }
272 }
273
274 static void
275 g_type_module_complete_type_info (GTypePlugin     *plugin,
276                                   GType            g_type,
277                                   GTypeInfo       *info,
278                                   GTypeValueTable *value_table)
279 {
280   GTypeModule *module = G_TYPE_MODULE (plugin);
281   ModuleTypeInfo *module_type_info = g_type_module_find_type_info (module, g_type);
282
283   *info = module_type_info->info;
284   
285   if (module_type_info->info.value_table)
286     *value_table = *module_type_info->info.value_table;
287 }
288
289 static void 
290 g_type_module_complete_interface_info (GTypePlugin    *plugin,
291                                        GType           interface_type,
292                                        GType           instance_type,
293                                        GInterfaceInfo *info)
294 {
295   GTypeModule *module = G_TYPE_MODULE (plugin);
296   ModuleInterfaceInfo *module_interface_info = g_type_module_find_interface_info (module, interface_type, instance_type);
297
298   *info = module_interface_info->info;
299 }
300
301 /**
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           
308  * 
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.
313  *
314  * As long as any instances of the type exist, the type plugin will
315  * not be unloaded.
316  *
317  * Return value: the type ID for the class.
318  **/
319 GType
320 g_type_module_register_type (GTypeModule     *module,
321                              GType            parent_type,
322                              const gchar     *type_name,
323                              const GTypeInfo *type_info,
324                              GTypeFlags       flags)
325 {
326   ModuleTypeInfo *module_type_info = NULL;
327   GType type;
328   
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);
332
333   type = g_type_from_name (type_name);
334   if (type)
335     {
336       GTypePlugin *old_plugin = g_type_get_plugin (type);
337
338       if (old_plugin != G_TYPE_PLUGIN (module))
339         {
340           g_warning ("Two different plugins tried to register '%s'.", type_name);
341           return 0;
342         }
343     }
344
345   if (type)
346     {
347       module_type_info = g_type_module_find_type_info (module, type);
348
349       if (module_type_info->parent_type != parent_type)
350         {
351           const gchar *parent_type_name = g_type_name (parent_type);
352           
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)");
357           return 0;
358         }
359     }
360   else
361     {
362       module_type_info = g_new (ModuleTypeInfo, 1);
363       
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);
366       
367       module->type_infos = g_slist_prepend (module->type_infos, module_type_info);
368     }
369
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));
375
376   return module_type_info->type;
377 }
378
379 /**
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
385  * 
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. 
389  *
390  * As long as any instances of the type exist, the type plugin will
391  * not be unloaded.
392  **/
393 void
394 g_type_module_add_interface (GTypeModule    *module,
395                              GType           instance_type,
396                              GType           interface_type,
397                              GInterfaceInfo *interface_info)
398 {
399   ModuleInterfaceInfo *module_interface_info = NULL;
400   
401   g_return_if_fail (module != NULL);
402   g_return_if_fail (interface_info != NULL);
403
404   if (g_type_conforms_to (instance_type, interface_type))
405     {
406       GTypePlugin *old_plugin = g_type_interface_get_plugin (instance_type,
407                                                              interface_type);
408
409       if (!old_plugin)
410         {
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));
413           return;
414         }
415       else if (old_plugin != G_TYPE_PLUGIN (module))
416         {
417           g_warning ("Two different plugins tried to register interface '%s' for '%s'.",
418                      g_type_name (interface_type), g_type_name (instance_type));
419           return;
420         }
421       
422       module_interface_info = g_type_module_find_interface_info (module, instance_type, interface_type);
423
424       g_assert (module_interface_info);
425     }
426   else
427     {
428       module_interface_info = g_new (ModuleInterfaceInfo, 1);
429       
430       module_interface_info->instance_type = instance_type;
431       module_interface_info->interface_type = interface_type;
432       
433       g_type_add_interface_dynamic (instance_type, interface_type, G_TYPE_PLUGIN (module));
434       
435       module->interface_infos = g_slist_prepend (module->interface_infos, module_interface_info);
436     }
437   
438   module_interface_info->loaded = TRUE;
439   module_interface_info->info = *interface_info;
440 }