[gtk+/wip/bitmask: 1/4] bitmasks: Make setters return a new value



commit 6a7a28eec3f5c63f411dbf7fa65be34f2d11b7e3
Author: Benjamin Otte <otte redhat com>
Date:   Wed Feb 8 19:19:59 2012 +0100

    bitmasks: Make setters return a new value
    
    (Actually, it's not a real 'new' value yet, but will be soon.
    
    This is the first step to make bitmasks immutable.

 gtk/gtkbitmask.c        |   45 +++++++++++++++++++++++++++------------------
 gtk/gtkbitmaskprivate.h |   20 ++++++++++----------
 gtk/gtkcsslookup.c      |    6 +++---
 gtk/gtkcssprovider.c    |    6 +++---
 gtk/tests/bitmask.c     |   40 ++++++++++++++++++++--------------------
 5 files changed, 63 insertions(+), 54 deletions(-)
---
diff --git a/gtk/gtkbitmask.c b/gtk/gtkbitmask.c
index a47d755..0d620e4 100644
--- a/gtk/gtkbitmask.c
+++ b/gtk/gtkbitmask.c
@@ -42,9 +42,8 @@ _gtk_bitmask_copy (const GtkBitmask *mask)
   g_return_val_if_fail (mask != NULL, NULL);
 
   copy = _gtk_bitmask_new ();
-  _gtk_bitmask_union (copy, mask);
-
-  return copy;
+  
+  return _gtk_bitmask_union (copy, mask);
 }
 
 void
@@ -110,14 +109,14 @@ gtk_bitmask_shrink (GtkBitmask *mask)
   g_array_set_size (mask, i);
 }
 
-void
+GtkBitmask *
 _gtk_bitmask_intersect (GtkBitmask       *mask,
                         const GtkBitmask *other)
 {
   guint i;
 
-  g_return_if_fail (mask != NULL);
-  g_return_if_fail (other != NULL);
+  g_return_val_if_fail (mask != NULL, NULL);
+  g_return_val_if_fail (other != NULL, NULL);
 
   g_array_set_size (mask, MIN (mask->len, other->len));
   for (i = 0; i < mask->len; i++)
@@ -126,32 +125,36 @@ _gtk_bitmask_intersect (GtkBitmask       *mask,
     }
 
   gtk_bitmask_shrink (mask);
+
+  return mask;
 }
 
-void
+GtkBitmask *
 _gtk_bitmask_union (GtkBitmask       *mask,
                     const GtkBitmask *other)
 {
   guint i;
 
-  g_return_if_fail (mask != NULL);
-  g_return_if_fail (other != NULL);
+  g_return_val_if_fail (mask != NULL, NULL);
+  g_return_val_if_fail (other != NULL, NULL);
 
   g_array_set_size (mask, MAX (mask->len, other->len));
   for (i = 0; i < other->len; i++)
     {
       g_array_index (mask, VALUE_TYPE, i) |= g_array_index (other, VALUE_TYPE, i);
     }
+
+  return mask;
 }
 
-void
+GtkBitmask *
 _gtk_bitmask_subtract (GtkBitmask       *mask,
                        const GtkBitmask *other)
 {
   guint i;
 
-  g_return_if_fail (mask != NULL);
-  g_return_if_fail (other != NULL);
+  g_return_val_if_fail (mask != NULL, NULL);
+  g_return_val_if_fail (other != NULL, NULL);
 
   for (i = 0; i < other->len; i++)
     {
@@ -159,6 +162,8 @@ _gtk_bitmask_subtract (GtkBitmask       *mask,
     }
 
   gtk_bitmask_shrink (mask);
+
+  return mask;
 }
 
 static void
@@ -186,14 +191,14 @@ _gtk_bitmask_get (const GtkBitmask *mask,
   return (g_array_index (mask, VALUE_TYPE, array_index) & VALUE_BIT (bit_index)) ? TRUE : FALSE;
 }
 
-void
+GtkBitmask *
 _gtk_bitmask_set (GtkBitmask *mask,
                   guint       index_,
                   gboolean    value)
 {
   guint array_index, bit_index;
 
-  g_return_if_fail (mask != NULL);
+  g_return_val_if_fail (mask != NULL, NULL);
 
   gtk_bitmask_indexes (index_, &array_index, &bit_index);
 
@@ -212,22 +217,26 @@ _gtk_bitmask_set (GtkBitmask *mask,
           gtk_bitmask_shrink (mask);
         }
     }
+
+  return mask;
 }
 
-void
+GtkBitmask *
 _gtk_bitmask_invert_range (GtkBitmask *mask,
                            guint       start,
                            guint       end)
 {
   guint i;
 
-  g_return_if_fail (mask != NULL);
-  g_return_if_fail (start < end);
+  g_return_val_if_fail (mask != NULL, NULL);
+  g_return_val_if_fail (start < end, NULL);
 
   /* I CAN HAS SPEEDUP? */
 
   for (i = start; i < end; i++)
-    _gtk_bitmask_set (mask, i, !_gtk_bitmask_get (mask, i));
+    mask = _gtk_bitmask_set (mask, i, !_gtk_bitmask_get (mask, i));
+
+  return mask;
 }
 
 gboolean
