gstvalue: add serialisation for GTypes
authorStefan Sauer <ensonic@users.sf.net>
Mon, 12 Dec 2016 21:14:24 +0000 (22:14 +0100)
committerStefan Sauer <ensonic@users.sf.net>
Tue, 13 Dec 2016 12:24:18 +0000 (13:24 +0100)
We need this in the GstTracerRecord. This will serialize GTypes to the typename
and vice versa.

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

index 5aa9b3e..c8d37e0 100644 (file)
@@ -1821,6 +1821,8 @@ gst_structure_get_abbrs (gint * n_abbrs)
       {"sample", GST_TYPE_SAMPLE}
       ,
       {"taglist", GST_TYPE_TAG_LIST}
+      ,
+      {"type", G_TYPE_GTYPE}
     };
     _num = G_N_ELEMENTS (dyn_abbrs);
     /* permanently allocate and copy the array now */
@@ -1982,7 +1984,9 @@ priv__gst_structure_append_template_to_gstring (GQuark field_id,
     g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
   } else if (g_type_is_a (type, G_TYPE_ENUM)
       || g_type_is_a (type, G_TYPE_FLAGS)) {
-    g_string_append (s, "%i");
+    g_string_append_len (s, "%i", 2);
+  } else if (type == G_TYPE_GTYPE) {
+    g_string_append_len (s, "%s", 2);
   } else if (type == G_TYPE_POINTER) {
     g_string_append_len (s, "%p", 2);
   } else {
index 57f7ba2..f103c62 100644 (file)
@@ -3427,6 +3427,38 @@ gst_value_deserialize_gflags (GValue * dest, const gchar * s)
   return res;
 }
 
+/*********
+ * gtype *
+ *********/
+
+static gint
+gst_value_compare_gtype (const GValue * value1, const GValue * value2)
+{
+  if (value1->data[0].v_pointer == value2->data[0].v_pointer)
+    return GST_VALUE_EQUAL;
+  return GST_VALUE_UNORDERED;
+}
+
+static gchar *
+gst_value_serialize_gtype (const GValue * value)
+{
+  return g_strdup (g_type_name (g_value_get_gtype (value)));
+}
+
+static gboolean
+gst_value_deserialize_gtype (GValue * dest, const gchar * s)
+{
+  GType t = g_type_from_name (s);
+  gboolean ret = TRUE;
+
+  if (t == G_TYPE_INVALID)
+    ret = FALSE;
+  if (ret) {
+    g_value_set_gtype (dest, t);
+  }
+  return ret;
+}
+
 /****************
  * subset *
  ****************/
@@ -6739,7 +6771,7 @@ G_STMT_START {                                                          \
 
 /* These initial sizes are used for the tables
  * below, and save a couple of reallocs at startup */
-static const gint GST_VALUE_TABLE_DEFAULT_SIZE = 34;
+static const gint GST_VALUE_TABLE_DEFAULT_SIZE = 35;
 static const gint GST_VALUE_UNION_TABLE_DEFAULT_SIZE = 3;
 static const gint GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE = 10;
 static const gint GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE = 12;
@@ -6804,6 +6836,8 @@ _priv_gst_value_initialize (void)
 
   REGISTER_SERIALIZATION_CONST (G_TYPE_UCHAR, uchar);
 
+  REGISTER_SERIALIZATION (G_TYPE_GTYPE, gtype);
+
   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
       gst_value_transform_int_range_string);
   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
index 64082c4..cca7260 100644 (file)
@@ -489,6 +489,51 @@ GST_START_TEST (test_deserialize_flags)
 
 GST_END_TEST;
 
+GST_START_TEST (test_deserialize_gtype)
+{
+  GValue value = { 0 };
+  const char *strings[] = {
+    "gchararray",
+    "gint",
+  };
+  GType results[] = {
+    G_TYPE_STRING,
+    G_TYPE_INT,
+  };
+  int i;
+
+  g_value_init (&value, G_TYPE_GTYPE);
+
+  for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
+    fail_unless (gst_value_deserialize (&value, strings[i]),
+        "could not deserialize %s (%d)", strings[i], i);
+    fail_unless (g_value_get_gtype (&value) == results[i],
+        "resulting value is %" G_GSIZE_FORMAT ", not %" G_GSIZE_FORMAT
+        ", for string %s (%d)",
+        g_value_get_gtype (&value), results[i], strings[i], i);
+  }
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_deserialize_gtype_failures)
+{
+  GValue value = { 0 };
+  const char *strings[] = {
+    "-",                        /* not a gtype */
+  };
+  int i;
+
+  g_value_init (&value, G_TYPE_GTYPE);
+
+  for (i = 0; i < G_N_ELEMENTS (strings); ++i) {
+    fail_if (gst_value_deserialize (&value, strings[i]),
+        "deserialized %s (%d), while it should have failed", strings[i], i);
+  }
+}
+
+GST_END_TEST;
+
 GST_START_TEST (test_deserialize_bitmask)
 {
   GValue value = { 0 };
@@ -3087,6 +3132,8 @@ gst_value_suite (void)
   tcase_add_test (tc_chain, test_deserialize_guint64);
   tcase_add_test (tc_chain, test_deserialize_guchar);
   tcase_add_test (tc_chain, test_deserialize_gstfraction);
+  tcase_add_test (tc_chain, test_deserialize_gtype);
+  tcase_add_test (tc_chain, test_deserialize_gtype_failures);
   tcase_add_test (tc_chain, test_deserialize_bitmask);
   tcase_add_test (tc_chain, test_serialize_flags);
   tcase_add_test (tc_chain, test_deserialize_flags);