mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-13 07:56:17 +01:00
Merge the main public API of GVariant
This commit is contained in:
parent
afa5dc0c6d
commit
ed3c914cdb
@ -2782,15 +2782,71 @@ GVariant
|
||||
g_variant_unref
|
||||
g_variant_ref
|
||||
g_variant_ref_sink
|
||||
g_variant_get_type
|
||||
g_variant_get_type_string
|
||||
g_variant_is_of_type
|
||||
g_variant_is_container
|
||||
|
||||
<SUBSECTION>
|
||||
g_variant_classify
|
||||
GVariantClass
|
||||
|
||||
<SUBSECTION>
|
||||
g_variant_new_boolean
|
||||
g_variant_new_byte
|
||||
g_variant_new_int16
|
||||
g_variant_new_uint16
|
||||
g_variant_new_int32
|
||||
g_variant_new_uint32
|
||||
g_variant_new_int64
|
||||
g_variant_new_uint64
|
||||
g_variant_new_handle
|
||||
g_variant_new_double
|
||||
g_variant_new_string
|
||||
g_variant_new_object_path
|
||||
g_variant_is_object_path
|
||||
g_variant_new_signature
|
||||
g_variant_is_signature
|
||||
g_variant_new_variant
|
||||
g_variant_new_strv
|
||||
|
||||
<SUBSECTION>
|
||||
g_variant_get_boolean
|
||||
g_variant_get_byte
|
||||
g_variant_get_int16
|
||||
g_variant_get_uint16
|
||||
g_variant_get_int32
|
||||
g_variant_get_uint32
|
||||
g_variant_get_int64
|
||||
g_variant_get_uint64
|
||||
g_variant_get_handle
|
||||
g_variant_get_double
|
||||
g_variant_get_string
|
||||
g_variant_dup_string
|
||||
g_variant_get_variant
|
||||
g_variant_get_strv
|
||||
g_variant_dup_strv
|
||||
|
||||
<SUBSECTION>
|
||||
g_variant_new_maybe
|
||||
g_variant_new_array
|
||||
g_variant_new_tuple
|
||||
g_variant_new_dict_entry
|
||||
|
||||
<SUBSECTION>
|
||||
g_variant_get_maybe
|
||||
g_variant_n_children
|
||||
g_variant_get_child_value
|
||||
g_variant_get_fixed_array
|
||||
|
||||
<SUBSECTION>
|
||||
g_variant_get_size
|
||||
g_variant_get_data
|
||||
g_variant_store
|
||||
|
||||
<SUBSECTION>
|
||||
g_variant_hash
|
||||
g_variant_equal
|
||||
</SECTION>
|
||||
|
||||
<SECTION>
|
||||
|
@ -174,6 +174,7 @@ libglib_2_0_la_SOURCES = \
|
||||
gunicodeprivate.h \
|
||||
gurifuncs.c \
|
||||
gutils.c \
|
||||
gvariant.c \
|
||||
gvariant-core.c \
|
||||
gvariant-private.h \
|
||||
gvariant-serialiser.h \
|
||||
|
@ -1699,6 +1699,62 @@ g_variant_get_size
|
||||
g_variant_get_data
|
||||
g_variant_store
|
||||
#endif
|
||||
|
||||
#if IN_FILE(__G_VARIANT_C__)
|
||||
g_variant_get_type
|
||||
g_variant_get_type_string
|
||||
g_variant_is_of_type
|
||||
g_variant_is_container
|
||||
g_variant_classify
|
||||
|
||||
g_variant_new_boolean
|
||||
g_variant_new_byte
|
||||
g_variant_new_int16
|
||||
g_variant_new_uint16
|
||||
g_variant_new_int32
|
||||
g_variant_new_uint32
|
||||
g_variant_new_int64
|
||||
g_variant_new_uint64
|
||||
g_variant_new_handle
|
||||
g_variant_new_double
|
||||
g_variant_new_string
|
||||
g_variant_new_object_path
|
||||
g_variant_is_object_path
|
||||
g_variant_new_signature
|
||||
g_variant_is_signature
|
||||
g_variant_new_variant
|
||||
g_variant_new_strv
|
||||
|
||||
g_variant_get_boolean
|
||||
g_variant_get_byte
|
||||
g_variant_get_int16
|
||||
g_variant_get_uint16
|
||||
g_variant_get_int32
|
||||
g_variant_get_uint32
|
||||
g_variant_get_int64
|
||||
g_variant_get_uint64
|
||||
g_variant_get_handle
|
||||
g_variant_get_double
|
||||
g_variant_get_string
|
||||
g_variant_dup_string
|
||||
g_variant_get_variant
|
||||
g_variant_get_strv
|
||||
g_variant_dup_strv
|
||||
|
||||
g_variant_new_maybe
|
||||
g_variant_new_array
|
||||
g_variant_new_tuple
|
||||
g_variant_new_dict_entry
|
||||
|
||||
g_variant_get_maybe
|
||||
g_variant_get_fixed_array
|
||||
|
||||
g_variant_print
|
||||
g_variant_print_string
|
||||
|
||||
g_variant_hash
|
||||
g_variant_equal
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if IN_HEADER(__G_WIN32_H__)
|
||||
|
@ -49,6 +49,8 @@
|
||||
*
|
||||
* #GVariant is an opaque data structure and can only be accessed
|
||||
* using the following functions.
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
struct _GVariant
|
||||
/* see below for field member documentation */
|
||||
@ -585,6 +587,8 @@ g_variant_is_trusted (GVariant *value)
|
||||
*
|
||||
* Decreases the reference count of @value. When its reference count
|
||||
* drops to 0, the memory used by the variant is freed.
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
void
|
||||
g_variant_unref (GVariant *value)
|
||||
@ -614,6 +618,8 @@ g_variant_unref (GVariant *value)
|
||||
* @returns: the same @value
|
||||
*
|
||||
* Increases the reference count of @value.
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
GVariant *
|
||||
g_variant_ref (GVariant *value)
|
||||
@ -650,6 +656,8 @@ g_variant_ref (GVariant *value)
|
||||
* makes certain common styles of programming much easier while still
|
||||
* maintaining normal refcounting semantics in situations where values
|
||||
* are not floating.
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
GVariant *
|
||||
g_variant_ref_sink (GVariant *value)
|
||||
@ -682,6 +690,8 @@ g_variant_ref_sink (GVariant *value)
|
||||
* then this function is O(1). Otherwise, the size is calculated, an
|
||||
* operation which is approximately O(n) in the number of values
|
||||
* involved.
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
gsize
|
||||
g_variant_get_size (GVariant *value)
|
||||
@ -714,6 +724,8 @@ g_variant_get_size (GVariant *value)
|
||||
* is O(1). If the value is not already in serialised form,
|
||||
* serialisation occurs implicitly and is approximately O(n) in the size
|
||||
* of the result.
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
gconstpointer
|
||||
g_variant_get_data (GVariant *value)
|
||||
@ -741,6 +753,8 @@ g_variant_get_data (GVariant *value)
|
||||
* only on the type). For dictionary entries, it is always 2
|
||||
*
|
||||
* This function is O(1).
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
gsize
|
||||
g_variant_n_children (GVariant *value)
|
||||
@ -782,6 +796,8 @@ g_variant_n_children (GVariant *value)
|
||||
* in the container. See g_variant_n_children().
|
||||
*
|
||||
* This function is O(1).
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
GVariant *
|
||||
g_variant_get_child_value (GVariant *value,
|
||||
@ -837,6 +853,8 @@ g_variant_get_child_value (GVariant *value,
|
||||
* g_variant_normalise() for a solution.
|
||||
*
|
||||
* This function is approximately O(n) in the size of @data.
|
||||
*
|
||||
* Since: 2.24
|
||||
**/
|
||||
void
|
||||
g_variant_store (GVariant *value,
|
||||
|
1858
glib/gvariant.c
Normal file
1858
glib/gvariant.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -24,24 +24,117 @@
|
||||
#define __G_VARIANT_H__
|
||||
|
||||
#include <glib/gvarianttype.h>
|
||||
#include <glib/gstring.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef struct _GVariant GVariant;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
G_VARIANT_CLASS_BOOLEAN = 'b',
|
||||
G_VARIANT_CLASS_BYTE = 'y',
|
||||
G_VARIANT_CLASS_INT16 = 'n',
|
||||
G_VARIANT_CLASS_UINT16 = 'q',
|
||||
G_VARIANT_CLASS_INT32 = 'i',
|
||||
G_VARIANT_CLASS_UINT32 = 'u',
|
||||
G_VARIANT_CLASS_INT64 = 'x',
|
||||
G_VARIANT_CLASS_UINT64 = 't',
|
||||
G_VARIANT_CLASS_HANDLE = 'h',
|
||||
G_VARIANT_CLASS_DOUBLE = 'd',
|
||||
G_VARIANT_CLASS_STRING = 's',
|
||||
G_VARIANT_CLASS_OBJECT_PATH = 'o',
|
||||
G_VARIANT_CLASS_SIGNATURE = 'g',
|
||||
G_VARIANT_CLASS_VARIANT = 'v',
|
||||
G_VARIANT_CLASS_MAYBE = 'm',
|
||||
G_VARIANT_CLASS_ARRAY = 'a',
|
||||
G_VARIANT_CLASS_TUPLE = '(',
|
||||
G_VARIANT_CLASS_DICT_ENTRY = '{'
|
||||
} GVariantClass;
|
||||
|
||||
void g_variant_unref (GVariant *value);
|
||||
GVariant * g_variant_ref (GVariant *value);
|
||||
GVariant * g_variant_ref_sink (GVariant *value);
|
||||
|
||||
const GVariantType * g_variant_get_type (GVariant *value);
|
||||
const gchar * g_variant_get_type_string (GVariant *value);
|
||||
gboolean g_variant_is_of_type (GVariant *value,
|
||||
const GVariantType *type);
|
||||
gboolean g_variant_is_container (GVariant *value);
|
||||
GVariantClass g_variant_classify (GVariant *value);
|
||||
|
||||
GVariant * g_variant_new_boolean (gboolean boolean);
|
||||
GVariant * g_variant_new_byte (guchar byte);
|
||||
GVariant * g_variant_new_int16 (gint16 int16);
|
||||
GVariant * g_variant_new_uint16 (guint16 uint16);
|
||||
GVariant * g_variant_new_int32 (gint32 int32);
|
||||
GVariant * g_variant_new_uint32 (guint32 uint32);
|
||||
GVariant * g_variant_new_int64 (gint64 int64);
|
||||
GVariant * g_variant_new_uint64 (guint64 uint64);
|
||||
GVariant * g_variant_new_handle (gint32 handle);
|
||||
GVariant * g_variant_new_double (gdouble floating);
|
||||
GVariant * g_variant_new_string (const gchar *string);
|
||||
GVariant * g_variant_new_object_path (const gchar *object_path);
|
||||
gboolean g_variant_is_object_path (const gchar *string);
|
||||
GVariant * g_variant_new_signature (const gchar *signature);
|
||||
gboolean g_variant_is_signature (const gchar *string);
|
||||
GVariant * g_variant_new_variant (GVariant *value);
|
||||
GVariant * g_variant_new_strv (const gchar * const *strv,
|
||||
gssize length);
|
||||
|
||||
gboolean g_variant_get_boolean (GVariant *value);
|
||||
guchar g_variant_get_byte (GVariant *value);
|
||||
gint16 g_variant_get_int16 (GVariant *value);
|
||||
guint16 g_variant_get_uint16 (GVariant *value);
|
||||
gint32 g_variant_get_int32 (GVariant *value);
|
||||
guint32 g_variant_get_uint32 (GVariant *value);
|
||||
gint64 g_variant_get_int64 (GVariant *value);
|
||||
guint64 g_variant_get_uint64 (GVariant *value);
|
||||
gint32 g_variant_get_handle (GVariant *value);
|
||||
gdouble g_variant_get_double (GVariant *value);
|
||||
GVariant * g_variant_get_variant (GVariant *value);
|
||||
const gchar * g_variant_get_string (GVariant *value,
|
||||
gsize *length);
|
||||
gchar * g_variant_dup_string (GVariant *value,
|
||||
gsize *length);
|
||||
const gchar ** g_variant_get_strv (GVariant *value,
|
||||
gsize *length);
|
||||
gchar ** g_variant_dup_strv (GVariant *value,
|
||||
gsize *length);
|
||||
|
||||
GVariant * g_variant_new_maybe (const GVariantType *child_type,
|
||||
GVariant *child);
|
||||
GVariant * g_variant_new_array (const GVariantType *child_type,
|
||||
GVariant * const *children,
|
||||
gsize n_children);
|
||||
GVariant * g_variant_new_tuple (GVariant * const *children,
|
||||
gsize n_children);
|
||||
GVariant * g_variant_new_dict_entry (GVariant *key,
|
||||
GVariant *value);
|
||||
|
||||
GVariant * g_variant_get_maybe (GVariant *value);
|
||||
gsize g_variant_n_children (GVariant *value);
|
||||
GVariant * g_variant_get_child_value (GVariant *value,
|
||||
gsize index);
|
||||
gsize index_);
|
||||
gconstpointer g_variant_get_fixed_array (GVariant *value,
|
||||
gsize *n_elements,
|
||||
gsize element_size);
|
||||
|
||||
gsize g_variant_get_size (GVariant *value);
|
||||
gconstpointer g_variant_get_data (GVariant *value);
|
||||
void g_variant_store (GVariant *value,
|
||||
gpointer data);
|
||||
|
||||
gchar * g_variant_print (GVariant *value,
|
||||
gboolean type_annotate);
|
||||
GString * g_variant_print_string (GVariant *value,
|
||||
GString *string,
|
||||
gboolean type_annotate);
|
||||
|
||||
guint g_variant_hash (gconstpointer value);
|
||||
gboolean g_variant_equal (gconstpointer one,
|
||||
gconstpointer two);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __G_VARIANT_H__ */
|
||||
|
@ -1833,6 +1833,7 @@ struct _TreeInstance
|
||||
|
||||
union {
|
||||
guint64 integer;
|
||||
gdouble floating;
|
||||
gchar string[32];
|
||||
} data;
|
||||
gsize data_size;
|
||||
@ -1876,8 +1877,8 @@ make_random_string (gchar *string,
|
||||
}
|
||||
|
||||
static TreeInstance *
|
||||
make_tree_instance (const GVariantType *type,
|
||||
int depth)
|
||||
tree_instance_new (const GVariantType *type,
|
||||
int depth)
|
||||
{
|
||||
const GVariantType *child_type = NULL;
|
||||
GVariantType *mytype = NULL;
|
||||
@ -1961,7 +1962,7 @@ make_tree_instance (const GVariantType *type,
|
||||
|
||||
for (i = 0; i < instance->n_children; i++)
|
||||
{
|
||||
instance->children[i] = make_tree_instance (child_type, depth - 1);
|
||||
instance->children[i] = tree_instance_new (child_type, depth - 1);
|
||||
|
||||
if (is_tuple_type)
|
||||
child_type = g_variant_type_next (child_type);
|
||||
@ -2158,7 +2159,7 @@ test_byteswap (void)
|
||||
GVariantSerialised one, two;
|
||||
TreeInstance *tree;
|
||||
|
||||
tree = make_tree_instance (NULL, 3);
|
||||
tree = tree_instance_new (NULL, 3);
|
||||
serialise_tree (tree, &one);
|
||||
|
||||
i_am_writing_byteswapped = TRUE;
|
||||
@ -2193,7 +2194,7 @@ test_fuzz (gdouble *fuzziness)
|
||||
TreeInstance *tree;
|
||||
|
||||
/* make an instance */
|
||||
tree = make_tree_instance (NULL, 3);
|
||||
tree = tree_instance_new (NULL, 3);
|
||||
|
||||
/* serialise it */
|
||||
serialise_tree (tree, &serialised);
|
||||
@ -2256,6 +2257,173 @@ test_fuzzes (gpointer data)
|
||||
assert_no_type_infos ();
|
||||
}
|
||||
|
||||
static GVariant *
|
||||
tree_instance_get_gvariant (TreeInstance *tree)
|
||||
{
|
||||
const GVariantType *type;
|
||||
GVariant *result;
|
||||
|
||||
type = (GVariantType *) g_variant_type_info_get_type_string (tree->info);
|
||||
|
||||
switch (g_variant_type_info_get_type_char (tree->info))
|
||||
{
|
||||
case G_VARIANT_TYPE_INFO_CHAR_MAYBE:
|
||||
{
|
||||
GVariant *child;
|
||||
|
||||
if (tree->n_children)
|
||||
child = tree_instance_get_gvariant (tree->children[0]);
|
||||
else
|
||||
child = NULL;
|
||||
|
||||
result = g_variant_new_maybe (g_variant_type_element (type), child);
|
||||
}
|
||||
break;
|
||||
|
||||
case G_VARIANT_TYPE_INFO_CHAR_ARRAY:
|
||||
{
|
||||
GVariant **children;
|
||||
gint i;
|
||||
|
||||
children = g_new (GVariant *, tree->n_children);
|
||||
for (i = 0; i < tree->n_children; i++)
|
||||
children[i] = tree_instance_get_gvariant (tree->children[i]);
|
||||
|
||||
result = g_variant_new_array (g_variant_type_element (type),
|
||||
children, tree->n_children);
|
||||
g_free (children);
|
||||
}
|
||||
break;
|
||||
|
||||
case G_VARIANT_TYPE_INFO_CHAR_TUPLE:
|
||||
{
|
||||
GVariant **children;
|
||||
gint i;
|
||||
|
||||
children = g_new (GVariant *, tree->n_children);
|
||||
for (i = 0; i < tree->n_children; i++)
|
||||
children[i] = tree_instance_get_gvariant (tree->children[i]);
|
||||
|
||||
result = g_variant_new_tuple (children, tree->n_children);
|
||||
g_free (children);
|
||||
}
|
||||
break;
|
||||
|
||||
case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:
|
||||
{
|
||||
GVariant *key, *val;
|
||||
|
||||
g_assert (tree->n_children == 2);
|
||||
|
||||
key = tree_instance_get_gvariant (tree->children[0]);
|
||||
val = tree_instance_get_gvariant (tree->children[1]);
|
||||
|
||||
result = g_variant_new_dict_entry (key, val);
|
||||
}
|
||||
break;
|
||||
|
||||
case G_VARIANT_TYPE_INFO_CHAR_VARIANT:
|
||||
{
|
||||
GVariant *value;
|
||||
|
||||
g_assert (tree->n_children == 1);
|
||||
|
||||
value = tree_instance_get_gvariant (tree->children[0]);
|
||||
result = g_variant_new_variant (value);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
result = g_variant_new_boolean (tree->data.integer > 0);
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
result = g_variant_new_byte (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
result = g_variant_new_int16 (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 'q':
|
||||
result = g_variant_new_uint16 (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
result = g_variant_new_int32 (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
result = g_variant_new_uint32 (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
result = g_variant_new_int64 (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 't':
|
||||
result = g_variant_new_uint64 (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
result = g_variant_new_handle (tree->data.integer);
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
result = g_variant_new_double (tree->data.floating);
|
||||
break;
|
||||
|
||||
case 's':
|
||||
result = g_variant_new_string (tree->data.string);
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
result = g_variant_new_object_path (tree->data.string);
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
result = g_variant_new_signature (tree->data.string);
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
test_constructor (void)
|
||||
{
|
||||
TreeInstance *tree;
|
||||
GVariant *value;
|
||||
gchar *s1, *s2;
|
||||
|
||||
tree = tree_instance_new (NULL, 3);
|
||||
value = g_variant_ref_sink (tree_instance_get_gvariant (tree));
|
||||
|
||||
s1 = g_variant_print (value, TRUE);
|
||||
g_variant_get_data (value);
|
||||
s2 = g_variant_print (value, TRUE);
|
||||
|
||||
g_assert_cmpstr (s1, ==, s2);
|
||||
tree_instance_free (tree);
|
||||
g_variant_unref (value);
|
||||
g_free (s2);
|
||||
g_free (s1);
|
||||
}
|
||||
|
||||
static void
|
||||
test_constructors (void)
|
||||
{
|
||||
gint i;
|
||||
|
||||
for (i = 0; i < 1000; i++)
|
||||
{
|
||||
test_constructor ();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@ -2271,6 +2439,7 @@ main (int argc, char **argv)
|
||||
g_test_add_func ("/gvariant/serialiser/variant", test_variants);
|
||||
g_test_add_func ("/gvariant/serialiser/strings", test_strings);
|
||||
g_test_add_func ("/gvariant/serialiser/byteswap", test_byteswaps);
|
||||
g_test_add_func ("/gvariant/constructors", test_constructors);
|
||||
|
||||
for (i = 1; i <= 20; i += 4)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user