* in the gio library, for those.)
*
* For space-efficiency, the #GVariant serialisation format does not
- * automatically include the variant's type or endianness, which must
- * either be implied from context (such as knowledge that a particular
- * file format always contains a little-endian %G_VARIANT_TYPE_VARIANT)
- * or supplied out-of-band (for instance, a type and/or endianness
+ * automatically include the variant's length, type or endianness,
+ * which must either be implied from context (such as knowledge that a
+ * particular file format always contains a little-endian
+ * %G_VARIANT_TYPE_VARIANT which occupies the whole length of the file)
+ * or supplied out-of-band (for instance, a length, type and/or endianness
* indicator could be placed at the beginning of a file, network message
* or network stream).
*
*
* This is the memory that is used for storing GVariant data in
* serialised form. This is what would be sent over the network or
- * what would end up on disk.
+ * what would end up on disk, not counting any indicator of the
+ * endianness, or of the length or type of the top-level variant.
*
* The amount of memory required to store a boolean is 1 byte. 16,
- * 32 and 64 bit integers and double precision floating point numbers
+ * 32 and 64 bit integers and floating point numbers
* use their "natural" size. Strings (including object path and
* signature strings) are stored with a nul terminator, and as such
* use the length of the string plus 1 byte.
* padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
*
* We now require extra padding between the two items in the array.
- * After the 14 bytes of the first item, that's 2 bytes required. We
- * now require 2 framing offsets for an extra two bytes. 14 + 2 + 11
- * + 2 = 29 bytes to encode the entire two-item dictionary.
+ * After the 14 bytes of the first item, that's 2 bytes required.
+ * We now require 2 framing offsets for an extra two
+ * bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item
+ * dictionary.
*
* ## Type Information Cache
*
gconstpointer data,
gsize size)
{
- GVariant *value;
- GBytes *bytes;
-
- bytes = g_bytes_new (data, size);
- value = g_variant_new_from_bytes (type, bytes, TRUE);
- g_bytes_unref (bytes);
+ gpointer mydata = g_memdup (data, size);
- return value;
+ return g_variant_new_serialised (g_variant_type_info_get (type),
+ g_bytes_new_take (mydata, size), mydata, size, TRUE);
}
/**
}
/* the constructors and accessors for byte, int{16,32,64}, handles and
- * doubles all look pretty much exactly the same, so we reduce
+ * floats all look pretty much exactly the same, so we reduce
* copy/pasting here.
*/
#define NUMERIC_TYPE(TYPE, type, ctype) \
NUMERIC_TYPE (HANDLE, handle, gint32)
/**
+ * g_variant_new_float:
+ * @value: a #gfloat floating point value
+ *
+ * Creates a new float #GVariant instance.
+ *
+ * Returns: (transfer none): a floating reference to a new float #GVariant instance
+ *
+ * Since: 2.44
+ **/
+/**
+ * g_variant_get_float:
+ * @value: a float #GVariant instance
+ *
+ * Returns the single 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_FLOAT.
+ *
+ * Returns: a #gfloat
+ *
+ * Since: 2.44
+ **/
+NUMERIC_TYPE (FLOAT, float, gfloat)
+
+/**
* g_variant_new_double:
* @value: a #gdouble floating point value
*
g_variant_new_maybe (const GVariantType *child_type,
GVariant *child)
{
+ GVariantTypeInfo *type_info;
GVariantType *maybe_type;
- GVariant *value;
g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite
(child_type), 0);
child_type = g_variant_get_type (child);
maybe_type = g_variant_type_new_maybe (child_type);
+ type_info = g_variant_type_info_get (maybe_type);
+ g_variant_type_free (maybe_type);
if (child != NULL)
{
children[0] = g_variant_ref_sink (child);
trusted = g_variant_is_trusted (children[0]);
- value = g_variant_new_from_children (maybe_type, children, 1, trusted);
+ return g_variant_new_from_children (type_info, children, 1, trusted);
}
else
- value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE);
-
- g_variant_type_free (maybe_type);
-
- return value;
+ return g_variant_new_from_children (type_info, NULL, 0, TRUE);
}
/**
g_variant_ref_sink (value);
- return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
+ return g_variant_new_from_children (g_variant_type_info_get (G_VARIANT_TYPE_VARIANT),
g_memdup (&value, sizeof value),
1, g_variant_is_trusted (value));
}
GVariant * const *children,
gsize n_children)
{
+ GVariantTypeInfo *type_info;
GVariantType *array_type;
GVariant **my_children;
gboolean trusted;
- GVariant *value;
gsize i;
g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
if (child_type == NULL)
child_type = g_variant_get_type (children[0]);
array_type = g_variant_type_new_array (child_type);
+ type_info = g_variant_type_info_get (array_type);
+ g_variant_type_free (array_type);
for (i = 0; i < n_children; i++)
{
trusted &= g_variant_is_trusted (children[i]);
}
- value = g_variant_new_from_children (array_type, my_children,
- n_children, trusted);
- g_variant_type_free (array_type);
-
- return value;
+ return g_variant_new_from_children (type_info, my_children, n_children, trusted);
}
/*< private >
g_variant_new_tuple (GVariant * const *children,
gsize n_children)
{
+ GVariantTypeInfo *type_info;
GVariantType *tuple_type;
GVariant **my_children;
gboolean trusted;
- GVariant *value;
gsize i;
g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
}
tuple_type = g_variant_make_tuple_type (children, n_children);
- value = g_variant_new_from_children (tuple_type, my_children,
- n_children, trusted);
+ type_info = g_variant_type_info_get (tuple_type);
g_variant_type_free (tuple_type);
- return value;
+ return g_variant_new_from_children (type_info, my_children, n_children, trusted);
}
/*< private >
g_variant_new_dict_entry (GVariant *key,
GVariant *value)
{
+ GVariantTypeInfo *type_info;
GVariantType *dict_type;
GVariant **children;
gboolean trusted;
trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
dict_type = g_variant_make_dict_entry_type (key, value);
- value = g_variant_new_from_children (dict_type, children, 2, trusted);
+ type_info = g_variant_type_info_get (dict_type);
g_variant_type_free (dict_type);
- return value;
+ return g_variant_new_from_children (type_info, children, 2, trusted);
}
/**
* - %G_VARIANT_TYPE_BOOLEAN: #guchar (not #gboolean!)
* - %G_VARIANT_TYPE_BYTE: #guchar
* - %G_VARIANT_TYPE_HANDLE: #guint32
+ * - %G_VARIANT_TYPE_FLOAT: #gfloat
* - %G_VARIANT_TYPE_DOUBLE: #gdouble
*
* For example, if calling this function for an array of 32-bit integers,
for (i = 0; i < length; i++)
strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i]));
- return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY,
+ return g_variant_new_from_children (g_variant_type_info_get (G_VARIANT_TYPE_STRING_ARRAY),
strings, length, TRUE);
}
for (i = 0; i < length; i++)
strings[i] = g_variant_ref_sink (g_variant_new_object_path (strv[i]));
- return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY,
+ return g_variant_new_from_children (g_variant_type_info_get (G_VARIANT_TYPE_OBJECT_PATH_ARRAY),
strings, length, TRUE);
}
for (i = 0; i < length; i++)
strings[i] = g_variant_ref_sink (g_variant_new_bytestring (strv[i]));
- return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY,
+ return g_variant_new_from_children (g_variant_type_info_get (G_VARIANT_TYPE_BYTESTRING_ARRAY),
strings, length, TRUE);
}
* @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
* @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
* @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
+ * @G_VARIANT_CLASS_FLOAT: The #GVariant is a single precision floating
+ * point value.
* @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
* point value.
* @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
g_variant_get_uint64 (value));
break;
+ case G_VARIANT_CLASS_FLOAT:
+ {
+ gchar buffer[100];
+ gint i;
+
+ g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_float (value));
+
+ for (i = 0; buffer[i]; i++)
+ if (buffer[i] == '.' || buffer[i] == 'e' ||
+ buffer[i] == 'n' || buffer[i] == 'N')
+ break;
+
+ /* if there is no '.' or 'e' in the float then add one */
+ if (buffer[i] == '\0')
+ {
+ buffer[i++] = '.';
+ buffer[i++] = '0';
+ buffer[i++] = '\0';
+ }
+
+ if (type_annotate)
+ g_string_append (string, "float ");
+ g_string_append (string, buffer);
+ }
+ break;
+
case G_VARIANT_CLASS_DOUBLE:
{
gchar buffer[100];
case G_VARIANT_CLASS_INT32:
case G_VARIANT_CLASS_UINT32:
case G_VARIANT_CLASS_HANDLE:
+ case G_VARIANT_CLASS_FLOAT:
{
const guint *ptr;
* two values that have types that are not exactly equal. For example,
* you cannot compare a 32-bit signed integer with a 32-bit unsigned
* integer. Also note that this function is not particularly
- * well-behaved when it comes to comparison of doubles; in particular,
+ * well-behaved when it comes to comparison of floats; in particular,
* the handling of incomparable values (ie: NaN) is undefined.
*
* If you only require an equality comparison, g_variant_equal() is more
* general.
*
- * Returns: negative value if a < b;
+ * Returns: negative value if a < b;
* zero if a = b;
- * positive value if a > b.
+ * positive value if a > b.
*
* Since: 2.26
**/
return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
}
+ case G_VARIANT_CLASS_FLOAT:
+ {
+ gfloat a_val = g_variant_get_float (a);
+ gfloat b_val = g_variant_get_float (b);
+
+ return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
+ }
+
case G_VARIANT_CLASS_DOUBLE:
{
gdouble a_val = g_variant_get_double (a);
*
* Here is an example for iterating with g_variant_iter_next_value():
* |[<!-- language="C" -->
- * /* recursively iterate a container */
+ * // recursively iterate a container
* void
* iterate_container_recursive (GVariant *container)
* {
else
g_assert_not_reached ();
- value = g_variant_new_from_children (my_type,
+ value = g_variant_new_from_children (g_variant_type_info_get (my_type),
g_renew (GVariant *,
GVSB(builder)->children,
GVSB(builder)->offset),
/* GVariantDict {{{1 */
/**
- * GVariantDict: (skip)
+ * GVariantDict:
*
* #GVariantDict is a mutable interface to #GVariant dictionaries.
*
* key is not found. Each returns the new dictionary as a floating
* #GVariant.
*
- * <example>
- * <title>Using stack-allocated #GVariantDict</title>
- * <programlisting>
+ * ## Using a stack-allocated GVariantDict
+ *
+ * |[<!-- language="C" -->
* GVariant *
* add_to_count (GVariant *orig,
* GError **error)
* GVariantDict dict;
* guint32 count;
*
- * g_variant_dict_init (&dict, orig);
- * if (!g_variant_dict_lookup (&dict, "count", "u", &count))
+ * g_variant_dict_init (&dict, orig);
+ * if (!g_variant_dict_lookup (&dict, "count", "u", &count))
* {
* g_set_error (...);
- * g_variant_dict_clear (&dict);
+ * g_variant_dict_clear (&dict);
* return NULL;
* }
*
- * g_variant_dict_insert (&dict, "count", "u", count + 1);
+ * g_variant_dict_insert (&dict, "count", "u", count + 1);
*
- * return g_variant_dict_end (&dict);
+ * return g_variant_dict_end (&dict);
* }
- * </programlisting>
- * </example>
+ * ]|
*
- * <example>
- * <title>Using heap-allocated #GVariantDict</title>
- * <programlisting>
+ * ## Using heap-allocated GVariantDict
+ *
+ * |[<!-- language="C" -->
* GVariant *
* add_to_count (GVariant *orig,
* GError **error)
*
* dict = g_variant_dict_new (orig);
*
- * if (g_variant_dict_lookup (dict, "count", "u", &count))
+ * if (g_variant_dict_lookup (dict, "count", "u", &count))
* {
* g_variant_dict_insert (dict, "count", "u", count + 1);
* result = g_variant_dict_end (dict);
*
* return result;
* }
- * </programlisting>
- * </example>
+ * ]|
*
* Since: 2.40
**/
switch (next_char())
{
case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
- case 'x': case 't': case 'h': case 'd': case 's': case 'o':
- case 'g': case 'v': case '*': case '?': case 'r':
+ case 'x': case 't': case 'h': case 'f': case 'd': case 's':
+ case 'o': case 'g': case 'v': case '*': case '?': case 'r':
break;
case 'm':
va_arg (*app, guint64);
return;
+ case 'f':
case 'd':
va_arg (*app, gdouble);
return;
case 'h':
return g_variant_new_handle (va_arg (*app, gint));
+ case 'f':
+ return g_variant_new_float (va_arg (*app, gdouble));
+
case 'd':
return g_variant_new_double (va_arg (*app, gdouble));
/* The code below assumes this */
G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32));
+G_STATIC_ASSERT (sizeof (gfloat) == sizeof (guint32));
G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
static void
*(gint32 *) ptr = g_variant_get_handle (value);
return;
+ case 'f':
+ *(gfloat *) ptr = g_variant_get_float (value);
+ return;
+
case 'd':
*(gdouble *) ptr = g_variant_get_double (value);
return;
case 'u':
case 'h':
case 'b':
+ case 'f':
*(guint32 *) ptr = 0;
return;
* specified in @format_string. This can be achieved by casting them. See
* the [GVariant varargs documentation][gvariant-varargs].
*
- * <programlisting>
+ * |[<!-- language="C" -->
* MyFlags some_flags = FLAG_ONE | FLAG_TWO;
* const gchar *some_strings[] = { "a", "b", "c", NULL };
* GVariant *new_variant;
* /<!-- -->* This cast is required. *<!-- -->/
* (guint64) some_flags,
* some_strings);
- * </programlisting>
+ * ]|
*
* Returns: a new floating #GVariant instance
*
*
* Here is an example for memory management with g_variant_iter_next():
* |[<!-- language="C" -->
- * /* Iterates a dictionary of type 'a{sv}' */
+ * // Iterates a dictionary of type 'a{sv}'
* void
* iterate_dictionary (GVariant *dictionary)
* {
* g_print ("Item '%s' has type '%s'\n", key,
* g_variant_get_type_string (value));
*
- * /* must free data for ourselves */
+ * // must free data for ourselves
* g_variant_unref (value);
* g_free (key);
* }
*
* Here is an example for memory management with g_variant_iter_loop():
* |[<!-- language="C" -->
- * /* Iterates a dictionary of type 'a{sv}' */
+ * // Iterates a dictionary of type 'a{sv}'
* void
* iterate_dictionary (GVariant *dictionary)
* {
* g_print ("Item '%s' has type '%s'\n", key,
* g_variant_get_type_string (value));
*
- * /* no need to free 'key' and 'value' here
- * * unless breaking out of this loop
- * */
+ * // no need to free 'key' and 'value' here
+ * // unless breaking out of this loop
* }
* }
* ]|
case G_VARIANT_CLASS_HANDLE:
return g_variant_new_handle (g_variant_get_handle (value));
+ case G_VARIANT_CLASS_FLOAT:
+ return g_variant_new_float (g_variant_get_float (value));
+
case G_VARIANT_CLASS_DOUBLE:
return g_variant_new_double (g_variant_get_double (value));
* Performs a byteswapping operation on the contents of @value. The
* result is that all multi-byte numeric data contained in @value is
* byteswapped. That includes 16, 32, and 64bit signed and unsigned
- * integers as well as file handles and double precision floating point
- * values.
+ * integers as well as file handles and floating point values.
*
* This function is an identity mapping on any value that does not
* contain multi-byte numeric data. That include strings, booleans,
GDestroyNotify notify,
gpointer user_data)
{
- GVariant *value;
GBytes *bytes;
g_return_val_if_fail (g_variant_type_is_definite (type), NULL);
g_return_val_if_fail (data != NULL || size == 0, NULL);
+ if (size == 0)
+ {
+ if (notify)
+ {
+ (* notify) (user_data);
+ notify = NULL;
+ }
+
+ data = NULL;
+ }
+
if (notify)
bytes = g_bytes_new_with_free_func (data, size, notify, user_data);
else
bytes = g_bytes_new_static (data, size);
- value = g_variant_new_from_bytes (type, bytes, trusted);
- g_bytes_unref (bytes);
+ return g_variant_new_serialised (g_variant_type_info_get (type), bytes, data, size, trusted);
+}
- return value;
+/**
+ * g_variant_new_from_bytes:
+ * @type: a #GVariantType
+ * @bytes: a #GBytes
+ * @trusted: if the contents of @bytes are trusted
+ *
+ * Constructs a new serialised-mode #GVariant instance. This is the
+ * inner interface for creation of new serialised values that gets
+ * called from various functions in gvariant.c.
+ *
+ * A reference is taken on @bytes.
+ *
+ * Returns: (transfer none): a new #GVariant with a floating reference
+ *
+ * Since: 2.36
+ */
+GVariant *
+g_variant_new_from_bytes (const GVariantType *type,
+ GBytes *bytes,
+ gboolean trusted)
+{
+ gconstpointer data;
+ gsize size;
+
+ g_return_val_if_fail (g_variant_type_is_definite (type), NULL);
+
+ data = g_bytes_get_data (bytes, &size);
+
+ return g_variant_new_serialised (g_variant_type_info_get (type), g_bytes_ref (bytes), data, size, trusted);
+}
+
+/**
+ * g_variant_get_data_as_bytes:
+ * @value: a #GVariant
+ *
+ * Returns a pointer to the serialised form of a #GVariant instance.
+ * The semantics of this function are exactly the same as
+ * g_variant_get_data(), except that the returned #GBytes holds
+ * a reference to the variant data.
+ *
+ * Returns: (transfer full): A new #GBytes representing the variant data
+ *
+ * Since: 2.36
+ */
+GBytes *
+g_variant_get_data_as_bytes (GVariant *value)
+{
+ gconstpointer data;
+ GBytes *bytes;
+ gsize size;
+ gconstpointer bytes_data;
+ gsize bytes_size;
+
+ data = g_variant_get_serialised (value, &bytes, &size);
+ bytes_data = g_bytes_get_data (bytes, &bytes_size);
+
+ /* Try to reuse the GBytes held internally by GVariant, if it exists
+ * and is covering exactly the correct range.
+ */
+ if (data == bytes_data && size == bytes_size)
+ return g_bytes_ref (bytes);
+
+ /* See g_variant_get_data() about why it can return NULL... */
+ else if (data == NULL)
+ return g_bytes_new_take (g_malloc0 (size), size);
+
+ /* Otherwise, make a new GBytes with reference to the old. */
+ else
+ return g_bytes_new_with_free_func (data, size, (GDestroyNotify) g_bytes_unref, g_bytes_ref (bytes));
}
/* Epilogue {{{1 */