[telepathy-glib/master] tp_asv_new()
Davyd Madeley
davyd at madeley.id.au
Mon Mar 30 06:00:45 PDT 2009
---
telepathy-glib/dbus.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
telepathy-glib/dbus.h | 1 +
tests/asv.c | 22 +++++++++++++----
3 files changed, 80 insertions(+), 5 deletions(-)
diff --git a/telepathy-glib/dbus.c b/telepathy-glib/dbus.c
index 8387252..68157e0 100644
--- a/telepathy-glib/dbus.c
+++ b/telepathy-glib/dbus.c
@@ -60,6 +60,8 @@
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
+#include <gobject/gvaluecollector.h>
+
#include <telepathy-glib/errors.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/proxy-subclass.h>
@@ -1344,6 +1346,66 @@ tp_g_value_slice_new_take_object_path (gchar *path)
}
/**
+ * tp_asv_new:
+ * @first_key: the name of the first key (or NULL)
+ * @...: type and value for the first key, followed by a NULL-terminated list
+ * of (key, type, value) tuples
+ *
+ * Creates a new #GHashTable for use with a{sv} maps, containing the values
+ * passed in as parameters.
+ *
+ * The #GHashTable is synonymous with:
+ * <informalexample><programlisting>
+ * GHashTable *asv = g_hash_table_new_full (g_str_hash, g_str_equal,
+ * NULL, (GDestroyNotify) tp_g_value_slice_free);
+ * </programlisting></informalexample>
+ * Followed by manual insertion of each of the parameters.
+ *
+ * Parameters are stored in slice-allocated GValues and should be set using
+ * tp_asv_set_*() and retrieved using tp_asv_get_*().
+ *
+ * Returns: a newly created #GHashTable, free with g_hash_table_destroy().
+ */
+GHashTable *
+tp_asv_new (const char *first_key, ...)
+{
+ va_list var_args;
+ char *key;
+ GType type;
+ GValue *value;
+ char *error = NULL; /* NB: not a GError! */
+
+ /* create a GHashTable */
+ GHashTable *asv = g_hash_table_new_full (g_str_hash, g_str_equal,
+ NULL, (GDestroyNotify) tp_g_value_slice_free);
+
+ va_start (var_args, first_key);
+
+ for (key = (char *) first_key; key != NULL; key = va_arg (var_args, char *))
+ {
+ type = va_arg (var_args, GType);
+
+ value = tp_g_value_slice_new (type);
+ G_VALUE_COLLECT (value, var_args, 0, &error);
+
+ if (error)
+ {
+ g_critical ("key %s: %s", key, error);
+ g_free (error);
+ error = NULL;
+ tp_g_value_slice_free (value);
+ continue;
+ }
+
+ g_hash_table_insert (asv, key, value);
+ }
+
+ va_end (var_args);
+
+ return asv;
+}
+
+/**
* tp_asv_get_boolean:
* @asv: A GHashTable where the keys are strings and the values are GValues
* @key: The key to look up
diff --git a/telepathy-glib/dbus.h b/telepathy-glib/dbus.h
index 1d3bc43..0bfc9b8 100644
--- a/telepathy-glib/dbus.h
+++ b/telepathy-glib/dbus.h
@@ -104,6 +104,7 @@ _tp_asv_size_inline (const GHashTable *asv)
return g_hash_table_size /* */ ((GHashTable *) asv);
}
+GHashTable *tp_asv_new (const char *first_key, ...);
gboolean tp_asv_get_boolean (const GHashTable *asv, const gchar *key,
gboolean *valid);
gpointer tp_asv_get_boxed (const GHashTable *asv, const gchar *key,
diff --git a/tests/asv.c b/tests/asv.c
index 545bb98..e9a9339 100644
--- a/tests/asv.c
+++ b/tests/asv.c
@@ -17,18 +17,20 @@ int main (int argc, char **argv)
g_type_init ();
- hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
- (GDestroyNotify) tp_g_value_slice_free);
+ hash = tp_asv_new (
+ "d:123.2", G_TYPE_DOUBLE, 123.2,
+ "s:test", G_TYPE_STRING, "test",
+ NULL);
- MYASSERT (tp_asv_size (hash) == 0, "%u != 0", tp_asv_size (hash));
+ MYASSERT (tp_asv_size (hash) == 2, "%u != 0", tp_asv_size (hash));
g_hash_table_insert (hash, "d:0", tp_g_value_slice_new_double (0.0));
- MYASSERT (tp_asv_size (hash) == 1, "%u != 1", tp_asv_size (hash));
+ MYASSERT (tp_asv_size (hash) == 3, "%u != 1", tp_asv_size (hash));
g_hash_table_insert (hash, "d:-123", tp_g_value_slice_new_double (-123.0));
- MYASSERT (tp_asv_size (hash) == 2, "%u != 2", tp_asv_size (hash));
+ MYASSERT (tp_asv_size (hash) == 4, "%u != 2", tp_asv_size (hash));
g_hash_table_insert (hash, "b:TRUE", tp_g_value_slice_new_boolean (TRUE));
g_hash_table_insert (hash, "b:FALSE", tp_g_value_slice_new_boolean (FALSE));
@@ -126,6 +128,11 @@ int main (int argc, char **argv)
MYASSERT (!tp_asv_get_boolean (hash, "d:-123", &valid), "");
MYASSERT (valid == FALSE, ": %u", (guint) valid);
+ valid = (gboolean) 123;
+ MYASSERT (!tp_asv_get_boolean (hash, "d:123.2", NULL), "");
+ MYASSERT (!tp_asv_get_boolean (hash, "d:123.2", &valid), "");
+ MYASSERT (valid == FALSE, ": %u", (guint) valid);
+
/* Tests: tp_asv_get_double */
valid = (gboolean) 123;
@@ -182,6 +189,11 @@ int main (int argc, char **argv)
MYASSERT (tp_asv_get_double (hash, "d:-123", &valid) == -123.0, "");
MYASSERT (valid == TRUE, ": %u", (guint) valid);
+ valid = (gboolean) 123;
+ MYASSERT (tp_asv_get_double (hash, "d:123.2", NULL) == 123.2, "");
+ MYASSERT (tp_asv_get_double (hash, "d:123.2", &valid) == 123.2, "");
+ MYASSERT (valid == TRUE, ": %u", (guint) valid);
+
/* Tests: tp_asv_get_int32 */
valid = (gboolean) 123;
--
1.5.6.5
More information about the telepathy-commits
mailing list