[glib] gvariant: Make g_variant_new_from_bytes() public



commit 4fb2d737ac69b60d9aef4f06884bb6a69752c607
Author: Colin Walters <walters verbum org>
Date:   Tue Oct 23 16:11:33 2012 +0200

    gvariant: Make g_variant_new_from_bytes() public
    
    Now that GBytes has been made public, we should make
    g_variant_new_from_bytes() public too.
    
    Add g_variant_get_data_as_bytes() to match.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=677062

 docs/reference/glib/glib-sections.txt |    2 ++
 glib/glib.symbols                     |    2 ++
 glib/gvariant-core.c                  |   31 +++++++++++++++++++++++++++++--
 glib/gvariant-core.h                  |    4 ----
 glib/gvariant.h                       |    8 ++++++++
 glib/tests/gvariant.c                 |   32 ++++++++++++++++++++++++++++++++
 6 files changed, 73 insertions(+), 6 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index aacb2cc..546bc98 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -3137,8 +3137,10 @@ g_variant_get_fixed_array
 <SUBSECTION>
 g_variant_get_size
 g_variant_get_data
+g_variant_get_data_as_bytes
 g_variant_store
 g_variant_new_from_data
+g_variant_new_from_bytes
 g_variant_byteswap
 g_variant_get_normal_form
 g_variant_is_normal_form
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 335b553..2ae79d9 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -1487,6 +1487,7 @@ g_variant_n_children
 g_variant_get_child_value
 g_variant_get_size
 g_variant_get_data
+g_variant_get_data_as_bytes
 g_variant_store
 g_variant_is_normal_form
 g_variant_get_type
@@ -1495,6 +1496,7 @@ g_variant_is_of_type
 g_variant_is_container
 g_variant_classify
 g_variant_compare
+g_variant_new_from_bytes
 g_variant_new_boolean
 g_variant_new_byte
 g_variant_new_int16
diff --git a/glib/gvariant-core.c b/glib/gvariant-core.c
index 1633463..9eecb10 100644
--- a/glib/gvariant-core.c
+++ b/glib/gvariant-core.c
@@ -484,8 +484,7 @@ g_variant_alloc (const GVariantType *type,
   return value;
 }
 
-/* -- internal -- */
-/* < internal >
+/**
  * g_variant_new_from_bytes:
  * @type: a #GVariantType
  * @bytes: a #GBytes
@@ -498,6 +497,8 @@ g_variant_alloc (const GVariantType *type,
  * A reference is taken on @bytes.
  *
  * Returns: a new #GVariant with a floating reference
+ *
+ * Since: 2.36
  */
 GVariant *
 g_variant_new_from_bytes (const GVariantType *type,
@@ -535,6 +536,8 @@ g_variant_new_from_bytes (const GVariantType *type,
   return value;
 }
 
+/* -- internal -- */
+
 /* < internal >
  * g_variant_new_from_children:
  * @type: a #GVariantType
@@ -862,6 +865,30 @@ g_variant_get_data (GVariant *value)
 }
 
 /**
+ * g_variant_get_data_as_bytes:
+ * @value: a #GVariant
+ *
+ * Returns a pointer to the serialised form of a #GVariant instance.
+ * The semantics of this function are exactly the same as
+ * g_variant_get_data(), except that the returned #GBytes holds
+ * a reference to the variant data.
+ *
+ * Returns: (transfer full): A new #GBytes representing the variant data
+ *
+ * Since: 2.36
+ */ 
+GBytes *
+g_variant_get_data_as_bytes (GVariant *value)
+{
+  g_variant_lock (value);
+  g_variant_ensure_serialised (value);
+  g_variant_unlock (value);
+
+  return g_bytes_ref (value->contents.serialised.bytes);
+}
+
+
+/**
  * g_variant_n_children:
  * @value: a container #GVariant
  *
diff --git a/glib/gvariant-core.h b/glib/gvariant-core.h
index 1fd4829..d2a6b46 100644
--- a/glib/gvariant-core.h
+++ b/glib/gvariant-core.h
@@ -26,10 +26,6 @@
 #include <glib/gbytes.h>
 
 /* gvariant-core.c */
-G_GNUC_INTERNAL
-GVariant *              g_variant_new_from_bytes                        (const GVariantType *type,
-                                                                         GBytes             *bytes,
-                                                                         gboolean            trusted);
 
 G_GNUC_INTERNAL
 GVariant *              g_variant_new_from_children                     (const GVariantType  *type,
diff --git a/glib/gvariant.h b/glib/gvariant.h
index 3c92d21..fc66116 100644
--- a/glib/gvariant.h
+++ b/glib/gvariant.h
@@ -29,6 +29,7 @@
 
 #include <glib/gvarianttype.h>
 #include <glib/gstring.h>
+#include <glib/gbytes.h>
 
 G_BEGIN_DECLS
 
@@ -159,6 +160,8 @@ gconstpointer                   g_variant_get_fixed_array               (GVarian
 
 gsize                           g_variant_get_size                      (GVariant             *value);
 gconstpointer                   g_variant_get_data                      (GVariant             *value);
+GLIB_AVAILABLE_IN_2_36
+GBytes *                        g_variant_get_data_as_bytes             (GVariant             *value);
 void                            g_variant_store                         (GVariant             *value,
                                                                          gpointer              data);
 
@@ -175,6 +178,11 @@ gboolean                        g_variant_equal                         (gconstp
 GVariant *                      g_variant_get_normal_form               (GVariant             *value);
 gboolean                        g_variant_is_normal_form                (GVariant             *value);
 GVariant *                      g_variant_byteswap                      (GVariant             *value);
+
+GLIB_AVAILABLE_IN_2_36
+GVariant *                      g_variant_new_from_bytes                (const GVariantType   *type,
+                                                                         GBytes               *bytes,
+                                                                         gboolean              trusted);
 GVariant *                      g_variant_new_from_data                 (const GVariantType   *type,
                                                                          gconstpointer         data,
                                                                          gsize                 size,
diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c
index 968b703..f7232d1 100644
--- a/glib/tests/gvariant.c
+++ b/glib/tests/gvariant.c
@@ -4254,6 +4254,36 @@ test_checksum_nested (void)
 			       "(yvu)", 254, g_variant_new ("(^as)", strv), 42);
 }
 
+static void
+test_gbytes (void)
+{
+  GVariant *a;
+  GBytes *bytes;
+  GBytes *bytes2;
+  const guint8 values[5] = { 1, 2, 3, 4, 5 };
+  const guint8 *elts;
+  gsize n_elts;
+  gint i;
+
+  bytes = g_bytes_new (&values, 5);
+  a = g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes, TRUE);
+  g_bytes_unref (bytes);
+  n_elts = 0;
+  elts = g_variant_get_fixed_array (a, &n_elts, sizeof (guint8));
+  g_assert (n_elts == 5);
+  for (i = 0; i < 5; i++)
+    g_assert_cmpint (elts[i], ==, i + 1);
+
+  bytes2 = g_variant_get_data_as_bytes (a);
+  g_variant_unref (a);
+
+  bytes = g_bytes_new (&values, 5);
+  g_assert (g_bytes_equal (bytes, bytes2));
+
+  g_bytes_unref (bytes);
+  g_bytes_unref (bytes2);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -4303,5 +4333,7 @@ main (int argc, char **argv)
   g_test_add_func ("/gvariant/checksum-basic", test_checksum_basic);
   g_test_add_func ("/gvariant/checksum-nested", test_checksum_nested);
 
+  g_test_add_func ("/gvariant/gbytes", test_gbytes);
+
   return g_test_run ();
 }



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