Change LGPL-2.1+ to LGPL-2.1-or-later
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "config.h"
21
22 #include "gtypeplugin.h"
23
24
25 /**
26  * SECTION:gtypeplugin
27  * @short_description: An interface for dynamically loadable types
28  * @see_also: #GTypeModule and g_type_register_dynamic().
29  * @title: GTypePlugin
30  *
31  * An interface that handles the lifecycle of dynamically loaded types.
32  *
33  * The GObject type system supports dynamic loading of types.
34  * It goes as follows:
35  *
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,
41  *                                           "TypeName",
42  *                                           new_type_plugin,
43  *                                           type_flags);
44  *    ]|
45  *    where @new_type_plugin is an implementation of the
46  *    #GTypePlugin interface.
47  *
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.
52  *
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.
56  * 
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).
60  *
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.
64  * 
65  * 6. Things may repeat from the second step.
66  *
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.
75  *
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.
79  */
80
81
82 /* --- functions --- */
83 GType
84 g_type_plugin_get_type (void)
85 {
86   static GType type_plugin_type = 0;
87   
88   if (!type_plugin_type)
89     {
90       const GTypeInfo type_plugin_info = {
91         sizeof (GTypePluginClass),
92         NULL,           /* base_init */
93         NULL,           /* base_finalize */
94         0,              /* class_init */
95         NULL,           /* class_destroy */
96         NULL,           /* class_data */
97         0,              /* instance_size */
98         0,              /* n_preallocs */
99         NULL,           /* instance_init */
100         NULL,           /* value_table */
101       };
102       
103       type_plugin_type = g_type_register_static (G_TYPE_INTERFACE, g_intern_static_string ("GTypePlugin"), &type_plugin_info, 0);
104     }
105   
106   return type_plugin_type;
107 }
108
109 /**
110  * g_type_plugin_use:
111  * @plugin: a #GTypePlugin
112  *
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.
116  */
117 void
118 g_type_plugin_use (GTypePlugin *plugin)
119 {
120   GTypePluginClass *iface;
121   
122   g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
123   
124   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
125   iface->use_plugin (plugin);
126 }
127
128 /**
129  * g_type_plugin_unuse:
130  * @plugin: a #GTypePlugin
131  *
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.
135  */
136 void
137 g_type_plugin_unuse (GTypePlugin *plugin)
138 {
139   GTypePluginClass *iface;
140   
141   g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
142   
143   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
144   iface->unuse_plugin (plugin);
145 }
146
147 /**
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
153  * 
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.
157  */
158 void
159 g_type_plugin_complete_type_info (GTypePlugin     *plugin,
160                                   GType            g_type,
161                                   GTypeInfo       *info,
162                                   GTypeValueTable *value_table)
163 {
164   GTypePluginClass *iface;
165   
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);
169   
170   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
171   iface->complete_type_info (plugin,
172                              g_type,
173                              info,
174                              value_table);
175 }
176
177 /**
178  * g_type_plugin_complete_interface_info:
179  * @plugin: the #GTypePlugin
180  * @instance_type: the #GType of an instantiatable type to which the interface
181  *  is added
182  * @interface_type: the #GType of the interface whose info is completed
183  * @info: the #GInterfaceInfo to fill in
184  *
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.
188  */
189 void
190 g_type_plugin_complete_interface_info (GTypePlugin    *plugin,
191                                        GType           instance_type,
192                                        GType           interface_type,
193                                        GInterfaceInfo *info)
194 {
195   GTypePluginClass *iface;
196   
197   g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
198   g_return_if_fail (info != NULL);
199   
200   iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
201   iface->complete_interface_info (plugin,
202                                   instance_type,
203                                   interface_type,
204                                   info);
205 }