Blame gobject/gtypeplugin.c

Packit ae235b
/* GObject - GLib Type, Object, Parameter and Signal Library
Packit ae235b
 * Copyright (C) 2000 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gtypeplugin.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gtypeplugin
Packit ae235b
 * @short_description: An interface for dynamically loadable types
Packit ae235b
 * @see_also: #GTypeModule and g_type_register_dynamic().
Packit ae235b
 * @title: GTypePlugin
Packit ae235b
 *
Packit ae235b
 * The GObject type system supports dynamic loading of types.
Packit ae235b
 * The #GTypePlugin interface is used to handle the lifecycle
Packit ae235b
 * of dynamically loaded types. It goes as follows:
Packit ae235b
 *
Packit ae235b
 * 1. The type is initially introduced (usually upon loading the module
Packit ae235b
 *    the first time, or by your main application that knows what modules
Packit ae235b
 *    introduces what types), like this:
Packit ae235b
 *    |[ 
Packit ae235b
 *    new_type_id = g_type_register_dynamic (parent_type_id,
Packit ae235b
 *                                           "TypeName",
Packit ae235b
 *                                           new_type_plugin,
Packit ae235b
 *                                           type_flags);
Packit ae235b
 *    ]|
Packit ae235b
 *    where @new_type_plugin is an implementation of the
Packit ae235b
 *    #GTypePlugin interface.
Packit ae235b
 *
Packit ae235b
 * 2. The type's implementation is referenced, e.g. through
Packit ae235b
 *    g_type_class_ref() or through g_type_create_instance() (this is
Packit ae235b
 *    being called by g_object_new()) or through one of the above done on
Packit ae235b
 *    a type derived from @new_type_id.
Packit ae235b
 *
Packit ae235b
 * 3. This causes the type system to load the type's implementation by
Packit ae235b
 *    calling g_type_plugin_use() and g_type_plugin_complete_type_info()
Packit ae235b
 *    on @new_type_plugin.
Packit ae235b
 * 
Packit ae235b
 * 4. At some point the type's implementation isn't required anymore,
Packit ae235b
 *    e.g. after g_type_class_unref() or g_type_free_instance() (called
Packit ae235b
 *    when the reference count of an instance drops to zero).
Packit ae235b
 *
Packit ae235b
 * 5. This causes the type system to throw away the information retrieved
Packit ae235b
 *    from g_type_plugin_complete_type_info() and then it calls
Packit ae235b
 *    g_type_plugin_unuse() on @new_type_plugin.
Packit ae235b
 * 
Packit ae235b
 * 6. Things may repeat from the second step.
Packit ae235b
 *
Packit ae235b
 * So basically, you need to implement a #GTypePlugin type that
Packit ae235b
 * carries a use_count, once use_count goes from zero to one, you need
Packit ae235b
 * to load the implementation to successfully handle the upcoming
Packit ae235b
 * g_type_plugin_complete_type_info() call. Later, maybe after
Packit ae235b
 * succeeding use/unuse calls, once use_count drops to zero, you can
Packit ae235b
 * unload the implementation again. The type system makes sure to call
Packit ae235b
 * g_type_plugin_use() and g_type_plugin_complete_type_info() again
Packit ae235b
 * when the type is needed again.
Packit ae235b
 *
Packit ae235b
 * #GTypeModule is an implementation of #GTypePlugin that already
Packit ae235b
 * implements most of this except for the actual module loading and
Packit ae235b
 * unloading. It even handles multiple registered types per module.
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
/* --- functions --- */
Packit ae235b
GType
Packit ae235b
g_type_plugin_get_type (void)
Packit ae235b
{
Packit ae235b
  static GType type_plugin_type = 0;
Packit ae235b
  
Packit ae235b
  if (!type_plugin_type)
Packit ae235b
    {
Packit ae235b
      const GTypeInfo type_plugin_info = {
Packit ae235b
	sizeof (GTypePluginClass),
Packit ae235b
	NULL,           /* base_init */
Packit ae235b
	NULL,           /* base_finalize */
Packit ae235b
      };
Packit ae235b
      
Packit ae235b
      type_plugin_type = g_type_register_static (G_TYPE_INTERFACE, g_intern_static_string ("GTypePlugin"), &type_plugin_info, 0);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return type_plugin_type;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_type_plugin_use:
Packit ae235b
 * @plugin: a #GTypePlugin
Packit ae235b
 *
Packit ae235b
 * Calls the @use_plugin function from the #GTypePluginClass of
Packit ae235b
 * @plugin.  There should be no need to use this function outside of
Packit ae235b
 * the GObject type system itself.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_type_plugin_use (GTypePlugin *plugin)
Packit ae235b
{
Packit ae235b
  GTypePluginClass *iface;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
Packit ae235b
  
Packit ae235b
  iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
Packit ae235b
  iface->use_plugin (plugin);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_type_plugin_unuse:
Packit ae235b
 * @plugin: a #GTypePlugin
Packit ae235b
 *
Packit ae235b
 * Calls the @unuse_plugin function from the #GTypePluginClass of
Packit ae235b
 * @plugin.  There should be no need to use this function outside of
Packit ae235b
 * the GObject type system itself.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_type_plugin_unuse (GTypePlugin *plugin)
Packit ae235b
{
Packit ae235b
  GTypePluginClass *iface;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
Packit ae235b
  
Packit ae235b
  iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
Packit ae235b
  iface->unuse_plugin (plugin);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_type_plugin_complete_type_info:
Packit ae235b
 * @plugin: a #GTypePlugin
Packit ae235b
 * @g_type: the #GType whose info is completed
Packit ae235b
 * @info: the #GTypeInfo struct to fill in
Packit ae235b
 * @value_table: the #GTypeValueTable to fill in
Packit ae235b
 * 
Packit ae235b
 * Calls the @complete_type_info function from the #GTypePluginClass of @plugin.
Packit ae235b
 * There should be no need to use this function outside of the GObject 
Packit ae235b
 * type system itself.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_type_plugin_complete_type_info (GTypePlugin     *plugin,
Packit ae235b
				  GType            g_type,
Packit ae235b
				  GTypeInfo       *info,
Packit ae235b
				  GTypeValueTable *value_table)
Packit ae235b
{
Packit ae235b
  GTypePluginClass *iface;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
Packit ae235b
  g_return_if_fail (info != NULL);
Packit ae235b
  g_return_if_fail (value_table != NULL);
Packit ae235b
  
Packit ae235b
  iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
Packit ae235b
  iface->complete_type_info (plugin,
Packit ae235b
			     g_type,
Packit ae235b
			     info,
Packit ae235b
			     value_table);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_type_plugin_complete_interface_info:
Packit ae235b
 * @plugin: the #GTypePlugin
Packit ae235b
 * @instance_type: the #GType of an instantiable type to which the interface
Packit ae235b
 *  is added
Packit ae235b
 * @interface_type: the #GType of the interface whose info is completed
Packit ae235b
 * @info: the #GInterfaceInfo to fill in
Packit ae235b
 *
Packit ae235b
 * Calls the @complete_interface_info function from the
Packit ae235b
 * #GTypePluginClass of @plugin. There should be no need to use this
Packit ae235b
 * function outside of the GObject type system itself.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_type_plugin_complete_interface_info (GTypePlugin    *plugin,
Packit ae235b
				       GType           instance_type,
Packit ae235b
				       GType           interface_type,
Packit ae235b
				       GInterfaceInfo *info)
Packit ae235b
{
Packit ae235b
  GTypePluginClass *iface;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (G_IS_TYPE_PLUGIN (plugin));
Packit ae235b
  g_return_if_fail (info != NULL);
Packit ae235b
  
Packit ae235b
  iface = G_TYPE_PLUGIN_GET_CLASS (plugin);
Packit ae235b
  iface->complete_interface_info (plugin,
Packit ae235b
				  instance_type,
Packit ae235b
				  interface_type,
Packit ae235b
				  info);
Packit ae235b
}