From ef2b8b40650370f241b65c5bb81d2e690d26a3f6 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Thu, 15 May 2014 09:54:07 +0200 Subject: [PATCH 2/3] add tp_intset_from_variant() https://bugs.freedesktop.org/show_bug.cgi?id=77773 --- .../telepathy-glib/telepathy-glib-sections.txt | 1 + telepathy-glib/intset.c | 33 ++++++++++++++++++++++ telepathy-glib/intset.h | 1 + tests/intset.c | 9 ++++-- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/reference/telepathy-glib/telepathy-glib-sections.txt b/docs/reference/telepathy-glib/telepathy-glib-sections.txt index 3ffca48..93f6f3f 100644 --- a/docs/reference/telepathy-glib/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib/telepathy-glib-sections.txt @@ -1784,6 +1784,7 @@ tp_intset_foreach tp_intset_to_array tp_intset_from_array tp_intset_to_variant +tp_intset_from_variant tp_intset_is_empty tp_intset_size tp_intset_is_equal diff --git a/telepathy-glib/intset.c b/telepathy-glib/intset.c index 54c93d1..2bc8157 100644 --- a/telepathy-glib/intset.c +++ b/telepathy-glib/intset.c @@ -412,6 +412,39 @@ tp_intset_to_variant (const TpIntset *set) return g_variant_builder_end (&builder); } +/** + * tp_intset_from_variant: + * @variant: a #GVariant of type 'au', consumed if floating + * + * + * + * Returns: A set containing the same integers as @variant. + */ +TpIntset * +tp_intset_from_variant (GVariant *variant) +{ + TpIntset *set; + GVariantIter iter; + guint32 value; + + g_return_val_if_fail (variant != NULL, NULL); + g_return_val_if_fail (g_variant_is_of_type (variant, G_VARIANT_TYPE ("au")), + NULL); + + g_variant_ref_sink (variant); + + set = tp_intset_new (); + + g_variant_iter_init (&iter, variant); + while (g_variant_iter_loop (&iter, "u", &value)) + { + tp_intset_add (set, value); + } + + g_variant_unref (variant); + return set; +} + /* these magic numbers would need adjusting for 64-bit storage */ G_STATIC_ASSERT (BITFIELD_BITS == 32); diff --git a/telepathy-glib/intset.h b/telepathy-glib/intset.h index 496bcfb..c66aade 100644 --- a/telepathy-glib/intset.h +++ b/telepathy-glib/intset.h @@ -57,6 +57,7 @@ GArray *tp_intset_to_array (const TpIntset *set) G_GNUC_WARN_UNUSED_RESULT; TpIntset *tp_intset_from_array (const GArray *array) G_GNUC_WARN_UNUSED_RESULT; GVariant *tp_intset_to_variant (const TpIntset *set) G_GNUC_WARN_UNUSED_RESULT; +TpIntset *tp_intset_from_variant (GVariant *variant) G_GNUC_WARN_UNUSED_RESULT; gboolean tp_intset_is_empty (const TpIntset *set) G_GNUC_WARN_UNUSED_RESULT; guint tp_intset_size (const TpIntset *set) G_GNUC_WARN_UNUSED_RESULT; diff --git a/tests/intset.c b/tests/intset.c index 2c189b0..89fec91 100644 --- a/tests/intset.c +++ b/tests/intset.c @@ -202,14 +202,19 @@ test (Fixture *f, { GVariant *v; + TpIntset *tmp; v = tp_intset_to_variant (a); g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE ("au"))); - g_variant_unref (v); + tmp = tp_intset_from_variant (v); + g_assert (tp_intset_is_equal (a, tmp)); + tp_intset_destroy (tmp); v = tp_intset_to_variant (b); g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE ("au"))); - g_variant_unref (v); + tmp = tp_intset_from_variant (v); + g_assert (tp_intset_is_equal (b, tmp)); + tp_intset_destroy (tmp); } value = tp_g_value_slice_new_take_boxed (TP_TYPE_INTSET, a); -- 1.9.0