[gtk/wip/baedert/css-values: 64/102] cssarrayvalue: Allow calling array API on non-arrays



commit 8d15efba8f0ed705ed23aeba529652395128bfa7
Author: Timm Bäder <mail baedert org>
Date:   Wed Jan 8 08:37:04 2020 +0100

    cssarrayvalue: Allow calling array API on non-arrays
    
    Just allow calling _get_nth() and _get_n_values() on every kind of css
    value. This way we can allow all values in places where only array
    values would be allowed before.
    
    This spares us around 1000 array values in the widget factory.
    
    css value stats before:
    
    GtkCssFilterValue: 3
    GtkCssRgbaValue: 1092
    GtkCssShadowValue: 708
    GtkCssEaseValue: 33
    GtkCssBorderValue: 2
    GtkCssTransformValue: 11
    GtkCssDimensionValue: 882
    GtkCssShadowsValue: 584
    GtkCssBgSizeValue: 23
    GtkCssIdentValue: 25
    GtkCssPositionValue: 81
    GtkCssCornerValue: 556
    GtkCssArrayValue: 1130
    GtkCssStringValue: 33
    GtkCssPaletteValue: 29
    GtkCssImageValue: 2765
    GtkCssColorValue: 1452
    SUM: 9415
    
    and after:
    
    GtkCssBgSizeValue: 23
    GtkCssIdentValue: 25
    GtkCssPositionValue: 81
    GtkCssCornerValue: 556
    GtkCssArrayValue: 143
    GtkCssStringValue: 33
    GtkCssPaletteValue: 29
    GtkCssImageValue: 2765
    GtkCssColorValue: 1452
    GtkCssFilterValue: 3
    GtkCssRgbaValue: 1092
    GtkCssShadowValue: 708
    GtkCssEaseValue: 33
    GtkCssBorderValue: 2
    GtkCssTransformValue: 11
    GtkCssDimensionValue: 882
    GtkCssShadowsValue: 584
    SUM: 8428
    
    9415 to 8428 is a 987 reduction (10.4%)

 gtk/gtkcssarrayvalue.c           | 13 +++++++++++--
 gtk/gtkcssarrayvalueprivate.h    |  2 +-
 gtk/gtkcssstylepropertyimpl.c    | 20 ++++++++++----------
 gtk/gtkcssstylepropertyprivate.h |  2 +-
 4 files changed, 23 insertions(+), 14 deletions(-)
