[glib/gvariantiter] Cleanup gvariant.c and add code folding



commit 7e9e1985502bf3a836b97a1879613e66efa01bd8
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Feb 21 15:41:47 2010 -0500

    Cleanup gvariant.c and add code folding

 glib/gvariant.c |  563 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 306 insertions(+), 257 deletions(-)
---
diff --git a/glib/gvariant.c b/glib/gvariant.c
index 9088414..f658488 100644
--- a/glib/gvariant.c
+++ b/glib/gvariant.c
@@ -20,6 +20,8 @@
  * Author: Ryan Lortie <desrt desrt ca>
  */
 
+/* Prologue {{{1 */
+
 #include "config.h"
 
 #include <glib/gvariant-serialiser.h>
@@ -285,6 +287,7 @@
     return val;                                                   \
   }
 
+/* Numeric Type Constructor/Getters {{{1 */
 /* < private >
  * g_variant_new_from_trusted:
  * @type: the #GVariantType
@@ -583,83 +586,7 @@ NUMERIC_TYPE (HANDLE, handle, gint32)
  **/
 NUMERIC_TYPE (DOUBLE, double, gdouble)
 
-/**
- * g_variant_get_type:
- * @value: a #GVariant
- * @returns: a #GVariantType
- *
- * Determines the type of @value.
- *
- * The return value is valid for the lifetime of @value and must not
- * be freed.
- *
- * Since: 2.24
- **/
-const GVariantType *
-g_variant_get_type (GVariant *value)
-{
-  GVariantTypeInfo *type_info;
-
-  g_return_val_if_fail (value != NULL, NULL);
-
-  type_info = g_variant_get_type_info (value);
-
-  return (GVariantType *) g_variant_type_info_get_type_string (type_info);
-}
-
-/**
- * g_variant_get_type_string:
- * @value: a #GVariant
- * @returns: the type string for the type of @value
- *
- * Returns the type string of @value.  Unlike the result of calling
- * g_variant_type_peek_string(), this string is nul-terminated.  This
- * string belongs to #GVariant and must not be freed.
- *
- * Since: 2.24
- **/
-const gchar *
-g_variant_get_type_string (GVariant *value)
-{
-  GVariantTypeInfo *type_info;
-
-  g_return_val_if_fail (value != NULL, NULL);
-
-  type_info = g_variant_get_type_info (value);
-
-  return g_variant_type_info_get_type_string (type_info);
-}
-
-/**
- * g_variant_is_of_type:
- * @value: a #GVariant instance
- * @type: a #GVariantType
- * @returns: %TRUE if the type of @value matches @type
- *
- * Checks if a value has a type matching the provided type.
- *
- * Since: 2.24
- **/
-gboolean
-g_variant_is_of_type (GVariant           *value,
-                      const GVariantType *type)
-{
-  return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
-}
-
-/**
- * g_variant_is_container:
- * @value: a #GVariant instance
- * @returns: %TRUE if @value is a container
- *
- * Checks if @value is a container.
- */
-gboolean
-g_variant_is_container (GVariant *value)
-{
-  return g_variant_type_is_container (g_variant_get_type (value));
-}
-
+/* Container type Constructor / Deconstructors {{{1 */
 /**
  * g_variant_new_maybe:
  * @child_type: the #GVariantType of the child
@@ -773,6 +700,259 @@ g_variant_get_variant (GVariant *value)
 }
 
 /**
+ * g_variant_new_array:
+ * @child_type: the element type of the new array
+ * @children: an array of #GVariant pointers, the children
+ * @n_children: the length of @children
+ * @returns: a new #GVariant array
+ *
+ * Creates a new #GVariant array from @children.
+ *
+ * @child_type must be non-%NULL if @n_children is zero.  Otherwise, the
+ * child type is determined by inspecting the first element of the
+ * @children array.  If @child_type is non-%NULL then it must be a
+ * definite type.
+ *
+ * The items of the array are taken from the @children array.  No entry
+ * in the @children array may be %NULL.
+ *
+ * All items in the array must have the same type, which must be the
+ * same as @child_type, if given.
+ *
+ * Since: 2.24
+ **/
+GVariant *
+g_variant_new_array (const GVariantType *child_type,
+                     GVariant * const   *children,
+                     gsize               n_children)
+{
+  GVariantType *array_type;
+  GVariant **my_children;
+  gboolean trusted;
+  GVariant *value;
+  gsize i;
+
+  g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
+  g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
+  g_return_val_if_fail (child_type == NULL ||
+                        g_variant_type_is_definite (child_type), NULL);
+
+  my_children = g_new (GVariant *, n_children);
+  trusted = TRUE;
+
+  if (child_type == NULL)
+    child_type = g_variant_get_type (children[0]);
+  array_type = g_variant_type_new_array (child_type);
+
+  for (i = 0; i < n_children; i++)
+    {
+      TYPE_CHECK (children[i], child_type, NULL);
+      my_children[i] = g_variant_ref_sink (children[i]);
+      trusted &= g_variant_is_trusted (children[i]);
+    }
+
+  value = g_variant_new_from_children (array_type, my_children,
+                                       n_children, trusted);
+  g_variant_type_free (array_type);
+
+  return value;
+}
+
+/*< private >
+ * g_variant_make_tuple_type:
+ * @children: an array of GVariant *
+ * @n_children: the length of @children
+ *
+ * Return the type of a tuple containing @children as its items.
+ **/
+static GVariantType *
+g_variant_make_tuple_type (GVariant * const *children,
+                           gsize             n_children)
+{
+  const GVariantType **types;
+  GVariantType *type;
+  gsize i;
+
+  types = g_new (const GVariantType *, n_children);
+
+  for (i = 0; i < n_children; i++)
+    types[i] = g_variant_get_type (children[i]);
+
+  type = g_variant_type_new_tuple (types, n_children);
+  g_free (types);
+
+  return type;
+}
+
+/**
+ * g_variant_new_tuple:
+ * @children: the items to make the tuple out of
+ * @n_children: the length of @children
+ * @returns: a new #GVariant tuple
+ *
+ * Creates a new tuple #GVariant out of the items in @children.  The
+ * type is determined from the types of @children.  No entry in the
+ * @children array may be %NULL.
+ *
+ * If @n_children is 0 then the unit tuple is constructed.
+ *
+ * Since: 2.24
+ **/
+GVariant *
+g_variant_new_tuple (GVariant * const *children,
+                     gsize             n_children)
+{
+  GVariantType *tuple_type;
+  GVariant **my_children;
+  gboolean trusted;
+  GVariant *value;
+  gsize i;
+
+  g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
+
+  my_children = g_new (GVariant *, n_children);
+  trusted = TRUE;
+
+  for (i = 0; i < n_children; i++)
+    {
+      my_children[i] = g_variant_ref_sink (children[i]);
+      trusted &= g_variant_is_trusted (children[i]);
+    }
+
+  tuple_type = g_variant_make_tuple_type (children, n_children);
+  value = g_variant_new_from_children (tuple_type, my_children,
+                                       n_children, trusted);
+  g_variant_type_free (tuple_type);
+
+  return value;
+}
+
+/*< private >
+ * g_variant_make_dict_entry_type:
+ * @key: a #GVariant, the key
+ * @val: a #GVariant, the value
+ *
+ * Return the type of a dictionary entry containing @key and @val as its
+ * children.
+ **/
+static GVariantType *
+g_variant_make_dict_entry_type (GVariant *key,
+                                GVariant *val)
+{
+  return g_variant_type_new_dict_entry (g_variant_get_type (key),
+                                        g_variant_get_type (val));
+}
+
+/**
+ * g_variant_new_dict_entry:
+ * @key: a basic #GVariant, the key
+ * @value: a #GVariant, the value
+ * @returns: a new dictionary entry #GVariant
+ *
+ * Creates a new dictionary entry #GVariant.  @key and @value must be
+ * non-%NULL.
+ *
+ * @key must be a value of a basic type (ie: not a container).
+ *
+ * Since: 2.24
+ **/
+GVariant *
+g_variant_new_dict_entry (GVariant *key,
+                          GVariant *value)
+{
+  GVariantType *dict_type;
+  GVariant **children;
+  gboolean trusted;
+
+  g_return_val_if_fail (key != NULL && value != NULL, NULL);
+  g_return_val_if_fail (!g_variant_is_container (key), NULL);
+
+  children = g_new (GVariant *, 2);
+  children[0] = g_variant_ref_sink (key);
+  children[1] = g_variant_ref_sink (value);
+  trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
+
+  dict_type = g_variant_make_dict_entry_type (key, value);
+  value = g_variant_new_from_children (dict_type, children, 2, trusted);
+  g_variant_type_free (dict_type);
+
+  return value;
+}
+
+/**
+ * g_variant_get_fixed_array:
+ * @value: a #GVariant array with fixed-sized elements
+ * @n_elements: a pointer to the location to store the number of items
+ * @element_size: the size of each element
+ * @returns: a pointer to the fixed array
+ *
+ * Provides access to the serialised data for an array of fixed-sized
+ * items.
+ *
+ * @value must be an array with fixed-sized elements.  Numeric types are
+ * fixed-size as are tuples containing only other fixed-sized types.
+ *
+ * @element_size must be the size of a single element in the array.  For
+ * example, if calling this function for an array of 32 bit integers,
+ * you might say <code>sizeof (gint32)</code>.  This value isn't used
+ * except for the purpose of a double-check that the form of the
+ * seralised data matches the caller's expectation.
+ *
+ * @n_elements, which must be non-%NULL is set equal to the number of
+ * items in the array.
+ *
+ * Since: 2.24
+ **/
+gconstpointer
+g_variant_get_fixed_array (GVariant *value,
+                           gsize    *n_elements,
+                           gsize     element_size)
+{
+  GVariantTypeInfo *array_info;
+  gsize array_element_size;
+  gconstpointer data;
+  gsize size;
+
+  TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
+
+  g_return_val_if_fail (n_elements != NULL, NULL);
+  g_return_val_if_fail (element_size > 0, NULL);
+
+  array_info = g_variant_get_type_info (value);
+  g_variant_type_info_query_element (array_info, NULL, &array_element_size);
+
+  g_return_val_if_fail (array_element_size, NULL);
+
+  if G_UNLIKELY (array_element_size != element_size)
+    {
+      if (array_element_size)
+        g_critical ("g_variant_get_fixed_array: assertion "
+                    "`g_variant_array_has_fixed_size (value, element_size)' "
+                    "failed: array size %"G_GSIZE_FORMAT" does not match "
+                    "given element_size %"G_GSIZE_FORMAT".",
+                    array_element_size, element_size);
+      else
+        g_critical ("g_variant_get_fixed_array: assertion "
+                    "`g_variant_array_has_fixed_size (value, element_size)' "
+                    "failed: array does not have fixed size.");
+    }
+
+  data = g_variant_get_data (value);
+  size = g_variant_get_size (value);
+
+  if (size % element_size)
+    *n_elements = 0;
+  else
+    *n_elements = size / element_size;
+
+  if (*n_elements)
+    return data;
+
+  return NULL;
+}
+
+/* String type constructor/getters/validation {{{1 */
+/**
  * g_variant_new_string:
  * @string: a normal C nul-terminated string
  * @returns: a new string #GVariant instance
@@ -834,7 +1014,6 @@ g_variant_is_object_path (const gchar *string)
   return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
 }
 
-
 /**
  * g_variant_new_signature:
  * @signature: a normal C nul-terminated string
@@ -1100,221 +1279,85 @@ g_variant_dup_strv (GVariant *value,
   return strv;
 }
 
+/* Type checking and querying {{{1 */
 /**
- * g_variant_new_array:
- * @child_type: the element type of the new array
- * @children: an array of #GVariant pointers, the children
- * @n_children: the length of @children
- * @returns: a new #GVariant array
- *
- * Creates a new #GVariant array from @children.
- *
- * @child_type must be non-%NULL if @n_children is zero.  Otherwise, the
- * child type is determined by inspecting the first element of the
- * @children array.  If @child_type is non-%NULL then it must be a
- * definite type.
+ * g_variant_get_type:
+ * @value: a #GVariant
+ * @returns: a #GVariantType
  *
- * The items of the array are taken from the @children array.  No entry
- * in the @children array may be %NULL.
+ * Determines the type of @value.
  *
- * All items in the array must have the same type, which must be the
- * same as @child_type, if given.
+ * The return value is valid for the lifetime of @value and must not
+ * be freed.
  *
  * Since: 2.24
  **/
