[telepathy-glib/master] Add tp_value_array_build

Sjoerd Simons sjoerd.simons at collabora.co.uk
Thu Nov 19 06:18:46 PST 2009


---
 docs/reference/telepathy-glib-sections.txt |    1 +
 telepathy-glib/util.c                      |   66 +++++++++++++++++++++++++++-
 telepathy-glib/util.h                      |    4 ++
 3 files changed, 70 insertions(+), 1 deletions(-)

diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt
index a62fd9e..1a54177 100644
--- a/docs/reference/telepathy-glib-sections.txt
+++ b/docs/reference/telepathy-glib-sections.txt
@@ -1087,6 +1087,7 @@ tp_strv_contains
 tp_g_key_file_get_int64
 tp_g_key_file_get_uint64
 tp_g_signal_connect_object
+tp_value_array_build
 </SECTION>
 
 <SECTION>
diff --git a/telepathy-glib/util.c b/telepathy-glib/util.c
index 391ae10..f8d9a68 100644
--- a/telepathy-glib/util.c
+++ b/telepathy-glib/util.c
@@ -28,6 +28,8 @@
  * GLib, but aren't.
  */
 
+#include <gobject/gvaluecollector.h>
+
 #include <telepathy-glib/util-internal.h>
 #include <telepathy-glib/util.h>
 
@@ -963,4 +965,66 @@ _tp_quark_array_copy (const GQuark *quarks)
     }
 
   return array;
-}
\ No newline at end of file
+}
+
+/**
+ * tp_value_array_build:
+ * @length: The number of elements that should be in the array
+ * @type: The type of the first argument.
+ * @...: The value of the first item in the struct followed by a list of type,
+ * value pairs terminated by G_TYPE_INVALID.
+ *
+ * Creates a new #GValueArray for use with structs, containing the values
+ * passed in as parameters. The values are copied or reffed as appropriate for
+ * their type.
+ *
+ * <example>
+ *   <title> using tp_value_array_build</title>
+ *    <programlisting>
+ * GValueArray *array = gabble_value_array_build (2,
+ *    G_TYPE_STRING, host,
+ *    G_TYPE_UINT, port,
+ *    G_TYPE_INVALID);
+ *    </programlisting>
+ * </example>
+ *
+ * Returns: a newly created #GValueArray, free with g_value_array_free.
+ */
+GValueArray *
+tp_value_array_build (gsize length,
+  GType type,
+  ...)
+{
+  GValueArray *arr;
+  GType t;
+  va_list var_args;
+  char *error = NULL;
+
+  arr = g_value_array_new (length);
+
+  va_start (var_args, type);
+
+  for (t = type; t != G_TYPE_INVALID; t = va_arg (var_args, GType))
+    {
+      GValue *v = arr->values + arr->n_values;
+
+      g_value_array_append (arr, NULL);
+
+      g_value_init (v, t);
+
+      G_VALUE_COLLECT (v, var_args, 0, &error);
+
+      if (error != NULL)
+        {
+          g_critical ("%s", error);
+          g_free (error);
+
+          g_value_array_free (arr);
+          return NULL;
+        }
+    }
+
+  g_warn_if_fail (arr->n_values == length);
+
+  return arr;
+}
diff --git a/telepathy-glib/util.h b/telepathy-glib/util.h
index 4c2abe6..250366c 100644
--- a/telepathy-glib/util.h
+++ b/telepathy-glib/util.h
@@ -77,6 +77,10 @@ gulong tp_g_signal_connect_object (gpointer instance,
     const gchar *detailed_signal, GCallback c_handler, gpointer gobject,
     GConnectFlags connect_flags);
 
+GValueArray *tp_value_array_build (gsize length,
+  GType type,
+  ...);
+
 G_END_DECLS
 
 #undef  __TP_IN_UTIL_H__
-- 
1.5.6.5




More information about the telepathy-commits mailing list