[glib/param-value-default: 1/4] Allow passing empty GValue to g_param_value_set_default()



commit 47d558baa7d77093d22af27789a0caf3e0d17332
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Oct 26 14:01:16 2019 +0100

    Allow passing empty GValue to g_param_value_set_default()
    
    Since we have the type of the GValue we're going to initialize, we can
    allow passing an empty (but valid) GValue when retrieving the default
    value of a GParamSpec.
    
    This will eliminate additional checks and an unnecessary reset.

 gobject/gparam.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
---
diff --git a/gobject/gparam.c b/gobject/gparam.c
index 5aa54c02c..17df8316b 100644
--- a/gobject/gparam.c
+++ b/gobject/gparam.c
@@ -595,7 +595,8 @@ g_param_spec_get_redirect_target (GParamSpec *pspec)
 /**
  * g_param_value_set_default:
  * @pspec: a valid #GParamSpec
- * @value: a #GValue of correct type for @pspec
+ * @value: a #GValue of correct type for @pspec; since 2.64, you
+ *   can also pass an empty #GValue, initialized with %G_VALUE_INIT
  *
  * Sets @value to its default value as specified in @pspec.
  */
@@ -604,10 +605,18 @@ g_param_value_set_default (GParamSpec *pspec,
                           GValue     *value)
 {
   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
-  g_return_if_fail (G_IS_VALUE (value));
-  g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
 
-  g_value_reset (value);
+  if (G_VALUE_TYPE (value) == G_TYPE_INVALID)
+    {
+      g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
+    }
+  else
+    {
+      g_return_if_fail (G_IS_VALUE (value));
+      g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
+      g_value_reset (value);
+    }
+
   G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]