-GVariant *
-g_variant_new_array (const GVariantType *child_type,
-                     GVariant * const   *children,
-                     gsize               n_children)
+const GVariantType *
+g_variant_get_type (GVariant *value)
 {
-  GVariantType *array_type;
-  GVariant **my_children;
-  gboolean trusted;
-  GVariant *value;
-  gsize i;
-
-  g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
-  g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
-  g_return_val_if_fail (child_type == NULL ||
-                        g_variant_type_is_definite (child_type), NULL);
-
-  my_children = g_new (GVariant *, n_children);
-  trusted = TRUE;
-
-  if (child_type == NULL)
-    child_type = g_variant_get_type (children[0]);
-  array_type = g_variant_type_new_array (child_type);
+  GVariantTypeInfo *type_info;
 
-  for (i = 0; i < n_children; i++)
-    {
-      TYPE_CHECK (children[i], child_type, NULL);
-      my_children[i] = g_variant_ref_sink (children[i]);
-      trusted &= g_variant_is_trusted (children[i]);
-    }
+  g_return_val_if_fail (value != NULL, NULL);
 
-  value = g_variant_new_from_children (array_type, my_children,
-                                       n_children, trusted);
-  g_variant_type_free (array_type);
+  type_info = g_variant_get_type_info (value);
 
-  return value;
+  return (GVariantType *) g_variant_type_info_get_type_string (type_info);
 }
 
 /**
- * g_variant_new_tuple:
- * @children: the items to make the tuple out of
- * @n_children: the length of @children
- * @returns: a new #GVariant tuple
- *
- * Creates a new tuple #GVariant out of the items in @children.  The
- * type is determined from the types of @children.  No entry in the
- * @children array may be %NULL.
+ * g_variant_get_type_string:
+ * @value: a #GVariant
+ * @returns: the type string for the type of @value
  *
- * If @n_children is 0 then the unit tuple is constructed.
+ * Returns the type string of @value.  Unlike the result of calling
+ * g_variant_type_peek_string(), this string is nul-terminated.  This
+ * string belongs to #GVariant and must not be freed.
  *
  * Since: 2.24
  **/
