From 76b4a251660e7b4a60b2ab5b2bebe484a530c2b5 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 | 7 +++++ 4 files changed, 42 insertions(+) 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 7b3c58b..30d27cb 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: (transfer full): 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 3695f1e..83580ff 100644 --- a/tests/intset.c +++ b/tests/intset.c @@ -202,16 +202,23 @@ test (Fixture *f, { GVariant *v; + TpIntset *tmp; v = tp_intset_to_variant (a); g_variant_ref_sink (v); g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE ("au"))); + tmp = tp_intset_from_variant (v); + g_assert (tp_intset_is_equal (a, tmp)); g_variant_unref (v); + tp_intset_destroy (tmp); v = tp_intset_to_variant (b); g_variant_ref_sink (v); g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE ("au"))); + tmp = tp_intset_from_variant (v); + g_assert (tp_intset_is_equal (b, tmp)); g_variant_unref (v); + tp_intset_destroy (tmp); } value = tp_g_value_slice_new_take_boxed (TP_TYPE_INTSET, a); -- 1.9.0