Do not use static GTypeInfo and GInterfaceInfo
[platform/upstream/glib.git] / gobject / gtypeplugin.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
15  * Public 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 "config.h"
21
22 #include "gtypeplugin.h"
23
24
25 /**
26  * SECTION:gtypeplugin
27  * @short_description: An interface for dynamically loadable types
28  * @see_also: #GTypeModule and g_type_register_dynamic().
29  * @title: GTypePlugin
30  *
31  * The GObject type system supports dynamic loading of types. The
32  * #GTypePlugin interface is used to handle the lifecycle of
33  * dynamically loaded types.  It goes as follows:
34  *
35  * <orderedlist>
36  * <listitem><para>
37  *   The type is initially introduced (usually upon loading the module
38  *   the first time, or by your main application that knows what modules
39  *   introduces what types), like this:
40  *   |[
41  *   new_type_id = g_type_register_dynamic (parent_type_id,
42  *                                                 "TypeName",
43  *                                                 new_type_plugin,
44  *                                                 type_flags);
45  *   ]|
46  *   where <literal>new_type_plugin</literal> is an implementation of the
47  *   #GTypePlugin interface.
48  * </para></listitem>
49  * <listitem><para>
50  *    The type's implementation is referenced, e.g. through
51  *    g_type_class_ref() or through g_type_create_instance() (this is
52  *    being called by g_object_new()) or through one of the above done on
53  *    a type derived from <literal>new_type_id</literal>.
54  * </para></listitem>
55  * <listitem><para>
56  *    This causes the type system to load the type's implementation by calling
57  *    g_type_plugin_use() and g_type_plugin_complete_type_info() on
58  *    <literal>new_type_plugin</literal>.
59  * </para></listitem>
60  * <listitem><para>
61  *    At some point the type's implementation isn't required anymore, e.g. after
62  *    g_type_class_unref() or g_type_free_instance() (called when the reference
63  *    count of an instance drops to zero).
64  * </para></listitem>
65  * <listitem><para>
66  *    This causes the type system to throw away the information retrieved from
67  *    g_type_plugin_complete_type_info() and then it calls
68  *    g_type_plugin_unuse() on <literal>new_type_plugin</literal>.
69  * </para></listitem>
70  * <listitem><para>
71  *    Things may repeat from the second step.
72  * </para></listitem>
73  * </orderedlist>
74  *
75  * So basically, you need to implement a #GTypePlugin type that
76  * carries a use_count, once use_count goes from zero to one, you need
77  * to load the implementation to successfully handle the upcoming
78  * g_type_plugin_complete_type_info() call. Later, maybe after
79  * succeeding use/unuse calls, once use_count drops to zero, you can
80  * unload the implementation again. The type system makes sure to call
81  * g_type_plugin_use() and g_type_plugin_complete_type_info() again
82  * when the type is needed again.
83  *
84  * #GTypeModule is an implementation of #GTypePlugin that already
85  * implements most of this except for the actual module loading and
86  * unloading. It even handles multiple registered types per module.
87  */
88
89
90 /* --- functions --- */
91 GType
92 g_type_plugin_get_type (void)
93 {
94   static GType type_plugin_type = 0;
95   
96   if (!type_plugin_type)
97     {
98       const GTypeInfo type_plugin_info = {
99         sizeof (GTypePluginClass),
100         NULL,           /* base_init */
101         NULL,           /* base_finalize */
102       };
103       
104       type_plugin_type = g_type_register_static (G_TYPE_INTERFACE, g_intern_static_string ("GTypePlugin"), &type_plugin_info, 0);
105     }
106   
107   return type_plugin_type;
108 }
109
110 /**
111  * g_type_plugin_use:
112  * @plugin: a #GTypePlugin
113  *
114  * Calls the @use_plugin function from the #GTypePluginClass of
115  * @plugin.  There should be no need to use this function outside of
116  * the GObject type system itself.
117  */
118 void
119 g_type_plugin_use (GTypePlugin *plugin)
120 {
121   GTypePluginClass *iface;
122   
123   g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
124   
125   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
126   iface->use_plugin (plugin);
127 }
128
129 /**
130  * g_type_plugin_unuse:
131  * @plugin: a #GTypePlugin
132  *
133  * Calls the @unuse_plugin function from the #GTypePluginClass of
134  * @plugin.  There should be no need to use this function outside of
135  * the GObject type system itself.
136  */
137 void
138 g_type_plugin_unuse (GTypePlugin *plugin)
139 {
140   GTypePluginClass *iface;
141   
142   g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
143   
144   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
145   iface->unuse_plugin (plugin);
146 }
147
148 /**
149  * g_type_plugin_complete_type_info:
150  * @plugin: a #GTypePlugin
151  * @g_type: the #GType whose info is completed
152  * @info: the #GTypeInfo struct to fill in
153  * @value_table: the #GTypeValueTable to fill in
154  * 
155  * Calls the @complete_type_info function from the #GTypePluginClass of @plugin.
156  * There should be no need to use this function outside of the GObject 
157  * type system itself.
158  */
159 void
160 g_type_plugin_complete_type_info (GTypePlugin     *plugin,
161                                   GType            g_type,
162                                   GTypeInfo       *info,
163                                   GTypeValueTable *value_table)
164 {
165   GTypePluginClass *iface;
166   
167   g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
168   g_return_if_fail (info != NULL);
169   g_return_if_fail (value_table != NULL);
170   
171   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
172   iface->complete_type_info (plugin,
173                              g_type,
174                              info,
175                              value_table);
176 }
177
178 /**
179  * g_type_plugin_complete_interface_info:
180  * @plugin: the #GTypePlugin
181  * @instance_type: the #GType of an instantiable type to which the interface
182  *  is added
183  * @interface_type: the #GType of the interface whose info is completed
184  * @info: the #GInterfaceInfo to fill in
185  *
186  * Calls the @complete_interface_info function from the
187  * #GTypePluginClass of @plugin. There should be no need to use this
188  * function outside of the GObject type system itself.
189  */
190 void
191 g_type_plugin_complete_interface_info (GTypePlugin    *plugin,
192                                        GType           instance_type,
193                                        GType           interface_type,
194                                        GInterfaceInfo *info)
195 {
196   GTypePluginClass *iface;
197   
198   g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
199   g_return_if_fail (info != NULL);
200   
201   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
202   iface->complete_interface_info (plugin,
203                                   instance_type,
204                                   interface_type,
205                                   info);
206 }