-GVariant *
-g_variant_new_tuple (GVariant * const *children,
-                     gsize             n_children)
+const gchar *
+g_variant_get_type_string (GVariant *value)
 {
-  const GVariantType **types;
-  GVariantType *tuple_type;
-  GVariant **my_children;
-  gboolean trusted;
-  GVariant *value;
-  gsize i;
-
-  g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
-
-  types = g_new (const GVariantType *, n_children);
-  my_children = g_new (GVariant *, n_children);
-  trusted = TRUE;
+  GVariantTypeInfo *type_info;
 
-  for (i = 0; i < n_children; i++)
-    {
-      types[i] = g_variant_get_type (children[i]);
-      my_children[i] = g_variant_ref_sink (children[i]);
-      trusted &= g_variant_is_trusted (children[i]);
-    }
+  g_return_val_if_fail (value != NULL, NULL);
 
-  tuple_type = g_variant_type_new_tuple (types, n_children);
-  value = g_variant_new_from_children (tuple_type, my_children,
-                                       n_children, trusted);
-  g_variant_type_free (tuple_type);
-  g_free (types);
+  type_info = g_variant_get_type_info (value);
 
-  return value;
+  return g_variant_type_info_get_type_string (type_info);
 }
 
 /**
- * g_variant_new_dict_entry:
- * @key: a basic #GVariant, the key
- * @value: a #GVariant, the value
- * @returns: a new dictionary entry #GVariant
- *
- * Creates a new dictionary entry #GVariant.  @key and @value must be
- * non-%NULL.
+ * g_variant_is_of_type:
+ * @value: a #GVariant instance
+ * @type: a #GVariantType
+ * @returns: %TRUE if the type of @value matches @type
  *
- * @key must be a value of a basic type (ie: not a container).
+ * Checks if a value has a type matching the provided type.
  *
  * Since: 2.24
  **/
