From e7942fefa6688b738dbf2688b505f34846f65f0c Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Thu, 15 May 2014 09:54:07 +0200 Subject: [PATCH 1/3] add tp_intset_to_variant() https://bugs.freedesktop.org/show_bug.cgi?id=77773 --- .../telepathy-glib/telepathy-glib-sections.txt | 1 + telepathy-glib/intset.c | 29 ++++++++++++++++++++++ telepathy-glib/intset.h | 2 ++ tests/intset.c | 14 +++++++++++ 4 files changed, 46 insertions(+) diff --git a/docs/reference/telepathy-glib/telepathy-glib-sections.txt b/docs/reference/telepathy-glib/telepathy-glib-sections.txt index d7f38e2..3ffca48 100644 --- a/docs/reference/telepathy-glib/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib/telepathy-glib-sections.txt @@ -1783,6 +1783,7 @@ tp_intset_is_member tp_intset_foreach tp_intset_to_array tp_intset_from_array +tp_intset_to_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 363cc0e..7b3c58b 100644 --- a/telepathy-glib/intset.c +++ b/telepathy-glib/intset.c @@ -355,6 +355,12 @@ tp_intset_to_array (const TpIntset *set) return array; } +static void +addint_builder (guint i, gpointer data) +{ + GVariantBuilder *builder = (GVariantBuilder *) data; + g_variant_builder_add (builder, "u", i); +} /** * tp_intset_from_array: @@ -383,6 +389,29 @@ tp_intset_from_array (const GArray *array) return set; } +/** + * tp_intset_to_variant: + * @set: set to convert + * + * + * + * Returns: (transfer none): a new floating #GVariant of type + * 'au' (array of uint32) containing the same integers as @set. + */ +GVariant * +tp_intset_to_variant (const TpIntset *set) +{ + GVariantBuilder builder; + + g_return_val_if_fail (set != NULL, NULL); + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("au")); + + tp_intset_foreach (set, addint_builder, &builder); + + return g_variant_builder_end (&builder); +} + /* 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 a3038b7..496bcfb 100644 --- a/telepathy-glib/intset.h +++ b/telepathy-glib/intset.h @@ -56,6 +56,8 @@ void tp_intset_foreach (const TpIntset *set, TpIntFunc func, 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; + 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 5f9e69a..3695f1e 100644 --- a/tests/intset.c +++ b/tests/intset.c @@ -200,6 +200,20 @@ test (Fixture *f, tmp = NULL; } + { + GVariant *v; + + v = tp_intset_to_variant (a); + g_variant_ref_sink (v); + g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE ("au"))); + g_variant_unref (v); + + v = tp_intset_to_variant (b); + g_variant_ref_sink (v); + g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE ("au"))); + g_variant_unref (v); + } + value = tp_g_value_slice_new_take_boxed (TP_TYPE_INTSET, a); copy = g_value_dup_boxed (value); -- 1.9.0