[glib/gobject-speedups: 4/13] param: Add a value_is_valid vfunc




commit c3afdd3085d78daf78ee2faf2f1892cfd8563a3b
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat May 14 22:35:10 2022 -0400

    param: Add a value_is_valid vfunc
    
    In constrast to value_validate, this one does not
    modify the passed-in value. It is optional, but
    we set it for a number of builtin pspec types.

 gobject/gparam.h      |  6 +++-
 gobject/gparamspecs.c | 94 +++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 88 insertions(+), 12 deletions(-)
---
diff --git a/gobject/gparam.h b/gobject/gparam.h
index e0f316682d..8843244ab0 100644
--- a/gobject/gparam.h
+++ b/gobject/gparam.h
@@ -257,8 +257,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:
diff --git a/gobject/gparamspecs.c b/gobject/gparamspecs.c
index c7879842b1..2be2c38670 100644
--- a/gobject/gparamspecs.c
+++ b/gobject/gparamspecs.c
@@ -128,6 +128,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 +165,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 +215,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)
@@ -461,6 +490,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)
@@ -643,6 +682,19 @@ param_string_set_default (GParamSpec *pspec,
   value->data[0].v_pointer = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value);
 }
 
+static gboolean
+param_string_is_valid (GParamSpec   *pspec,
+                       const GValue *value)
+{
+  GParamSpecString *sspec = G_PARAM_SPEC_STRING (pspec);
+
+  if (sspec->cset_first == NULL && sspec->cset_nth == NULL)
+    return TRUE;
+
+  /* FIXME: check charset */
+  return FALSE;
+}
+
 static gboolean
 param_string_validate (GParamSpec *pspec,
                       GValue     *value)
@@ -785,16 +837,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 +1014,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)
@@ -1188,6 +1241,7 @@ _g_param_spec_types_init (void)
 {
   const guint n_types = 23;
   GType type, *spec_types;
+  GParamSpecClass *class;
 #ifndef G_DISABLE_ASSERT
   GType *spec_types_bound;
 #endif
@@ -1248,6 +1302,9 @@ _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);
+    class = g_type_class_ref (type);
+    class->value_is_valid = param_boolean_is_valid;
+    g_type_class_unref (class);
     *spec_types++ = type;
     g_assert (type == G_TYPE_PARAM_BOOLEAN);
   }
@@ -1266,6 +1323,9 @@ _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);
+    class = g_type_class_ref (type);
+    class->value_is_valid = param_int_is_valid;
+    g_type_class_unref (class);
     *spec_types++ = type;
     g_assert (type == G_TYPE_PARAM_INT);
   }
@@ -1284,6 +1344,9 @@ _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);
+    class = g_type_class_ref (type);
+    class->value_is_valid = param_uint_is_valid;
+    g_type_class_unref (class);
     *spec_types++ = type;
     g_assert (type == G_TYPE_PARAM_UINT);
   }
@@ -1392,6 +1455,9 @@ _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);
+    class = g_type_class_ref (type);
+    class->value_is_valid = param_enum_is_valid;
+    g_type_class_unref (class);
     *spec_types++ = type;
     g_assert (type == G_TYPE_PARAM_ENUM);
   }
@@ -1464,6 +1530,9 @@ _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);
+    class = g_type_class_ref (type);
+    class->value_is_valid = param_string_is_valid;
+    g_type_class_unref (class);
     *spec_types++ = type;
     g_assert (type == G_TYPE_PARAM_STRING);
   }
@@ -1514,7 +1583,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 +1624,9 @@ _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);
+    class = g_type_class_ref (type);
+    class->value_is_valid = param_object_is_valid;
+    g_type_class_unref (class);
     *spec_types++ = type;
     g_assert (type == G_TYPE_PARAM_OBJECT);
   }


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