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