Retrieving an Array of Strings, that is attached to aG_HASH_TABLE variant

David Stockwell dstockwell at frequency-one.com
Fri Jun 6 09:22:21 PDT 2008


Simon...

Thank you, thank you, thank you.

I had worked out all of the other variant types (including the boxed Object Path), but for this one, I just could not find the 
"right incantation", and although searching many times, had not found the code example you referred to.

FWIW, in your sample code the first param of the G_VALUE_HOLDS should have been "tmp" (the GValue * returned by 
g_hash_table_lookup).  Not a problem for me, but for others that may be reading the thread...

Thanks again, O DBus Wizard...

DS

----- Original Message ----- 
From: "Simon McVittie" <simon.mcvittie at collabora.co.uk>
To: <dbus at lists.freedesktop.org>
Sent: Friday, June 06, 2008 7:02 AM
Subject: Re: Retrieving an Array of Strings, that is attached to aG_HASH_TABLE variant


On Thu, 05 Jun 2008 at 23:42:00 -0500, David Stockwell wrote:
> (GStrv) gPropValue = (GStrv) g_hash_table_lookup(propTable, "Strings"); // Assume that the "key" is "Strings"

This is wrong - an a{sv} maps to a GHashTable from gchar * to GValue *.

The right way to access it is like this:

GValue *tmp = g_hash_table_lookup (propTable, "Strings");
gchar **gPropValue;

if (G_VALUE_HOLDS (value, G_TYPE_STRV))
  {
    gPropValue = g_value_get_boxed (tmp);
  }
else
  {
    /* caller has done the wrong thing - recover in whatever way you
     * think best (but crashing in response to D-Bus messages is never the
     * right answer) */
    g_debug ("wtf? someone's using the wrong type - ignoring this message");
    return;
  }

Similarly, if you have a (string|integer|...) value in your dictionary,
it'll actually be a GValue containing a (string|integer|...), and you
have to check with (G_VALUE_HOLDS_STRING|G_VALUE_HOLDS_INT|...) that the
caller gave you the right thing, then call
(g_value_get_string|g_value_get_int|...) to get the actual value.

The functions tp_asv_get_uint32 (etc.) in telepathy-glib/dbus.[ch],
available from <http://telepathy.freedesktop.org/releases/telepathy-glib/> or
<http://darcs.collabora.co.uk/>, illustrate how to use an a{sv} in
dbus-glib code.

Regards,
    Simon




More information about the dbus mailing list