[gimp] libgimp: add more comfortable ways to run procedures to GimpPDB



commit 84cb07f99558a57c412d8cb674921b5c64bab962
Author: Michael Natterer <mitch gimp org>
Date:   Tue Aug 6 19:10:04 2019 +0200

    libgimp: add more comfortable ways to run procedures to GimpPDB
    
    return_vals = gimp_pdb_run_procedure (pdb, "procedure-name",
                                          G_TYPE_STRING,  "string arg",
                                          G_TYPE_INT      23,
                                          G_TYPE_BOOLEAN, FALSE,
                                          G_TYPE_NONE);

 libgimp/gimppdb.c       | 87 +++++++++++++++++++++++++++++++++++++++++++++++--
 libgimp/gimppdb.h       | 30 ++++++++++-------
 libgimp/gimpprocedure.c |  4 ++-
 3 files changed, 106 insertions(+), 15 deletions(-)
---
diff --git a/libgimp/gimppdb.c b/libgimp/gimppdb.c
index 6aad0f3a82..cd5d90493f 100644
--- a/libgimp/gimppdb.c
+++ b/libgimp/gimppdb.c
@@ -20,6 +20,8 @@
 
 #include "config.h"
 
+#include <gobject/gvaluecollector.h>
+
 #include "gimp.h"
 
 #include "libgimpbase/gimpprotocol.h"
@@ -155,9 +157,88 @@ gimp_pdb_lookup_procedure (GimpPDB     *pdb,
 }
 
 GimpValueArray *
-gimp_pdb_run_procedure (GimpPDB        *pdb,
-                        const gchar    *procedure_name,
-                        GimpValueArray *arguments)
+gimp_pdb_run_procedure (GimpPDB     *pdb,
+                        const gchar *procedure_name,
+                        GType        first_type,
+                        ...)
+{
+  GimpValueArray *return_values;
+  va_list         args;
+
+  g_return_val_if_fail (GIMP_IS_PDB (pdb), NULL);
+  g_return_val_if_fail (procedure_name != NULL, NULL);
+
+  va_start (args, first_type);
+
+  return_values = gimp_pdb_run_procedure_valist (pdb, procedure_name,
+                                                 first_type, args);
+
+  va_end (args);
+
+  return return_values;
+}
+
+GimpValueArray *
+gimp_pdb_run_procedure_valist (GimpPDB     *pdb,
+                               const gchar *procedure_name,
+                               GType        first_type,
+                               va_list      args)
+{
+  GimpValueArray *arguments;
+  GimpValueArray *return_values;
+  GType           type;
+
+  g_return_val_if_fail (GIMP_IS_PDB (pdb), NULL);
+  g_return_val_if_fail (procedure_name != NULL, NULL);
+
+  arguments = gimp_value_array_new (0);
+
+  type = first_type;
+
+  while (type != G_TYPE_NONE)
+    {
+      GValue  value     = G_VALUE_INIT;
+      gchar  *error_msg = NULL;
+
+      g_value_init (&value, type);
+
+      G_VALUE_COLLECT (&value, args, G_VALUE_NOCOPY_CONTENTS, &error_msg);
+
+      if (error_msg)
+        {
+          GError *error = g_error_new_literal (GIMP_PDB_ERROR,
+                                               GIMP_PDB_ERROR_INTERNAL_ERROR,
+                                               error_msg);
+          g_printerr ("%s: %s", G_STRFUNC, error_msg);
+          g_free (error_msg);
+
+          gimp_value_array_unref (arguments);
+
+          return_values = gimp_procedure_new_return_values (NULL,
+                                                            GIMP_PDB_CALLING_ERROR,
+                                                            error);
+          va_end (args);
+
+          return return_values;
+        }
+
+      gimp_value_array_append (arguments, &value);
+      g_value_unset (&value);
+
+      type = va_arg (args, GType);
+    }
+
+  return_values = gimp_pdb_run_procedure_array (pdb, procedure_name,
+                                                arguments);
+  gimp_value_array_unref (arguments);
+
+  return return_values;
+}
+
+GimpValueArray *
+gimp_pdb_run_procedure_array (GimpPDB        *pdb,
+                              const gchar    *procedure_name,
+                              GimpValueArray *arguments)
 {
   GPProcRun        proc_run;
   GPProcReturn    *proc_return;
diff --git a/libgimp/gimppdb.h b/libgimp/gimppdb.h
index c7af7b6acc..a94cb2367b 100644
--- a/libgimp/gimppdb.h
+++ b/libgimp/gimppdb.h
@@ -65,17 +65,25 @@ struct _GimpPDBClass
 };
 
 
-GType            gimp_pdb_get_type         (void) G_GNUC_CONST;
-
-gboolean         gimp_pdb_procedure_exists (GimpPDB        *pdb,
-                                            const gchar    *procedure_name);
-
-GimpProcedure  * gimp_pdb_lookup_procedure (GimpPDB        *pdb,
-                                            const gchar    *procedure_name);
-
-GimpValueArray * gimp_pdb_run_procedure    (GimpPDB        *pdb,
-                                            const gchar    *procedure_name,
-                                            GimpValueArray *arguments);
+GType            gimp_pdb_get_type             (void) G_GNUC_CONST;
+
+gboolean         gimp_pdb_procedure_exists     (GimpPDB        *pdb,
+                                                const gchar    *procedure_name);
+
+GimpProcedure  * gimp_pdb_lookup_procedure     (GimpPDB        *pdb,
+                                                const gchar    *procedure_name);
+
+GimpValueArray * gimp_pdb_run_procedure        (GimpPDB        *pdb,
+                                                const gchar    *procedure_name,
+                                                GType           first_type,
+                                                ...);
+GimpValueArray * gimp_pdb_run_procedure_valist (GimpPDB        *pdb,
+                                                const gchar    *procedure_name,
+                                                GType           first_type,
+                                                va_list         args);
+GimpValueArray * gimp_pdb_run_procedure_array  (GimpPDB        *pdb,
+                                                const gchar    *procedure_name,
+                                                GimpValueArray *arguments);
 
 
 /*  Cruft API  */
diff --git a/libgimp/gimpprocedure.c b/libgimp/gimpprocedure.c
index c22677cd8e..a8656c47d2 100644
--- a/libgimp/gimpprocedure.c
+++ b/libgimp/gimpprocedure.c
@@ -976,13 +976,14 @@ gimp_procedure_new_return_values (GimpProcedure     *procedure,
   GValue          value = G_VALUE_INIT;
   gint            i;
 
-  g_return_val_if_fail (GIMP_IS_PROCEDURE (procedure), NULL);
   g_return_val_if_fail (status != GIMP_PDB_PASS_THROUGH, NULL);
 
   switch (status)
     {
     case GIMP_PDB_SUCCESS:
     case GIMP_PDB_CANCEL:
+      g_return_val_if_fail (GIMP_IS_PROCEDURE (procedure), NULL);
+
       args = gimp_value_array_new (procedure->priv->n_values + 1);
 
       g_value_init (&value, GIMP_TYPE_PDB_STATUS_TYPE);
@@ -1022,6 +1023,7 @@ gimp_procedure_new_return_values (GimpProcedure     *procedure,
     default:
       g_return_val_if_reached (NULL);
     }
+
   g_clear_error (&error);
 
   return args;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]