Add G_VALUE_INIT
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Sun, 17 Jul 2011 19:18:04 +0000 (21:18 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 13 Aug 2011 22:16:44 +0000 (18:16 -0400)
The implementation of GValue is not public or documented.  When
allocated on the stack, initializing a GValue is usually done as
documented with:

GValue value = { 0, };

There is lot code around (including WebKit) that added all the missing
fields, resulting in this ugly and non-obvious:

GValue value = { 0, { { 0 } } };

However, this doesn't play nice with -Wmissing-field-initializers for
example. Thus, G_VALUE_INIT.

http://bugzilla.gnome.org/show_bug.cgi?id=654793
http://bugzilla.gnome.org/show_bug.cgi?id=577231

docs/reference/gobject/gobject-sections.txt
gobject/gvalue.c
gobject/gvalue.h

index fefe1b7..0b1483a 100644 (file)
@@ -376,6 +376,7 @@ g_variant_type_get_gtype
 <SECTION>
 <TITLE>Generic values</TITLE>
 <FILE>generic_values</FILE>
+G_VALUE_INIT
 G_VALUE_HOLDS
 G_VALUE_TYPE
 G_VALUE_TYPE_NAME
index 7757474..bca9aa5 100644 (file)
@@ -73,9 +73,9 @@
  * main (int   argc,
  *       char *argv[])
  * {
- *   /&ast; GValues must start zero-filled &ast;/
- *   GValue a = {0};
- *   GValue b = {0};
+ *   /&ast; GValues must be initialized &ast;/
+ *   GValue a = G_VALUE_INIT;
+ *   GValue b = G_VALUE_INIT;
  *   const gchar *message;
  *
  *   g_type_init ();
index 60c1919..e1f6980 100644 (file)
@@ -161,6 +161,22 @@ void       g_value_register_transform_func (GType           src_type,
  */
 #define G_VALUE_NOCOPY_CONTENTS (1 << 27)
 
+/**
+ * G_VALUE_INIT:
+ *
+ * A #GValue must be initialized before it can be used.
+ * This macro can be used as initializer instead of an explicit
+ * <literal>{ 0 }</literal> when declaring a variable,
+ * but it cannot be assigned to a variable.
+ *
+ * |[
+ *   GValue value = G_VALUE_INIT;
+ * ]|
+ *
+ * Since: 2.30
+ */
+#define G_VALUE_INIT  { 0, { { 0 } } }
+
 
 G_END_DECLS