Skip to content
Snippets Groups Projects
Commit 6fbf2527 authored by Julian Bouzas's avatar Julian Bouzas
Browse files

factory: add async support

parent 661010a3
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment