diff options
author | Michael Gratton <mike@vee.net> | 2020-05-11 21:23:20 +1000 |
---|---|---|
committer | Michael Gratton <mike@vee.net> | 2020-05-11 21:23:20 +1000 |
commit | 8c3be95651a1ebe61d402363b768654259ee9387 (patch) | |
tree | b1b17440db5527e5e969f45682f4673a2af327ce | |
parent | 2283d3e9eb8d63b46c79b736ea9c93d21e000429 (diff) | |
download | glib-8c3be95651a1ebe61d402363b768654259ee9387.tar.gz |
gobject: Add support for G_TYPE(U)SHORT to GParamSpec
Define and implement `GParamSpec(U)Short` and related macros,
`g_param_spec_(u)short`.
-rw-r--r-- | gobject/gparamspecs.c | 211 | ||||
-rw-r--r-- | gobject/gparamspecs.h | 114 | ||||
-rw-r--r-- | gobject/tests/param.c | 34 |
3 files changed, 358 insertions, 1 deletions
diff --git a/gobject/gparamspecs.c b/gobject/gparamspecs.c index c45ebc548..0cc63e49e 100644 --- a/gobject/gparamspecs.c +++ b/gobject/gparamspecs.c @@ -134,6 +134,87 @@ param_boolean_validate (GParamSpec *pspec, return value->data[0].v_int != oval; } + +static void +param_short_init (GParamSpec *pspec) +{ + GParamSpecShort *ispec = G_PARAM_SPEC_SHORT (pspec); + + ispec->minimum = 0x7fff; + ispec->maximum = 0x8000; + ispec->default_value = 0; +} + +static void +param_short_set_default (GParamSpec *pspec, + GValue *value) +{ + value->data[0].v_int = G_PARAM_SPEC_SHORT (pspec)->default_value; +} + +static gboolean +param_short_validate (GParamSpec *pspec, + GValue *value) +{ + GParamSpecShort *ispec = G_PARAM_SPEC_SHORT (pspec); + gshort 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 gint +param_short_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 void +param_ushort_init (GParamSpec *pspec) +{ + GParamSpecUShort *uspec = G_PARAM_SPEC_USHORT (pspec); + + uspec->minimum = 0; + uspec->maximum = 0xffff; + uspec->default_value = 0; +} + +static void +param_ushort_set_default (GParamSpec *pspec, + GValue *value) +{ + value->data[0].v_uint = G_PARAM_SPEC_USHORT (pspec)->default_value; +} + +static gboolean +param_ushort_validate (GParamSpec *pspec, + GValue *value) +{ + GParamSpecUShort *uspec = G_PARAM_SPEC_USHORT (pspec); + gushort oval = value->data[0].v_uint; + + value->data[0].v_uint = CLAMP (value->data[0].v_uint, uspec->minimum, uspec->maximum); + + return value->data[0].v_uint != oval; +} + +static gint +param_ushort_values_cmp (GParamSpec *pspec, + const GValue *value1, + const GValue *value2) +{ + if (value1->data[0].v_uint < value2->data[0].v_uint) + return -1; + else + return value1->data[0].v_uint > value2->data[0].v_uint; +} + static void param_int_init (GParamSpec *pspec) { @@ -1193,7 +1274,7 @@ GType *g_param_spec_types = NULL; void _g_param_spec_types_init (void) { - const guint n_types = 23; + const guint n_types = 25; GType type, *spec_types; #ifndef G_DISABLE_ASSERT GType *spec_types_bound; @@ -1620,6 +1701,42 @@ _g_param_spec_types_init (void) *spec_types++ = type; g_assert (type == G_TYPE_PARAM_VARIANT); } + + /* G_TYPE_PARAM_SHORT + */ + { + const GParamSpecTypeInfo pspec_info = { + sizeof (GParamSpecShort), /* instance_size */ + 16, /* n_preallocs */ + param_short_init, /* instance_init */ + G_TYPE_SHORT, /* value_type */ + NULL, /* finalize */ + param_short_set_default, /* value_set_default */ + param_short_validate, /* value_validate */ + param_short_values_cmp, /* values_cmp */ + }; + type = g_param_type_register_static (g_intern_static_string ("GParamShort"), &pspec_info); + *spec_types++ = type; + g_assert (type == G_TYPE_PARAM_SHORT); + } + + /* G_TYPE_PARAM_USHORT + */ + { + const GParamSpecTypeInfo pspec_info = { + sizeof (GParamSpecUShort), /* instance_size */ + 16, /* n_preallocs */ + param_ushort_init, /* instance_init */ + G_TYPE_USHORT, /* value_type */ + NULL, /* finalize */ + param_ushort_set_default, /* value_set_default */ + param_ushort_validate, /* value_validate */ + param_ushort_values_cmp, /* values_cmp */ + }; + type = g_param_type_register_static (g_intern_static_string ("GParamUShort"), &pspec_info); + *spec_types++ = type; + g_assert (type == G_TYPE_PARAM_USHORT); + } g_assert (spec_types == spec_types_bound); } @@ -1753,6 +1870,98 @@ g_param_spec_boolean (const gchar *name, } /** + * g_param_spec_short: + * @name: canonical name of the property specified + * @nick: nick name for the property specified + * @blurb: description of the property specified + * @minimum: minimum value for the property specified + * @maximum: maximum value for the property specified + * @default_value: default value for the property specified + * @flags: flags for the property specified + * + * Creates a new #GParamSpecShort instance specifying a %G_TYPE_SHORT property. + * + * See g_param_spec_internal() for details on property names. + * + * Returns: (transfer full): a newly created parameter specification + * + * Since: 2.66 + */ +GParamSpec* +g_param_spec_short (const gchar *name, + const gchar *nick, + const gchar *blurb, + gshort minimum, + gshort maximum, + gshort default_value, + GParamFlags flags) +{ + GParamSpecShort *ispec; + + g_return_val_if_fail (default_value >= minimum && default_value <= maximum, NULL); + + ispec = g_param_spec_internal (G_TYPE_PARAM_SHORT, + name, + nick, + blurb, + flags); + if (ispec == NULL) + return NULL; + + ispec->minimum = minimum; + ispec->maximum = maximum; + ispec->default_value = default_value; + + return G_PARAM_SPEC (ispec); +} + +/** + * g_param_spec_ushort: + * @name: canonical name of the property specified + * @nick: nick name for the property specified + * @blurb: description of the property specified + * @minimum: minimum value for the property specified + * @maximum: maximum value for the property specified + * @default_value: default value for the property specified + * @flags: flags for the property specified + * + * Creates a new #GParamSpecUShort instance specifying a %G_TYPE_USHORT property. + * + * See g_param_spec_internal() for details on property names. + * + * Returns: (transfer full): a newly created parameter specification + * + * Since: 2.66 + */ +GParamSpec* +g_param_spec_ushort (const gchar *name, + const gchar *nick, + const gchar *blurb, + gushort minimum, + gushort maximum, + gushort default_value, + GParamFlags flags) +{ + GParamSpecUShort *uspec; + + g_return_val_if_fail (default_value >= minimum && default_value <= maximum, NULL); + + uspec = g_param_spec_internal (G_TYPE_PARAM_USHORT, + name, + nick, + blurb, + flags); + if (uspec == NULL) + return NULL; + + uspec->minimum = minimum; + uspec->maximum = maximum; + uspec->default_value = default_value; + + return G_PARAM_SPEC (uspec); +} + +/** * g_param_spec_int: * @name: canonical name of the property specified * @nick: nick name for the property specified diff --git a/gobject/gparamspecs.h b/gobject/gparamspecs.h index d0e4d5953..8404f2d50 100644 --- a/gobject/gparamspecs.h +++ b/gobject/gparamspecs.h @@ -582,6 +582,64 @@ G_BEGIN_DECLS */ #define G_PARAM_SPEC_VARIANT(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), G_TYPE_PARAM_VARIANT, GParamSpecVariant)) +/** + * G_TYPE_PARAM_SHORT: + * + * The #GType of #GParamSpecShort. + * + * Since: 2.66 + */ +#define G_TYPE_PARAM_SHORT (g_param_spec_types[23]) +/** + * G_IS_PARAM_SPEC_SHORT: + * @pspec: a valid #GParamSpec instance + * + * Checks whether the given #GParamSpec is of type %G_TYPE_PARAM_SHORT. + * + * Returns: %TRUE on success. + * + * Since: 2.66 + */ +#define G_IS_PARAM_SPEC_SHORT(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), G_TYPE_PARAM_SHORT)) +/** + * G_PARAM_SPEC_SHORT: + * @pspec: a valid #GParamSpec instance + * + * Cast a #GParamSpec instance shorto a #GParamSpecShort. + * + * Since: 2.66 + */ +#define G_PARAM_SPEC_SHORT(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), G_TYPE_PARAM_SHORT, GParamSpecShort)) + +/** + * G_TYPE_PARAM_USHORT: + * + * The #GType of #GParamSpecUShort. + * + * Since: 2.66 + */ +#define G_TYPE_PARAM_USHORT (g_param_spec_types[24]) +/** + * G_IS_PARAM_SPEC_USHORT: + * @pspec: a valid #GParamSpec instance + * + * Checks whether the given #GParamSpec is of type %G_TYPE_PARAM_USHORT. + * + * Returns: %TRUE on success. + * + * Since: 2.66 + */ +#define G_IS_PARAM_SPEC_USHORT(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), G_TYPE_PARAM_USHORT)) +/** + * G_PARAM_SPEC_USHORT: + * @pspec: a valid #GParamSpec instance + * + * Cast a #GParamSpec instance shorto a #GParamSpecUShort. + * + * Since: 2.66 + */ +#define G_PARAM_SPEC_USHORT(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), G_TYPE_PARAM_USHORT, GParamSpecUShort)) + /* --- typedefs & structures --- */ typedef struct _GParamSpecChar GParamSpecChar; typedef struct _GParamSpecUChar GParamSpecUChar; @@ -606,6 +664,8 @@ typedef struct _GParamSpecObject GParamSpecObject; typedef struct _GParamSpecOverride GParamSpecOverride; typedef struct _GParamSpecGType GParamSpecGType; typedef struct _GParamSpecVariant GParamSpecVariant; +typedef struct _GParamSpecShort GParamSpecShort; +typedef struct _GParamSpecUShort GParamSpecUShort; /** * GParamSpecChar: @@ -979,6 +1039,44 @@ struct _GParamSpecVariant /*< private >*/ gpointer padding[4]; }; +/** + * GParamSpecShort: + * @parent_instance: private #GParamSpec portion + * @minimum: minimum value for the property specified + * @maximum: maximum value for the property specified + * @default_value: default value for the property specified + * + * A #GParamSpec derived structure that contains the meta data for short properties. + * + * Since: 2.66 + */ +struct _GParamSpecShort +{ + GParamSpec parent_instance; + + gshort minimum; + gshort maximum; + gshort default_value; +}; +/** + * GParamSpecUShort: + * @parent_instance: private #GParamSpec portion + * @minimum: minimum value for the property specified + * @maximum: maximum value for the property specified + * @default_value: default value for the property specified + * + * A #GParamSpec derived structure that contains the meta data for unsigned short properties. + * + * Since: 2.66 + */ +struct _GParamSpecUShort +{ + GParamSpec parent_instance; + + gushort minimum; + gushort maximum; + gushort default_value; +}; /* --- GParamSpec prototypes --- */ GLIB_AVAILABLE_IN_ALL @@ -1138,6 +1236,22 @@ GParamSpec* g_param_spec_variant (const gchar *name, const GVariantType *type, GVariant *default_value, GParamFlags flags); +GLIB_AVAILABLE_IN_ALL +GParamSpec* g_param_spec_short (const gchar *name, + const gchar *nick, + const gchar *blurb, + gshort minimum, + gshort maximum, + gshort default_value, + GParamFlags flags); +GLIB_AVAILABLE_IN_ALL +GParamSpec* g_param_spec_ushort (const gchar *name, + const gchar *nick, + const gchar *blurb, + gushort minimum, + gushort maximum, + gushort default_value, + GParamFlags flags); /* --- internal --- */ /* We prefix variable declarations so they can diff --git a/gobject/tests/param.c b/gobject/tests/param.c index 44faef10c..ea16a4724 100644 --- a/gobject/tests/param.c +++ b/gobject/tests/param.c @@ -179,6 +179,8 @@ test_value_transform (void) CHECK_INT_CONVERSION(G_TYPE_CHAR, schar, 124) CHECK_INT_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_INT_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_INT_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_INT_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_INT_CONVERSION(G_TYPE_INT, int, -12345) CHECK_INT_CONVERSION(G_TYPE_INT, int, 12345) CHECK_INT_CONVERSION(G_TYPE_UINT, uint, 0) @@ -204,6 +206,10 @@ test_value_transform (void) CHECK_UINT_CONVERSION(G_TYPE_CHAR, char, 124) CHECK_UINT_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_UINT_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_UINT_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_UINT_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_UINT_CONVERSION(G_TYPE_USHORT, ushort, 12345) + CHECK_UINT_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_UINT_CONVERSION(G_TYPE_INT, int, 12345) CHECK_UINT_CONVERSION(G_TYPE_INT, int, 12345) CHECK_UINT_CONVERSION(G_TYPE_UINT, uint, 0) @@ -229,6 +235,10 @@ test_value_transform (void) CHECK_LONG_CONVERSION(G_TYPE_CHAR, schar, 124) CHECK_LONG_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_LONG_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_LONG_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_LONG_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_LONG_CONVERSION(G_TYPE_USHORT, ushort, 0) + CHECK_LONG_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_LONG_CONVERSION(G_TYPE_INT, int, -12345) CHECK_LONG_CONVERSION(G_TYPE_INT, int, 12345) CHECK_LONG_CONVERSION(G_TYPE_UINT, uint, 0) @@ -254,6 +264,10 @@ test_value_transform (void) CHECK_ULONG_CONVERSION(G_TYPE_CHAR, char, 124) CHECK_ULONG_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_ULONG_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_ULONG_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_ULONG_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_ULONG_CONVERSION(G_TYPE_USHORT, ushort, 0) + CHECK_ULONG_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_ULONG_CONVERSION(G_TYPE_INT, int, -12345) CHECK_ULONG_CONVERSION(G_TYPE_INT, int, 12345) CHECK_ULONG_CONVERSION(G_TYPE_UINT, uint, 0) @@ -279,6 +293,10 @@ test_value_transform (void) CHECK_INT64_CONVERSION(G_TYPE_CHAR, schar, 124) CHECK_INT64_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_INT64_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_INT64_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_INT64_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_INT64_CONVERSION(G_TYPE_USHORT, ushort, 0) + CHECK_INT64_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_INT64_CONVERSION(G_TYPE_INT, int, -12345) CHECK_INT64_CONVERSION(G_TYPE_INT, int, 12345) CHECK_INT64_CONVERSION(G_TYPE_UINT, uint, 0) @@ -304,6 +322,10 @@ test_value_transform (void) CHECK_UINT64_CONVERSION(G_TYPE_CHAR, schar, 124) CHECK_UINT64_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_UINT64_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_UINT64_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_UINT64_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_UINT64_CONVERSION(G_TYPE_USHORT, ushort, 0) + CHECK_UINT64_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_UINT64_CONVERSION(G_TYPE_INT, int, -12345) CHECK_UINT64_CONVERSION(G_TYPE_INT, int, 12345) CHECK_UINT64_CONVERSION(G_TYPE_UINT, uint, 0) @@ -329,6 +351,10 @@ test_value_transform (void) CHECK_FLOAT_CONVERSION(G_TYPE_CHAR, schar, 124) CHECK_FLOAT_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_FLOAT_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_FLOAT_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_FLOAT_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_FLOAT_CONVERSION(G_TYPE_USHORT, ushort, 0) + CHECK_FLOAT_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_FLOAT_CONVERSION(G_TYPE_INT, int, -12345) CHECK_FLOAT_CONVERSION(G_TYPE_INT, int, 12345) CHECK_FLOAT_CONVERSION(G_TYPE_UINT, uint, 0) @@ -354,6 +380,10 @@ test_value_transform (void) CHECK_DOUBLE_CONVERSION(G_TYPE_CHAR, schar, 124) CHECK_DOUBLE_CONVERSION(G_TYPE_UCHAR, uchar, 0) CHECK_DOUBLE_CONVERSION(G_TYPE_UCHAR, uchar, 255) + CHECK_DOUBLE_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_DOUBLE_CONVERSION(G_TYPE_SHORT, short, 12345) + CHECK_DOUBLE_CONVERSION(G_TYPE_USHORT, ushort, 0) + CHECK_DOUBLE_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_DOUBLE_CONVERSION(G_TYPE_INT, int, -12345) CHECK_DOUBLE_CONVERSION(G_TYPE_INT, int, 12345) CHECK_DOUBLE_CONVERSION(G_TYPE_UINT, uint, 0) @@ -378,6 +408,8 @@ test_value_transform (void) g_value_unset (&src); \ g_value_unset (&dest); + CHECK_BOOLEAN_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_BOOLEAN_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_BOOLEAN_CONVERSION(G_TYPE_INT, int, -12345) CHECK_BOOLEAN_CONVERSION(G_TYPE_UINT, uint, 12345) CHECK_BOOLEAN_CONVERSION(G_TYPE_LONG, long, -12345678) @@ -395,6 +427,8 @@ test_value_transform (void) g_value_unset (&src); \ g_value_unset (&dest); + CHECK_STRING_CONVERSION(G_TYPE_SHORT, short, -12345) + CHECK_STRING_CONVERSION(G_TYPE_USHORT, ushort, 12345) CHECK_STRING_CONVERSION(G_TYPE_INT, int, -12345) CHECK_STRING_CONVERSION(G_TYPE_UINT, uint, 12345) CHECK_STRING_CONVERSION(G_TYPE_LONG, long, -12345678) |