[glib/g-property: 5/16] gproperty: Do not box values in g_property_get_range()



commit be529c0e8a80028c1e12ca58bf6f6e099dcc5707
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Wed May 18 09:49:56 2011 +0100

    gproperty: Do not box values in g_property_get_range()
    
    The variadic arguments function can just use the va_list API and avoid a
    useless boxing/lcopy/unboxing.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=648526

 gobject/gproperty.c            |  123 ++++++++++++++++++++++++++++++++--------
 gobject/tests/autoproperties.c |   16 ++++-
 2 files changed, 111 insertions(+), 28 deletions(-)
---
diff --git a/gobject/gproperty.c b/gobject/gproperty.c
index dcc3942..79bb54d 100644
--- a/gobject/gproperty.c
+++ b/gobject/gproperty.c
@@ -3426,44 +3426,119 @@ gboolean
 g_property_get_range (GProperty *property,
                       ...)
 {
-  GValue min_value = { 0, };
-  GValue max_value = { 0, };
-  gchar *error;
   va_list var_args;
+  GType gtype;
+  gboolean retval;
+  gpointer min_p, max_p;
 
   g_return_val_if_fail (G_IS_PROPERTY (property), FALSE);
   g_return_val_if_fail (property->gtype != G_TYPE_INVALID, FALSE);
 
-  g_value_init (&min_value, property->gtype);
-  g_value_init (&max_value, property->gtype);
-
-  g_property_get_range_values (property, &min_value, &max_value);
+  if (property->prerequisite != G_TYPE_INVALID)
+    gtype = property->prerequisite;
+  else
+    gtype = property->gtype;
 
   va_start (var_args, property);
 
-  G_VALUE_LCOPY (&min_value, var_args, 0, &error);
-  if (error != NULL)
-    {
-      g_warning (G_STRLOC ": %s", error);
-      g_free (error);
-      goto out;
-    }
+  min_p = va_arg (var_args, gpointer);
+  max_p = va_arg (var_args, gpointer);
 
-  G_VALUE_LCOPY (&max_value, var_args, 0, &error);
-  if (error != NULL)
+  switch (G_TYPE_FUNDAMENTAL (gtype))
     {
-      g_warning (G_STRLOC ": %s", error);
-      g_free (error);
-      goto out;
+    case G_TYPE_BOOLEAN:
+      g_boolean_property_get_range (property, (gboolean *) min_p, (gboolean *) max_p);
+      retval = TRUE;
+      break;
+
+    case G_TYPE_INT:
+      switch (property->type_size)
+        {
+        case 1:
+          g_int8_property_get_range (property, (gint8 *) min_p, (gint8 *) max_p);
+          retval = TRUE;
+          break;
+
+        case 2:
+          g_int16_property_get_range (property, (gint16 *) min_p, (gint16 *) max_p);
+          retval = TRUE;
+          break;
+
+        case 4:
+          g_int32_property_get_range (property, (gint32 *) min_p, (gint32 *) max_p);
+          retval = TRUE;
+          break;
+
+        default:
+          g_int_property_get_range (property, (gint *) min_p, (gint *) max_p);
+          retval = TRUE;
+          break;
+        }
+      break;
+
+    case G_TYPE_INT64:
+      g_int64_property_get_range (property, (gint64 *) min_p, (gint64 *) max_p);
+      retval = TRUE;
+      break;
+
+    case G_TYPE_LONG:
+      g_long_property_get_range (property, (glong *) min_p, (glong *) max_p);
+      retval = TRUE;
+      break;
+
+    case G_TYPE_UINT:
+      switch (property->type_size)
+        {
+        case 1:
+          g_uint8_property_get_range (property, (guint8 *) min_p, (guint8 *) max_p);
+          retval = TRUE;
+          break;
+
+        case 2:
+          g_uint16_property_get_range (property, (guint16 *) min_p, (guint16 *) max_p);
+          retval = TRUE;
+          break;
+
+        case 4:
+          g_uint32_property_get_range (property, (guint32 *) min_p, (guint32 *) max_p);
+          retval = TRUE;
+          break;
+
+        default:
+          g_uint_property_get_range (property, (guint *) min_p, (guint *) max_p);
+          retval = TRUE;
+          break;
+        }
+      break;
+
+    case G_TYPE_UINT64:
+      g_uint64_property_get_range (property, (guint64 *) min_p, (guint64 *) max_p);
+      retval = TRUE;
+      break;
+
+    case G_TYPE_ULONG:
+      g_ulong_property_get_range (property, (gulong *) min_p, (gulong *) max_p);
+      retval = TRUE;
+      break;
+
+    case G_TYPE_FLOAT:
+      g_float_property_get_range (property, (gfloat *) min_p, (gfloat *) max_p);
+      retval = TRUE;
+      break;
+
+    case G_TYPE_DOUBLE:
+      g_double_property_get_range (property, (gdouble *) min_p, (gdouble *) max_p);
+      retval = TRUE;
+      break;
+
+    default:
+      g_critical (G_STRLOC ": Invalid type %s", g_type_name (gtype));
+      retval = FALSE;
     }
 
-out:
   va_end (var_args);
 
-  g_value_unset (&min_value);
-  g_value_unset (&max_value);
-
-  return FALSE;
+  return retval;
 }
 
 /**
diff --git a/gobject/tests/autoproperties.c b/gobject/tests/autoproperties.c
index 38bb40a..2106c52 100644
--- a/gobject/tests/autoproperties.c
+++ b/gobject/tests/autoproperties.c
@@ -347,14 +347,22 @@ autoproperties_range (void)
 {
   TestObject *t = g_object_new (test_object_get_type (), NULL);
   GProperty *p;
-  gint min, max;
+  gint i_min, i_max;
+  gdouble d_min, d_max;
 
   p = (GProperty *) g_object_class_find_property (G_OBJECT_GET_CLASS (t), "foo");
   g_assert (G_IS_PROPERTY (p));
 
-  g_property_get_range (p, &min, &max);
-  g_assert_cmpint (min, ==, -1);
-  g_assert_cmpint (max, ==, 100);
+  g_property_get_range (p, &i_min, &i_max);
+  g_assert_cmpint (i_min, ==, -1);
+  g_assert_cmpint (i_max, ==, 100);
+
+  p = (GProperty *) g_object_class_find_property (G_OBJECT_GET_CLASS (t), "x-align");
+  g_assert (G_IS_PROPERTY (p));
+
+  g_property_get_range (p, &d_min, &d_max);
+  g_assert_cmpfloat (d_min, ==, 0.0);
+  g_assert_cmpfloat (d_max, ==, 1.0);
 
   g_object_unref (t);
 }



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