1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "gtypeplugin.h"
27 * @short_description: An interface for dynamically loadable types
28 * @see_also: #GTypeModule and g_type_register_dynamic().
31 * An interface that handles the lifecycle of dynamically loaded types.
33 * The GObject type system supports dynamic loading of types.
36 * 1. The type is initially introduced (usually upon loading the module
37 * the first time, or by your main application that knows what modules
38 * introduces what types), like this:
39 * |[<!-- language="C" -->
40 * new_type_id = g_type_register_dynamic (parent_type_id,
45 * where @new_type_plugin is an implementation of the
46 * #GTypePlugin interface.
48 * 2. 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 @new_type_id.
53 * 3. This causes the type system to load the type's implementation by
54 * calling g_type_plugin_use() and g_type_plugin_complete_type_info()
55 * on @new_type_plugin.
57 * 4. At some point the type's implementation isn't required anymore,
58 * e.g. after g_type_class_unref() or g_type_free_instance() (called
59 * when the reference count of an instance drops to zero).
61 * 5. This causes the type system to throw away the information retrieved
62 * from g_type_plugin_complete_type_info() and then it calls
63 * g_type_plugin_unuse() on @new_type_plugin.
65 * 6. Things may repeat from the second step.
67 * So basically, you need to implement a #GTypePlugin type that
68 * carries a use_count, once use_count goes from zero to one, you need
69 * to load the implementation to successfully handle the upcoming
70 * g_type_plugin_complete_type_info() call. Later, maybe after
71 * succeeding use/unuse calls, once use_count drops to zero, you can
72 * unload the implementation again. The type system makes sure to call
73 * g_type_plugin_use() and g_type_plugin_complete_type_info() again
74 * when the type is needed again.
76 * #GTypeModule is an implementation of #GTypePlugin that already
77 * implements most of this except for the actual module loading and
78 * unloading. It even handles multiple registered types per module.
82 /* --- functions --- */
84 g_type_plugin_get_type (void)
86 static GType type_plugin_type = 0;
88 if (!type_plugin_type)
90 const GTypeInfo type_plugin_info = {
91 sizeof (GTypePluginClass),
93 NULL, /* base_finalize */
95 NULL, /* class_destroy */
96 NULL, /* class_data */
97 0, /* instance_size */
99 NULL, /* instance_init */
100 NULL, /* value_table */
103 type_plugin_type = g_type_register_static (G_TYPE_INTERFACE, g_intern_static_string ("GTypePlugin"), &type_plugin_info, 0);
106 return type_plugin_type;
111 * @plugin: a #GTypePlugin
113 * Calls the @use_plugin function from the #GTypePluginClass of
114 * @plugin. There should be no need to use this function outside of
115 * the GObject type system itself.
118 g_type_plugin_use (GTypePlugin *plugin)
120 GTypePluginClass *iface;
122 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
124 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
125 iface->use_plugin (plugin);
129 * g_type_plugin_unuse:
130 * @plugin: a #GTypePlugin
132 * Calls the @unuse_plugin function from the #GTypePluginClass of
133 * @plugin. There should be no need to use this function outside of
134 * the GObject type system itself.
137 g_type_plugin_unuse (GTypePlugin *plugin)
139 GTypePluginClass *iface;
141 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
143 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
144 iface->unuse_plugin (plugin);
148 * g_type_plugin_complete_type_info:
149 * @plugin: a #GTypePlugin
150 * @g_type: the #GType whose info is completed
151 * @info: the #GTypeInfo struct to fill in
152 * @value_table: the #GTypeValueTable to fill in
154 * Calls the @complete_type_info function from the #GTypePluginClass of @plugin.
155 * There should be no need to use this function outside of the GObject
156 * type system itself.
159 g_type_plugin_complete_type_info (GTypePlugin *plugin,
162 GTypeValueTable *value_table)
164 GTypePluginClass *iface;
166 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
167 g_return_if_fail (info != NULL);
168 g_return_if_fail (value_table != NULL);
170 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
171 iface->complete_type_info (plugin,
178 * g_type_plugin_complete_interface_info:
179 * @plugin: the #GTypePlugin
180 * @instance_type: the #GType of an instantiatable type to which the interface
182 * @interface_type: the #GType of the interface whose info is completed
183 * @info: the #GInterfaceInfo to fill in
185 * Calls the @complete_interface_info function from the
186 * #GTypePluginClass of @plugin. There should be no need to use this
187 * function outside of the GObject type system itself.
190 g_type_plugin_complete_interface_info (GTypePlugin *plugin,
192 GType interface_type,
193 GInterfaceInfo *info)
195 GTypePluginClass *iface;
197 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
198 g_return_if_fail (info != NULL);
200 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
201 iface->complete_interface_info (plugin,