---
diff --git a/gtk/gtkcssarrayvalue.c b/gtk/gtkcssarrayvalue.c
index 25bfbe47de..60019fea5c 100644
--- a/gtk/gtkcssarrayvalue.c
+++ b/gtk/gtkcssarrayvalue.c
@@ -395,6 +395,9 @@ _gtk_css_array_value_new_from_array (GtkCssValue **values,
            
   g_return_val_if_fail (values != NULL, NULL);
   g_return_val_if_fail (n_values > 0, NULL);
+
+  if (n_values == 1)
+    return values[0];
          
   result = _gtk_css_value_alloc (&GTK_CSS_VALUE_ARRAY, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * 
(n_values - 1));
   result->n_values = n_values;
@@ -431,9 +434,12 @@ _gtk_css_array_value_parse (GtkCssParser *parser,
 }
 
 GtkCssValue *
-_gtk_css_array_value_get_nth (const GtkCssValue *value,
-                              guint              i)
+_gtk_css_array_value_get_nth (GtkCssValue *value,
+                              guint        i)
 {
+  if (value->class != &GTK_CSS_VALUE_ARRAY)
+      return value;
+
   g_return_val_if_fail (value != NULL, NULL);
   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_ARRAY, NULL);
   g_return_val_if_fail (value->n_values > 0, NULL);
@@ -444,6 +450,9 @@ _gtk_css_array_value_get_nth (const GtkCssValue *value,
 guint
 _gtk_css_array_value_get_n_values (const GtkCssValue *value)
 {
+  if (value->class != &GTK_CSS_VALUE_ARRAY)
+    return 1;
+
   g_return_val_if_fail (value != NULL, 0);
   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_ARRAY, 0);
 
diff --git a/gtk/gtkcssarrayvalueprivate.h b/gtk/gtkcssarrayvalueprivate.h
index 23fb5c4697..0ff1295f48 100644
--- a/gtk/gtkcssarrayvalueprivate.h
+++ b/gtk/gtkcssarrayvalueprivate.h
@@ -32,7 +32,7 @@ GtkCssValue *       _gtk_css_array_value_new_from_array (GtkCssValue          **
 GtkCssValue *       _gtk_css_array_value_parse          (GtkCssParser          *parser,
                                                          GtkCssValue *          (* parse_func) (GtkCssParser 
*));
 
-GtkCssValue *       _gtk_css_array_value_get_nth        (const GtkCssValue     *value,
+GtkCssValue *       _gtk_css_array_value_get_nth        (GtkCssValue           *value,
                                                          guint                  i);
 guint               _gtk_css_array_value_get_n_values   (const GtkCssValue     *value);
 
diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c
index 6a59e702fe..02e9521403 100644
--- a/gtk/gtkcssstylepropertyimpl.c
+++ b/gtk/gtkcssstylepropertyimpl.c
@@ -104,7 +104,7 @@ gtk_css_style_property_register (const char *                   name,
 
 static void
 query_length_as_int (GtkCssStyleProperty *property,
-                     const GtkCssValue   *css_value,
+                     GtkCssValue         *css_value,
                      GValue              *value)
 {
   g_value_init (value, G_TYPE_INT);
@@ -113,7 +113,7 @@ query_length_as_int (GtkCssStyleProperty *property,
 
 static void
 query_font_size (GtkCssStyleProperty *property,
-                 const GtkCssValue   *css_value,
+                 GtkCssValue         *css_value,
                  GValue              *value)
 {
   g_value_init (value, G_TYPE_DOUBLE);
@@ -122,7 +122,7 @@ query_font_size (GtkCssStyleProperty *property,
 
 static void
 query_border (GtkCssStyleProperty *property,
-              const GtkCssValue   *css_value,
+              GtkCssValue         *css_value,
               GValue              *value)
 {
   GtkBorder border;
@@ -146,7 +146,7 @@ color_parse (GtkCssStyleProperty *property,
 
 static void
 color_query (GtkCssStyleProperty *property,
-             const GtkCssValue   *css_value,
+             GtkCssValue         *css_value,
              GValue              *value)
 {
   g_value_init (value, GDK_TYPE_RGBA);
@@ -199,7 +199,7 @@ font_family_parse (GtkCssStyleProperty *property,
 
 static void
 font_family_query (GtkCssStyleProperty *property,
-                   const GtkCssValue   *css_value,
+                   GtkCssValue         *css_value,
                    GValue              *value)
 {
   GPtrArray *array;
@@ -233,7 +233,7 @@ font_style_parse (GtkCssStyleProperty *property,
 
 static void
 font_style_query (GtkCssStyleProperty *property,
-                  const GtkCssValue   *css_value,
+                  GtkCssValue         *css_value,
                   GValue              *value)
 {
   g_value_init (value, PANGO_TYPE_STYLE);
@@ -266,7 +266,7 @@ font_weight_parse (GtkCssStyleProperty *property,
 
 static void
 font_weight_query (GtkCssStyleProperty *property,
-                   const GtkCssValue   *css_value,
+                   GtkCssValue         *css_value,
                    GValue              *value)
 {
   g_value_init (value, PANGO_TYPE_WEIGHT);
@@ -287,7 +287,7 @@ font_stretch_parse (GtkCssStyleProperty *property,
 
 static void
 font_stretch_query (GtkCssStyleProperty *property,
-                    const GtkCssValue   *css_value,
+                    GtkCssValue         *css_value,
                     GValue              *value)
 {
   g_value_init (value, PANGO_TYPE_STRETCH);
@@ -308,7 +308,7 @@ parse_border_style (GtkCssStyleProperty *property,
 
 static void
 query_border_style (GtkCssStyleProperty *property,
-                    const GtkCssValue   *css_value,
+                    GtkCssValue         *css_value,
                     GValue              *value)
 {
   g_value_init (value, GTK_TYPE_BORDER_STYLE);
@@ -360,7 +360,7 @@ opacity_parse (GtkCssStyleProperty *property,
 
 static void
 opacity_query (GtkCssStyleProperty *property,
-               const GtkCssValue   *css_value,
+               GtkCssValue         *css_value,
                GValue              *value)
 {
   g_value_init (value, G_TYPE_DOUBLE);
diff --git a/gtk/gtkcssstylepropertyprivate.h b/gtk/gtkcssstylepropertyprivate.h
index 2258bec314..005ada4e70 100644
--- a/gtk/gtkcssstylepropertyprivate.h
+++ b/gtk/gtkcssstylepropertyprivate.h
@@ -37,7 +37,7 @@ typedef struct _GtkCssStylePropertyClass      GtkCssStylePropertyClass;
 typedef GtkCssValue *    (* GtkCssStylePropertyParseFunc)  (GtkCssStyleProperty    *property,
                                                             GtkCssParser           *parser);
 typedef void             (* GtkCssStylePropertyQueryFunc)  (GtkCssStyleProperty    *property,
-                                                            const GtkCssValue      *cssvalue,
+                                                            GtkCssValue            *cssvalue,
                                                             GValue                 *value);
 struct _GtkCssStyleProperty
 {


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