[gimp] libgimpbase: add gimp_value_array_new_from_types()



commit e9443b25619a2b9a90aa52fa9b4928ad34c766e7
Author: Michael Natterer <mitch gimp org>
Date:   Tue Jul 30 10:12:28 2019 +0200

    libgimpbase: add gimp_value_array_new_from_types()
    
    and _new_from_types_valist()
    
    which take a va_list of GTypes and creates a GimpValueArray
    initialized with these types, so one can simply have a list of
    
    g_value_set_foo (gimp_value_array_index (array, i), foo);
    
    in the next lines. I'm not so sure this is the best API ever...

 libgimpbase/gimpbase.def     |  2 ++
 libgimpbase/gimpvaluearray.c | 71 +++++++++++++++++++++++++++++++++++++++++---
 libgimpbase/gimpvaluearray.h |  6 ++++
 3 files changed, 75 insertions(+), 4 deletions(-)
---
diff --git a/libgimpbase/gimpbase.def b/libgimpbase/gimpbase.def
index 00b2833374..7bc6e9d2dc 100644
--- a/libgimpbase/gimpbase.def
+++ b/libgimpbase/gimpbase.def
@@ -225,6 +225,8 @@ EXPORTS
        gimp_value_array_insert
        gimp_value_array_length
        gimp_value_array_new
+       gimp_value_array_new_from_types
+       gimp_value_array_new_from_types_valist
        gimp_value_array_prepend
        gimp_value_array_ref
        gimp_value_array_remove
diff --git a/libgimpbase/gimpvaluearray.c b/libgimpbase/gimpvaluearray.c
index eb23b82a70..dcc819ad18 100644
--- a/libgimpbase/gimpvaluearray.c
+++ b/libgimpbase/gimpvaluearray.c
@@ -136,11 +136,8 @@ value_array_shrink (GimpValueArray *value_array)
 GimpValueArray *
 gimp_value_array_new (gint n_prealloced)
 {
-  GimpValueArray *value_array = g_slice_new (GimpValueArray);
+  GimpValueArray *value_array = g_slice_new0 (GimpValueArray);
 
-  value_array->n_values = 0;
-  value_array->n_prealloced = 0;
-  value_array->values = NULL;
   value_array_grow (value_array, n_prealloced, TRUE);
   value_array->n_values = 0;
   value_array->ref_count = 1;
@@ -148,6 +145,72 @@ gimp_value_array_new (gint n_prealloced)
   return value_array;
 }
 
+/**
+ * gimp_value_array_new_from_types:
+ * @first_type: first type in the array, or #G_TYPE_NONE.
+ * @...:        the remaining types in the array, terminated by #G_TYPE_NONE
+ *
+ * Allocate and initialize a new #GimpValueArray, and fill it with
+ * values that are initialized to the types passed.
+ *
+ * Returns: a newly allocated #GimpValueArray
+ *
+ * Since: 3.0
+ */
+GimpValueArray *
+gimp_value_array_new_from_types (GType first_type,
+                                 ...)
+{
+  GimpValueArray *value_array;
+  va_list         va_args;
+
+  va_start (va_args, first_type);
+
+  value_array = gimp_value_array_new_from_types_valist (first_type,
+                                                        va_args);
+
+  va_end (va_args);
+
+  return value_array;
+}
+
+/**
+ * gimp_value_array_new_from_types_valist:
+ * @first_type: first type in the array, or #G_TYPE_NONE.
+ * @va_args:    a va_list of GTypes and values, terminated by #G_TYPE_NONE
+ *
+ * Allocate and initialize a new #GimpValueArray, and fill it with
+ * values that are initialized to the types passed.
+ *
+ * Returns: a newly allocated #GimpValueArray
+ *
+ * Since: 3.0
+ */
+GimpValueArray *
+gimp_value_array_new_from_types_valist (GType   first_type,
+                                        va_list va_args)
+{
+  GimpValueArray *value_array = gimp_value_array_new (0);
+  GType           type;
+
+  type = first_type;
+
+  while (type != G_TYPE_NONE)
+    {
+      GValue value = G_VALUE_INIT;
+
+      g_value_init (&value, type);
+      gimp_value_array_append (value_array, &value);
+      g_value_unset (&value);
+
+      type = va_arg (va_args, GType);
+    }
+
+  va_end (va_args);
+
+  return value_array;
+}
+
 /**
  * gimp_value_array_ref:
  * @value_array: #GimpValueArray to ref
diff --git a/libgimpbase/gimpvaluearray.h b/libgimpbase/gimpvaluearray.h
index d7aefcb8aa..bc70424270 100644
--- a/libgimpbase/gimpvaluearray.h
+++ b/libgimpbase/gimpvaluearray.h
@@ -41,6 +41,12 @@ G_BEGIN_DECLS
 GType            gimp_value_array_get_type (void) G_GNUC_CONST;
 
 GimpValueArray * gimp_value_array_new      (gint                  n_prealloced);
+GimpValueArray * gimp_value_array_new_from_types
+                                           (GType                 first_type,
+                                            ...);
+GimpValueArray * gimp_value_array_new_from_types_valist
+                                           (GType                 first_type,
+                                            va_list               va_args);
 
 GimpValueArray * gimp_value_array_ref      (GimpValueArray       *value_array);
 void             gimp_value_array_unref    (GimpValueArray       *value_array);


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