Merge branch 'param-speedups2' into 'main'

param: Add a value_is_valid vfunc

See merge request GNOME/glib!2677
This commit is contained in:
Philip Withnall 2022-05-25 13:41:12 +00:00
commit 321fe5d0a4
6 changed files with 734 additions and 41 deletions

View File

@ -531,6 +531,7 @@ g_param_spec_get_default_value
g_param_value_set_default
g_param_value_defaults
g_param_value_validate
g_param_value_is_valid
g_param_value_convert
g_param_values_cmp
g_param_spec_is_valid_name

View File

@ -1573,12 +1573,12 @@ object_set_property (GObject *object,
const GValue *value,
GObjectNotifyQueue *nqueue)
{
GValue tmp_value = G_VALUE_INIT;
GObjectClass *class = g_type_class_peek (pspec->owner_type);
GParamSpecClass *pclass;
guint param_id = PARAM_SPEC_PARAM_ID (pspec);
GParamSpec *redirect;
if (class == NULL)
if (G_UNLIKELY (class == NULL))
{
g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
@ -1589,33 +1589,46 @@ object_set_property (GObject *object,
if (redirect)
pspec = redirect;
/* provide a copy to work from, convert (if necessary) and validate */
g_value_init (&tmp_value, pspec->value_type);
if (!g_value_transform (value, &tmp_value))
g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
pspec->name,
g_type_name (pspec->value_type),
G_VALUE_TYPE_NAME (value));
else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
pclass = G_PARAM_SPEC_GET_CLASS (pspec);
if (g_value_type_compatible (G_VALUE_TYPE (value), pspec->value_type) &&
(pclass->value_validate == NULL ||
(pclass->value_is_valid != NULL && pclass->value_is_valid (pspec, value))))
{
gchar *contents = g_strdup_value_contents (value);
g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
contents,
G_VALUE_TYPE_NAME (value),
pspec->name,
g_type_name (pspec->value_type));
g_free (contents);
class->set_property (object, param_id, value, pspec);
}
else
{
class->set_property (object, param_id, &tmp_value, pspec);
/* provide a copy to work from, convert (if necessary) and validate */
GValue tmp_value = G_VALUE_INIT;
if (~pspec->flags & G_PARAM_EXPLICIT_NOTIFY &&
pspec->flags & G_PARAM_READABLE)
g_object_notify_queue_add (object, nqueue, pspec);
g_value_init (&tmp_value, pspec->value_type);
if (!g_value_transform (value, &tmp_value))
g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
pspec->name,
g_type_name (pspec->value_type),
G_VALUE_TYPE_NAME (value));
else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
{
gchar *contents = g_strdup_value_contents (value);
g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
contents,
G_VALUE_TYPE_NAME (value),
pspec->name,
g_type_name (pspec->value_type));
g_free (contents);
}
else
{
class->set_property (object, param_id, &tmp_value, pspec);
}
g_value_unset (&tmp_value);
}
g_value_unset (&tmp_value);
if ((pspec->flags & (G_PARAM_EXPLICIT_NOTIFY|G_PARAM_READABLE)) == G_PARAM_READABLE)
g_object_notify_queue_add (object, nqueue, pspec);
}
static void

View File

