[glib/gvariant-utils] rebase gvariant.c after merging -core
- From: Ryan Lortie <ryanl src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [glib/gvariant-utils] rebase gvariant.c after merging -core
- Date: Wed, 10 Feb 2010 19:00:52 +0000 (UTC)
commit 988beb4d9b0b5ec205065268fcfc376dc24b5839
Author: Ryan Lortie <desrt desrt ca>
Date: Wed Feb 10 12:51:23 2010 -0500
rebase gvariant.c after merging -core
docs/reference/glib/Makefile.am | 2 +
docs/reference/glib/glib-sections.txt | 56 ++-
glib/Makefile.am | 1 +
glib/glib.symbols | 50 ++
glib/gvariant.c | 913 +++++++++++++++++++++++++++++++++
glib/gvariant.h | 82 +++
6 files changed, 1102 insertions(+), 2 deletions(-)
---
diff --git a/docs/reference/glib/Makefile.am b/docs/reference/glib/Makefile.am
index 200aa99..4dfb496 100644
--- a/docs/reference/glib/Makefile.am
+++ b/docs/reference/glib/Makefile.am
@@ -42,6 +42,8 @@ IGNORE_HFILES= \
galias.h \
gmirroringtable.h \
gscripttable.h \
+ gvariant-serialiser.h \
+ gvarianttypeinfo.h \
glib-mirroring-tab \
gnulib \
pcre \
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 6f3c21c..685ffd4 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -2782,10 +2782,62 @@ GVariant
g_variant_unref
g_variant_ref
g_variant_ref_sink
-
-<SUBSECTION>
+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
diff --git a/glib/Makefile.am b/glib/Makefile.am
index 433bb85..e441d64 100644
--- a/glib/Makefile.am
+++ b/glib/Makefile.am
@@ -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 \
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 39665f2..069d5e5 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -1699,6 +1699,56 @@ 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
+#endif
#endif
#if IN_HEADER(__G_WIN32_H__)
diff --git a/glib/gvariant.c b/glib/gvariant.c
new file mode 100644
index 0000000..529218c
--- /dev/null
+++ b/glib/gvariant.c
@@ -0,0 +1,913 @@
+/*
+ * Copyright © 2007, 2008 Ryan Lortie
+ * Copyright © 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>
+ */
+
+#include "config.h"
+
+#include <glib/gvariant-serialiser.h>
+#include <glib/gvariant-core.h>
+#include <glib/gtestutils.h>
+#include <glib/gstrfuncs.h>
+#include <glib/gmem.h>
+
+#include <string.h>
+
+#include "galias.h"
+
+/**
+ * SECTION: gvariant
+ * @title: GVariant
+ * @short_description: strongly typed value datatype
+ * @see_also: GVariantType
+ *
+ * #GVariant is a variant datatype; it stores a value along with
+ * information about the type of that value. The range of possible
+ * values is determined by the type. The type system used by #GVariant
+ * is #GVariantType.
+ *
+ * #GVariant instances always have a type and a value (which are given
+ * at construction time). The type and value of a #GVariant instance
+ * can never change other than by the #GVariant itself being
+ * destroyed. A #GVariant can not contain a pointer.
+ *
+ * #GVariant is reference counted using g_variant_ref() and
+ * g_variant_unref(). #GVariant also has floating reference counts --
+ * see g_variant_ref_sink().
+ *
+ * #GVariant is completely threadsafe. A #GVariant instance can be
+ * concurrently accessed in any way from any number of threads without
+ * problems.
+ *
+ * #GVariant is heavily optimised for dealing with data in serialised
+ * form. It works particularly well with data located in memory-mapped
+ * files. It can perform nearly all deserialisation operations in a
+ * small constant time, usually touching only a single memory page.
+ * Serialised #GVariant data can also be sent over the network.
+ *
+ * #GVariant is largely compatible with DBus. Almost all types of
+ * #GVariant instances can be sent over DBus. See #GVariantType for
+ * exceptions.
+ *
+ * For convenience to C programmers, #GVariant features powerful
+ * varargs-based value construction and destruction. This feature is
+ * designed to be embedded in other libraries.
+ *
+ * There is a Python-inspired text language for describing #GVariant
+ * values. #GVariant includes a printer for this language and a parser
+ * with type inferencing.
+ */
+
+/* definition of GVariant structure is in gvariant-core.c */
+
+#define TYPE_CHECK(value, TYPE, val) \
+ if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \
+ g_return_if_fail_warning (G_LOG_DOMAIN, __PRETTY_FUNCTION__, \
+ "g_variant_is_of_type (" #value \
+ ", G_VARIANT_TYPE_" #TYPE ")"); \
+ return val; \
+ }
+
+/* < private >
+ * g_variant_new_from_trusted:
+ * @type: the #GVariantType
+ * @data: the data to use
+ * @size: the size of @data
+ * @returns: a new floating #GVariant
+ *
+ * Constructs a new trusted #GVariant instance from the provided data.
+ * This is used to implement g_variant_new_* for all the basic types.
+ */
+static GVariant *
+g_variant_new_from_trusted (const GVariantType *type,
+ gconstpointer data,
+ gsize size)
+{
+ GVariant *value;
+ GBuffer *buffer;
+
+ buffer = g_buffer_new_from_data (data, size);
+ value = g_variant_new_from_buffer (type, buffer, TRUE);
+ g_buffer_unref (buffer);
+
+ return value;
+}
+
+/**
+ * g_variant_new_boolean:
+ * @boolean: a #gboolean value
+ * @returns: a new boolean #GVariant instance
+ *
+ * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
+ **/
+GVariant *
+g_variant_new_boolean (gboolean value)
+{
+ guchar v = value;
+
+ return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1);
+}
+
+/**
+ * g_variant_get_boolean:
+ * @value: a boolean #GVariant instance
+ * @returns: %TRUE or %FALSE
+ *
+ * Returns the boolean value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_BOOLEAN.
+ **/
+gboolean
+g_variant_get_boolean (GVariant *value)
+{
+ const guchar *data;
+
+ TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE);
+
+ data = g_variant_get_data (value);
+
+ return data != NULL ? *data != 0 : FALSE;
+}
+
+/**
+ * g_variant_new_handle:
+ * @handle: a #gint32 value
+ * @returns: a new handle #GVariant instance
+ *
+ * Creates a new handle #GVariant instance.
+ *
+ * By convention, handles are indexes into an array of file descriptors
+ * that are sent alongside a DBus message. If you're not interacting
+ * with DBus, you probably don't need them.
+ **/
+GVariant *
+g_variant_new_handle (gint32 value)
+{
+ return g_variant_new_from_trusted (G_VARIANT_TYPE_HANDLE,
+ &value, sizeof value);
+}
+
+/**
+ * g_variant_get_handle:
+ * @value: a handle #GVariant instance
+ * @returns: a #gint32
+ *
+ * Returns the 32-bit signed integer value of @value.
+ *
+ * It is an error to call this function with a @value of any type other
+ * than %G_VARIANT_TYPE_HANDLE.
+ *
+ * By convention, handles are indexes into an array of file descriptors
+ * that are sent alongside a DBus message. If you're not interacting
+ * with DBus, you probably don't need them.
+ **/
+gint32
+g_variant_get_handle (GVariant *value)
+{
+ const gint32 *data;
+
+ TYPE_CHECK (value, G_VARIANT_TYPE_HANDLE, -1);
+
+ data = g_variant_get_data (value);
+
+ return data != NULL ? *data : 0;
+}
+
+#define NUMERIC_TYPE(TYPE, type) \
+ GVariant *g_variant_new_##type (g##type value) { \
+ return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \
+ &value, sizeof value); \
+ } \
+ g##type g_variant_get_##type (GVariant *value) { \
+ const g##type *data; \
+ TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \
+ data = g_variant_get_data (value); \
+ return data != NULL ? *data : 0; \
+ }
+
+
+/**
+ * g_variant_new_byte:
+ * @byte: a #guint8 value
+ * @returns: a new byte #GVariant instance
+ *
+ * Creates a new byte #GVariant instance.
+ **/
+/**
+ * g_variant_get_byte:
+ * @value: a byte #GVariant instance
+ * @returns: a #guchar
+ *
+ * Returns the byte value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_BYTE.
+ **/
+#define gbyte guchar
+NUMERIC_TYPE (BYTE, byte)
+
+/**
+ * g_variant_new_int16:
+ * @int16: a #gint16 value
+ * @returns: a new int16 #GVariant instance
+ *
+ * Creates a new int16 #GVariant instance.
+ **/
+/**
+ * g_variant_get_int16:
+ * @value: a int16 #GVariant instance
+ * @returns: a #gint16
+ *
+ * Returns the 16-bit signed integer value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_INT16.
+ **/
+NUMERIC_TYPE (INT16, int16)
+
+/**
+ * g_variant_new_uint16:
+ * @uint16: a #guint16 value
+ * @returns: a new uint16 #GVariant instance
+ *
+ * Creates a new uint16 #GVariant instance.
+ **/
+/**
+ * g_variant_get_uint16:
+ * @value: a uint16 #GVariant instance
+ * @returns: a #guint16
+ *
+ * Returns the 16-bit unsigned integer value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_UINT16.
+ **/
+NUMERIC_TYPE (UINT16, uint16)
+
+/**
+ * g_variant_new_int32:
+ * @int32: a #gint32 value
+ * @returns: a new int32 #GVariant instance
+ *
+ * Creates a new int32 #GVariant instance.
+ **/
+/**
+ * g_variant_get_int32:
+ * @value: a int32 #GVariant instance
+ * @returns: a #gint32
+ *
+ * Returns the 32-bit signed integer value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_INT32.
+ **/
+NUMERIC_TYPE (INT32, int32)
+
+/**
+ * g_variant_new_uint32:
+ * @uint32: a #guint32 value
+ * @returns: a new uint32 #GVariant instance
+ *
+ * Creates a new uint32 #GVariant instance.
+ **/
+/**
+ * g_variant_get_uint32:
+ * @value: a uint32 #GVariant instance
+ * @returns: a #guint32
+ *
+ * Returns the 32-bit unsigned integer value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_UINT32.
+ **/
+NUMERIC_TYPE (UINT32, uint32)
+
+/**
+ * g_variant_new_int64:
+ * @int64: a #gint64 value
+ * @returns: a new int64 #GVariant instance
+ *
+ * Creates a new int64 #GVariant instance.
+ **/
+/**
+ * g_variant_get_int64:
+ * @value: a int64 #GVariant instance
+ * @returns: a #gint64
+ *
+ * Returns the 64-bit signed integer value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_INT64.
+ **/
+NUMERIC_TYPE (INT64, int64)
+
+/**
+ * g_variant_new_uint64:
+ * @uint64: a #guint64 value
+ * @returns: a new uint64 #GVariant instance
+ *
+ * Creates a new uint64 #GVariant instance.
+ **/
+/**
+ * g_variant_get_uint64:
+ * @value: a uint64 #GVariant instance
+ * @returns: a #guint64
+ *
+ * Returns the 64-bit unsigned integer value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_UINT64.
+ **/
+NUMERIC_TYPE (UINT64, uint64)
+
+/**
+ * g_variant_new_double:
+ * @floating: a #gdouble floating point value
+ * @returns: a new double #GVariant instance
+ *
+ * Creates a new double #GVariant instance.
+ **/
+/**
+ * g_variant_get_double:
+ * @value: a double #GVariant instance
+ * @returns: a #gdouble
+ *
+ * Returns the double precision floating point value of @value.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than %G_VARIANT_TYPE_DOUBLE.
+ **/
+NUMERIC_TYPE (DOUBLE, double)
+
+/**
+ * g_variant_get_type:
+ * @value: a #GVariant
+ * @returns: a #GVariantType
+ *
+ * Determines the type of @value.
+ *
+ * The return value is valid for the lifetime of @value and must not
+ * be freed.
+ **/
+const GVariantType *
+g_variant_get_type (GVariant *value)
+{
+ GVariantTypeInfo *type_info;
+
+ g_return_val_if_fail (value != NULL, NULL);
+
+ type_info = g_variant_get_type_info (value);
+
+ return (GVariantType *) g_variant_type_info_get_type_string (type_info);
+}
+
+/**
+ * g_variant_get_type_string:
+ * @value: a #GVariant
+ * @returns: the type string for the type of @value
+ *
+ * Returns the type string of @value. Unlike the result of calling
+ * g_variant_type_peek_string(), this string is nul-terminated. This
+ * string belongs to #GVariant and must not be freed.
+ **/
+const gchar *
+g_variant_get_type_string (GVariant *value)
+{
+ GVariantTypeInfo *type_info;
+
+ g_return_val_if_fail (value != NULL, NULL);
+
+ type_info = g_variant_get_type_info (value);
+
+ return g_variant_type_info_get_type_string (type_info);
+}
+
+/**
+ * g_variant_is_of_type:
+ * @value: a #GVariant instance
+ * @pattern: a #GVariantType
+ * @returns: %TRUE if the type of @value matches @type
+ *
+ * Checks if a value has a type matching the provided type.
+ **/
+gboolean
+g_variant_is_of_type (GVariant *value,
+ const GVariantType *type)
+{
+ return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
+}
+
+/**
+ * g_variant_is_container:
+ * @value: a #GVariant instance
+ * @returns: %TRUE if @value is a container
+ *
+ * Checks if @value is a container.
+ */
+gboolean
+g_variant_is_container (GVariant *value)
+{
+ return g_variant_type_is_container (g_variant_get_type (value));
+}
+
+GVariant *
+g_variant_new_maybe (const GVariantType *type,
+ GVariant *value)
+{
+ GVariantType *maybe_type;
+
+ g_return_val_if_fail (type == NULL || g_variant_type_is_definite (type), 0);
+ g_return_val_if_fail (type != NULL || value != NULL, NULL);
+ g_return_val_if_fail (type == NULL || value == NULL ||
+ g_variant_is_of_type (value, type),
+ NULL);
+
+ if (type == NULL)
+ type = g_variant_get_type (value);
+
+ maybe_type = g_variant_type_new_maybe (type);
+
+ if (value != NULL)
+ {
+ GVariant **children;
+ gboolean trusted;
+
+ children = g_new (GVariant *, 1);
+ children[0] = g_variant_ref_sink (value);
+ trusted = g_variant_is_trusted (children[0]);
+
+ value = g_variant_new_from_children (maybe_type, children, 1, trusted);
+ }
+ else
+ value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE);
+
+ g_variant_type_free (maybe_type);
+
+ return value;
+}
+
+GVariant *
+g_variant_get_maybe (GVariant *value)
+{
+ TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL);
+
+ if (g_variant_n_children (value))
+ return g_variant_get_child_value (value, 0);
+
+ return NULL;
+}
+
+/**
+ * g_variant_new_variant:
+ * @value: a #GVariance instance
+ * @returns: a new variant #GVariant instance
+ *
+ * Boxes @value. The result is a #GVariant instance representing a
+ * variant containing the original value.
+ **/
+GVariant *
+g_variant_new_variant (GVariant *value)
+{
+ g_return_val_if_fail (value != NULL, NULL);
+
+ return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
+ g_memdup (&value, sizeof value),
+ 1, g_variant_is_trusted (value));
+}
+
+/**
+ * g_variant_get_variant:
+ * @value: a variant #GVariance instance
+ * @returns: the item contained in the variant
+ *
+ * Unboxes @value. The result is the #GVariant instance that was
+ * contained in @value.
+ **/
+GVariant *
+g_variant_get_variant (GVariant *value)
+{
+ TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL);
+
+ return g_variant_get_child_value (value, 0);
+}
+
+/**
+ * g_variant_new_string:
+ * @string: a normal C nul-terminated string
+ * @returns: a new string #GVariant instance
+ *
+ * Creates a string #GVariant with the contents of @string.
+ **/
+GVariant *
+g_variant_new_string (const gchar *string)
+{
+ g_return_val_if_fail (string != NULL, NULL);
+
+ return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
+ string, strlen (string) + 1);
+}
+
+/**
+ * g_variant_new_object_path:
+ * @string: a normal C nul-terminated string
+ * @returns: a new object path #GVariant instance
+ *
+ * Creates a DBus object path #GVariant with the contents of @string.
+ * @string must be a valid DBus object path. Use
+ * g_variant_is_object_path() if you're not sure.
+ **/
+GVariant *
+g_variant_new_object_path (const gchar *object_path)
+{
+ g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
+
+ return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH,
+ object_path, strlen (object_path) + 1);
+}
+
+/**
+ * g_variant_is_object_path:
+ * @string: a normal C nul-terminated string
+ * @returns: %TRUE if @string is a DBus object path
+ *
+ * Determines if a given string is a valid DBus object path. You
+ * should ensure that a string is a valid DBus object path before
+ * passing it to g_variant_new_object_path().
+ *
+ * A valid object path starts with '/' followed by zero or more
+ * sequences of characters separated by '/' characters. Each sequence
+ * must contain only the characters "[A-Z][a-z][0-9]_". No sequence
+ * (including the one following the final '/' character) may be empty.
+ **/
+gboolean
+g_variant_is_object_path (const gchar *string)
+{
+ g_return_val_if_fail (string != NULL, FALSE);
+
+ return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
+}
+
+
+/**
+ * g_variant_new_signature:
+ * @string: a normal C nul-terminated string
+ * @returns: a new signature #GVariant instance
+ *
+ * Creates a DBus type signature #GVariant with the contents of
+ * @string. @string must be a valid DBus type signature. Use
+ * g_variant_is_signature() if you're not sure.
+ **/
+GVariant *
+g_variant_new_signature (const gchar *signature)
+{
+ g_return_val_if_fail (g_variant_is_signature (signature), NULL);
+
+ return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE,
+ signature, strlen (signature) + 1);
+}
+
+/**
+ * g_variant_is_signature:
+ * @string: a normal C nul-terminated string
+ * @returns: %TRUE if @string is a DBus type signature
+ *
+ * Determines if a given string is a valid DBus type signature. You
+ * should ensure that a string is a valid DBus object path before
+ * passing it to g_variant_new_signature().
+ *
+ * DBus type signatures consist of zero or more definite #GVariantType
+ * strings in sequence.
+ **/
+gboolean
+g_variant_is_signature (const gchar *string)
+{
+ g_return_val_if_fail (string != NULL, FALSE);
+
+ return g_variant_serialiser_is_signature (string, strlen (string) + 1);
+}
+
+/**
+ * g_variant_get_string:
+ * @value: a string #GVariant instance
+ * @length: a pointer to a #gsize, to store the length
+ * @returns: the constant string
+ *
+ * Returns the string value of a #GVariant instance with a string
+ * type. This includes the types %G_VARIANT_TYPE_STRING,
+ * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
+ *
+ * If @length is non-%NULL then the length of the string (in bytes) is
+ * returned there. For trusted values, this information is already
+ * known. For untrusted values, a strlen() will be performed.
+ *
+ * It is an error to call this function with a @value of any type
+ * other than those three.
+ *
+ * The return value remains valid as long as @value exists.
+ **/
+const gchar *
+g_variant_get_string (GVariant *value,
+ gsize *length)
+{
+ g_return_val_if_fail (value != NULL, NULL);
+ g_return_val_if_fail (
+ g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
+ g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) ||
+ g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL);
+ gconstpointer data = g_variant_get_data (value);
+ gsize size = g_variant_get_size (value);
+
+ if (!g_variant_is_trusted (value))
+ {
+ switch (g_variant_classify (value))
+ {
+ case G_VARIANT_CLASS_STRING:
+ if (g_variant_serialiser_is_string (data, size))
+ break;
+
+ data = "";
+ size = 1;
+ break;
+
+ case G_VARIANT_CLASS_OBJECT_PATH:
+ if (g_variant_serialiser_is_object_path (data, size))
+ break;
+
+ data = "/";
+ size = 2;
+ break;
+
+ case G_VARIANT_CLASS_SIGNATURE:
+ if (g_variant_serialiser_is_signature (data, size))
+ break;
+
+ data = "";
+ size = 1;
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+ }
+
+ if (length)
+ *length = size - 1;
+
+ return data;
+}
+
+/**
+ * g_variant_dup_string:
+ * @value: a string #GVariant instance
+ * @length: a pointer to a #gsize, to store the length
+ * @returns: a newly allocated string
+ *
+ * Similar to g_variant_get_string() except that instead of returning
+ * a constant string, the string is duplicated.
+ *
+ * The return value must be freed using g_free().
+ **/
+gchar *
+g_variant_dup_string (GVariant *value,
+ gsize *length)
+{
+ return g_strdup (g_variant_get_string (value, length));
+}
+
+/**
+ * g_variant_new_strv:
+ * @strv: an array of strings
+ * @length: the length of @strv, or -1
+ * @returns: a new floating #GVariant instance
+ *
+ * Constructs an array of strings #GVariant from the given array of
+ * strings.
+ *
+ * If @length is not -1 then it gives the maximum length of @strv. In
+ * any case, a %NULL pointer in @strv is taken as a terminator.
+ **/
+GVariant *
+g_variant_new_strv (const gchar * const *strv,
+ gssize length)
+{
+ GVariant **strings;
+ gsize i;
+
+ g_return_val_if_fail (length == 0 || strv != NULL, NULL);
+
+ if (length < 0)
+ for (length = 0; strv[length]; length++);
+
+ strings = g_new (GVariant *, length);
+ for (i = 0; i < length; i++)
+ strings[i] = g_variant_new_string (strv[i]);
+
+ return g_variant_new_from_children (G_VARIANT_TYPE ("as"),
+ strings, length, TRUE);
+}
+
+/**
+ * g_variant_get_strv:
+ * @value: an array of strings #GVariant
+ * @length: the length of the result, or %NULL
+ * @returns: an array of constant strings
+ *
+ * Gets the contents of an array of strings #GVariant. This call
+ * makes a shallow copy; the return result should be released with
+ * g_free(), but the individual strings must not be modified.
+ *
+ * If @length is non-%NULL then the number of elements in the result
+ * is stored there. In any case, the resulting array will be
+ * %NULL-terminated.
+ *
+ * For an empty array, @length will be set to 0 and a pointer to a
+ * %NULL pointer will be returned.
+ **/
+const gchar **
+g_variant_get_strv (GVariant *value,
+ gsize *length)
+{
+ const gchar **strv;
+ gsize n;
+ gsize i;
+
+ TYPE_CHECK (value, G_VARIANT_TYPE ("as"), NULL);
+
+ g_variant_get_data (value);
+ n = g_variant_n_children (value);
+ strv = g_new (const gchar *, n + 1);
+
+ for (i = 0; i < n; i++)
+ {
+ GVariant *string;
+
+ string = g_variant_get_child_value (value, i);
+ strv[i] = g_variant_get_string (string, NULL);
+ g_variant_unref (string);
+ }
+ strv[i] = NULL;
+
+ if (length)
+ *length = n;
+
+ return strv;
+}
+
+/**
+ * g_variant_dup_strv:
+ * @value: an array of strings #GVariant
+ * @length: the length of the result, or %NULL
+ * @returns: an array of constant strings
+ *
+ * Gets the contents of an array of strings #GVariant. This call
+ * makes a deep copy; the return result should be released with
+ * g_strfreev().
+ *
+ * If @length is non-%NULL then the number of elements in the result
+ * is stored there. In any case, the resulting array will be
+ * %NULL-terminated.
+ *
+ * For an empty array, @length will be set to 0 and a pointer to a
+ * %NULL pointer will be returned.
+ **/
+gchar **
+g_variant_dup_strv (GVariant *value,
+ gsize *length)
+{
+ gchar **strv;
+ gsize n;
+ gsize i;
+
+ TYPE_CHECK (value, G_VARIANT_TYPE ("as"), NULL);
+
+ n = g_variant_n_children (value);
+ strv = g_new (gchar *, n + 1);
+
+ for (i = 0; i < n; i++)
+ {
+ GVariant *string;
+
+ string = g_variant_get_child_value (value, i);
+ strv[i] = g_variant_dup_string (string, NULL);
+ g_variant_unref (string);
+ }
+ strv[i] = NULL;
+
+ if (length)
+ *length = n;
+
+ return strv;
+}
+
+GVariant *
+g_variant_new_array (const GVariantType *child_type,
+ GVariant * const *children,
+ gsize n_children)
+{
+ g_assert_not_reached ();
+}
+
+GVariant *
+g_variant_new_tuple (GVariant * const *children,
+ gsize n_children)
+{
+ g_assert_not_reached ();
+}
+
+GVariant *
+g_variant_new_dict_entry (GVariant *key,
+ GVariant *value)
+{
+ g_assert_not_reached ();
+}
+
+gconstpointer
+g_variant_get_fixed_array (GVariant *value,
+ gsize *n_elements,
+ gsize element_size)
+{
+ GVariantTypeInfo *array_info;
+ gsize array_element_size;
+ gconstpointer data;
+ gsize size;
+
+ TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
+
+ g_return_val_if_fail (n_elements != NULL, NULL);
+ g_return_val_if_fail (element_size > 0, NULL);
+
+ array_info = g_variant_get_type_info (value);
+ g_variant_type_info_query_element (array_info, NULL, &array_element_size);
+
+ g_return_val_if_fail (array_element_size, NULL);
+
+ if G_UNLIKELY (array_element_size != element_size)
+ {
+ if (array_element_size)
+ g_critical ("g_variant_get_fixed_array: assertion "
+ "`g_variant_array_has_fixed_size (value, element_size)' "
+ "failed: array size %"G_GSIZE_FORMAT" does not match "
+ "given element_size %"G_GSIZE_FORMAT".",
+ array_element_size, element_size);
+ else
+ g_critical ("g_variant_get_fixed_array: assertion "
+ "`g_variant_array_has_fixed_size (value, element_size)' "
+ "failed: array does not have fixed size.");
+ }
+
+ data = g_variant_get_data (value);
+ size = g_variant_get_size (value);
+
+ if (size % element_size)
+ *n_elements = 0;
+ else
+ *n_elements = size / element_size;
+
+ if (*n_elements)
+ return data;
+
+ return NULL;
+}
+
+/**
+ * g_variant_classify:
+ * @value: a #GVariant
+ * @returns: the #GVariantClass of @value
+ *
+ * Classifies @value according to its top-level type.
+ **/
+/**
+ * GVariantClass:
+ *
+ *
+ * The range of possible top-level types of #GVariant instances.
+ **/
+GVariantClass
+g_variant_classify (GVariant *value)
+{
+ g_return_val_if_fail (value != NULL, 0);
+
+ return *g_variant_get_type_string (value);
+}
+
+#define __G_VARIANT_C__
+#include "galiasdef.c"
+
diff --git a/glib/gvariant.h b/glib/gvariant.h
index be9c5f3..9a2f326 100644
--- a/glib/gvariant.h
+++ b/glib/gvariant.h
@@ -29,13 +29,95 @@ 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 value);
+GVariant * g_variant_new_byte (guchar value);
+GVariant * g_variant_new_int16 (gint16 value);
+GVariant * g_variant_new_uint16 (guint16 value);
+GVariant * g_variant_new_int32 (gint32 value);
+GVariant * g_variant_new_uint32 (guint32 value);
+GVariant * g_variant_new_int64 (gint64 value);
+GVariant * g_variant_new_uint64 (guint64 value);
+GVariant * g_variant_new_handle (gint32 value);
+GVariant * g_variant_new_double (gdouble value);
+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);
+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);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]