gvariant: Make g_variant_new_from_bytes() public
authorColin Walters <walters@verbum.org>
Tue, 23 Oct 2012 14:11:33 +0000 (16:11 +0200)
committerRyan Lortie <desrt@desrt.ca>
Tue, 23 Oct 2012 14:25:49 +0000 (16:25 +0200)
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
glib/glib.symbols
glib/gvariant-core.c
glib/gvariant-core.h
glib/gvariant.h
glib/tests/gvariant.c

index aacb2cc..546bc98 100644 (file)
@@ -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
index 335b553..2ae79d9 100644 (file)
@@ -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
index 1633463..9eecb10 100644 (file)
@@ -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
  *
index 1fd4829..d2a6b46 100644 (file)
 #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,
index 3c92d21..fc66116 100644 (file)
@@ -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,
index 968b703..f7232d1 100644 (file)
@@ -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 ();
 }