lib: move gtype out of WpPluginMetadata and simplify plugin macros
the GType cannot be in statically allocated data, since it's not known at compile time
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
gsize block_size;
|
gsize block_size;
|
||||||
|
GType gtype;
|
||||||
const WpPluginMetadata *metadata;
|
const WpPluginMetadata *metadata;
|
||||||
WpPlugin *instance;
|
WpPlugin *instance;
|
||||||
} PluginData;
|
} PluginData;
|
||||||
@@ -90,6 +91,7 @@ compare_ranks (const WpPluginMetadata * a, const WpPluginMetadata * b)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* wp_plugin_registry_register_with_metadata: (skip)
|
* wp_plugin_registry_register_with_metadata: (skip)
|
||||||
|
* @plugin_type: the #GType of the #WpPlugin subclass
|
||||||
* @metadata: the metadata
|
* @metadata: the metadata
|
||||||
* @metadata_size: the sizeof (@metadata), to allow ABI-compatible future
|
* @metadata_size: the sizeof (@metadata), to allow ABI-compatible future
|
||||||
* expansion of the structure
|
* expansion of the structure
|
||||||
@@ -100,6 +102,7 @@ compare_ranks (const WpPluginMetadata * a, const WpPluginMetadata * b)
|
|||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
wp_plugin_registry_register_with_metadata (WpPluginRegistry * self,
|
wp_plugin_registry_register_with_metadata (WpPluginRegistry * self,
|
||||||
|
GType plugin_type,
|
||||||
const WpPluginMetadata * metadata,
|
const WpPluginMetadata * metadata,
|
||||||
gsize metadata_size)
|
gsize metadata_size)
|
||||||
{
|
{
|
||||||
@@ -107,7 +110,7 @@ wp_plugin_registry_register_with_metadata (WpPluginRegistry * self,
|
|||||||
|
|
||||||
g_return_if_fail (WP_IS_PLUGIN_REGISTRY (self));
|
g_return_if_fail (WP_IS_PLUGIN_REGISTRY (self));
|
||||||
g_return_if_fail (metadata_size == sizeof (WpPluginMetadata));
|
g_return_if_fail (metadata_size == sizeof (WpPluginMetadata));
|
||||||
g_return_if_fail (g_type_is_a (metadata->gtype, wp_plugin_get_type ()));
|
g_return_if_fail (g_type_is_a (plugin_type, wp_plugin_get_type ()));
|
||||||
g_return_if_fail (metadata->name != NULL);
|
g_return_if_fail (metadata->name != NULL);
|
||||||
g_return_if_fail (metadata->description != NULL);
|
g_return_if_fail (metadata->description != NULL);
|
||||||
g_return_if_fail (metadata->author != NULL);
|
g_return_if_fail (metadata->author != NULL);
|
||||||
@@ -117,6 +120,7 @@ wp_plugin_registry_register_with_metadata (WpPluginRegistry * self,
|
|||||||
|
|
||||||
data = g_slice_alloc (sizeof (PluginData));
|
data = g_slice_alloc (sizeof (PluginData));
|
||||||
data->block_size = sizeof (PluginData);
|
data->block_size = sizeof (PluginData);
|
||||||
|
data->gtype = plugin_type;
|
||||||
data->metadata = metadata;
|
data->metadata = metadata;
|
||||||
data->instance = NULL;
|
data->instance = NULL;
|
||||||
|
|
||||||
@@ -165,9 +169,9 @@ wp_plugin_registry_register (WpPluginRegistry * self,
|
|||||||
|
|
||||||
data = g_slice_alloc (sizeof (PluginData) + sizeof (WpPluginMetadata));
|
data = g_slice_alloc (sizeof (PluginData) + sizeof (WpPluginMetadata));
|
||||||
data->block_size = sizeof (PluginData) + sizeof (WpPluginMetadata);
|
data->block_size = sizeof (PluginData) + sizeof (WpPluginMetadata);
|
||||||
|
data->gtype = plugin_type;
|
||||||
|
|
||||||
metadata = (WpPluginMetadata *) ((guint8 *) data) + sizeof (PluginData);
|
metadata = (WpPluginMetadata *) ((guint8 *) data) + sizeof (PluginData);
|
||||||
metadata->gtype = plugin_type;
|
|
||||||
metadata->rank = rank;
|
metadata->rank = rank;
|
||||||
metadata->name = g_string_chunk_insert (self->metadata_strings, name);
|
metadata->name = g_string_chunk_insert (self->metadata_strings, name);
|
||||||
metadata->description = g_string_chunk_insert (self->metadata_strings,
|
metadata->description = g_string_chunk_insert (self->metadata_strings,
|
||||||
@@ -187,7 +191,7 @@ wp_plugin_registry_register (WpPluginRegistry * self,
|
|||||||
static inline void
|
static inline void
|
||||||
make_plugin (WpPluginRegistry * self, PluginData * plugin_data)
|
make_plugin (WpPluginRegistry * self, PluginData * plugin_data)
|
||||||
{
|
{
|
||||||
plugin_data->instance = g_object_new (plugin_data->metadata->gtype,
|
plugin_data->instance = g_object_new (plugin_data->gtype,
|
||||||
"registry", self, "metadata", plugin_data->metadata, NULL);
|
"registry", self, "metadata", plugin_data->metadata, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -21,6 +21,7 @@ G_DECLARE_FINAL_TYPE (WpPluginRegistry, wp_plugin_registry, WP, PLUGIN_REGISTRY,
|
|||||||
WpPluginRegistry * wp_plugin_registry_new (void);
|
WpPluginRegistry * wp_plugin_registry_new (void);
|
||||||
|
|
||||||
void wp_plugin_registry_register_with_metadata (WpPluginRegistry * self,
|
void wp_plugin_registry_register_with_metadata (WpPluginRegistry * self,
|
||||||
|
GType plugin_type,
|
||||||
const WpPluginMetadata * metadata,
|
const WpPluginMetadata * metadata,
|
||||||
gsize metadata_size);
|
gsize metadata_size);
|
||||||
|
|
||||||
|
@@ -40,7 +40,6 @@ typedef enum {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* WpPluginMetadata: (skip)
|
* WpPluginMetadata: (skip)
|
||||||
* @gtype: the #GType of the plugin
|
|
||||||
* @rank: the rank of the plugin
|
* @rank: the rank of the plugin
|
||||||
* @name: the name of the plugin
|
* @name: the name of the plugin
|
||||||
* @description: plugin description
|
* @description: plugin description
|
||||||
@@ -56,10 +55,7 @@ typedef enum {
|
|||||||
struct _WpPluginMetadata
|
struct _WpPluginMetadata
|
||||||
{
|
{
|
||||||
union {
|
union {
|
||||||
struct {
|
guint rank;
|
||||||
GType gtype;
|
|
||||||
guint rank;
|
|
||||||
};
|
|
||||||
gpointer _unused_for_alignment[2];
|
gpointer _unused_for_alignment[2];
|
||||||
};
|
};
|
||||||
const gchar *name;
|
const gchar *name;
|
||||||
@@ -190,67 +186,20 @@ const WpPluginMetadata * wp_plugin_get_metadata (WpPlugin * self);
|
|||||||
#define WP_MODULE_INIT_SYMBOL wireplumber__module_init
|
#define WP_MODULE_INIT_SYMBOL wireplumber__module_init
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WP_MODULE_DEFINE: (skip)
|
* WP_PLUGIN_DEFINE_TYPE: (skip)
|
||||||
*
|
*
|
||||||
* A convenience macro to register modules in C/C++.
|
* Convenience macro for defining a #WpPlugin subclass in a module
|
||||||
* A module can contain multiple plugins, which are meant to be registered
|
|
||||||
* with WP_PLUGIN_REGISTER in the place of @plugin_reg
|
|
||||||
*
|
|
||||||
* Example usage:
|
|
||||||
* |[
|
|
||||||
* WP_MODULE_DEFINE (
|
|
||||||
* WP_PLUGIN_REGISTER (
|
|
||||||
* MY_PLUGIN_TYPE,
|
|
||||||
* WP_PLUGIN_RANK_PLATFORM_OVERRIDE,
|
|
||||||
* "myplugin",
|
|
||||||
* "A custom policy plugin for Awesome Platform",
|
|
||||||
* "George Kiagiadakis <george.kiagiadakis@collabora.com>",
|
|
||||||
* "LGPL-2.1-or-later",
|
|
||||||
* "3.0.1",
|
|
||||||
* "https://awesome-platform.example"
|
|
||||||
* );
|
|
||||||
* WP_PLUGIN_REGISTER (
|
|
||||||
* SECONDARY_PLUGIN_TYPE,
|
|
||||||
* WP_PLUGIN_RANK_PLATFORM_OVERRIDE - 1,
|
|
||||||
* "secondaryplugin",
|
|
||||||
* "A secondary policy plugin for Awesome Platform",
|
|
||||||
* "George Kiagiadakis <george.kiagiadakis@collabora.com>",
|
|
||||||
* "LGPL-2.1-or-later",
|
|
||||||
* "3.0.1",
|
|
||||||
* "https://awesome-platform.example"
|
|
||||||
* );
|
|
||||||
* )
|
|
||||||
* ]|
|
|
||||||
*/
|
*/
|
||||||
#define WP_MODULE_DEFINE(plugin_reg) \
|
#define WP_PLUGIN_DEFINE_TYPE(TN, t_n) \
|
||||||
G_MODULE_EXPORT void \
|
typedef struct _##TN { \
|
||||||
WP_MODULE_INIT_SYMBOL (WpPluginRegistry * registry) \
|
WpPlugin parent; \
|
||||||
{ \
|
} TN; \
|
||||||
plugin_reg; \
|
\
|
||||||
}
|
typedef struct _##TN##Class { \
|
||||||
|
WpPluginClass parent; \
|
||||||
/**
|
} TN##Class; \
|
||||||
* WP_PLUGIN_REGISTER: (skip)
|
\
|
||||||
*
|
G_DEFINE_TYPE_WITH_PRIVATE (TN, t_n, wp_plugin_get_type ())
|
||||||
* A convenience macro to register plugins in C/C++.
|
|
||||||
* See WP_MODULE_DEFINE() for a usage example.
|
|
||||||
* See wp_plugin_registry_register() for a description of the parameters.
|
|
||||||
*/
|
|
||||||
#define WP_PLUGIN_REGISTER(gtype_, rank_, name_, description_, author_, license_, version_, origin_) \
|
|
||||||
G_STMT_START \
|
|
||||||
static const WpPluginMetadata plugin_metadata = { \
|
|
||||||
.gtype = gtype_, \
|
|
||||||
.rank = rank_, \
|
|
||||||
.name = name_, \
|
|
||||||
.description = description_, \
|
|
||||||
.author = author_, \
|
|
||||||
.license = license_, \
|
|
||||||
.version = version_, \
|
|
||||||
.origin = origin_ \
|
|
||||||
}; \
|
|
||||||
wp_plugin_registry_register_with_metadata (registry, &plugin_metadata, \
|
|
||||||
sizeof (plugin_metadata)); \
|
|
||||||
G_STMT_END
|
|
||||||
|
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
Reference in New Issue
Block a user