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...
This commit is contained in:
Michael Natterer 2019-07-30 10:12:28 +02:00
parent 9e50f1ed8f
commit e9443b2561
3 changed files with 75 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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);