[glib/g-property: 10/26] gproperty: Remove unnecessary GValue collections



commit 1443eae65adccbe8876115ae3d23a91d4b4ef7d0
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Mon Jun 6 14:33:50 2011 +0100

    gproperty: Remove unnecessary GValue collections
    
    Remove some redundant GValue copies in the variadic arguments functions
    for range and default, and add a couple more comments to clarify the
    intent of the code.

 gobject/gproperty.c |  217 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 164 insertions(+), 53 deletions(-)
---
diff --git a/gobject/gproperty.c b/gobject/gproperty.c
index 69e5a5e..2a34b7b 100644
--- a/gobject/gproperty.c
+++ b/gobject/gproperty.c
@@ -360,6 +360,17 @@
  *       when needed.</para></listitem>
  *     </itemizedlist>
  *   </refsect3>
+ *
+ *   <refsect3>
+ *     <title>Collecting properties value</title>
+ *     <para>#GProperty provides macros for collecting values from variable
+ *     length arguments stored inside a <structname>va_list</structname>,
+ *     similar to the %G_VALUE_COLLECT_INIT and %G_VALUE_LCOPY macros for
+ *     #GValue. The %G_PROPERTY_COLLECT and %G_PROPERTY_LCOPY should be used
+ *     to avoid the #GValue boxing and unboxing of their #GValue
+ *     counterparts when collecting values and return locations for
+ *     values.</para>
+ *   </refsect3>
  * </refsect2>
  *
  * <refsect2>
