2000-11-05 06:07:26 +01:00
|
|
|
/* GObject - GLib Type, Object, Parameter and Signal Library
|
|
|
|
* Copyright (C) 2000 Red Hat, Inc.
|
|
|
|
*
|
2022-06-01 13:44:23 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2000-11-05 06:07:26 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 14:09:06 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2000-11-05 06:07:26 +01:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2000-11-05 06:07:26 +01:00
|
|
|
*/
|
2008-06-22 16:29:25 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtypeplugin.h"
|
|
|
|
|
|
|
|
|
2008-06-21 20:20:43 +02:00
|
|
|
/**
|
2023-09-25 21:24:31 +02:00
|
|
|
* GTypePlugin:
|
2008-06-22 16:29:25 +02:00
|
|
|
*
|
2021-07-31 23:47:26 +02:00
|
|
|
* An interface that handles the lifecycle of dynamically loaded types.
|
|
|
|
*
|
2014-02-01 16:19:07 +01:00
|
|
|
* The GObject type system supports dynamic loading of types.
|
2021-07-31 23:47:26 +02:00
|
|
|
* It goes as follows:
|
2008-06-22 16:29:25 +02:00
|
|
|
*
|
2014-02-01 16:19:07 +01:00
|
|
|
* 1. The type is initially introduced (usually upon loading the module
|
|
|
|
* the first time, or by your main application that knows what modules
|
|
|
|
* introduces what types), like this:
|
2023-09-25 21:24:31 +02:00
|
|
|
* ```c
|
2014-02-01 16:19:07 +01:00
|
|
|
* new_type_id = g_type_register_dynamic (parent_type_id,
|
|
|
|
* "TypeName",
|
|
|
|
* new_type_plugin,
|
|
|
|
* type_flags);
|
2023-09-25 21:24:31 +02:00
|
|
|
* ```
|
2023-10-23 12:16:44 +02:00
|
|
|
* where `new_type_plugin` is an implementation of the
|
2023-09-25 21:24:31 +02:00
|
|
|
* `GTypePlugin` interface.
|
2014-02-01 16:19:07 +01:00
|
|
|
*
|
|
|
|
* 2. The type's implementation is referenced, e.g. through
|
2023-10-23 12:16:44 +02:00
|
|
|
* [func@GObject.TypeClass.ref] or through [func@GObject.type_create_instance]
|
|
|
|
* (this is being called by [ctor@GObject.Object.new]) or through one of the above
|
|
|
|
* done on a type derived from `new_type_id`.
|
2014-02-01 16:19:07 +01:00
|
|
|
*
|
2023-09-25 21:24:31 +02:00
|
|
|
* 3. This causes the type system to load the type's implementation by calling
|
2023-10-23 12:16:44 +02:00
|
|
|
* [method@GObject.TypePlugin.use] and [method@GObject.TypePlugin.complete_type_info]
|
|
|
|
* on `new_type_plugin`.
|
2023-09-25 21:24:31 +02:00
|
|
|
*
|
|
|
|
* 4. At some point the type's implementation isn't required anymore, e.g. after
|
2023-10-23 12:16:44 +02:00
|
|
|
* [method@GObject.TypeClass.unref] or [func@GObject.type_free_instance]
|
2023-09-25 21:24:31 +02:00
|
|
|
* (called when the reference count of an instance drops to zero).
|
2014-02-01 16:19:07 +01:00
|
|
|
*
|
|
|
|
* 5. This causes the type system to throw away the information retrieved
|
2023-09-25 21:24:31 +02:00
|
|
|
* from [method@GObject.TypePlugin.complete_type_info] and then it calls
|
2023-10-23 12:16:44 +02:00
|
|
|
* [method@GObject.TypePlugin.unuse] on `new_type_plugin`.
|
2023-09-25 21:24:31 +02:00
|
|
|
*
|
2014-02-01 16:19:07 +01:00
|
|
|
* 6. Things may repeat from the second step.
|
2008-06-22 16:29:25 +02:00
|
|
|
*
|
2023-09-25 21:24:31 +02:00
|
|
|
* So basically, you need to implement a `GTypePlugin` type that
|
2008-06-22 16:29:25 +02:00
|
|
|
* carries a use_count, once use_count goes from zero to one, you need
|
|
|
|
* to load the implementation to successfully handle the upcoming
|
2023-09-25 21:24:31 +02:00
|
|
|
* [method@GObject.TypePlugin.complete_type_info] call. Later, maybe after
|
2008-06-22 16:29:25 +02:00
|
|
|
* succeeding use/unuse calls, once use_count drops to zero, you can
|
|
|
|
* unload the implementation again. The type system makes sure to call
|
2023-09-25 21:24:31 +02:00
|
|
|
* [method@GObject.TypePlugin.use] and [method@GObject.TypePlugin.complete_type_info]
|
|
|
|
* again when the type is needed again.
|
2008-06-22 16:29:25 +02:00
|
|
|
*
|
2023-10-23 12:16:44 +02:00
|
|
|
* [class@GObject.TypeModule] is an implementation of `GTypePlugin` that
|
2023-09-25 21:24:31 +02:00
|
|
|
* already implements most of this except for the actual module loading and
|
2008-06-22 16:29:25 +02:00
|
|
|
* unloading. It even handles multiple registered types per module.
|
2008-06-21 20:20:43 +02:00
|
|
|
*/
|
2000-11-05 06:07:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* --- functions --- */
|
|
|
|
GType
|
|
|
|
g_type_plugin_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type_plugin_type = 0;
|
|
|
|
|
|
|
|
if (!type_plugin_type)
|
|
|
|
{
|
2011-11-30 04:02:00 +01:00
|
|
|
const GTypeInfo type_plugin_info = {
|
2020-11-16 16:55:46 +01:00
|
|
|
sizeof (GTypePluginClass),
|
|
|
|
NULL, /* base_init */
|
|
|
|
NULL, /* base_finalize */
|
|
|
|
0, /* class_init */
|
|
|
|
NULL, /* class_destroy */
|
|
|
|
NULL, /* class_data */
|
|
|
|
0, /* instance_size */
|
|
|
|
0, /* n_preallocs */
|
|
|
|
NULL, /* instance_init */
|
|
|
|
NULL, /* value_table */
|
2000-11-05 06:07:26 +01:00
|
|
|
};
|
|
|
|
|
2005-08-31 16:38:18 +02:00
|
|
|
type_plugin_type = g_type_register_static (G_TYPE_INTERFACE, g_intern_static_string ("GTypePlugin"), &type_plugin_info, 0);
|
2000-11-05 06:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return type_plugin_type;
|
|
|
|
}
|
|
|
|
|
2008-06-21 20:20:43 +02:00
|
|
|
/**
|
|
|
|
* g_type_plugin_use:
|
|
|
|
* @plugin: a #GTypePlugin
|
2008-06-22 16:53:09 +02:00
|
|
|
*
|
|
|
|
* Calls the @use_plugin function from the #GTypePluginClass of
|
|
|
|
* @plugin. There should be no need to use this function outside of
|
|
|
|
* the GObject type system itself.
|
2008-06-21 20:20:43 +02:00
|
|
|
*/
|
2000-11-05 06:07:26 +01:00
|
|
|
void
|
|
|
|
g_type_plugin_use (GTypePlugin *plugin)
|
|
|
|
{
|
|
|
|
GTypePluginClass *iface;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
|
|
|
|
|
|
|
|
iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
|
|
|
|
iface->use_plugin (plugin);
|
|
|
|
}
|
|
|
|
|
2008-06-21 20:20:43 +02:00
|
|
|
/**
|
|
|
|
* g_type_plugin_unuse:
|
|
|
|
* @plugin: a #GTypePlugin
|
2008-06-22 16:53:09 +02:00
|
|
|
*
|
|
|
|
* Calls the @unuse_plugin function from the #GTypePluginClass of
|
|
|
|
* @plugin. There should be no need to use this function outside of
|
|
|
|
* the GObject type system itself.
|
2008-06-21 20:20:43 +02:00
|
|
|
*/
|
2000-11-05 06:07:26 +01:00
|
|
|
void
|
|
|
|
g_type_plugin_unuse (GTypePlugin *plugin)
|
|
|
|
{
|
|
|
|
GTypePluginClass *iface;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
|
|
|
|
|
|
|
|
iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
|
|
|
|
iface->unuse_plugin (plugin);
|
|
|
|
}
|
|
|
|
|
2008-06-21 20:20:43 +02:00
|
|
|
/**
|
|
|
|
* g_type_plugin_complete_type_info:
|
|
|
|
* @plugin: a #GTypePlugin
|
|
|
|
* @g_type: the #GType whose info is completed
|
|
|
|
* @info: the #GTypeInfo struct to fill in
|
|
|
|
* @value_table: the #GTypeValueTable to fill in
|
|
|
|
*
|
|
|
|
* Calls the @complete_type_info function from the #GTypePluginClass of @plugin.
|
|
|
|
* There should be no need to use this function outside of the GObject
|
|
|
|
* type system itself.
|
|
|
|
*/
|
2000-11-05 06:07:26 +01:00
|
|
|
void
|
|
|
|
g_type_plugin_complete_type_info (GTypePlugin *plugin,
|
|
|
|
GType g_type,
|
|
|
|
GTypeInfo *info,
|
|
|
|
GTypeValueTable *value_table)
|
|
|
|
{
|
|
|
|
GTypePluginClass *iface;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
|
|
|
|
g_return_if_fail (info != NULL);
|
|
|
|
g_return_if_fail (value_table != NULL);
|
|
|
|
|
|
|
|
iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
|
|
|
|
iface->complete_type_info (plugin,
|
|
|
|
g_type,
|
|
|
|
info,
|
|
|
|
value_table);
|
|
|
|
}
|
|
|
|
|
2008-06-21 20:20:43 +02:00
|
|
|
/**
|
|
|
|
* g_type_plugin_complete_interface_info:
|
|
|
|
* @plugin: the #GTypePlugin
|
2020-11-02 18:22:01 +01:00
|
|
|
* @instance_type: the #GType of an instantiatable type to which the interface
|
2008-06-21 20:20:43 +02:00
|
|
|
* is added
|
|
|
|
* @interface_type: the #GType of the interface whose info is completed
|
|
|
|
* @info: the #GInterfaceInfo to fill in
|
2008-06-22 16:53:09 +02:00
|
|
|
*
|
|
|
|
* Calls the @complete_interface_info function from the
|
|
|
|
* #GTypePluginClass of @plugin. There should be no need to use this
|
|
|
|
* function outside of the GObject type system itself.
|
2008-06-21 20:20:43 +02:00
|
|
|
*/
|
2000-11-05 06:07:26 +01:00
|
|
|
void
|
|
|
|
g_type_plugin_complete_interface_info (GTypePlugin *plugin,
|
|
|
|
GType instance_type,
|
2001-04-01 06:04:46 +02:00
|
|
|
GType interface_type,
|
2000-11-05 06:07:26 +01:00
|
|
|
GInterfaceInfo *info)
|
|
|
|
{
|
|
|
|
GTypePluginClass *iface;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
|
|
|
|
g_return_if_fail (info != NULL);
|
|
|
|
|
|
|
|
iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
|
|
|
|
iface->complete_interface_info (plugin,
|
|
|
|
instance_type,
|
2001-04-01 06:04:46 +02:00
|
|
|
interface_type,
|
2000-11-05 06:07:26 +01:00
|
|
|
info);
|
|
|
|
}
|