GVariant variable arguments, tests, cleanups

Merge GVariant variable arguments support and put it under tests.

Also, remove the hack of the test case directly '#include'ing .c files
from glib/.  Instead, create a non-installed gvariant-internal.h that
the tests can include and make the symbols in it visible on the symbol
table of the shared library.  These symbols (as they are present in no
installed header files) are not part of the API of GLib.

Increase test coverage in a few other areas.
This commit is contained in:
Ryan Lortie 2010-03-08 10:30:59 -05:00
parent 8cd5c94f42
commit 75f761bcaa
12 changed files with 3110 additions and 208 deletions

View File

@ -121,6 +121,7 @@ synchronize their operation.
<xi:include href="xml/allocators.xml" />
<xi:include href="xml/gvarianttype.xml"/>
<xi:include href="xml/gvariant.xml"/>
<xi:include href="gvariant-varargs.xml"/>
</chapter>
<chapter id="tools">

View File

@ -2798,6 +2798,12 @@ g_variant_is_container
g_variant_classify
GVariantClass
<SUBSECTION>
g_variant_get
g_variant_get_va
g_variant_new
g_variant_new_va
<SUBSECTION>
g_variant_new_boolean
g_variant_new_byte
@ -2844,6 +2850,7 @@ g_variant_new_dict_entry
g_variant_get_maybe
g_variant_n_children
g_variant_get_child_value
g_variant_get_child
g_variant_get_fixed_array
<SUBSECTION>
@ -2878,9 +2885,13 @@ g_variant_builder_new
g_variant_builder_init
g_variant_builder_clear
g_variant_builder_add_value
g_variant_builder_add
g_variant_builder_end
g_variant_builder_open
g_variant_builder_close
<SUBSECTION Private>
g_variant_type_checked_
</SECTION>

View File