@@ -829,7 +840,7 @@ property_get_default_for_type (GProperty *property,
 
   if (gtype == G_TYPE_INVALID)
     {
-      if (property->prop_id == 0)
+      if (G_UNLIKELY (property->prop_id == 0))
         {
           gchar *lock_name = g_strconcat ("__g_property_id_", pspec->name, NULL);
 
@@ -840,10 +851,7 @@ property_get_default_for_type (GProperty *property,
       return g_param_spec_get_qdata (pspec, property->prop_id);
     }
   else
-    {
-
-      return g_param_spec_get_qdata (pspec, g_type_qname (gtype));
-    }
+    return g_param_spec_get_qdata (pspec, g_type_qname (gtype));
 }
 
 static void
@@ -864,7 +872,7 @@ property_set_default_for_type (GProperty *property,
 
   if (gtype == G_TYPE_INVALID)
     {
-      if (property->prop_id == 0)
+      if (G_UNLIKELY (property->prop_id == 0))
         {
           gchar *lock_name = g_strconcat ("__g_property_id_", pspec->name, NULL);
 
@@ -885,12 +893,9 @@ property_set_default_for_type (GProperty *property,
                                    value_unset_and_free);
     }
   else
-    {
-
-      g_param_spec_set_qdata_full (pspec, g_type_qname (gtype),
-                                   value,
-                                   value_unset_and_free);
-    }
+    g_param_spec_set_qdata_full (pspec, g_type_qname (gtype),
+                                 value,
+                                 value_unset_and_free);
 }
 
 /**
@@ -3621,11 +3626,8 @@ void
 g_property_set_range (GProperty *property,
                       ...)
 {
-  GValue min_value = { 0, };
-  GValue max_value = { 0, };
   GType gtype;
-  gchar *error;
-  va_list var_args;
+  va_list args;
 
   g_return_if_fail (G_IS_PROPERTY (property));
   g_return_if_fail (!property->is_installed);
@@ -3633,32 +3635,130 @@ g_property_set_range (GProperty *property,
   gtype = G_PARAM_SPEC (property)->value_type;
   g_return_if_fail (gtype != G_TYPE_INVALID);
 
-  va_start (var_args, property);
+  va_start (args, property);
 
-  G_VALUE_COLLECT_INIT (&min_value, gtype, var_args, 0, &error);
-  if (error != NULL)
+  switch (G_TYPE_FUNDAMENTAL (gtype))
     {
-      g_warning (G_STRLOC ": %s", error);
-      g_free (error);
-      va_end (var_args);
-      return;
-    }
+    case G_TYPE_BOOLEAN:
+      {
+        gboolean min_v = va_arg (args, gboolean);
+        gboolean max_v = va_arg (args, gboolean);
 
-  G_VALUE_COLLECT_INIT (&max_value, gtype, var_args, 0, &error);
-  if (error != NULL)
-    {
-      g_warning (G_STRLOC ": %s", error);
-      g_free (error);
-      va_end (var_args);
-      return;
-    }
+        g_boolean_property_set_range (property, min_v, max_v);
+      }
+      break;
 
-  va_end (var_args);
+    case G_TYPE_INT:
+      {
+        gint min_v = va_arg (args, gint);
+        gint max_v = va_arg (args, gint);
+
+        switch (property->type_size)
+          {
+          case 1:
+            g_int8_property_set_range (property, min_v, max_v);
+            break;
+
+          case 2:
+            g_int16_property_set_range (property, min_v, max_v);
+            break;
+
+          case 4:
+            g_int32_property_set_range (property, min_v, max_v);
+            break;
+
+          default:
+            g_int_property_set_range (property, min_v, max_v);
+            break;
+          }
+      }
+      break;
+
+    case G_TYPE_INT64:
+      {
+        gint64 min_v = va_arg (args, gint64);
+        gint64 max_v = va_arg (args, gint64);
+
+        g_int64_property_set_range (property, min_v, max_v);
+      }
+      break;
+
+    case G_TYPE_LONG:
+      {
+        glong min_v = va_arg (args, glong);
+        glong max_v = va_arg (args, glong);
+
+        g_long_property_set_range (property, min_v, max_v);
+      }
+      break;
+
+    case G_TYPE_UINT:
+      {
+        guint min_v = va_arg (args, guint);
+        guint max_v = va_arg (args, guint);
+
+        switch (property->type_size)
+          {
+          case 1:
+            g_uint8_property_set_range (property, min_v, max_v);
+            break;
+
+          case 2:
+            g_uint16_property_set_range (property, min_v, max_v);
+            break;
+
+          case 4:
+            g_uint32_property_set_range (property, min_v, max_v);
+            break;
+
+          default:
+            g_uint_property_set_range (property, min_v, max_v);
+            break;
+          }
+      }
+      break;
+
+    case G_TYPE_UINT64:
+      {
+        guint64 min_v = va_arg (args, guint64);
+        guint64 max_v = va_arg (args, guint64);
+
+        g_uint64_property_set_range (property, min_v, max_v);
+      }
+      break;
+
+    case G_TYPE_ULONG:
+      {
+        gulong min_v = va_arg (args, gulong);
+        gulong max_v = va_arg (args, gulong);
+
+        g_ulong_property_set_range (property, min_v, max_v);
+      }
+      break;
+
+    case G_TYPE_FLOAT:
+      {
+        gfloat min_v = va_arg (args, gdouble);
+        gfloat max_v = va_arg (args, gdouble);
+
+        g_float_property_set_range (property, min_v, max_v);
+      }
+      break;
+
+    case G_TYPE_DOUBLE:
+      {
+        gdouble min_v = va_arg (args, gdouble);
+        gdouble max_v = va_arg (args, gdouble);
+
+        g_double_property_set_range (property, min_v, max_v);
+      }
+      break;
 
-  g_property_set_range_values (property, &min_value, &max_value);
+    default:
+      g_critical (G_STRLOC ": Invalid type %s", g_type_name (gtype));
+    }
 
-  g_value_unset (&min_value);
-  g_value_unset (&max_value);
+  va_end (args);
 }
 
 /**
@@ -4021,7 +4121,7 @@ void
 g_property_set_default (GProperty *property,
                         ...)
 {
-  GValue value = { 0, };
+  GValue *value;
   GType p_type;
   gchar *error;
   va_list var_args;
@@ -4033,16 +4133,20 @@ g_property_set_default (GProperty *property,
 
   va_start (var_args, property);
 
-  G_VALUE_COLLECT_INIT (&value, p_type, var_args, 0, &error);
+  value = g_new0 (GValue, 1);
+  G_VALUE_COLLECT_INIT (value, p_type, var_args, 0, &error);
   if (error != NULL)
     {
       g_critical (G_STRLOC ": %s", error);
       g_free (error);
+      g_value_unset (value);
+      g_free (value);
     }
   else
-    g_property_set_default_value (property, &value);
-
-  g_value_unset (&value);
+    {
+      /* takes ownership of the GValue */
+      property_set_default_for_type (property, G_TYPE_INVALID, value);
+    }
 
   va_end (var_args);
 }
@@ -4075,6 +4179,10 @@ g_property_get_default (GProperty *property,
 
   p_type = G_PARAM_SPEC (property)->value_type;
 
+  /* we perform a copy here because if the default value was not found,
+   * we can use the pre-initialized value safely in G_VALUE_LCOPY and
+   * return something sensible
+   */
   g_value_init (&value, p_type);
 
   gtype = G_OBJECT_TYPE (gobject);
@@ -4114,15 +4222,13 @@ g_property_get_default (GProperty *property,
   default_value = property_get_default_for_type (property, G_TYPE_INVALID);
 
 lcopy:
-  if (default_value == NULL)
-    {
-      g_critical (G_STRLOC ": No default value of property '%s' "
-                  "was found for type '%s'",
-                  G_PARAM_SPEC (property)->name,
-                  G_OBJECT_TYPE_NAME (gobject));
-    }
-  else
+  if (default_value != NULL)
     g_value_copy (default_value, &value);
+  else
+    g_critical (G_STRLOC ": No default value of property '%s' "
+                "was found for type '%s'",
+                G_PARAM_SPEC (property)->name,
+                G_OBJECT_TYPE_NAME (gobject));
 
   va_start (var_args, gobject);
 
@@ -4152,7 +4258,7 @@ g_property_override_default (GProperty *property,
                              GType      class_gtype,
                              ...)
 {
-  GValue value = { 0, };
+  GValue *value;
   GType p_type;
   gchar *error;
   va_list var_args;
@@ -4165,17 +4271,22 @@ g_property_override_default (GProperty *property,
 
   va_start (var_args, class_gtype);
 
-  G_VALUE_COLLECT_INIT (&value, p_type, var_args, 0, &error);
+  value = g_new0 (GValue, 1);
+  G_VALUE_COLLECT_INIT (value, p_type, var_args, 0, &error);
   if (error != NULL)
     {
       g_critical (G_STRLOC ": %s", error);
       g_free (error);
+      g_value_unset (value);
+      g_free (value);
     }
   else
-    g_property_override_default_value (property, class_gtype, &value);
+    {
+      /* takes ownership of the GValue */
+      property_set_default_for_type (property, class_gtype, value);
+    }
 
   va_end (var_args);
-  g_value_unset (&value);
 }
 
 /**



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