From 96304d613f664fd749f87939c5be40f86e5d0f2a Mon Sep 17 00:00:00 2001 From: Stefan Sauer Date: Mon, 12 Dec 2016 22:14:24 +0100 Subject: [PATCH] gstvalue: add serialisation for GTypes We need this in the GstTracerRecord. This will serialize GTypes to the typename and vice versa. --- gst/gststructure.c | 6 +++++- gst/gstvalue.c | 36 ++++++++++++++++++++++++++++++++++- tests/check/gst/gstvalue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/gst/gststructure.c b/gst/gststructure.c index 5aa9b3e..c8d37e0 100644 --- a/gst/gststructure.c +++ b/gst/gststructure.c @@ -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 { diff --git a/gst/gstvalue.c b/gst/gstvalue.c index 57f7ba2..f103c62 100644 --- a/gst/gstvalue.c +++ b/gst/gstvalue.c @@ -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, diff --git a/tests/check/gst/gstvalue.c b/tests/check/gst/gstvalue.c index 64082c4..cca7260 100644 --- a/tests/check/gst/gstvalue.c +++ b/tests/check/gst/gstvalue.c @@ -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); -- 2.7.4