diff --git a/gtk/gtkbitmaskprivate.h b/gtk/gtkbitmaskprivate.h
index adb65cf..a5f4ed0 100644
--- a/gtk/gtkbitmaskprivate.h
+++ b/gtk/gtkbitmaskprivate.h
@@ -40,22 +40,22 @@ char *         _gtk_bitmask_to_string            (const GtkBitmask  *mask);
 void           _gtk_bitmask_print                (const GtkBitmask  *mask,
                                                   GString           *string);
 
-void           _gtk_bitmask_intersect            (GtkBitmask        *mask,
-                                                  const GtkBitmask  *other);
-void           _gtk_bitmask_union                (GtkBitmask        *mask,
-                                                  const GtkBitmask  *other);
-void           _gtk_bitmask_subtract             (GtkBitmask        *mask,
-                                                  const GtkBitmask  *other);
+GtkBitmask *   _gtk_bitmask_intersect            (GtkBitmask        *mask,
+                                                  const GtkBitmask  *other) G_GNUC_WARN_UNUSED_RESULT;
+GtkBitmask *   _gtk_bitmask_union                (GtkBitmask        *mask,
+                                                  const GtkBitmask  *other) G_GNUC_WARN_UNUSED_RESULT;
+GtkBitmask *   _gtk_bitmask_subtract             (GtkBitmask        *mask,
+                                                  const GtkBitmask  *other) G_GNUC_WARN_UNUSED_RESULT;
 
 gboolean       _gtk_bitmask_get                  (const GtkBitmask  *mask,
                                                   guint              index_);
-void           _gtk_bitmask_set                  (GtkBitmask        *mask,
+GtkBitmask *   _gtk_bitmask_set                  (GtkBitmask        *mask,
                                                   guint              index_,
-                                                  gboolean           value);
+                                                  gboolean           value) G_GNUC_WARN_UNUSED_RESULT;
 
-void           _gtk_bitmask_invert_range         (GtkBitmask        *mask,
+GtkBitmask *   _gtk_bitmask_invert_range         (GtkBitmask        *mask,
                                                   guint              start,
-                                                  guint              end);
+                                                  guint              end) G_GNUC_WARN_UNUSED_RESULT;
 
 gboolean       _gtk_bitmask_is_empty             (const GtkBitmask  *mask);
 gboolean       _gtk_bitmask_equals               (const GtkBitmask  *mask,
diff --git a/gtk/gtkcsslookup.c b/gtk/gtkcsslookup.c
index c3dd7d0..eb6577a 100644
--- a/gtk/gtkcsslookup.c
+++ b/gtk/gtkcsslookup.c
@@ -45,7 +45,7 @@ _gtk_css_lookup_new (void)
 
   lookup = g_malloc0 (sizeof (GtkCssLookup) + sizeof (GtkCssLookupValue) * n);
   lookup->missing = _gtk_bitmask_new ();
-  _gtk_bitmask_invert_range (lookup->missing, 0, n);
+  lookup->missing = _gtk_bitmask_invert_range (lookup->missing, 0, n);
 
   return lookup;
 }
@@ -99,7 +99,7 @@ _gtk_css_lookup_set (GtkCssLookup  *lookup,
   g_return_if_fail (_gtk_bitmask_get (lookup->missing, id));
   g_return_if_fail (value != NULL);
 
-  _gtk_bitmask_set (lookup->missing, id, FALSE);
+  lookup->missing = _gtk_bitmask_set (lookup->missing, id, FALSE);
   lookup->values[id].value = value;
   lookup->values[id].section = section;
 }
@@ -133,7 +133,7 @@ _gtk_css_lookup_set_computed (GtkCssLookup  *lookup,
   g_return_if_fail (_gtk_bitmask_get (lookup->missing, id));
   g_return_if_fail (value != NULL);
 
-  _gtk_bitmask_set (lookup->missing, id, FALSE);
+  lookup->missing = _gtk_bitmask_set (lookup->missing, id, FALSE);
   lookup->values[id].computed = value;
   lookup->values[id].section = section;
 }
diff --git a/gtk/gtkcssprovider.c b/gtk/gtkcssprovider.c
index a637eee..446350f 100644
--- a/gtk/gtkcssprovider.c
+++ b/gtk/gtkcssprovider.c
@@ -1216,9 +1216,9 @@ gtk_css_ruleset_add (GtkCssRuleset    *ruleset,
       g_return_if_fail (_gtk_css_style_property_is_specified_type (GTK_CSS_STYLE_PROPERTY (prop),
                                                                    G_VALUE_TYPE (&value->value)));
 
-      _gtk_bitmask_set (ruleset->set_styles,
-                        _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)),
-                        TRUE);
+      ruleset->set_styles = _gtk_bitmask_set (ruleset->set_styles,
+                                              _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)),
+                                              TRUE);
       g_hash_table_insert (ruleset->style, prop, value);
     }
   else
