1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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/>.
20 #include "gtypeplugin.h"
25 * @short_description: An interface for dynamically loadable types
26 * @see_also: #GTypeModule and g_type_register_dynamic().
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:
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:
39 * new_type_id = g_type_register_dynamic (parent_type_id,
44 * where <literal>new_type_plugin</literal> is an implementation of the
45 * #GTypePlugin interface.
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>.
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>.
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).
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>.
69 * Things may repeat from the second step.
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.
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.
88 /* --- functions --- */
90 g_type_plugin_get_type (void)
92 static GType type_plugin_type = 0;
94 if (!type_plugin_type)
96 const GTypeInfo type_plugin_info = {
97 sizeof (GTypePluginClass),
99 NULL, /* base_finalize */
102 type_plugin_type = g_type_register_static (G_TYPE_INTERFACE, g_intern_static_string ("GTypePlugin"), &type_plugin_info, 0);
105 return type_plugin_type;
110 * @plugin: a #GTypePlugin
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.
117 g_type_plugin_use (GTypePlugin *plugin)
119 GTypePluginClass *iface;
121 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
123 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
124 iface->use_plugin (plugin);
128 * g_type_plugin_unuse:
129 * @plugin: a #GTypePlugin
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.
136 g_type_plugin_unuse (GTypePlugin *plugin)
138 GTypePluginClass *iface;
140 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
142 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
143 iface->unuse_plugin (plugin);
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
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.
158 g_type_plugin_complete_type_info (GTypePlugin *plugin,
161 GTypeValueTable *value_table)
163 GTypePluginClass *iface;
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);
169 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
170 iface->complete_type_info (plugin,
177 * g_type_plugin_complete_interface_info:
178 * @plugin: the #GTypePlugin
179 * @instance_type: the #GType of an instantiable type to which the interface
181 * @interface_type: the #GType of the interface whose info is completed
182 * @info: the #GInterfaceInfo to fill in
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.
189 g_type_plugin_complete_interface_info (GTypePlugin *plugin,
191 GType interface_type,
192 GInterfaceInfo *info)
194 GTypePluginClass *iface;
196 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
197 g_return_if_fail (info != NULL);
199 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
200 iface->complete_interface_info (plugin,