Re: Function completion for GVariant maybe types?



I imagine that this approach might need a bit more fine-tuning to avoid
unnecessary type checks.

Can the following function implementation be eventually integrated into your
source code repository?

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

+GVariant *
+g_variant_new_nothing_from_type (GVariant const * value)
+{
+  if (value == NULL)
+    {
+      gchar const type_string[] = "s";
+
+      return g_variant_new_from_children ((GVariantType const *) type_string,
NULL, 0, TRUE);
+    }
+  else
+    {
+      GVariantType const * type = g_variant_get_type (value);
+      gchar const * type_string = (gchar const *) type;
+
+      if (*type_string == G_VARIANT_TYPE_INFO_CHAR_MAYBE)
+          type = (GVariantType const *) ++type_string;
+
+      return g_variant_new_from_children (type, NULL, 0, TRUE);
+    }
+}
+
 /**
  * g_variant_get_maybe:
  * @value: a maybe-typed value


Regards,
Markus



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