@ -705,6 +705,51 @@ g_param_value_validate (GParamSpec *pspec,
return FALSE;
}
/**
* g_param_value_is_valid:
* @pspec: a valid #GParamSpec
* @value: a #GValue of correct type for @pspec
*
* Return whether the contents of @value comply with the specifications
* set out by @pspec.
*
* Returns: whether the contents of @value comply with the specifications
* set out by @pspec.
*
* Since: 2.74
*/
gboolean
g_param_value_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecClass *class;
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), TRUE);
g_return_val_if_fail (G_IS_VALUE (value), TRUE);
g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), TRUE);
class = G_PARAM_SPEC_GET_CLASS (pspec);
if (class->value_is_valid)
return class->value_is_valid (pspec, value);
else if (class->value_validate)
{
GValue val = G_VALUE_INIT;
gboolean changed;
g_value_init (&val, G_VALUE_TYPE (value));
g_value_copy (value, &val);
changed = class->value_validate (pspec, &val);
g_value_unset (&val);
return !changed;
}
return TRUE;
}
/**
* g_param_value_convert:
* @pspec: a valid #GParamSpec

View File

@ -242,7 +242,10 @@ struct _GParamSpec
* g_param_value_validate().
* @values_cmp: Compares @value1 with @value2 according to this type
* (recommended, the default is memcmp()), see g_param_values_cmp().
*
* @value_is_valid: Checks if contents of @value comply with the specifications
* set out by this type, without modifying the value. This vfunc is optional.
* If it isn't set, GObject will use @value_validate. Since 2.74
*
* The class structure for the GParamSpec type.
* Normally, GParamSpec classes are filled by
* g_param_type_register_static().
@ -263,8 +266,12 @@ struct _GParamSpecClass
gint (*values_cmp) (GParamSpec *pspec,
const GValue *value1,
const GValue *value2);
gboolean (*value_is_valid) (GParamSpec *pspec,
const GValue *value);
/*< private >*/
gpointer dummy[4];
gpointer dummy[3];
};
/**
* GParameter:
@ -319,6 +326,9 @@ gboolean g_param_value_defaults (GParamSpec *pspec,
GLIB_AVAILABLE_IN_ALL
gboolean g_param_value_validate (GParamSpec *pspec,
GValue *value);
GLIB_AVAILABLE_IN_2_74
gboolean g_param_value_is_valid (GParamSpec *pspec,
const GValue *value);
GLIB_AVAILABLE_IN_ALL
gboolean g_param_value_convert (GParamSpec *pspec,
const GValue *src_value,

View File

@ -80,6 +80,16 @@ param_char_set_default (GParamSpec *pspec,
value->data[0].v_int = G_PARAM_SPEC_CHAR (pspec)->default_value;
}
static gboolean
param_char_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecChar *cspec = G_PARAM_SPEC_CHAR (pspec);
gint oval = value->data[0].v_int;
return cspec->minimum <= oval && oval <= cspec->maximum;
}
static gboolean
param_char_validate (GParamSpec *pspec,
GValue *value)
@ -109,6 +119,16 @@ param_uchar_set_default (GParamSpec *pspec,
value->data[0].v_uint = G_PARAM_SPEC_UCHAR (pspec)->default_value;
}
static gboolean
param_uchar_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecUChar *uspec = G_PARAM_SPEC_UCHAR (pspec);
guint oval = value->data[0].v_uint;
return uspec->minimum <= oval && oval <= uspec->maximum;
}
static gboolean
param_uchar_validate (GParamSpec *pspec,
GValue *value)
@ -128,6 +148,15 @@ param_boolean_set_default (GParamSpec *pspec,
value->data[0].v_int = G_PARAM_SPEC_BOOLEAN (pspec)->default_value;
}
static gboolean
param_boolean_is_valid (GParamSpec *pspec,
const GValue *value)
{
int oval = value->data[0].v_int;
return oval == FALSE || oval == TRUE;
}
static gboolean
param_boolean_validate (GParamSpec *pspec,
GValue *value)
@ -156,6 +185,16 @@ param_int_set_default (GParamSpec *pspec,
value->data[0].v_int = G_PARAM_SPEC_INT (pspec)->default_value;
}
static gboolean
param_int_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecInt *ispec = G_PARAM_SPEC_INT (pspec);
int oval = value->data[0].v_int;
return ispec->minimum <= oval && oval <= ispec->maximum;
}
static gboolean
param_int_validate (GParamSpec *pspec,
GValue *value)
@ -196,6 +235,16 @@ param_uint_set_default (GParamSpec *pspec,
value->data[0].v_uint = G_PARAM_SPEC_UINT (pspec)->default_value;
}
static gboolean
param_uint_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecUInt *uspec = G_PARAM_SPEC_UINT (pspec);
guint oval = value->data[0].v_uint;
return uspec->minimum <= oval && oval <= uspec->maximum;
}
static gboolean
param_uint_validate (GParamSpec *pspec,
GValue *value)
@ -241,6 +290,16 @@ param_long_set_default (GParamSpec *pspec,
value->data[0].v_long = G_PARAM_SPEC_LONG (pspec)->default_value;
}
static gboolean
param_long_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecLong *lspec = G_PARAM_SPEC_LONG (pspec);
glong oval = value->data[0].v_long;
return lspec->minimum <= oval && oval <= lspec->maximum;
}
static gboolean
param_long_validate (GParamSpec *pspec,
GValue *value)
@ -285,6 +344,16 @@ param_ulong_set_default (GParamSpec *pspec,
value->data[0].v_ulong = G_PARAM_SPEC_ULONG (pspec)->default_value;
}
static gboolean
param_ulong_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecULong *uspec = G_PARAM_SPEC_ULONG (pspec);
gulong oval = value->data[0].v_ulong;
return uspec->minimum <= oval && oval <= uspec->maximum;
}
static gboolean
param_ulong_validate (GParamSpec *pspec,
GValue *value)
@ -325,6 +394,16 @@ param_int64_set_default (GParamSpec *pspec,
value->data[0].v_int64 = G_PARAM_SPEC_INT64 (pspec)->default_value;
}
static gboolean
param_int64_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecInt64 *lspec = G_PARAM_SPEC_INT64 (pspec);
gint64 oval = value->data[0].v_int64;
return lspec->minimum <= oval && oval <= lspec->maximum;
}
static gboolean
param_int64_validate (GParamSpec *pspec,
GValue *value)
@ -365,6 +444,16 @@ param_uint64_set_default (GParamSpec *pspec,
value->data[0].v_uint64 = G_PARAM_SPEC_UINT64 (pspec)->default_value;
}
static gboolean
param_uint64_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecUInt64 *uspec = G_PARAM_SPEC_UINT64 (pspec);
guint64 oval = value->data[0].v_uint64;
return uspec->minimum <= oval && oval <= uspec->maximum;
}
static gboolean
param_uint64_validate (GParamSpec *pspec,
GValue *value)
@ -403,6 +492,13 @@ param_unichar_set_default (GParamSpec *pspec,
value->data[0].v_uint = G_PARAM_SPEC_UNICHAR (pspec)->default_value;
}
static gboolean
param_unichar_is_valid (GParamSpec *pspec,
const GValue *value)
{
return g_unichar_validate (value->data[0].v_uint);
}
static gboolean
param_unichar_validate (GParamSpec *pspec,
GValue *value)
@ -461,6 +557,16 @@ param_enum_set_default (GParamSpec *pspec,
value->data[0].v_long = G_PARAM_SPEC_ENUM (pspec)->default_value;
}
static gboolean
param_enum_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecEnum *espec = G_PARAM_SPEC_ENUM (pspec);
glong oval = value->data[0].v_long;
return g_enum_get_value (espec->enum_class, oval) != NULL;
}
static gboolean
param_enum_validate (GParamSpec *pspec,
GValue *value)
@ -506,6 +612,15 @@ param_flags_set_default (GParamSpec *pspec,
value->data[0].v_ulong = G_PARAM_SPEC_FLAGS (pspec)->default_value;
}
static gboolean
param_flags_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecFlags *fspec = G_PARAM_SPEC_FLAGS (pspec);
gulong oval = value->data[0].v_ulong;
return (oval & ~fspec->flags_class->mask) == 0;
}
static gboolean
param_flags_validate (GParamSpec *pspec,
GValue *value)
@ -539,6 +654,16 @@ param_float_set_default (GParamSpec *pspec,
value->data[0].v_float = G_PARAM_SPEC_FLOAT (pspec)->default_value;
}
static gboolean
param_float_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecFloat *fspec = G_PARAM_SPEC_FLOAT (pspec);
gfloat oval = value->data[0].v_float;
return fspec->minimum <= oval && oval <= fspec->maximum;
}
static gboolean
param_float_validate (GParamSpec *pspec,
GValue *value)
@ -582,6 +707,16 @@ param_double_set_default (GParamSpec *pspec,
value->data[0].v_double = G_PARAM_SPEC_DOUBLE (pspec)->default_value;
}
static gboolean
param_double_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecDouble *dspec = G_PARAM_SPEC_DOUBLE (pspec);
gfloat oval = value->data[0].v_double;
return dspec->minimum <= oval && oval <= dspec->maximum;
}
static gboolean
param_double_validate (GParamSpec *pspec,
GValue *value)
@ -702,6 +837,29 @@ param_string_validate (GParamSpec *pspec,
return changed;
}
static gboolean
param_string_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecString *sspec = G_PARAM_SPEC_STRING (pspec);
gboolean ret = TRUE;
if (sspec->cset_first != NULL || sspec->cset_nth != NULL ||
sspec->ensure_non_null || sspec->null_fold_if_empty)
{
GValue tmp_value = G_VALUE_INIT;
g_value_init (&tmp_value, G_VALUE_TYPE (value));
g_value_copy (value, &tmp_value);
ret = !param_string_validate (pspec, &tmp_value);
g_value_unset (&tmp_value);
}
return ret;
}
static gint
param_string_values_cmp (GParamSpec *pspec,
const GValue *value1,
@ -728,6 +886,15 @@ param_param_set_default (GParamSpec *pspec,
value->data[0].v_pointer = NULL;
}
static gboolean
param_param_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpec *param = value->data[0].v_pointer;
return g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_PARAM_SPEC_VALUE_TYPE (pspec));
}
static gboolean
param_param_validate (GParamSpec *pspec,
GValue *value)
@ -785,16 +952,6 @@ param_pointer_set_default (GParamSpec *pspec,
value->data[0].v_pointer = NULL;
}
static gboolean
param_pointer_validate (GParamSpec *pspec,
GValue *value)
{
/* GParamSpecPointer *spec = G_PARAM_SPEC_POINTER (pspec); */
guint changed = 0;
return changed;
}
static gint
param_pointer_values_cmp (GParamSpec *pspec,
const GValue *value1,
@ -972,6 +1129,17 @@ param_object_set_default (GParamSpec *pspec,
value->data[0].v_pointer = NULL;
}
static gboolean
param_object_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecObject *ospec = G_PARAM_SPEC_OBJECT (pspec);
GObject *object = value->data[0].v_pointer;
return object &&
g_value_type_compatible (G_OBJECT_TYPE (object), G_PARAM_SPEC_VALUE_TYPE (ospec));
}
static gboolean
param_object_validate (GParamSpec *pspec,
GValue *value)
@ -1033,6 +1201,15 @@ param_override_set_default (GParamSpec *pspec,
g_param_value_set_default (ospec->overridden, value);
}
static gboolean
param_override_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
return g_param_value_is_valid (ospec->overridden, value);
}
static gboolean
param_override_validate (GParamSpec *pspec,
GValue *value)
@ -1066,6 +1243,17 @@ param_gtype_set_default (GParamSpec *pspec,
value->data[0].v_pointer = GSIZE_TO_POINTER (tspec->is_a_type);
}
static gboolean
param_gtype_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecGType *tspec = G_PARAM_SPEC_GTYPE (pspec);
GType gtype = GPOINTER_TO_SIZE (value->data[0].v_pointer);
return tspec->is_a_type == G_TYPE_NONE ||
g_type_is_a (gtype, tspec->is_a_type);
}
static gboolean
param_gtype_validate (GParamSpec *pspec,
GValue *value)
@ -1126,6 +1314,19 @@ param_variant_set_default (GParamSpec *pspec,
value->data[1].v_uint |= G_VALUE_NOCOPY_CONTENTS;
}
static gboolean
param_variant_is_valid (GParamSpec *pspec,
const GValue *value)
{
GParamSpecVariant *vspec = G_PARAM_SPEC_VARIANT (pspec);
GVariant *variant = value->data[0].v_pointer;
if (variant == NULL)
return vspec->default_value == NULL;
else
return g_variant_is_of_type (variant, vspec->type);
}
static gboolean
param_variant_validate (GParamSpec *pspec,
GValue *value)
@ -1181,6 +1382,13 @@ param_variant_values_cmp (GParamSpec *pspec,
}
/* --- type initialization --- */
#define set_is_valid_vfunc(type,func) { \
GParamSpecClass *class = g_type_class_ref (type); \
class->value_is_valid = func; \
g_type_class_unref (class); \
}
GType *g_param_spec_types = NULL;
void
@ -1212,6 +1420,7 @@ _g_param_spec_types_init (void)
param_int_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamChar"), &pspec_info);
set_is_valid_vfunc (type, param_char_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_CHAR);
}
@ -1230,6 +1439,7 @@ _g_param_spec_types_init (void)
param_uint_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamUChar"), &pspec_info);
set_is_valid_vfunc (type, param_uchar_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_UCHAR);
}
@ -1248,6 +1458,7 @@ _g_param_spec_types_init (void)
param_int_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamBoolean"), &pspec_info);
set_is_valid_vfunc (type, param_boolean_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_BOOLEAN);
}
@ -1266,6 +1477,7 @@ _g_param_spec_types_init (void)
param_int_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamInt"), &pspec_info);
set_is_valid_vfunc (type, param_int_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_INT);
}
@ -1284,6 +1496,7 @@ _g_param_spec_types_init (void)
param_uint_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamUInt"), &pspec_info);
set_is_valid_vfunc (type, param_uint_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_UINT);
}
@ -1302,6 +1515,7 @@ _g_param_spec_types_init (void)
param_long_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamLong"), &pspec_info);
set_is_valid_vfunc (type, param_long_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_LONG);
}
@ -1320,6 +1534,7 @@ _g_param_spec_types_init (void)
param_ulong_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamULong"), &pspec_info);
set_is_valid_vfunc (type, param_ulong_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_ULONG);
}
@ -1338,6 +1553,7 @@ _g_param_spec_types_init (void)
param_int64_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamInt64"), &pspec_info);
set_is_valid_vfunc (type, param_int64_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_INT64);
}
@ -1356,6 +1572,7 @@ _g_param_spec_types_init (void)
param_uint64_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamUInt64"), &pspec_info);
set_is_valid_vfunc (type, param_uint64_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_UINT64);
}
@ -1374,6 +1591,7 @@ _g_param_spec_types_init (void)
param_unichar_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamUnichar"), &pspec_info);
set_is_valid_vfunc (type, param_unichar_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_UNICHAR);
}
@ -1392,6 +1610,7 @@ _g_param_spec_types_init (void)
param_long_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamEnum"), &pspec_info);
set_is_valid_vfunc (type, param_enum_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_ENUM);
}
@ -1410,6 +1629,7 @@ _g_param_spec_types_init (void)
param_ulong_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamFlags"), &pspec_info);
set_is_valid_vfunc (type, param_flags_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_FLAGS);
}
@ -1428,6 +1648,7 @@ _g_param_spec_types_init (void)
param_float_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamFloat"), &pspec_info);
set_is_valid_vfunc (type, param_float_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_FLOAT);
}
@ -1446,6 +1667,7 @@ _g_param_spec_types_init (void)
param_double_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamDouble"), &pspec_info);
set_is_valid_vfunc (type, param_double_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_DOUBLE);
}
@ -1464,6 +1686,7 @@ _g_param_spec_types_init (void)
param_string_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamString"), &pspec_info);
set_is_valid_vfunc (type, param_string_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_STRING);
}
@ -1482,6 +1705,7 @@ _g_param_spec_types_init (void)
param_pointer_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamParam"), &pspec_info);
set_is_valid_vfunc (type, param_param_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_PARAM);
}
@ -1514,7 +1738,7 @@ _g_param_spec_types_init (void)
G_TYPE_POINTER, /* value_type */
NULL, /* finalize */
param_pointer_set_default, /* value_set_default */
param_pointer_validate, /* value_validate */
NULL,
param_pointer_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamPointer"), &pspec_info);
@ -1555,6 +1779,7 @@ _g_param_spec_types_init (void)
param_object_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamObject"), &pspec_info);
set_is_valid_vfunc (type, param_object_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_OBJECT);
}
@ -1573,6 +1798,7 @@ _g_param_spec_types_init (void)
param_override_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamOverride"), &pspec_info);
set_is_valid_vfunc (type, param_override_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_OVERRIDE);
}
@ -1592,6 +1818,7 @@ _g_param_spec_types_init (void)
};
pspec_info.value_type = G_TYPE_GTYPE;
type = g_param_type_register_static (g_intern_static_string ("GParamGType"), &pspec_info);
set_is_valid_vfunc (type, param_gtype_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_GTYPE);
}
@ -1610,6 +1837,7 @@ _g_param_spec_types_init (void)
param_variant_values_cmp, /* values_cmp */
};
type = g_param_type_register_static (g_intern_static_string ("GParamVariant"), &pspec_info);
set_is_valid_vfunc (type, param_variant_is_valid);
*spec_types++ = type;
g_assert (type == G_TYPE_PARAM_VARIANT);
}