@ -0,0 +1,987 @@
<?xml version='1.0' encoding='utf-8'?>
<refentry id='gvariant-format-strings'>
<refmeta>
<refentrytitle>GVariant Format Strings</refentrytitle>
</refmeta>
<refsect1>
<title>Variable Argument Conversions</title>
<para>
This page attempts to document how to perform variable argument conversions with GVariant.
</para>
<para>
Conversions occur according to format strings. A format string is a two-way mapping between a single
<link linkend='GVariant'>GVariant</link> value and one or more C values.
</para>
<para>
A conversion from C values into a <link linkend='GVariant'>GVariant</link> value is made using the
<link linkend='g-variant-new'><function>g_variant_new()</function></link> function. A conversion from a
<link linkend='GVariant'>GVariant</link> into C values is made using the
<link linkend='g-variant-get'><function>g_variant_get()</function></link> function.
</para>
</refsect1>
<refsect1>
<title>Syntax</title>
<para>
This section exhaustively describes all possibilities for GVariant format strings. There are no valid forms of
format strings other than those described here. Please note that the format string syntax is likely to expand in the
future.
</para>
<para>
Valid format strings have one of the following forms:
</para>
<itemizedlist>
<listitem>
<para>any type string</para>
</listitem>
<listitem>
<para>
a type string prefixed with a '<literal>@</literal>'
</para>
</listitem>
<listitem>
<para>
a type string of a concrete, fixed-sized type, prefixed with a '<literal>&amp;</literal>'
</para>
</listitem>
<listitem>
<para>
a type string of a concrete, fixed-sized type, prefixed with a '<literal>&amp;a</literal>'
</para>
</listitem>
<listitem>
<para>
'<literal>&amp;s</literal>' '<literal>&amp;o</literal>', '<literal>&amp;g</literal>', '<literal>^as</literal>',
'<literal>^ao</literal>', '<literal>^ag</literal>', '<literal>^a&amp;s</literal>', '<literal>^a&amp;o</literal>' or
'<literal>^a&amp;g</literal>'
</para>
</listitem>
<listitem>
<para>
any conversion string, prefixed with an '<literal>m</literal>'
</para>
</listitem>
<listitem>
<para>
a sequence of zero or more format strings strings, concatenated and enclosed in parentheses
</para>
</listitem>
<listitem>
<para>
an opening brace, followed by two format strings, followed by a closing brace (subject to the constraint that the
first format string correspond to a type valid for use as the key type of a dictionary)
</para>
</listitem>
</itemizedlist>
</refsect1>
<refsect1>
<title>Symbols</title>
<para>
The following table describes the rough meaning of symbols that may appear inside a GVariant format string. Each
symbol is described in detail in its own section, including usage examples.
</para>
<informaltable>
<tgroup cols='2'>
<colspec colname='col_0'/>
<colspec colname='col_1'/>
<tbody>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>Symbol</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>Meaning</emphasis>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>, <literal>i</literal>,
<literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>, <literal>d</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used for building or deconstructing boolean, byte and numeric types. See
<link linkend='gvariant-format-strings-numeric-types'>Numeric Types</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>s</literal>, <literal>o</literal>, <literal>g</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used for building or deconstructing string types. See
<link linkend='gvariant-format-strings-strings'>Strings</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'><literal>v</literal></emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used for building or deconstructing variant types. See
<link linkend='gvariant-format-strings-variant'>Variants</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>a</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used for building or deconstructing arrays. See
<link linkend='gvariant-format-strings-arrays'>Arrays</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>m</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used for building or deconstructing maybe types. See
<link linkend='gvariant-format-strings-maybe-types'>Maybe Types</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>()</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used for building or deconstructing tuples. See
<link linkend='gvariant-format-strings-tuples'>Tuples</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>{}</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used for building or deconstructing dictionary entries. See
<link linkend='gvariant-format-strings-dictionaries'>Dictionaries</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>@</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used as a prefix on a GVariant type string (not format string). Denotes that a pointer to a
<link linkend='GVariant'>GVariant</link> should be used in place of the normal C type or types. For
<link linkend='g-variant-new'><function>g_variant_new()</function></link> this means that you must pass a
non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> <code>(<link linkend='GVariant'>GVariant</link>
*)</code>. For <link linkend='g-variant-get'><function>g_variant_get()</function></link> this means that you
must pass a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> for the value to be returned
by reference or <link linkend='NULL--CAPS'><literal>NULL</literal></link> to ignore the value. See
<link linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>*</literal>, <literal>?</literal>, <literal>r</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Exactly equivalent to <literal>@*</literal>, <literal>@?</literal> and <literal>@r</literal>. Provided only for
completeness so that all GVariant type strings can be used also as format strings. See <link
linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'><literal>&amp;</literal></emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used as a prefix on a GVariant type string (not format string). Denotes that a C pointer to serialised data
should be used in place of the normal C type. See
<link linkend='gvariant-format-strings-pointers'>Pointers</link> below.
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'><literal>^</literal></emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
Used as a prefix on some specific types of format strings. See
<link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
</para>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<refsect2 id='gvariant-format-strings-numeric-types'>
<title>Numeric Types</title>
<para>
<emphasis role='strong'>
Characters: <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>,
<literal>i</literal>, <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>,
<literal>d</literal>
</emphasis>
</para>
<para>
Variable argument conversions from numeric types work in the most obvious way possible. Upon encountering one of
these characters, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes the equivalent C
type as an argument. <link linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to
the equivalent C type (or <link linkend='NULL--CAPS'><literal>NULL</literal></link> to ignore the value).
</para>
<para>
The equivalent C types are as follows:
</para>
<informaltable>
<tgroup cols='2'>
<colspec colname='col_0'/><colspec colname='col_1'/>
<tbody>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>Character</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>Equivalent C type</emphasis>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>b</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='gboolean'><type>gboolean</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>y</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='guchar'><type>guchar</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>n</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='gint16'><type>gint16</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>q</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='guint16'><type>guint16</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>i</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='gint32'><type>gint32</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>u</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='guint32'><type>guint32</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>x</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='gint64'><type>gint64</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>t</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='guint64'><type>guint64</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>h</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='gint32'><type>gint32</type></link>
</para>
</entry>
</row>
<row rowsep='1'>
<entry colsep='1' rowsep='1'>
<para>
<emphasis role='strong'>
<literal>d</literal>
</emphasis>
</para>
</entry>
<entry colsep='1' rowsep='1'>
<para>
<link linkend='gdouble'><type>gdouble</type></link>
</para>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
Note that in C, small integer types in variable argument lists are promoted up to <link
linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
read back accordingly. <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
currently suported. This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
'<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
'<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'. Specifically, you can use integer
literals with these characters.
</para>
<para>
When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
provide is 64 bit. This means that you should use a cast or make use of the
<link linkend='G-GINT64-CONSTANT--CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
<link linkend='G-GUINT64-CONSTANT--CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
</para>
<para>
No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariant *value1, *value2, *value3, *value4;
value1 = g_variant_new ("y", 200);
value2 = g_variant_new ("b", TRUE);
value3 = g_variant_new ("d", 37.5):
value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
{
gdouble floating;
gboolean truth;
gint64 bignum;
g_variant_get (value1, "y", NULL); /* ignore the value. */
g_variant_get (value2, "b", &truth);
g_variant_get (value3, "d", &floating);
g_variant_get (value4, "x", &bignum);
}]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-strings'>
<title>Strings</title>
<para>
<emphasis role='strong'>
Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
</emphasis>
</para>
<para>
String conversions occur to and from standard nul-terminated C strings. Upon encountering an
'<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
<link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
<link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
<link linkend='NULL--CAPS'><literal>NULL</literal></link> is not a valid string. If the '<literal>o</literal>' or
'<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
object path or DBus type signature, respectively.
</para>
<para>
Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
<code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
<link linkend='g-free'><function>g_free()</function></link>.
<link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
string should be ignored (in which case no copy is made).
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariant *value1, *value2, *value3;
value1 = g_variant_new ("s", "hello world!");
value2 = g_variant_new ("o", "/must/be/a/valid/path");
value3 = g_variant_new ("g", "iias");
#if 0
g_variant_new ("s", NULL); /* not valid: NULL is not a string. */
#endif
{
gchar *result;
g_variant_get (value1, "s", &result);
g_print ("It was '%s'\n", result);
g_free (result);
}]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-variants'>
<title>Variants</title>
<para>
<emphasis role='strong'>
Characters: <literal>v</literal>
</emphasis>
</para>
<para>
Upon encountering a '<literal>v</literal>',
<link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
linkend='GVariant'>GVariant</link> *)</code>. The value of the
<link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
</para>
<para>
Upon encountering a '<literal>v</literal>', <link
linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
<code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
</code>). It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
containing the contents of the variant value. It is appropriate to free this reference using
<link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
<link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
ignored (in which case no new reference is created).
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariant *x, *y;
/* the following two lines are equivalent: */
x = g_variant_new ("v", y);
x = g_variant_new_variant (y);
/* as are these: */
g_variant_get (x, "v", &y);
y = g_variant_get_variant (x);]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-arrays'>
<title>Arrays</title>
<para>
<emphasis role='strong'>
Characters: <literal>a</literal>
</emphasis>
</para>
<para>
Upon encountering an '<literal>a</literal>' character followed by a type string,
<link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
<code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
for an array of the type given in the type string. The builder will have
<link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
result will be used as the value. As a special exception, if the given type string is a definite type, then
<link linkend='NULL--CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
</para>
<para>
Upon encountering an '<literal>a</literal>' character followed by a type string,
<link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
<code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
<code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
This iterator should be freed when you are done with it, using
<link linkend='g-variant-iter-unref'><function>g_variant_iter_unref()</function></link>.
<link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
should be ignored.
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariantBuilder *builder;
GVariant *value;
builder = g_variant_builder_new (G_VARIANT_TYPE_CLASS_ARRAY, NULL);
g_variant_builder_add (builder, "s", "when");
g_variant_builder_add (builder, "s", "in");
g_variant_builder_add (builder, "s", "the");
g_variant_builder_add (builder, "s", "course");
value = g_variant_new ("as", builder);
{
GVariantIter *iter;
gchar *str;
g_variant_get (value, "as", &iter);
while (g_variant_iter_loop (iter, "s", &str))
g_print ("%s\n");
g_variant_iter_unref (iter);
}]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-maybe-types'>
<title>Maybe Types</title>
<para>
<emphasis role='strong'>
Characters: <literal>m</literal>
</emphasis>
</para>
<para>
Maybe types are handled in two separate ways depending on the format string that follows the
'<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
'<literal>m</literal>'.
</para>
<para>
The first way is used with format strings starting with '<literal>s</literal>', '<literal>o</literal>',
'<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>', '<literal>*</literal>',
'<literal>?</literal>', '<literal>r</literal>', '<literal>&amp;</literal>', or '<literal>^</literal>'. In all of
these cases, for non-maybe types, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes
a pointer to a non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> value and
<link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
<link linkend='NULL--CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
</para>
<para>
The second way is used with all other format strings. For
<link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
<link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
<link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
<code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
collected for the equivalent non-maybe type will be collected.
</para>
<para>
If <link linkend='FALSE--CAPS'><literal>FALSE</literal></link> is given to
<link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
the collected arguments are ignored. Otherwise (if <link linkend='TRUE--CAPS'><literal>TRUE</literal></link> was
given), the arguments are used in the normal way to create the Just value.
</para>
<para>
If <link linkend='NULL--CAPS'><literal>NULL</literal></link> is given to
<link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
whether the value was Just. In the case that the value was Just, the
<link linkend='gboolean'><type>gboolean</type></link> will be set to
<link linkend='TRUE--CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
<link linkend='FALSE--CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
but have their values set to binary zero.
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
value1 = g_variant_new ("ms", "Hello world");
value2 = g_variant_new ("ms", NULL);
value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
{
GVariant *contents;
const gchar *cstr;
gboolean just;
gint32 x, y;
gchar *str;
g_variant_get (value1, "ms", &str);
if (str != NULL)
g_print ("str: %s\n", str);
else
g_print ("it was null\n");
g_free (str);
g_variant_get (value2, "m&s", &cstr);
if (cstr != NULL)
g_print ("str: %s\n", cstr);
else
g_print ("it was null\n");
/* don't free 'cstr' */
/* NULL passed for the gboolean *, but two 'gint32 *' still collected */
g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
g_print ("string is %s\n", str);
g_free (str);
/* note: &s used, so g_free() not needed */
g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
if (just)
g_print ("it was (%d, %d)\n", x, y);
else
g_print ("it was null\n");
g_print ("string is %s\n", cstr);
/* don't free 'cstr' */
g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
if (contents != NULL)
{
g_variant_get (contents, "(ii)", &x, &y);
g_print ("it was (%d, %d)\n", x, y);
g_variant_unref (contents);
}
else
g_print ("it was null\n");
}]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-tuples'>
<title>Tuples</title>
<para>
<emphasis role='strong'>
Characters: <code>()</code>
</emphasis>
</para>
<para>
Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariant *value1, *value2;
value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
value2 = g_variant_new ("()");
{
gchar *string;
gint x, y;
g_variant_get (value1, "(s(ii))", &string, &x, &y);
g_print ("%s, %d, %d\n", string, x, y);
g_free (string);
g_variant_get (value2, "()"); /* do nothing... */
}]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-dictionaries'>
<title>Dictionaries</title>
<para>
<emphasis role='strong'>
Characters: <code>{}</code>
</emphasis>
</para>
<para>
Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariantBuilder *b;
GVariant *dict;
b = g_variant_builder_new (G_VARIANT_TYPE_CLASS_ARRAY, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
dict = g_variant_builder_end (b);]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-gvariant'>
<title>GVariant *</title>
<para>
<emphasis role='strong'>
Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
</emphasis>
</para>
<para>
Upon encountering a '<literal>@</literal>' in front of a type string,
<link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer to a
<link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
type string following the '<literal>@</literal>'. '<literal>*</literal>' is
the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
'<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
<link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
'<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
</para>
<para>
Upon encountering a '<literal>@</literal>' in front of a type string,
<link linkend='g-variant-get'><function>g_variant_get()</function></link>
takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
<code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
<link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
C types in the usual way). <link linkend='NULL--CAPS'><literal>NULL</literal></link> can be given to ignore the
value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
what is stated above.
</para>
<para>
You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
improvements in type safety and code self-documentation.
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[GVariant *value1, *value2;
value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
/* note: consumes floating reference count on 'value1' */
value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
{
const gchar *string;
GVariant *tmp;
gsize length;
gint x, y, z;
g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
string = g_variant_get_string (tmp, &length);
g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
g_variant_unref (tmp);
/* quick way to skip all the values in a tuple */
g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
g_print ("i only got the string: %s\n", string);
g_free (string);
}]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-pointers'>
<title>Pointers</title>
<para>
<emphasis role='strong'>
Characters: <code>&amp;</code>
</emphasis>
</para>
<para>
The '<code>&amp;</code>' character is used to indicate that serialised data should be directly exchanged via a
pointer.
</para>
<para>
Currently there is only one use for this character -- to avoid making a copy of a string when using
<link linkend='g-variant-get'><function>g_variant_get()</function></link>.
</para>
<para>
The three cases are as follows:
</para>
<para>
Currently, the only use for this character is when it is applied to a string (ie: '<literal>&amp;s</literal>',
'<literal>&amp;o</literal>' or '<code>&amp;g</code>'). For
<link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
always be properly nul-terminated.
</para>
<refsect3>
<title>Examples</title>
<informalexample><programlisting>
<![CDATA[{
const gchar *str;
GVariant *value;
value = g_variant_new ("&s", "hello world");
str = g_variant_get ("&s", &str);
g_print ("string is: %s\n", str);
/* no need to free str */
}]]></programlisting></informalexample>
</refsect3>
</refsect2>
<refsect2 id='gvariant-format-strings-convenience'>
<title>Convenience Conversions</title>
<para>
<emphasis role='strong'>
Characters: <literal>^</literal>
</emphasis>
</para>
<para>
The '<literal>^</literal>' character currently only has one purpose: to convert to and from
<link linkend='G-TYPE-STRV'><literal>G_TYPE_STRV</literal></link> type arrays of strings. It is always used with
arrays of strings (or other string types). It has two forms.
</para>
<itemizedlist>
<listitem>
<para>
'<literal>^as</literal>' (or <literal>o</literal> or <literal>g</literal>)
</para>
</listitem>
<listitem>
<para>
'<literal>^a&amp;s</literal>' (or <literal>o</literal> or <literal>g</literal>)
</para>
</listitem>
</itemizedlist>
<para>
When used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> both forms are equivalent.
A <code>(const <link linkend='gchar'>gchar</link> * const *)</code> is collected. This must be a pointer to the
array of <link linkend='NULL--CAPS'><literal>NULL</literal></link>-terminated pointers to strings. This array is
converted to a <link linkend='GVariant'><type>GVariant</type></link> instance. Copies are made, so the original
array may be freed immediately.
</para>
<para>
When used with <link linkend='g-variant-get'><function>g_variant_get()</function></link> the two forms have
different meaning. Both return a freshly allocated
<link linkend='NULL--CAPS'><literal>NULL</literal></link>-terminated array of pointers to strings. In the case of
'<literal>^as</literal>', the strings are owned by the caller -- it is appropriate to free the array with
<link linkend='g-strfreev'><function>g_strfreev()</function></link>. In the case of '<literal>^a&amp;s</literal>',
a shallow copy is made; the strings themselves are embedded in the serialised data and owned by the original
<link linkend='GVariant'><type>GVariant</type></link> instance -- it is only appropriate to free the outer array
with <link linkend='g-free'><function>g_free()</function></link>.
</para>
</refsect2>
</refsect1>
</refentry>

