gstvalue: Add transformation to/from GValueArray
authorNicolas Dufresne <nicolas.dufresne@collabora.com>
Mon, 20 Mar 2017 19:40:25 +0000 (15:40 -0400)
committerNicolas Dufresne <nicolas.dufresne@collabora.com>
Fri, 24 Mar 2017 17:30:45 +0000 (13:30 -0400)
This allow transforming a GValue of type G_TYPE_VALUE_ARRAY to
and from GST_TYPE_ARRAY/LIST.

https://bugzilla.gnome.org/show_bug.cgi?id=753754

gst/gstvalue.c
tests/check/gst/gstvalue.c

index 37f10dd..749e867 100644 (file)
@@ -938,6 +938,45 @@ gst_value_transform_g_value_array_string (const GValue * src_value,
   _gst_value_transform_g_value_array_string (src_value, dest_value, "< ", " >");
 }
 
+static void
+gst_value_transform_g_value_array_any_list (const GValue * src_value,
+    GValue * dest_value)
+{
+  const GValueArray *varray;
+  GArray *array;
+  gint i;
+
+  /* GLib will unset the value, memset to 0 the data instead of doing a proper
+   * reset. That's why we need to allocate the array here */
+  gst_value_init_list_or_array (dest_value);
+
+  varray = g_value_get_boxed (src_value);
+  array = dest_value->data[0].v_pointer;
+
+  for (i = 0; i < varray->n_values; i++) {
+    GValue val = G_VALUE_INIT;
+    gst_value_init_and_copy (&val, &varray->values[i]);
+    g_array_append_vals (array, &val, 1);
+  }
+}
+
+static void
+gst_value_transform_any_list_g_value_array (const GValue * src_value,
+    GValue * dest_value)
+{
+  GValueArray *varray;
+  const GArray *array;
+  gint i;
+
+  array = src_value->data[0].v_pointer;
+  varray = g_value_array_new (array->len);
+
+  for (i = 0; i < array->len; i++)
+    g_value_array_append (varray, &g_array_index (array, GValue, i));
+
+  g_value_take_boxed (dest_value, varray);
+}
+
 /* Do an unordered compare of the contents of a list */
 static gint
 gst_value_compare_value_list (const GValue * value1, const GValue * value2)
@@ -7525,10 +7564,18 @@ _priv_gst_value_initialize (void)
       gst_value_transform_fraction_range_string);
   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
       gst_value_transform_list_string);
+  g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_VALUE_ARRAY,
+      gst_value_transform_any_list_g_value_array);
   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
       gst_value_transform_array_string);
+  g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_VALUE_ARRAY,
+      gst_value_transform_any_list_g_value_array);
   g_value_register_transform_func (G_TYPE_VALUE_ARRAY, G_TYPE_STRING,
       gst_value_transform_g_value_array_string);
+  g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_ARRAY,
+      gst_value_transform_g_value_array_any_list);
+  g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_LIST,
+      gst_value_transform_g_value_array_any_list);
   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
       gst_value_transform_fraction_string);
   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
index 644d244..9d3ae43 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 
+#define GLIB_DISABLE_DEPRECATION_WARNINGS
 #include <gst/check/gstcheck.h>
 
 
@@ -3311,6 +3312,85 @@ GST_START_TEST (test_structure_ops)
 
 GST_END_TEST;
 
+static void
+setup_test_value_array (GValue * value)
+{
+  GValueArray *array;
+  GValue v = G_VALUE_INIT;
+
+  g_value_init (&v, G_TYPE_INT);
+  g_value_init (value, G_TYPE_VALUE_ARRAY);
+
+  array = g_value_array_new (3);
+  g_value_set_int (&v, 1);
+  g_value_array_append (array, &v);
+  g_value_set_int (&v, 2);
+  g_value_array_append (array, &v);
+  g_value_set_int (&v, 3);
+  g_value_array_append (array, &v);
+
+  g_value_take_boxed (value, array);
+}
+
+static void
+test_revert_array_transform (GValue * v1, GValue * v2)
+{
+  GValueArray *array;
+
+  g_value_reset (v1);
+
+  fail_unless (g_value_transform (v2, v1));
+  array = g_value_get_boxed (v1);
+  fail_unless (array->n_values == 3);
+  fail_unless (g_value_get_int (g_value_array_get_nth (array, 0)) == 1);
+  fail_unless (g_value_get_int (g_value_array_get_nth (array, 1)) == 2);
+  fail_unless (g_value_get_int (g_value_array_get_nth (array, 2)) == 3);
+}
+
+GST_START_TEST (test_transform_array)
+{
+  GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT;
+
+  setup_test_value_array (&v1);
+
+  g_value_init (&v2, GST_TYPE_ARRAY);
+
+  fail_unless (g_value_transform (&v1, &v2));
+  fail_unless (gst_value_array_get_size (&v2) == 3);
+  fail_unless (g_value_get_int (gst_value_array_get_value (&v2, 0)) == 1);
+  fail_unless (g_value_get_int (gst_value_array_get_value (&v2, 1)) == 2);
+  fail_unless (g_value_get_int (gst_value_array_get_value (&v2, 2)) == 3);
+
+  test_revert_array_transform (&v1, &v2);
+
+  g_value_unset (&v1);
+  g_value_unset (&v2);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_transform_list)
+{
+  GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT;
+
+  setup_test_value_array (&v1);
+
+  g_value_init (&v2, GST_TYPE_LIST);
+
+  fail_unless (g_value_transform (&v1, &v2));
+  fail_unless (gst_value_list_get_size (&v2) == 3);
+  fail_unless (g_value_get_int (gst_value_list_get_value (&v2, 0)) == 1);
+  fail_unless (g_value_get_int (gst_value_list_get_value (&v2, 1)) == 2);
+  fail_unless (g_value_get_int (gst_value_list_get_value (&v2, 2)) == 3);
+
+  test_revert_array_transform (&v1, &v2);
+
+  g_value_unset (&v1);
+  g_value_unset (&v2);
+}
+
+GST_END_TEST;
+
 static Suite *
 gst_value_suite (void)
 {
@@ -3360,6 +3440,8 @@ gst_value_suite (void)
   tcase_add_test (tc_chain, test_structure_basic);
   tcase_add_test (tc_chain, test_structure_single_ops);
   tcase_add_test (tc_chain, test_structure_ops);
+  tcase_add_test (tc_chain, test_transform_array);
+  tcase_add_test (tc_chain, test_transform_list);
 
   return s;
 }