diff --git a/gtk/tests/bitmask.c b/gtk/tests/bitmask.c
index 71a3c97..52f37b7 100644
--- a/gtk/tests/bitmask.c
+++ b/gtk/tests/bitmask.c
@@ -48,9 +48,9 @@ gtk_bitmask_new_parse (const char *string)
   for (i = 0; i < length; i++)
     {
       if (string[i] == '0')
-        _gtk_bitmask_set (mask, length - i - 1, FALSE);
+        mask = _gtk_bitmask_set (mask, length - i - 1, FALSE);
       else if (string[i] == '1')
-        _gtk_bitmask_set (mask, length - i - 1, TRUE);
+        mask = _gtk_bitmask_set (mask, length - i - 1, TRUE);
       else
         g_assert_not_reached ();
     }
@@ -142,12 +142,12 @@ test_set (void)
       for (j = 0; j < N_TRIES; j++)
         {
           indexes[j] = g_test_rand_int_range (0, MAX_INDEX);
-          _gtk_bitmask_set (copy, indexes[j], g_test_rand_bit ());
+          copy = _gtk_bitmask_set (copy, indexes[j], g_test_rand_bit ());
         }
 
       for (j = 0; j < N_TRIES; j++)
         {
-          _gtk_bitmask_set (copy, indexes[j], _gtk_bitmask_get (mask, indexes[j]));
+          copy = _gtk_bitmask_set (copy, indexes[j], _gtk_bitmask_get (mask, indexes[j]));
         }
 
       assert_cmpmasks (copy, mask);
@@ -173,15 +173,15 @@ test_union (void)
           guint id = g_test_rand_int_range (0, MAX_INDEX);
 
           if (g_test_rand_bit ())
-            _gtk_bitmask_set (left, id, TRUE);
+            left = _gtk_bitmask_set (left, id, TRUE);
           else
-            _gtk_bitmask_set (right, id, TRUE);
+            right = _gtk_bitmask_set (right, id, TRUE);
 
-          _gtk_bitmask_set (expected, id, TRUE);
+          expected = _gtk_bitmask_set (expected, id, TRUE);
         }
 
-      _gtk_bitmask_union (left, right);
-      _gtk_bitmask_union (right, left);
+      left = _gtk_bitmask_union (left, right);
+      right = _gtk_bitmask_union (right, left);
 
       assert_cmpmasks (left, expected);
       assert_cmpmasks (right, expected);
@@ -211,13 +211,13 @@ test_intersect (void)
 
           if (g_test_rand_bit ())
             {
-              _gtk_bitmask_set (left, id, set);
-              _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (right, id) : 0);
+              left = _gtk_bitmask_set (left, id, set);
+              expected = _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (right, id) : 0);
             }
           else
             {
-              _gtk_bitmask_set (right, id, set);
-              _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (left, id) : 0);
+              right = _gtk_bitmask_set (right, id, set);
+              expected = _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (left, id) : 0);
             }
         }
 
@@ -225,8 +225,8 @@ test_intersect (void)
       g_assert_cmpint (intersects, ==, _gtk_bitmask_intersects (right, left));
       g_assert_cmpint (intersects, !=, _gtk_bitmask_is_empty (expected));
 
-      _gtk_bitmask_intersect (left, right);
-      _gtk_bitmask_intersect (right, left);
+      left = _gtk_bitmask_intersect (left, right);
+      right = _gtk_bitmask_intersect (right, left);
 
       assert_cmpmasks (left, expected);
       assert_cmpmasks (right, expected);
@@ -267,19 +267,19 @@ test_invert_range (void)
       end = MIN (left_end, right_end);
 
       if (left_start != left_end)
-        _gtk_bitmask_invert_range (left, left_start, left_end);
+        left = _gtk_bitmask_invert_range (left, left_start, left_end);
       if (right_start != right_end)
-        _gtk_bitmask_invert_range (right, right_start, right_end);
+        right = _gtk_bitmask_invert_range (right, right_start, right_end);
       if (start < end)
-        _gtk_bitmask_invert_range (expected, start, end);
+        expected = _gtk_bitmask_invert_range (expected, start, end);
 
       intersection = _gtk_bitmask_copy (left);
-      _gtk_bitmask_intersect (intersection, right);
+      intersection = _gtk_bitmask_intersect (intersection, right);
 
       assert_cmpmasks (intersection, expected);
 
       if (start < end)
-        _gtk_bitmask_invert_range (expected, start, end);
+        expected = _gtk_bitmask_invert_range (expected, start, end);
 
       g_assert_cmpint (_gtk_bitmask_is_empty (expected), ==, TRUE);
 



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