From 06db0e414afd073d13566f9dbe71828f8188d075 Mon Sep 17 00:00:00 2001
From: George Kiagiadakis <george.kiagiadakis@collabora.com>
Date: Wed, 22 May 2019 13:03:24 +0300
Subject: [PATCH] factory: improve API and hide its implementation (better for
 introspection)

---
 lib/wp/endpoint.c                          |  3 +-
 lib/wp/factory.c                           | 44 ++++++++++++++++++-
 lib/wp/factory.h                           | 50 ++++------------------
 modules/module-pipewire.c                  |  7 ++-
 modules/module-pw-audio-softdsp-endpoint.c |  3 +-
 5 files changed, 55 insertions(+), 52 deletions(-)

diff --git a/lib/wp/endpoint.c b/lib/wp/endpoint.c
index b12afaeb..9c42cb0d 100644
--- a/lib/wp/endpoint.c
+++ b/lib/wp/endpoint.c
@@ -547,8 +547,7 @@ WpEndpointLink * wp_endpoint_link_new (WpCore * core, WpEndpoint * src,
 
   /* create link object */
 
-  link = wp_core_make_from_factory (core, src_factory, WP_TYPE_ENDPOINT_LINK,
-      NULL);
+  link = wp_factory_make (core, src_factory, WP_TYPE_ENDPOINT_LINK, NULL);
   if (!link) {
     g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
         "Failed to create link object from factory %s", src_factory);
diff --git a/lib/wp/factory.c b/lib/wp/factory.c
index a49921eb..7ce68bfa 100644
--- a/lib/wp/factory.c
+++ b/lib/wp/factory.c
@@ -8,6 +8,15 @@
 
 #include "factory.h"
 
+struct _WpFactory
+{
+  GObject parent;
+
+  gchar *name;
+  GQuark name_quark;
+  WpFactoryFunc create_object;
+};
+
 G_DEFINE_TYPE (WpFactory, wp_factory, G_TYPE_OBJECT)
 
 static void
@@ -33,9 +42,9 @@ wp_factory_class_init (WpFactoryClass * klass)
 }
 
 WpFactory *
-wp_factory_new (const gchar * name, WpFactoryFunc func)
+wp_factory_new (WpCore * core, const gchar * name, WpFactoryFunc func)
 {
-  WpFactory *f;
+  g_autoptr (WpFactory) f = NULL;
 
   g_return_val_if_fail (name != NULL && *name != '\0', NULL);
   g_return_val_if_fail (func != NULL, NULL);
@@ -44,5 +53,36 @@ wp_factory_new (const gchar * name, WpFactoryFunc func)
   f->name = g_strdup (name);
   f->name_quark = g_quark_from_string (f->name);
   f->create_object = func;
+
+  if (!wp_core_register_global (core, f->name_quark, f, g_object_unref))
+    return NULL;
+
   return f;
 }
+
+const gchar *
+wp_factory_get_name (WpFactory * self)
+{
+  return self->name;
+}
+
+gpointer
+wp_factory_create_object (WpFactory * self, GType type, GVariant * properties)
+{
+  return self->create_object (self, type, properties);
+}
+
+WpFactory *
+wp_factory_find (WpCore * core, const gchar * name)
+{
+  return wp_core_get_global (core, g_quark_from_string (name));
+}
+
+gpointer
+wp_factory_make (WpCore * core, const gchar * name, GType type,
+    GVariant * properties)
+{
+  WpFactory *f = wp_factory_find (core, name);
+  if (!f) return NULL;
+  return wp_factory_create_object (f, type, properties);
+}
diff --git a/lib/wp/factory.h b/lib/wp/factory.h
index 9e993068..83737029 100644
--- a/lib/wp/factory.h
+++ b/lib/wp/factory.h
@@ -19,50 +19,16 @@ G_DECLARE_FINAL_TYPE (WpFactory, wp_factory, WP, FACTORY, GObject)
 typedef gpointer (*WpFactoryFunc) (WpFactory * self, GType type,
     GVariant * properties);
 
-struct _WpFactory
-{
-  GObject parent;
+WpFactory * wp_factory_new (WpCore * core, const gchar * name,
+    WpFactoryFunc func);
 
-  gchar *name;
-  GQuark name_quark;
-  WpFactoryFunc create_object;
-};
-
-WpFactory * wp_factory_new (const gchar * name, WpFactoryFunc func);
-
-static inline const gchar * wp_factory_get_name (WpFactory * factory)
-{
-  return factory->name;
-}
-
-static inline gpointer wp_factory_create_object (WpFactory * factory,
-    GType type, GVariant * properties)
-{
-  return factory->create_object (factory, type, properties);
-}
-
-
-static inline gboolean
-wp_core_register_factory (WpCore * core, WpFactory * factory)
-{
-  return wp_core_register_global (core, factory->name_quark, factory,
-      g_object_unref);
-}
-
-static inline WpFactory *
-wp_core_find_factory (WpCore * core, const gchar * name)
-{
-  return wp_core_get_global (core, g_quark_from_string (name));
-}
+const gchar * wp_factory_get_name (WpFactory * self);
+gpointer wp_factory_create_object (WpFactory * self, GType type,
+    GVariant * properties);
 
-static inline gpointer
-wp_core_make_from_factory (WpCore * core, const gchar * name, GType type,
-    GVariant * properties)
-{
-  WpFactory *f = wp_core_find_factory (core, name);
-  if (!f) return NULL;
-  return wp_factory_create_object (f, type, properties);
-}
+WpFactory * wp_factory_find (WpCore * core, const gchar * name);
+gpointer wp_factory_make (WpCore * core, const gchar * name, GType type,
+    GVariant * properties);
 
 G_END_DECLS
 
diff --git a/modules/module-pipewire.c b/modules/module-pipewire.c
index 5040723e..2389f982 100644
--- a/modules/module-pipewire.c
+++ b/modules/module-pipewire.c
@@ -59,10 +59,9 @@ wireplumber__module_init (WpModule * module, WpCore * core, GVariant * args)
 
   wp_module_set_destroy_callback (module, module_destroy, pw_remote);
 
-  wp_core_register_factory (core, wp_factory_new (
-          "pipewire-simple-endpoint", simple_endpoint_factory));
-  wp_core_register_factory (core, wp_factory_new (
-          "pipewire-simple-endpoint-link", simple_endpoint_link_factory));
+  wp_factory_new (core, "pipewire-simple-endpoint", simple_endpoint_factory);
+  wp_factory_new (core, "pipewire-simple-endpoint-link",
+      simple_endpoint_link_factory);
 
   g_idle_add ((GSourceFunc) connect_in_idle, pw_remote);
 }
diff --git a/modules/module-pw-audio-softdsp-endpoint.c b/modules/module-pw-audio-softdsp-endpoint.c
index a590dadc..b2534c0a 100644
--- a/modules/module-pw-audio-softdsp-endpoint.c
+++ b/modules/module-pw-audio-softdsp-endpoint.c
@@ -60,6 +60,5 @@ endpoint_factory (WpFactory * factory, GType type, GVariant * properties)
 void
 wireplumber__module_init (WpModule * module, WpCore * core, GVariant * args)
 {
-  wp_core_register_factory (core, wp_factory_new (
-          "pw-audio-softdsp-endpoint", endpoint_factory));
+  wp_factory_new (core, "pw-audio-softdsp-endpoint", endpoint_factory);
 }
-- 
GitLab