[glib/wip/gbytes-takeover: 1/3] GVariant: Make g_variant_new_from_bytes() public, add more GBytes API



commit 567a3c9eb571695ae461a5283c448e66da0c6940
Author: Colin Walters <walters verbum org>
Date:   Tue May 29 17:23:39 2012 -0400

    GVariant: Make g_variant_new_from_bytes() public, add more GBytes API
    
    Now that GBytes has been made public, we should make
    g_variant_new_from_bytes() public too.
    
    While we have the patient open, add g_variant_new_byte_array() which
    accepts GBytes.  The main advantage of this is that we don't have to
    do a copy of the data.
    
    NOTE: Missing gio.symbols and the docs update, will do before commit
    
    https://bugzilla.gnome.org/show_bug.cgi?id=677062

 glib/gvariant-core.c  |   29 +++++++++++++++++++++++++++--
 glib/gvariant-core.h  |    4 ----
 glib/gvariant.c       |   16 ++++++++++++++++
 glib/gvariant.h       |   10 ++++++++++
 glib/tests/gvariant.c |   31 +++++++++++++++++++++++++++++++
 5 files changed, 84 insertions(+), 6 deletions(-)
---
diff --git a/glib/gvariant-core.c b/glib/gvariant-core.c
index 1633463..aeaaf93 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,7 @@ g_variant_alloc (const GVariantType *type,
  * A reference is taken on @bytes.
  *
  * Returns: a new #GVariant with a floating reference
+ * Since: 2.34
  */
 GVariant *
 g_variant_new_from_bytes (const GVariantType *type,
@@ -535,6 +535,8 @@ g_variant_new_from_bytes (const GVariantType *type,
   return value;
 }
 
+/* -- internal -- */
+
 /* < internal >
  * g_variant_new_from_children:
  * @type: a #GVariantType
@@ -862,6 +864,29 @@ 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.34
+ */ 
+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.c b/glib/gvariant.c
index 6ff7714..2a0b275 100644
--- a/glib/gvariant.c
+++ b/glib/gvariant.c
@@ -1250,6 +1250,22 @@ g_variant_new_fixed_array (const GVariantType  *element_type,
   return value;
 }
 
+/**
+ * g_variant_new_byte_array:
+ * @bytes: a #GBytes
+ *
+ * Construct a new #GVariant of type 'ay', using the provided @bytes.
+ * A reference is taken on @bytes.
+ *
+ * Returns: (transfer none): a floating reference to a new byte array #GVariant instance
+ * Since: 2.34
+ */
+GVariant *
+g_variant_new_byte_array (GBytes *bytes)
+{
+  return g_variant_new_from_bytes (G_VARIANT_TYPE ("ay"), bytes, TRUE);
+}
+
 /* String type constructor/getters/validation {{{1 */
 /**
  * g_variant_new_string:
diff --git a/glib/gvariant.h b/glib/gvariant.h
index d0cd857..a058509 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
 
@@ -96,6 +97,8 @@ GVariant *                      g_variant_new_fixed_array               (const G
                                                                          gconstpointer         elements,
                                                                          gsize                 n_elements,
                                                                          gsize                 element_size);
+GLIB_AVAILABLE_IN_2_34
+GVariant *                      g_variant_new_byte_array                (GBytes               *bytes);
 gboolean                        g_variant_get_boolean                   (GVariant             *value);
 guchar                          g_variant_get_byte                      (GVariant             *value);
 gint16                          g_variant_get_int16                     (GVariant             *value);
@@ -159,6 +162,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_34
+GBytes *                        g_variant_get_data_as_bytes             (GVariant             *value);
 void                            g_variant_store                         (GVariant             *value,
                                                                          gpointer              data);
 
@@ -175,6 +180,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_34
+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 7f7d56c..e9c68d8 100644
--- a/glib/tests/gvariant.c
+++ b/glib/tests/gvariant.c
@@ -4111,6 +4111,36 @@ test_fixed_array (void)
   g_variant_unref (a);
 }
 
+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_byte_array (bytes);
+  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)
 {
@@ -4155,6 +4185,7 @@ main (int argc, char **argv)
   g_test_add_func ("/gvariant/lookup", test_lookup);
   g_test_add_func ("/gvariant/compare", test_compare);
   g_test_add_func ("/gvariant/fixed-array", test_fixed_array);
+  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]