View File

@ -48,40 +48,305 @@ test_param_spec_char (void)
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_char (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 20);
g_value_set_char (&value, 20);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 20);
g_value_set_char (&value, 40);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 40);
g_value_set_char (&value, 60);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 40);
g_value_set_schar (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_schar (&value), ==, 20);
g_value_set_schar (&value, 20);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_schar (&value), ==, 20);
g_value_set_schar (&value, 40);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_schar (&value), ==, 40);
g_value_set_schar (&value, 60);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_schar (&value), ==, 40);
g_param_spec_unref (pspec);
}
static void
test_param_spec_uchar (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_uchar ("char", NULL, NULL,
20, 40, 30, G_PARAM_READWRITE);
g_assert_cmpstr (g_param_spec_get_name (pspec), ==, "char");
g_value_init (&value, G_TYPE_UCHAR);
g_value_set_uchar (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_uchar (&value), ==, 20);
g_value_set_uchar (&value, 20);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_uchar (&value), ==, 20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_int (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_int ("int", NULL, NULL,
20, 40, 30, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_INT);
g_assert_cmpint (g_value_get_int (&value), ==, 30);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_int (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_int (&value), ==, 20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_uint (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_uint ("uint", NULL, NULL,
20, 40, 30, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_UINT);
g_assert_cmpint (g_value_get_uint (&value), ==, 30);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_uint (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_uint (&value), ==, 20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_long (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_long ("long", NULL, NULL,
20, 40, 30, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_LONG);
g_assert_cmpint (g_value_get_long (&value), ==, 30);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_long (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_long (&value), ==, 20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_ulong (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_ulong ("ulong", NULL, NULL,
20, 40, 30, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_ULONG);
g_assert_cmpint (g_value_get_ulong (&value), ==, 30);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_ulong (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_ulong (&value), ==, 20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_int64 (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_int64 ("int64", NULL, NULL,
20, 40, 30, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_INT64);
g_assert_cmpint (g_value_get_int64 (&value), ==, 30);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_int64 (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_int64 (&value), ==, 20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_uint64 (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_uint64 ("uint64", NULL, NULL,
20, 40, 30, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_UINT64);
g_assert_cmpint (g_value_get_uint64 (&value), ==, 30);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_uint64 (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_uint64 (&value), ==, 20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_float (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_float ("float", NULL, NULL,
20.0, 40.0, 30.0, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_FLOAT);
g_assert_cmpfloat (g_value_get_float (&value), ==, 30.0);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_float (&value, 0.0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_float (&value), ==, 20.0);
g_param_spec_unref (pspec);
}
static void
test_param_spec_double (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_double ("double", NULL, NULL,
20.0, 40.0, 30.0, G_PARAM_READWRITE);
g_param_value_set_default (pspec, &value);
g_assert_true (G_VALUE_TYPE (&value) == G_TYPE_DOUBLE);
g_assert_cmpfloat (g_value_get_double (&value), ==, 30.0);
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_double (&value, 0.0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_double (&value), ==, 20.0);
g_param_spec_unref (pspec);
}
static void
test_param_spec_unichar (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
pspec = g_param_spec_unichar ("unichar", NULL, NULL,
0x1F4A9, G_PARAM_READWRITE);
g_assert_cmpstr (g_param_spec_get_name (pspec), ==, "unichar");
g_value_init (&value, G_TYPE_UINT);
/* Unicode codepoints cant be 0x110000 or above, as thats not representable
* in UTF-16. */
g_value_set_uint (&value, 0x110000);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_uint (&value), ==, 0);
g_value_set_uint (&value, 0x20);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_uint (&value), ==, 0x20);
g_param_spec_unref (pspec);
}
static void
test_param_spec_param (void)
{
GParamSpec *wrapped_pspec_uint;
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
wrapped_pspec_uint = g_param_spec_uint ("uint", NULL, NULL,
0, G_MAXUINT, 5, G_PARAM_READWRITE);
pspec = g_param_spec_param ("param", NULL, NULL,
G_TYPE_PARAM_UINT, G_PARAM_READWRITE);
g_assert_cmpstr (g_param_spec_get_name (pspec), ==, "param");
g_value_init (&value, G_TYPE_PARAM_UINT);
g_value_set_param (&value, wrapped_pspec_uint);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_true (g_value_get_param (&value) == wrapped_pspec_uint);
g_value_unset (&value);
g_param_spec_unref (pspec);
g_param_spec_unref (wrapped_pspec_uint);
}
static void
test_param_spec_string (void)
{
@ -93,9 +358,11 @@ test_param_spec_string (void)
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, "foobar");
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_value_set_string (&value, "");
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_nonnull (g_value_get_string (&value));
@ -104,6 +371,7 @@ test_param_spec_string (void)
G_PARAM_SPEC_STRING (pspec)->ensure_non_null = TRUE;
g_value_set_string (&value, NULL);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_nonnull (g_value_get_string (&value));
@ -114,10 +382,12 @@ test_param_spec_string (void)
G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = TRUE;
g_value_set_string (&value, "");
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_null (g_value_get_string (&value));
g_value_set_static_string (&value, "");
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_null (g_value_get_string (&value));
@ -129,10 +399,12 @@ test_param_spec_string (void)
G_PARAM_SPEC_STRING (pspec)->substitutor = '-';
g_value_set_string (&value, "ABC");
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_string (&value)[0], ==, '-');
g_value_set_static_string (&value, "ABC");
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_string (&value)[0], ==, '-');
@ -141,10 +413,12 @@ test_param_spec_string (void)
G_PARAM_SPEC_STRING (pspec)->cset_nth = g_strdup ("abc");
g_value_set_string (&value, "aBC");
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_string (&value)[1], ==, '-');
g_value_set_static_string (&value, "aBC");
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_string (&value)[1], ==, '-');
@ -173,18 +447,22 @@ test_param_spec_override (void)
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_char (&value, 0);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 20);
g_value_set_char (&value, 20);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 20);
g_value_set_char (&value, 40);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 40);
g_value_set_char (&value, 60);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_char (&value), ==, 40);
@ -207,10 +485,12 @@ test_param_spec_gtype (void)
g_assert_true (g_param_value_defaults (pspec, &value));
g_value_set_gtype (&value, G_TYPE_INT);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_gtype (&value), ==, G_TYPE_PARAM);
g_value_set_gtype (&value, G_TYPE_PARAM_INT);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_assert_cmpint (g_value_get_gtype (&value), ==, G_TYPE_PARAM_INT);
@ -226,7 +506,6 @@ test_param_spec_variant (void)
GValue value3 = G_VALUE_INIT;
GValue value4 = G_VALUE_INIT;
GValue value5 = G_VALUE_INIT;
gboolean modified;
pspec = g_param_spec_variant ("variant", "nick", "blurb",
G_VARIANT_TYPE ("i"),
@ -254,13 +533,14 @@ test_param_spec_variant (void)
g_assert_false (g_param_value_defaults (pspec, &value4));
g_assert_false (g_param_value_defaults (pspec, &value5));
modified = g_param_value_validate (pspec, &value);
g_assert_false (modified);
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_false (g_param_value_validate (pspec, &value));
g_value_reset (&value);
g_value_set_variant (&value, g_variant_new_uint32 (41));
modified = g_param_value_validate (pspec, &value);
g_assert_true (modified);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_true (g_param_value_validate (pspec, &value));
g_assert_true (g_param_value_is_valid (pspec, &value));
g_assert_cmpint (g_variant_get_int32 (g_value_get_variant (&value)), ==, 42);
g_value_unset (&value);
@ -1218,6 +1498,110 @@ test_param_is_valid_name (void)
g_assert_false (g_param_spec_is_valid_name (invalid_names[i]));
}
static void
param_int_init (GParamSpec *pspec)
{
GParamSpecInt *ispec = (GParamSpecInt *)pspec;
ispec->minimum = 0x7fffffff;
ispec->maximum = 0x80000000;
ispec->default_value = 0;
}
static void
param_int_set_default (GParamSpec *pspec,
GValue *value)
{
value->data[0].v_int = ((GParamSpecInt *)pspec)->default_value;
}
static gboolean
param_int_validate (GParamSpec *pspec,
GValue *value)
{
GParamSpecInt *ispec = (GParamSpecInt *)pspec;
int oval = value->data[0].v_int;
value->data[0].v_int = CLAMP (value->data[0].v_int, ispec->minimum, ispec->maximum);
return value->data[0].v_int != oval;
}
static int
param_int_values_cmp (GParamSpec *pspec,
const GValue *value1,
const GValue *value2)
{
if (value1->data[0].v_int < value2->data[0].v_int)
return -1;
else
return value1->data[0].v_int > value2->data[0].v_int;
}
static GType custom_type;
/* Register a pspec that has a validate vfunc, but not
* value_is_valid, to test the fallback in g_param_value_is_valid
*/
static void
register_custom_pspec (void)
{
const GParamSpecTypeInfo pspec_info = {
sizeof (GParamSpecInt), /* instance_size */
16, /* n_preallocs */
param_int_init, /* instance_init */
G_TYPE_INT, /* value_type */
NULL, /* finalize */
param_int_set_default, /* value_set_default */
param_int_validate, /* value_validate */
param_int_values_cmp, /* values_cmp */
};
custom_type = g_param_type_register_static ("GParamInt2", &pspec_info);
}
static GParamSpec *
g_param_spec_custom (const char *name,
int minimum,
int maximum,
int default_value,
GParamFlags flags)
{
GParamSpecInt *ispec;
g_return_val_if_fail (default_value >= minimum && default_value <= maximum, NULL);
ispec = g_param_spec_internal (custom_type, name, NULL, NULL, flags);
if (ispec == NULL)
return NULL;
ispec->minimum = minimum;
ispec->maximum = maximum;
ispec->default_value = default_value;
return G_PARAM_SPEC (ispec);
}
static void
test_param_spec_custom (void)
{
GParamSpec *pspec;
GValue value = G_VALUE_INIT;
register_custom_pspec ();
pspec = g_param_spec_custom ("myint", 10, 30, 20, G_PARAM_READWRITE);
g_value_init (&value, G_TYPE_INT);
g_value_set_int (&value, 40);
g_assert_false (g_param_value_is_valid (pspec, &value));
g_assert_cmpint (g_value_get_int (&value), ==, 40);
g_param_spec_unref (pspec);
}
int
main (int argc, char *argv[])
{
@ -1256,11 +1640,23 @@ main (int argc, char *argv[])
g_test_add_func ("/param/default", test_param_default);
g_test_add_func ("/param/is-valid-name", test_param_is_valid_name);
g_test_add_func ("/paramspec/char", test_param_spec_char);
g_test_add_func ("/paramspec/uchar", test_param_spec_uchar);
g_test_add_func ("/paramspec/int", test_param_spec_int);
g_test_add_func ("/paramspec/uint", test_param_spec_uint);
g_test_add_func ("/paramspec/long", test_param_spec_long);
g_test_add_func ("/paramspec/ulong", test_param_spec_ulong);
g_test_add_func ("/paramspec/int64", test_param_spec_int64);
g_test_add_func ("/paramspec/uint64", test_param_spec_uint64);
g_test_add_func ("/paramspec/float", test_param_spec_float);
g_test_add_func ("/paramspec/double", test_param_spec_double);
g_test_add_func ("/paramspec/unichar", test_param_spec_unichar);
g_test_add_func ("/paramspec/param", test_param_spec_param);
g_test_add_func ("/paramspec/string", test_param_spec_string);
g_test_add_func ("/paramspec/override", test_param_spec_override);
g_test_add_func ("/paramspec/gtype", test_param_spec_gtype);
g_test_add_func ("/paramspec/variant", test_param_spec_variant);
g_test_add_func ("/paramspec/variant/cmp", test_param_spec_variant_cmp);
g_test_add_func ("/paramspec/custom", test_param_spec_custom);
return g_test_run ();
}