View File

@ -1767,8 +1767,6 @@ g_variant_iter_free
g_variant_iter_init
g_variant_iter_n_children
g_variant_iter_new
g_variant_iter_next
g_variant_iter_loop
g_variant_iter_next_value
g_variant_builder_add_value
@ -1780,6 +1778,52 @@ g_variant_builder_end
g_variant_builder_new
g_variant_builder_unref
g_variant_builder_ref
g_variant_new_va
g_variant_get_va
g_variant_new
g_variant_get
g_variant_builder_add
g_variant_get_child
g_variant_iter_next
g_variant_iter_loop
#endif
#endif
#if IN_HEADER(__G_VARIANT_TYPE_INFO_H__)
#if IN_FILE(__G_VARIANT_TYPE_INFO_C__)
g_variant_type_info_get_type_string
g_variant_type_info_query
g_variant_type_info_element
g_variant_type_info_query_element
g_variant_type_info_n_members
g_variant_type_info_member_info
g_variant_type_info_get
g_variant_type_info_ref
g_variant_type_info_unref
g_variant_type_info_assert_no_infos
#endif
#endif
#if IN_HEADER(__G_VARIANT_SERIALISER_H__)
#if IN_FILE(__G_VARIANT_SERIALISER_C__)
g_variant_serialised_byteswap
g_variant_serialised_get_child
g_variant_serialised_is_normal
g_variant_serialised_n_children
g_variant_serialiser_is_object_path
g_variant_serialiser_is_signature
g_variant_serialiser_is_string
g_variant_serialiser_needed_size
g_variant_serialiser_serialise
#endif
#endif
#if IN_HEADER(__G_VARIANT_INTERNAL_H__)
#if IN_FILE(__G_VARIANT_C__)
g_variant_format_string_scan_type
g_variant_format_string_scan
#endif
#endif

