From 47d558baa7d77093d22af27789a0caf3e0d17332 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sat, 26 Oct 2019 14:01:16 +0100 Subject: [PATCH 1/4] 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); } From 6ad799ac677ae241da8b32ecf33b5cc9dc1d30bc Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sat, 26 Oct 2019 14:03:16 +0100 Subject: [PATCH 2/4] Constify g_param_value_defaults() argument The GValue we pass in is supposed to not be modified by the GParamSpec. --- gobject/gparam.c | 5 ++--- gobject/gparam.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/gobject/gparam.c b/gobject/gparam.c index 17df8316b..1158cddb7 100644 --- a/gobject/gparam.c +++ b/gobject/gparam.c @@ -630,8 +630,8 @@ g_param_value_set_default (GParamSpec *pspec, * Returns: whether @value contains the canonical default for this @pspec */ gboolean -g_param_value_defaults (GParamSpec *pspec, - GValue *value) +g_param_value_defaults (GParamSpec *pspec, + const GValue *value) { GValue dflt_value = G_VALUE_INIT; gboolean defaults; @@ -640,7 +640,6 @@ g_param_value_defaults (GParamSpec *pspec, g_return_val_if_fail (G_IS_VALUE (value), FALSE); g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE); - g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec)); G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value); defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0; g_value_unset (&dflt_value); diff --git a/gobject/gparam.h b/gobject/gparam.h index 33f95f0c5..b6a554653 100644 --- a/gobject/gparam.h +++ b/gobject/gparam.h @@ -307,7 +307,7 @@ void g_param_value_set_default (GParamSpec *pspec, GValue *value); GLIB_AVAILABLE_IN_ALL gboolean g_param_value_defaults (GParamSpec *pspec, - GValue *value); + const GValue *value); GLIB_AVAILABLE_IN_ALL gboolean g_param_value_validate (GParamSpec *pspec, GValue *value); From f7824da85f2870a7deef7374f9fcf86a6a6a47cf Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sat, 26 Oct 2019 14:04:26 +0100 Subject: [PATCH 3/4] Do not validate a GValue initialized with the default There's really no point in going through validation, if we know the value we're validating is coming straight from the GParamSpec. --- gobject/gparamspecs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gobject/gparamspecs.c b/gobject/gparamspecs.c index 5d15c26e1..a110281ca 100644 --- a/gobject/gparamspecs.c +++ b/gobject/gparamspecs.c @@ -912,8 +912,11 @@ param_value_array_validate (GParamSpec *pspec, g_param_value_set_default (element_spec, element); changed++; } - /* validate array value against element_spec */ - changed += g_param_value_validate (element_spec, element); + else + { + /* validate array value against element_spec */ + changed += g_param_value_validate (element_spec, element); + } } } } From ca1dbb38d8b8213408add742b3f7461810f14a70 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Sat, 26 Oct 2019 14:06:31 +0100 Subject: [PATCH 4/4] tests: Do not init the default value The call to g_param_value_set_default() will do that for us, now. --- gio/tests/defaultvalue.c | 1 - 1 file changed, 1 deletion(-) diff --git a/gio/tests/defaultvalue.c b/gio/tests/defaultvalue.c index 84a8635da..de0e9b104 100644 --- a/gio/tests/defaultvalue.c +++ b/gio/tests/defaultvalue.c @@ -29,7 +29,6 @@ check_property (const char *output, if (g_param_value_defaults (pspec, value)) return; - g_value_init (&default_value, G_PARAM_SPEC_VALUE_TYPE (pspec)); g_param_value_set_default (pspec, &default_value); v = g_strdup_value_contents (value);