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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * @Short_description: An interface for dynamically loadable types
22 * @See_also:#GTypeModule and g_type_register_dynamic().
23 * @Titile: GTypePlugin
25 * The GObject type system supports dynamic loading of types. The #GTypePlugin
26 * interface is used to handle the lifecycle of dynamically loaded types.
31 * The type is initially introduced (usually upon loading the module
32 * the first time, or by your main application that knows what modules
33 * introduces what types), like this:
35 * new_type_id = g_type_register_dynamic (parent_type_id,
40 * where <literal>new_type_plugin</literal> is an implementation of the
41 * #GTypePlugin interface.
44 * The type's implementation is referenced, e.g. through
45 * g_type_class_ref() or through g_type_create_instance() (this is
46 * being called by g_object_new()) or through one of the above done on
47 * a type derived from <literal>new_type_id</literal>.
50 * This causes the type system to load the type's implementation by calling
51 * g_type_plugin_use() and g_type_plugin_complete_type_info() on
52 * <literal>new_type_plugin</literal>.
55 * At some point the type's implementation isn't required anymore, e.g. after
56 * g_type_class_unref() or g_type_free_instance() (called when the reference
57 * count of an instance drops to zero).
60 * This causes the type system to throw away the information retrieved from
61 * g_type_plugin_complete_type_info() and then it calls
62 * g_type_plugin_unuse() on <literal>new_type_plugin</literal>.
65 * Things may repeat from the second step.
69 * So basically, you need to implement a #GTypePlugin type that carries a
70 * use_count, once use_count goes from zero to one, you need to load the
71 * implementation to successfully handle the upcoming
72 * g_type_plugin_complete_type_info() call. Later, maybe after succeeding
73 * use/unuse calls, once use_count drops to zero, you can unload the
74 * implementation again. The type system makes sure to call g_type_plugin_use()
75 * and g_type_plugin_complete_type_info() again when the type is needed again.
77 * #GTypeModule is an implementation of #GTypePlugin that already implements
78 * most of this except for the actual module loading and unloading. It even
79 * handles multiple registered types per module.
81 #include "gtypeplugin.h"
82 #include "gobjectalias.h"
86 /* --- functions --- */
88 g_type_plugin_get_type (void)
90 static GType type_plugin_type = 0;
92 if (!type_plugin_type)
94 static const GTypeInfo type_plugin_info = {
95 sizeof (GTypePluginClass),
97 NULL, /* base_finalize */
100 type_plugin_type = g_type_register_static (G_TYPE_INTERFACE, g_intern_static_string ("GTypePlugin"), &type_plugin_info, 0);
103 return type_plugin_type;
108 * @plugin: a #GTypePlugin
110 * Calls the @use_plugin function from the #GTypePluginClass of @plugin.
111 * There should be no need to use this function outside of the GObject
112 * type system itself.
115 g_type_plugin_use (GTypePlugin *plugin)
117 GTypePluginClass *iface;
119 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
121 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
122 iface->use_plugin (plugin);
126 * g_type_plugin_unuse:
127 * @plugin: a #GTypePlugin
129 * Calls the @unuse_plugin function from the #GTypePluginClass of @plugin.
130 * There should be no need to use this function outside of the GObject
131 * type system itself.
134 g_type_plugin_unuse (GTypePlugin *plugin)
136 GTypePluginClass *iface;
138 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
140 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
141 iface->unuse_plugin (plugin);
145 * g_type_plugin_complete_type_info:
146 * @plugin: a #GTypePlugin
147 * @g_type: the #GType whose info is completed
148 * @info: the #GTypeInfo struct to fill in
149 * @value_table: the #GTypeValueTable to fill in
151 * Calls the @complete_type_info function from the #GTypePluginClass of @plugin.
152 * There should be no need to use this function outside of the GObject
153 * type system itself.
156 g_type_plugin_complete_type_info (GTypePlugin *plugin,
159 GTypeValueTable *value_table)
161 GTypePluginClass *iface;
163 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
164 g_return_if_fail (info != NULL);
165 g_return_if_fail (value_table != NULL);
167 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
168 iface->complete_type_info (plugin,
175 * g_type_plugin_complete_interface_info:
176 * @plugin: the #GTypePlugin
177 * @instance_type: the #GType of an instantiable type to which the interface
179 * @interface_type: the #GType of the interface whose info is completed
180 * @info: the #GInterfaceInfo to fill in
182 * Calls the @complete_interface_info function from the #GTypePluginClass
183 * of @plugin. There should be no need to use this function outside of the
184 * GObject type system itself.
187 g_type_plugin_complete_interface_info (GTypePlugin *plugin,
189 GType interface_type,
190 GInterfaceInfo *info)
192 GTypePluginClass *iface;
194 g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
195 g_return_if_fail (info != NULL);
197 iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
198 iface->complete_interface_info (plugin,
204 #define __G_TYPE_PLUGIN_C__
205 #include "gobjectaliasdef.c"