49
glib/gvariant-internal.h Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright © 2007, 2008 Ryan Lortie
* Copyright © 2009, 2010 Codethink Limited
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the licence, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
/* The purpose of this header is to allow certain internal symbols of
* GVariant to be put under test cases.
*/
#ifndef __G_VARIANT_INTERNAL_H__
#define __G_VARIANT_INTERNAL_H__
#ifndef GLIB_COMPILATION
#define GLIB_COMPILATION
#endif
#include <glib/gvarianttype.h>
#include <glib/gtypes.h>
#include "gvariant-serialiser.h"
#include "gvarianttypeinfo.h"
gboolean g_variant_format_string_scan (const gchar *string,
const gchar *limit,
const gchar **endptr);
GVariantType * g_variant_format_string_scan_type (const gchar *string,
const gchar *limit,
const gchar **endptr);
#endif /* __G_VARIANT_INTERNAL_H__ */

View File

@ -1654,4 +1654,8 @@ g_variant_serialiser_is_signature (gconstpointer data,
return TRUE;
}
/* Epilogue {{{1 */
#define __G_VARIANT_SERIALISER_C__
#include "galiasdef.c"
/* vim:set foldmethod=marker: */

View File

@ -33,9 +33,7 @@ typedef struct
} GVariantSerialised;
/* deserialisation */
G_GNUC_INTERNAL
gsize g_variant_serialised_n_children (GVariantSerialised container);
G_GNUC_INTERNAL
GVariantSerialised g_variant_serialised_get_child (GVariantSerialised container,
gsize index);
@ -43,32 +41,25 @@ GVariantSerialised g_variant_serialised_get_child (GVarian
typedef void (*GVariantSerialisedFiller) (GVariantSerialised *serialised,
gpointer data);
G_GNUC_INTERNAL
gsize g_variant_serialiser_needed_size (GVariantTypeInfo *info,
GVariantSerialisedFiller gsv_filler,
const gpointer *children,
gsize n_children);
G_GNUC_INTERNAL
void g_variant_serialiser_serialise (GVariantSerialised container,
GVariantSerialisedFiller gsv_filler,
const gpointer *children,
gsize n_children);
/* misc */
G_GNUC_INTERNAL
gboolean g_variant_serialised_is_normal (GVariantSerialised value);
G_GNUC_INTERNAL
void g_variant_serialised_byteswap (GVariantSerialised value);
/* validation of strings */
G_GNUC_INTERNAL
gboolean g_variant_serialiser_is_string (gconstpointer data,
gsize size);
G_GNUC_INTERNAL
gboolean g_variant_serialiser_is_object_path (gconstpointer data,
gsize size);
G_GNUC_INTERNAL
gboolean g_variant_serialiser_is_signature (gconstpointer data,
gsize size);

File diff suppressed because it is too large Load Diff

View File

@ -114,6 +114,10 @@ GVariant * g_variant_new_dict_entry (GVarian
GVariant * g_variant_get_maybe (GVariant *value);
gsize g_variant_n_children (GVariant *value);
void g_variant_get_child (GVariant *value,
gsize index_,
const gchar *format_string,
...);
GVariant * g_variant_get_child_value (GVariant *value,
gsize index_);
gconstpointer g_variant_get_fixed_array (GVariant *value,
@ -178,6 +182,20 @@ void g_variant_builder_add (GVarian
const gchar *format_string,
...);
GVariant * g_variant_new (const gchar *format_string,
...);
void g_variant_get (GVariant *value,
const gchar *format_string,
...);
GVariant * g_variant_new_va (const gchar *format_string,
const gchar **endptr,
va_list *app);
void g_variant_get_va (GVariant *value,
const gchar *format_string,
const gchar **endptr,
va_list *app);
G_END_DECLS
#endif /* __G_VARIANT_H__ */

View File

@ -861,6 +861,11 @@ g_variant_type_info_unref (GVariantTypeInfo *info)
}
}
/* used from the test cases */
#define assert_no_type_infos() \
g_assert (g_variant_type_info_table == NULL)
void
g_variant_type_info_assert_no_infos (void)
{
g_assert (g_variant_type_info_table == NULL);
}
#define __G_VARIANT_TYPE_INFO_C__
#include "galiasdef.c"

View File

@ -125,35 +125,27 @@ typedef struct
#define G_VARIANT_MEMBER_ENDING_OFFSET 2
/* query */
G_GNUC_INTERNAL
const gchar * g_variant_type_info_get_type_string (GVariantTypeInfo *typeinfo);
G_GNUC_INTERNAL
void g_variant_type_info_query (GVariantTypeInfo *typeinfo,
guint *alignment,
gsize *size);
/* array */
G_GNUC_INTERNAL
GVariantTypeInfo * g_variant_type_info_element (GVariantTypeInfo *typeinfo);
G_GNUC_INTERNAL
void g_variant_type_info_query_element (GVariantTypeInfo *typeinfo,
guint *alignment,
gsize *size);
/* structure */
G_GNUC_INTERNAL
gsize g_variant_type_info_n_members (GVariantTypeInfo *typeinfo);
G_GNUC_INTERNAL
const GVariantMemberInfo * g_variant_type_info_member_info (GVariantTypeInfo *typeinfo,
gsize index);
/* new/ref/unref */
G_GNUC_INTERNAL
GVariantTypeInfo * g_variant_type_info_get (const GVariantType *type);
G_GNUC_INTERNAL
GVariantTypeInfo * g_variant_type_info_ref (GVariantTypeInfo *typeinfo);
G_GNUC_INTERNAL
void g_variant_type_info_unref (GVariantTypeInfo *typeinfo);
void g_variant_type_info_assert_no_infos (void);
#endif /* __G_VARIANT_TYPE_INFO_H__ */

View File

@ -11,7 +11,9 @@
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#include <glib/gvariant-internal.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#define BASIC "bynqiuxthdsog?"
@ -645,13 +647,6 @@ test_gvarianttype (void)
}
}
#undef G_GNUC_INTERNAL
#define G_GNUC_INTERNAL static
#define DISABLE_VISIBILITY
#define GLIB_COMPILATION
#include <glib/gvarianttypeinfo.c>
#define ALIGNED(x, y) (((x + (y - 1)) / y) * y)
/* do our own calculation of the fixed_size and alignment of a type
@ -1027,11 +1022,9 @@ test_gvarianttypeinfo (void)
g_free (desc);
}
assert_no_type_infos ();
g_variant_type_info_assert_no_infos ();
}
#include <glib/gvariant-serialiser.c>
#define MAX_FIXED_MULTIPLIER 256
#define MAX_INSTANCE_SIZE 1024
#define MAX_ARRAY_CHILDREN 128
@ -1384,7 +1377,7 @@ test_maybes (void)
for (i = 0; i < 1000; i++)
test_maybe ();
assert_no_type_infos ();
g_variant_type_info_assert_no_infos ();
}
static void
@ -1523,7 +1516,7 @@ test_arrays (void)
for (i = 0; i < 100; i++)
test_array ();
assert_no_type_infos ();
g_variant_type_info_assert_no_infos ();
}
static void
@ -1683,7 +1676,7 @@ test_tuples (void)
for (i = 0; i < 100; i++)
test_tuple ();
assert_no_type_infos ();
g_variant_type_info_assert_no_infos ();
}
static void
@ -1756,7 +1749,7 @@ test_variants (void)
for (i = 0; i < 100; i++)
test_variant ();
assert_no_type_infos ();
g_variant_type_info_assert_no_infos ();
}
static void
@ -2184,7 +2177,7 @@ test_byteswaps (void)
for (i = 0; i < 200; i++)
test_byteswap ();
assert_no_type_infos ();
g_variant_type_info_assert_no_infos ();
}
static void
@ -2254,7 +2247,7 @@ test_fuzzes (gpointer data)
for (i = 0; i < 200; i++)
test_fuzz (&fuzziness);
assert_no_type_infos ();
g_variant_type_info_assert_no_infos ();
}
static GVariant *
@ -2470,11 +2463,8 @@ tree_instance_check_gvariant (TreeInstance *tree,
child = g_variant_get_variant (value);
str1 = g_variant_get_type_string (child);
str2 = g_variant_type_info_get_type_string (tree->children[0]->info);
/* can't pointer-compare str1 and str2 since one comes from the
* real GVariantTypeInfo and one comes from our private copy...
*/
equal = strcmp (str1, str2) == 0 &&
/* GVariant only keeps one copy of type strings around */
equal = str1 == str2 &&
tree_instance_check_gvariant (tree->children[0], child);
g_variant_unref (child);
@ -2530,7 +2520,8 @@ tree_instance_check_gvariant (TreeInstance *tree,
static void
tree_instance_build_gvariant (TreeInstance *tree,
GVariantBuilder *builder)
GVariantBuilder *builder,
gboolean guess_ok)
{
const GVariantType *type;
@ -2540,10 +2531,28 @@ tree_instance_build_gvariant (TreeInstance *tree,
{
gsize i;
/* force GVariantBuilder to guess the type half the time */
if (guess_ok && randomly (0.5))
{
if (g_variant_type_is_array (type) && tree->n_children)
type = G_VARIANT_TYPE_ARRAY;
if (g_variant_type_is_maybe (type) && tree->n_children)
type = G_VARIANT_TYPE_MAYBE;
if (g_variant_type_is_tuple (type))
type = G_VARIANT_TYPE_TUPLE;
if (g_variant_type_is_dict_entry (type))
type = G_VARIANT_TYPE_DICT_ENTRY;
}
else
guess_ok = FALSE;
g_variant_builder_open (builder, type);
for (i = 0; i < tree->n_children; i++)
tree_instance_build_gvariant (tree->children[i], builder);
tree_instance_build_gvariant (tree->children[i], builder, guess_ok);
g_variant_builder_close (builder);
}
@ -2626,7 +2635,7 @@ test_container (void)
gchar *s3;
g_variant_builder_init (&builder, G_VARIANT_TYPE_VARIANT);
tree_instance_build_gvariant (tree, &builder);
tree_instance_build_gvariant (tree, &builder, TRUE);
built = g_variant_builder_end (&builder);
g_variant_ref_sink (built);
g_variant_get_data (built);
@ -2659,6 +2668,716 @@ test_containers (void)
{
test_container ();
}
g_variant_type_info_assert_no_infos ();
}
static void
test_format_strings (void)
{
GVariantType *type;
const gchar *end;
g_assert (g_variant_format_string_scan ("i", NULL, &end) && *end == '\0');
g_assert (g_variant_format_string_scan ("@i", NULL, &end) && *end == '\0');
g_assert (g_variant_format_string_scan ("@ii", NULL, &end) && *end == 'i');
g_assert (g_variant_format_string_scan ("^a&s", NULL, &end) && *end == '\0');
g_assert (g_variant_format_string_scan ("(^as)", NULL, &end) &&
*end == '\0');
g_assert (!g_variant_format_string_scan ("(^s)", NULL, &end));
g_assert (!g_variant_format_string_scan ("(^a)", NULL, &end));
g_assert (!g_variant_format_string_scan ("(z)", NULL, &end));
g_assert (!g_variant_format_string_scan ("az", NULL, &end));
g_assert (!g_variant_format_string_scan ("{**}", NULL, &end));
g_assert (!g_variant_format_string_scan ("{@**}", NULL, &end));
g_assert (g_variant_format_string_scan ("{@y*}", NULL, &end) &&
*end == '\0');
g_assert (g_variant_format_string_scan ("{yv}", NULL, &end) &&
*end == '\0');
g_assert (!g_variant_format_string_scan ("{vv}", NULL, &end));
g_assert (!g_variant_format_string_scan ("{y}", NULL, &end));
g_assert (!g_variant_format_string_scan ("{yyy}", NULL, &end));
g_assert (!g_variant_format_string_scan ("{ya}", NULL, &end));
g_assert (g_variant_format_string_scan ("&s", NULL, &end) && *end == '\0');
g_assert (!g_variant_format_string_scan ("&as", NULL, &end));
g_assert (!g_variant_format_string_scan ("@z", NULL, &end));
g_assert (!g_variant_format_string_scan ("az", NULL, &end));
g_assert (!g_variant_format_string_scan ("a&s", NULL, &end));
type = g_variant_format_string_scan_type ("mm(@xy^a&s*?@?)", NULL, &end);
g_assert (type && *end == '\0');
g_assert (g_variant_type_equal (type, G_VARIANT_TYPE ("mm(xyas*?\?)")));
g_variant_type_free (type);
type = g_variant_format_string_scan_type ("mm(@xy^a&*?@?)", NULL, NULL);
g_assert (type == NULL);
}
static void
exit_on_abort (int signal)
{
exit (signal);
}
static gboolean
do_failed_test (const gchar *pattern)
{
if (g_test_trap_fork (1000000, G_TEST_TRAP_SILENCE_STDERR))
{
signal (SIGABRT, exit_on_abort);
return TRUE;
}
g_test_trap_assert_failed ();
g_test_trap_assert_stderr (pattern);
return FALSE;
}
static void
test_invalid_varargs (void)
{
if (do_failed_test ("*not a valid GVariant format string*"))
{
g_variant_new ("z");
abort ();
}
if (do_failed_test ("*valid GVariant format string as a prefix*"))
{
const gchar *end;
g_variant_new_va ("z", &end, NULL);
abort ();
}
if (do_failed_test ("*type of `q' but * has a type of `y'*"))
{
g_variant_get (g_variant_new ("y", 'a'), "q");
abort ();
}
}
static void
check_and_free (GVariant *value,
const gchar *str)
{
gchar *valstr = g_variant_print (value, FALSE);
g_assert_cmpstr (str, ==, valstr);
g_variant_unref (value);
g_free (valstr);
}
static void
test_varargs (void)
{
{
GVariantBuilder array;
g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
g_variant_builder_add (&array, "{sv}", "size",
g_variant_new ("(ii)", 800, 600));
g_variant_builder_add (&array, "{sv}", "title",
g_variant_new_string ("Test case"));
g_variant_builder_add_value (&array,
g_variant_new_dict_entry (g_variant_new_string ("temperature"),
g_variant_new_variant (
g_variant_new_double (37.5))));
check_and_free (g_variant_new ("(ma{sv}m(a{sv})ma{sv}ii)",
NULL, FALSE, NULL, &array, 7777, 8888),
"(Nothing, Nothing, {'size': <(800, 600)>, "
"'title': <'Test case'>, "
"'temperature': <37.5>}, "
"7777, 8888)");
check_and_free (g_variant_new ("(imimimmimmimmi)",
123,
FALSE, 321,
TRUE, 123,
FALSE, TRUE, 321,
TRUE, FALSE, 321,
TRUE, TRUE, 123),
"(123, Nothing, 123, Nothing, Just Nothing, 123)");
check_and_free (g_variant_new ("(ybnixd)",
'a', 1, 22, 33, (guint64) 44, 5.5),
"(0x61, true, 22, 33, 44, 5.5)");
check_and_free (g_variant_new ("(@y?*rv)",
g_variant_new ("y", 'a'),
g_variant_new ("y", 'b'),
g_variant_new ("y", 'c'),
g_variant_new ("(y)", 'd'),
g_variant_new ("y", 'e')),
"(0x61, 0x62, 0x63, (0x64,), <byte 0x65>)");
}
{
GVariantBuilder array;
GVariantIter iter;
GVariant *value;
gchar *number;
gboolean just;
gint i, val;
g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
for (i = 0; i < 100; i++)
{
number = g_strdup_printf ("%d", i);
g_variant_builder_add (&array, "s", number);
g_free (number);
}
value = g_variant_builder_end (&array);
g_variant_iter_init (&iter, value);
i = 0;
while (g_variant_iter_loop (&iter, "s", &number))
{
gchar *check = g_strdup_printf ("%d", i++);
g_assert_cmpstr (number, ==, check);
g_free (check);
}
g_assert (number == NULL);
g_assert (i == 100);
g_variant_unref (value);
g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
for (i = 0; i < 100; i++)
g_variant_builder_add (&array, "mi", i % 2 == 0, i);
value = g_variant_builder_end (&array);
i = 0;
g_variant_iter_init (&iter, value);
while (g_variant_iter_loop (&iter, "mi", NULL, &val))
g_assert (val == i++ || val == 0);
g_assert (i == 100);
i = 0;
g_variant_iter_init (&iter, value);
while (g_variant_iter_loop (&iter, "mi", &just, &val))
{
gint this = i++;
if (this % 2 == 0)
{
g_assert (just);
g_assert (val == this);
}
else
{
g_assert (!just);
g_assert (val == 0);
}
}
g_assert (i == 100);
g_variant_unref (value);
}
{
const gchar *strvector[] = {"/hello", "/world", NULL};
const gchar *test_strs[] = {"/foo", "/bar", "/baz" };
GVariantBuilder builder;
GVariantIter *array;
GVariantIter tuple;
const gchar **strv;
gchar **my_strv;
GVariant *value;
gchar *str;
gint i;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
g_variant_builder_add (&builder, "o", "/foo");
g_variant_builder_add (&builder, "o", "/bar");
g_variant_builder_add (&builder, "o", "/baz");
value = g_variant_new("(ao^ao^a&o)", &builder, strvector, strvector);
g_variant_iter_init (&tuple, value);
g_variant_iter_next (&tuple, "ao", &array);
i = 0;
while (g_variant_iter_loop (array, "o", &str))
g_assert_cmpstr (str, ==, test_strs[i++]);
g_assert (i == 3);
g_variant_iter_free (array);
/* start over */
g_variant_iter_init (&tuple, value);
g_variant_iter_next (&tuple, "ao", &array);
i = 0;
while (g_variant_iter_loop (array, "&o", &str))
g_assert_cmpstr (str, ==, test_strs[i++]);
g_assert (i == 3);
g_variant_iter_free (array);
g_variant_iter_next (&tuple, "^a&o", &strv);
g_variant_iter_next (&tuple, "^ao", &my_strv);
g_assert_cmpstr (strv[0], ==, "/hello");
g_assert_cmpstr (strv[1], ==, "/world");
g_assert (strv[2] == NULL);
g_assert_cmpstr (my_strv[0], ==, "/hello");
g_assert_cmpstr (my_strv[1], ==, "/world");
g_assert (my_strv[2] == NULL);
g_variant_unref (value);
g_strfreev (my_strv);
g_free (strv);
}
{
const gchar *strvector[] = { "i", "ii", "iii", "iv", "v", "vi", NULL };
GVariantBuilder builder;
GVariantIter iter;
GVariantIter *i2;
GVariantIter *i3;
GVariant *value;
GVariant *sub;
gchar **strv;
gint i;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aag"));
g_variant_builder_open (&builder, G_VARIANT_TYPE ("ag"));
for (i = 0; i < 6; i++)
if (i & 1)
g_variant_builder_add (&builder, "g", strvector[i]);
else
g_variant_builder_add (&builder, "&g", strvector[i]);
g_variant_builder_close (&builder);
g_variant_builder_add (&builder, "^ag", strvector);
g_variant_builder_add (&builder, "^ag", strvector);
value = g_variant_new ("aag", &builder);
g_variant_iter_init (&iter, value);
while (g_variant_iter_loop (&iter, "^ag", &strv))
for (i = 0; i < 6; i++)
g_assert_cmpstr (strv[i], ==, strvector[i]);
g_variant_iter_init (&iter, value);
while (g_variant_iter_loop (&iter, "^a&g", &strv))
for (i = 0; i < 6; i++)
g_assert_cmpstr (strv[i], ==, strvector[i]);
g_variant_iter_init (&iter, value);
while (g_variant_iter_loop (&iter, "ag", &i2))
{
gchar *str;
i = 0;
while (g_variant_iter_loop (i2, "g", &str))
g_assert_cmpstr (str, ==, strvector[i++]);
g_assert (i == 6);
}
g_variant_iter_init (&iter, value);
i3 = g_variant_iter_copy (&iter);
while (g_variant_iter_loop (&iter, "@ag", &sub))
{
gchar *str = g_variant_print (sub, TRUE);
g_assert_cmpstr (str, ==,
"[signature 'i', 'ii', 'iii', 'iv', 'v', 'vi']");
g_free (str);
}
if (do_failed_test ("*NULL has already been returned*"))
{
g_variant_iter_next_value (&iter);
abort ();
}
while (g_variant_iter_loop (i3, "*", &sub))
{
gchar *str = g_variant_print (sub, TRUE);
g_assert_cmpstr (str, ==,
"[signature 'i', 'ii', 'iii', 'iv', 'v', 'vi']");
g_free (str);
}
g_variant_iter_free (i3);
for (i = 0; i < g_variant_n_children (value); i++)
{
gint j;
g_variant_get_child (value, i, "*", &sub);
for (j = 0; j < g_variant_n_children (sub); j++)
{
const gchar *str = NULL;
GVariant *cval;
g_variant_get_child (sub, j, "&g", &str);
g_assert_cmpstr (str, ==, strvector[j]);
cval = g_variant_get_child_value (sub, j);
g_variant_get (cval, "&g", &str);
g_assert_cmpstr (str, ==, strvector[j]);
g_variant_unref (cval);
}
g_variant_unref (sub);
}
g_variant_unref (value);
}
{
gboolean justs[10];
GVariant *value;
GVariant *vval;
guchar byteval;
gboolean bval;
gint16 i16val;
guint16 u16val;
gint32 i32val;
guint32 u32val;
gint64 i64val;
guint64 u64val;
gdouble dval;
gint32 hval;
/* test all 'Nothing' */
value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)",
FALSE, 'a',
FALSE, TRUE,
FALSE, (gint16) 123,
FALSE, (guint16) 123,
FALSE, (gint32) 123,
FALSE, (guint32) 123,
FALSE, (gint64) 123,
FALSE, (guint64) 123,
FALSE, (gint32) -1,
FALSE, (gdouble) 37.5,
NULL);
/* both NULL */
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL);
/* NULL values */
memset (justs, 1, sizeof justs);
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
&justs[0], NULL,
&justs[1], NULL,
&justs[2], NULL,
&justs[3], NULL,
&justs[4], NULL,
&justs[5], NULL,
&justs[6], NULL,
&justs[7], NULL,
&justs[8], NULL,
&justs[9], NULL,
NULL);
g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] ||
justs[5] || justs[6] || justs[7] || justs[8] || justs[9]));
/* both non-NULL */
memset (justs, 1, sizeof justs);
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
vval = (void *) 1;
bval = TRUE;
dval = 88.88;
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
&justs[0], &byteval,
&justs[1], &bval,
&justs[2], &i16val,
&justs[3], &u16val,
&justs[4], &i32val,
&justs[5], &u32val,
&justs[6], &i64val,
&justs[7], &u64val,
&justs[8], &hval,
&justs[9], &dval,
&vval);
g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] ||
justs[5] || justs[6] || justs[7] || justs[8] || justs[9]));
g_assert (byteval == '\0' && bval == FALSE);
g_assert (i16val == 0 && u16val == 0 && i32val == 0 &&
u32val == 0 && i64val == 0 && u64val == 0 &&
hval == 0 && dval == 0.0);
g_assert (vval == NULL);
/* NULL justs */
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
vval = (void *) 1;
bval = TRUE;
dval = 88.88;
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
NULL, &byteval,
NULL, &bval,
NULL, &i16val,
NULL, &u16val,
NULL, &i32val,
NULL, &u32val,
NULL, &i64val,
NULL, &u64val,
NULL, &hval,
NULL, &dval,
&vval);
g_assert (byteval == '\0' && bval == FALSE);
g_assert (i16val == 0 && u16val == 0 && i32val == 0 &&
u32val == 0 && i64val == 0 && u64val == 0 &&
hval == 0 && dval == 0.0);
g_assert (vval == NULL);
g_variant_unref (value);
/* test all 'Just' */
value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)",
TRUE, 'a',
TRUE, TRUE,
TRUE, (gint16) 123,
TRUE, (guint16) 123,
TRUE, (gint32) 123,
TRUE, (guint32) 123,
TRUE, (gint64) 123,
TRUE, (guint64) 123,
TRUE, (gint32) -1,
TRUE, (gdouble) 37.5,
g_variant_new ("()"));
/* both NULL */
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL);
/* NULL values */
memset (justs, 0, sizeof justs);
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
&justs[0], NULL,
&justs[1], NULL,
&justs[2], NULL,
&justs[3], NULL,
&justs[4], NULL,
&justs[5], NULL,
&justs[6], NULL,
&justs[7], NULL,
&justs[8], NULL,
&justs[9], NULL,
NULL);
g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] &&
justs[5] && justs[6] && justs[7] && justs[8] && justs[9]);
/* both non-NULL */
memset (justs, 0, sizeof justs);
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
vval = (void *) 1;
bval = FALSE;
dval = 88.88;
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
&justs[0], &byteval,
&justs[1], &bval,
&justs[2], &i16val,
&justs[3], &u16val,
&justs[4], &i32val,
&justs[5], &u32val,
&justs[6], &i64val,
&justs[7], &u64val,
&justs[8], &hval,
&justs[9], &dval,
&vval);
g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] &&
justs[5] && justs[6] && justs[7] && justs[8] && justs[9]);
g_assert (byteval == 'a' && bval == TRUE);
g_assert (i16val == 123 && u16val == 123 && i32val == 123 &&
u32val == 123 && i64val == 123 && u64val == 123 &&
hval == -1 && dval == 37.5);
g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT));
g_variant_unref (vval);
/* NULL justs */
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
vval = (void *) 1;
bval = TRUE;
dval = 88.88;
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
NULL, &byteval,
NULL, &bval,
NULL, &i16val,
NULL, &u16val,
NULL, &i32val,
NULL, &u32val,
NULL, &i64val,
NULL, &u64val,
NULL, &hval,
NULL, &dval,
&vval);
g_assert (byteval == 'a' && bval == TRUE);
g_assert (i16val == 123 && u16val == 123 && i32val == 123 &&
u32val == 123 && i64val == 123 && u64val == 123 &&
hval == -1 && dval == 37.5);
g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT));
g_variant_unref (vval);
g_variant_unref (value);
}
g_variant_type_info_assert_no_infos ();
}
static void
hash_get (GVariant *value,
const gchar *format,
...)
{
const gchar *endptr = NULL;
gboolean hash;
va_list ap;
hash = g_str_has_suffix (format, "#");
va_start (ap, format);
g_variant_get_va (value, format, hash ? &endptr : NULL, &ap);
va_end (ap);
if (hash)
g_assert (*endptr == '#');
}
static GVariant *
hash_new (const gchar *format,
...)
{
const gchar *endptr = NULL;
GVariant *value;
gboolean hash;
va_list ap;
hash = g_str_has_suffix (format, "#");
va_start (ap, format);
value = g_variant_new_va (format, hash ? &endptr : NULL, &ap);
va_end (ap);
if (hash)
g_assert (*endptr == '#');
return value;
}
static void
test_valist (void)
{
GVariant *value;
gint32 x;
x = 0;
value = hash_new ("i", 234);
hash_get (value, "i", &x);
g_assert (x == 234);
g_variant_unref (value);
x = 0;
value = hash_new ("i#", 234);
hash_get (value, "i#", &x);
g_assert (x == 234);
g_variant_unref (value);
g_variant_type_info_assert_no_infos ();
}
static void
test_builder_memory (void)
{
GVariantBuilder *hb;
GVariantBuilder sb;
hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
g_variant_builder_add (hb, "s", "some value");
g_variant_builder_ref (hb);
g_variant_builder_unref (hb);
g_variant_builder_unref (hb);
hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
g_variant_builder_unref (hb);
hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
g_variant_builder_clear (hb);
g_variant_builder_unref (hb);
g_variant_builder_init (&sb, G_VARIANT_TYPE_ARRAY);
g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY);
g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY);
g_variant_builder_add (&sb, "s", "some value");
g_variant_builder_clear (&sb);
g_variant_type_info_assert_no_infos ();
}
static void
test_hashing (void)
{
const gint n_items = 4096;
GVariant *items[n_items];
GHashTable *table;
gint i;
table = g_hash_table_new_full (g_variant_hash, g_variant_equal,
(GDestroyNotify ) g_variant_unref,
NULL);
for (i = 0; i < n_items; i++)
{
TreeInstance *tree;
gint j;
again:
tree = tree_instance_new (NULL, 0);
items[i] = tree_instance_get_gvariant (tree);
tree_instance_free (tree);
for (j = 0; j < i; j++)
if (g_variant_equal (items[i], items[j]))
{
g_variant_unref (items[i]);
goto again;
}
g_hash_table_insert (table,
g_variant_ref_sink (items[i]),
GINT_TO_POINTER (i));
}
for (i = 0; i < n_items; i++)
{
gpointer result;
result = g_hash_table_lookup (table, items[i]);
g_assert_cmpint (GPOINTER_TO_INT (result), ==, i);
}
g_hash_table_unref (table);
g_variant_type_info_assert_no_infos ();
}
int
@ -2688,6 +3407,12 @@ main (int argc, char **argv)
}
g_test_add_func ("/gvariant/containers", test_containers);
g_test_add_func ("/gvariant/format-strings", test_format_strings);
g_test_add_func ("/gvariant/invalid-varargs", test_invalid_varargs);
g_test_add_func ("/gvariant/varargs", test_varargs);
g_test_add_func ("/gvariant/valist", test_valist);
g_test_add_func ("/gvariant/builder-memory", test_builder_memory);
g_test_add_func ("/gvariant/hashing", test_hashing);
return g_test_run ();
}