From 6fbf2527fbef1bad3fca6b293393276e4b507bba Mon Sep 17 00:00:00 2001 From: Julian Bouzas <julian.bouzas@collabora.com> Date: Thu, 20 Jun 2019 14:39:20 -0400 Subject: [PATCH] factory: add async support --- lib/wp/factory.c | 62 +++++++++++++++++++++++++++++++++++++++++++----- lib/wp/factory.h | 10 ++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/lib/wp/factory.c b/lib/wp/factory.c index 861f86de..82cb6562 100644 --- a/lib/wp/factory.c +++ b/lib/wp/factory.c @@ -15,7 +15,10 @@ struct _WpFactory GWeakRef core; gchar *name; GQuark name_quark; - WpFactoryFunc create_object; + union { + WpFactoryFunc sync; + WpFactoryAsyncFunc async; + } create_object; }; G_DEFINE_TYPE (WpFactory, wp_factory, G_TYPE_OBJECT) @@ -45,19 +48,29 @@ wp_factory_class_init (WpFactoryClass * klass) object_class->finalize = wp_factory_finalize; } -WpFactory * -wp_factory_new (WpCore * core, const gchar * name, WpFactoryFunc func) +static +WpFactory * create_factory (WpCore * core, const gchar * name) { WpFactory *f = NULL; g_return_val_if_fail (name != NULL && *name != '\0', NULL); - g_return_val_if_fail (func != NULL, NULL); f = g_object_new (WP_TYPE_FACTORY, NULL); g_weak_ref_init (&f->core, core); f->name = g_strdup (name); f->name_quark = g_quark_from_string (f->name); - f->create_object = func; + + return f; +} + +WpFactory * +wp_factory_new (WpCore * core, const gchar * name, WpFactoryFunc func) +{ + WpFactory *f = NULL; + g_return_val_if_fail (func, NULL); + + f = create_factory(core, name); + f->create_object.sync = func; g_info ("WpFactory:%p new factory: %s", f, name); @@ -66,6 +79,23 @@ wp_factory_new (WpCore * core, const gchar * name, WpFactoryFunc func) return f; } +WpFactory * +wp_factory_new_async (WpCore * core, const gchar * name, + WpFactoryAsyncFunc func) +{ + WpFactory *f = NULL; + g_return_val_if_fail (func, NULL); + + f = create_factory(core, name); + f->create_object.async = func; + + g_info ("WpFactory:%p new async factory: %s", f, name); + + wp_core_register_global (core, WP_GLOBAL_FACTORY, f, g_object_unref); + + return f; +} + const gchar * wp_factory_get_name (WpFactory * self) { @@ -89,7 +119,18 @@ wp_factory_create_object (WpFactory * self, GType type, GVariant * properties) { g_debug ("WpFactory:%p (%s) create object of type %s", self, self->name, g_type_name (type)); - return self->create_object (self, type, properties); + + return self->create_object.sync (self, type, properties); +} + +void +wp_factory_create_object_async (WpFactory * self, GType type, + GVariant * properties, GAsyncReadyCallback ready, gpointer user_data) +{ + g_debug ("WpFactory:%p (%s) create object async of type %s", self, self->name, + g_type_name (type)); + + self->create_object.async (self, type, properties, ready, user_data); } struct find_factory_data @@ -127,3 +168,12 @@ wp_factory_make (WpCore * core, const gchar * name, GType type, if (!f) return NULL; return wp_factory_create_object (f, type, properties); } + +void +wp_factory_make_async (WpCore * core, const gchar * name, GType type, + GVariant * properties, GAsyncReadyCallback ready, gpointer user_data) +{ + WpFactory *f = wp_factory_find (core, name); + if (!f) return; + wp_factory_create_object_async (f, type, properties, ready, user_data); +} diff --git a/lib/wp/factory.h b/lib/wp/factory.h index 6ab9613e..9f74c22c 100644 --- a/lib/wp/factory.h +++ b/lib/wp/factory.h @@ -9,6 +9,8 @@ #ifndef __WIREPLUMBER_FACTORY_H__ #define __WIREPLUMBER_FACTORY_H__ +#include <gio/gio.h> + #include "core.h" G_BEGIN_DECLS @@ -18,18 +20,26 @@ G_DECLARE_FINAL_TYPE (WpFactory, wp_factory, WP, FACTORY, GObject) typedef gpointer (*WpFactoryFunc) (WpFactory * self, GType type, GVariant * properties); +typedef void (*WpFactoryAsyncFunc) (WpFactory * self, GType type, + GVariant * properties, GAsyncReadyCallback ready, gpointer user_data); WpFactory * wp_factory_new (WpCore * core, const gchar * name, WpFactoryFunc func); +WpFactory * wp_factory_new_async (WpCore * core, const gchar * name, + WpFactoryAsyncFunc func); const gchar * wp_factory_get_name (WpFactory * self); WpCore * wp_factory_get_core (WpFactory * self); gpointer wp_factory_create_object (WpFactory * self, GType type, GVariant * properties); +void wp_factory_create_object_async (WpFactory * self, GType type, + GVariant * properties, GAsyncReadyCallback ready, gpointer user_data); WpFactory * wp_factory_find (WpCore * core, const gchar * name); gpointer wp_factory_make (WpCore * core, const gchar * name, GType type, GVariant * properties); +void wp_factory_make_async (WpCore * core, const gchar * name, GType type, + GVariant * properties, GAsyncReadyCallback ready, gpointer user_data); G_END_DECLS -- GitLab