compositor: Use subclassing macros for Module

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3520>
This commit is contained in:
Bilal Elmoussaoui 2024-01-10 16:06:24 +01:00 committed by Marge Bot
parent b1bc03a314
commit 2dd04f7cbe
2 changed files with 37 additions and 55 deletions

View File

@ -33,36 +33,38 @@ enum
PROP_PATH, PROP_PATH,
}; };
struct _MetaModulePrivate struct _MetaModule
{ {
GModule *lib; GTypeModule parent_instance;
gchar *path;
GType plugin_type; GModule *lib;
gchar *path;
GType plugin_type;
}; };
G_DEFINE_TYPE_WITH_PRIVATE (MetaModule, meta_module, G_TYPE_TYPE_MODULE); G_DEFINE_FINAL_TYPE (MetaModule, meta_module, G_TYPE_TYPE_MODULE);
static gboolean static gboolean
meta_module_load (GTypeModule *gmodule) meta_module_load (GTypeModule *gmodule)
{ {
MetaModulePrivate *priv = META_MODULE (gmodule)->priv; MetaModule *module = META_MODULE (gmodule);
GType (*register_type) (GTypeModule *) = NULL; GType (*register_type) (GTypeModule *) = NULL;
if (priv->lib && priv->plugin_type) if (module->lib && module->plugin_type)
return TRUE; return TRUE;
g_assert (priv->path); g_assert (module->path);
if (!priv->lib && if (!module->lib &&
!(priv->lib = g_module_open (priv->path, 0))) !(module->lib = g_module_open (module->path, 0)))
{ {
g_warning ("Could not load library [%s (%s)]", g_warning ("Could not load library [%s (%s)]",
priv->path, g_module_error ()); module->path, g_module_error ());
return FALSE; return FALSE;
} }
if (g_module_symbol (priv->lib, "meta_plugin_register_type", if (g_module_symbol (module->lib, "meta_plugin_register_type",
(gpointer *)(void *)&register_type) && (gpointer *)(void *)&register_type) &&
register_type) register_type)
{ {
GType plugin_type; GType plugin_type;
@ -70,18 +72,18 @@ meta_module_load (GTypeModule *gmodule)
if (!(plugin_type = register_type (gmodule))) if (!(plugin_type = register_type (gmodule)))
{ {
g_warning ("Could not register type for plugin %s", g_warning ("Could not register type for plugin %s",
priv->path); module->path);
return FALSE; return FALSE;
} }
else else
{ {
priv->plugin_type = plugin_type; module->plugin_type = plugin_type;
} }
return TRUE; return TRUE;
} }
else else
g_warning ("Broken plugin module [%s]", priv->path); g_warning ("Broken plugin module [%s]", module->path);
return FALSE; return FALSE;
} }
@ -89,21 +91,21 @@ meta_module_load (GTypeModule *gmodule)
static void static void
meta_module_unload (GTypeModule *gmodule) meta_module_unload (GTypeModule *gmodule)
{ {
MetaModulePrivate *priv = META_MODULE (gmodule)->priv; MetaModule *module = META_MODULE (gmodule);
g_module_close (priv->lib); g_module_close (module->lib);
priv->lib = NULL; module->lib = NULL;
priv->plugin_type = 0; module->plugin_type = 0;
} }
static void static void
meta_module_finalize (GObject *object) meta_module_finalize (GObject *object)
{ {
MetaModulePrivate *priv = META_MODULE (object)->priv; MetaModule *module = META_MODULE (object);
g_free (priv->path); g_free (module->path);
priv->path = NULL; module->path = NULL;
G_OBJECT_CLASS (meta_module_parent_class)->finalize (object); G_OBJECT_CLASS (meta_module_parent_class)->finalize (object);
} }
@ -114,13 +116,13 @@ meta_module_set_property (GObject *object,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
MetaModulePrivate *priv = META_MODULE (object)->priv; MetaModule *module = META_MODULE (object);
switch (prop_id) switch (prop_id)
{ {
case PROP_PATH: case PROP_PATH:
g_free (priv->path); g_free (module->path);
priv->path = g_value_dup_string (value); module->path = g_value_dup_string (value);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -134,12 +136,12 @@ meta_module_get_property (GObject *object,
GValue *value, GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
MetaModulePrivate *priv = META_MODULE (object)->priv; MetaModule *module = META_MODULE (object);
switch (prop_id) switch (prop_id)
{ {
case PROP_PATH: case PROP_PATH:
g_value_set_string (value, priv->path); g_value_set_string (value, module->path);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -171,14 +173,11 @@ meta_module_class_init (MetaModuleClass *klass)
static void static void
meta_module_init (MetaModule *self) meta_module_init (MetaModule *self)
{ {
self->priv = meta_module_get_instance_private (self);
} }
GType GType
meta_module_get_plugin_type (MetaModule *module) meta_module_get_plugin_type (MetaModule *module)
{ {
MetaModulePrivate *priv = META_MODULE (module)->priv; return module->plugin_type;
return priv->plugin_type;
} }

View File

@ -24,29 +24,12 @@
#include <glib-object.h> #include <glib-object.h>
#define META_TYPE_MODULE (meta_module_get_type ()) #define META_TYPE_MODULE (meta_module_get_type ())
#define META_MODULE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), META_TYPE_MODULE, MetaModule))
#define META_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), META_TYPE_MODULE, MetaModuleClass)) G_DECLARE_FINAL_TYPE (MetaModule,
#define META_IS_MODULE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), META_MODULE_TYPE)) meta_module,
#define META_IS_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), META_TYPE_MODULE)) META, MODULE,
#define META_MODULE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), META_TYPE_MODULE, MetaModuleClass)) GTypeModule)
typedef struct _MetaModule MetaModule; typedef struct _MetaModule MetaModule;
typedef struct _MetaModuleClass MetaModuleClass;
typedef struct _MetaModulePrivate MetaModulePrivate;
struct _MetaModule
{
GTypeModule parent;
MetaModulePrivate *priv;
};
struct _MetaModuleClass
{
GTypeModuleClass parent_class;
};
GType meta_module_get_type (void);
GType meta_module_get_plugin_type (MetaModule *module); GType meta_module_get_plugin_type (MetaModule *module);