Re: Function completion for GVariant maybe types?



g_variant_get_type() and g_variant_type_element() are enough.

I find it not obvious from the documentation that the "element" can also refer
to the "child".
http://developer.gnome.org/glib/2.35/glib-GVariantType.html#g-variant-type-element


How do you think about the following update suggestion?

diff --git a/glib/gvariant.c b/glib/gvariant.c
index c3d1947..43830d7 100644
--- a/glib/gvariant.c
+++ b/glib/gvariant.c
@@ -682,6 +682,22 @@ g_variant_new_maybe (const GVariantType *child_type,
   return value;
 }

+GVariant *
+g_variant_new_nothing_from_type (GVariant const * value)
+{
+  if (value == NULL)
+     return g_variant_new ("ms", NULL);
+  else
+    {
+      GVariantType* type = g_variant_get_type (value);
+
+      if (g_variant_type_is_maybe (type))
+          return g_variant_new_maybe (g_variant_type_element (type), NULL);
+      else
+          return g_variant_new_maybe (type, value);
+    }
+}
+
 /**
  * g_variant_get_maybe:
  * @value: a maybe-typed value
diff --git a/glib/gvariant.h b/glib/gvariant.h
index 9abcc0b..50877f2 100644
--- a/glib/gvariant.h
+++ b/glib/gvariant.h
@@ -128,6 +128,7 @@ gchar **
g_variant_dup_bytestring_array          (GVarian

 GVariant *                      g_variant_new_maybe                     (const
GVariantType   *child_type,

GVariant             *child);
+GVariant *                      g_variant_new_nothing_from_type
(GVariant const * value);
 GVariant *                      g_variant_new_array                     (const
GVariantType   *child_type,

GVariant * const     *children,
                                                                          gsize
                n_children);


I imagine that this approach might need a bit more fine-tuning to avoid
unnecessary type checks.
http://git.gnome.org/browse/glib/tree/glib/gvarianttype.c?id=7936af693477f5b8dfeef4e0d90e6fa4531cf395#n1148

Regards,
Markus


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]