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