-GVariant *
-g_variant_new_dict_entry (GVariant *key,
-                          GVariant *value)
+gboolean
+g_variant_is_of_type (GVariant           *value,
+                      const GVariantType *type)
 {
-  GVariantType *dict_type;
-  GVariant **children;
-  gboolean trusted;
-
-  g_return_val_if_fail (key != NULL && value != NULL, NULL);
-  g_return_val_if_fail (!g_variant_is_container (key), NULL);
-
-  children = g_new (GVariant *, 2);
-  children[0] = g_variant_ref_sink (key);
-  children[1] = g_variant_ref_sink (value);
-  trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
-
-  dict_type = g_variant_type_new_dict_entry (g_variant_get_type (key),
-                                             g_variant_get_type (value));
-  value = g_variant_new_from_children (dict_type, children, 2, trusted);
-  g_variant_type_free (dict_type);
-
-  return value;
+  return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
 }
 
 /**
- * g_variant_get_fixed_array:
- * @value: a #GVariant array with fixed-sized elements
- * @n_elements: a pointer to the location to store the number of items
- * @element_size: the size of each element
- * @returns: a pointer to the fixed array
- *
- * Provides access to the serialised data for an array of fixed-sized
- * items.
- *
- * @value must be an array with fixed-sized elements.  Numeric types are
- * fixed-size as are tuples containing only other fixed-sized types.
- *
- * @element_size must be the size of a single element in the array.  For
- * example, if calling this function for an array of 32 bit integers,
- * you might say <code>sizeof (gint32)</code>.  This value isn't used
- * except for the purpose of a double-check that the form of the
- * seralised data matches the caller's expectation.
- *
- * @n_elements, which must be non-%NULL is set equal to the number of
- * items in the array.
+ * g_variant_is_container:
+ * @value: a #GVariant instance
+ * @returns: %TRUE if @value is a container
  *
- * Since: 2.24
- **/
-gconstpointer
-g_variant_get_fixed_array (GVariant *value,
-                           gsize    *n_elements,
-                           gsize     element_size)
+ * Checks if @value is a container.
+ */
+gboolean
+g_variant_is_container (GVariant *value)
 {
-  GVariantTypeInfo *array_info;
-  gsize array_element_size;
-  gconstpointer data;
-  gsize size;
-
-  TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
-
-  g_return_val_if_fail (n_elements != NULL, NULL);
-  g_return_val_if_fail (element_size > 0, NULL);
-
-  array_info = g_variant_get_type_info (value);
-  g_variant_type_info_query_element (array_info, NULL, &array_element_size);
-
-  g_return_val_if_fail (array_element_size, NULL);
-
-  if G_UNLIKELY (array_element_size != element_size)
-    {
-      if (array_element_size)
-        g_critical ("g_variant_get_fixed_array: assertion "
-                    "`g_variant_array_has_fixed_size (value, element_size)' "
-                    "failed: array size %"G_GSIZE_FORMAT" does not match "
-                    "given element_size %"G_GSIZE_FORMAT".",
-                    array_element_size, element_size);
-      else
-        g_critical ("g_variant_get_fixed_array: assertion "
-                    "`g_variant_array_has_fixed_size (value, element_size)' "
-                    "failed: array does not have fixed size.");
-    }
-
-  data = g_variant_get_data (value);
-  size = g_variant_get_size (value);
-
-  if (size % element_size)
-    *n_elements = 0;
-  else
-    *n_elements = size / element_size;
-
-  if (*n_elements)
-    return data;
-
-  return NULL;
+  return g_variant_type_is_container (g_variant_get_type (value));
 }
 
+
 /**
  * g_variant_classify:
  * @value: a #GVariant
@@ -1359,6 +1402,7 @@ g_variant_classify (GVariant *value)
   return *g_variant_get_type_string (value);
 }
 
+/* Pretty printer {{{1 */
 /**
  * g_variant_print_string:
  * @value: a #GVariant
@@ -1703,6 +1747,7 @@ g_variant_print (GVariant *value,
                         FALSE);
 };
 
+/* Hash, Equal {{{1 */
 /**
  * g_variant_hash:
  * @value: a basic #GVariant value as a #gconstpointer
@@ -1854,6 +1899,7 @@ g_variant_equal (gconstpointer one,
   return equal;
 }
 
+/* GVariantIter {{{1 */
 /**
  * GVariantIter:
  *
@@ -2205,5 +2251,8 @@ g_variant_iter_next_nofree (GVariantIter *iter,
   return FALSE;
 }
 
+/* Epilogue {{{1 */
 #define __G_VARIANT_C__
 #include "galiasdef.c"
+
+/* vim:set foldmethod=marker: */



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