[gtksourceview] region: use g_clear_object() to destroy regions



commit ef1c3628b8c3c5bb106f46a695411074cd1e7aaa
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Apr 2 16:00:34 2016 +0200

    region: use g_clear_object() to destroy regions

 .../words/gtksourcecompletionwordsbuffer.c         |   11 +--
 gtksourceview/gtksourcecontextengine.c             |    6 +-
 gtksourceview/gtksourcesearchcontext.c             |  134 +++++---------------
 testsuite/test-region.c                            |    2 +-
 4 files changed, 36 insertions(+), 117 deletions(-)
---
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c 
b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
index 4ccb7c4..ff2b4ce 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
@@ -108,12 +108,6 @@ gtk_source_completion_words_buffer_dispose (GObject *object)
                buffer->priv->words = NULL;
        }
 
-       if (buffer->priv->scan_region != NULL)
-       {
-               g_object_unref (buffer->priv->scan_region);
-               buffer->priv->scan_region = NULL;
-       }
-
        if (buffer->priv->batch_scan_id != 0)
        {
                g_source_remove (buffer->priv->batch_scan_id);
@@ -126,6 +120,7 @@ gtk_source_completion_words_buffer_dispose (GObject *object)
                buffer->priv->initiate_scan_id = 0;
        }
 
+       g_clear_object (&buffer->priv->scan_region);
        g_clear_object (&buffer->priv->buffer);
        g_clear_object (&buffer->priv->library);
 
@@ -475,7 +470,7 @@ invalidate_region (GtkSourceCompletionWordsBuffer *buffer,
        remove_region = compute_remove_region (buffer, &start_iter, &end_iter);
 
        remove_words_in_region (buffer, remove_region);
-       g_object_unref (remove_region);
+       g_clear_object (&remove_region);
 }
 
 static void
@@ -542,7 +537,7 @@ on_delete_range_before_cb (GtkTextBuffer                  *text_buffer,
        {
                remove_all_words (buffer);
 
-               g_object_unref (buffer->priv->scan_region);
+               g_clear_object (&buffer->priv->scan_region);
                buffer->priv->scan_region = gtk_source_region_new (text_buffer);
        }
        else
diff --git a/gtksourceview/gtksourcecontextengine.c b/gtksourceview/gtksourcecontextengine.c
index cdd26fa..5dcbd31 100644
--- a/gtksourceview/gtksourcecontextengine.c
+++ b/gtksourceview/gtksourcecontextengine.c
@@ -966,7 +966,7 @@ ensure_highlighted (GtkSourceContextEngine *ce,
                gtk_source_region_iter_next (&reg_iter);
        }
 
-       g_object_unref (region);
+       g_clear_object (&region);
 
        /* Remove the just highlighted region. */
        gtk_source_region_subtract (ce->priv->refresh_region, start, end);
@@ -2559,9 +2559,7 @@ gtk_source_context_engine_attach_buffer (GtkSourceEngine *engine,
 
                destroy_context_classes_list (ce);
 
-               if (ce->priv->refresh_region != NULL)
-                       g_object_unref (ce->priv->refresh_region);
-               ce->priv->refresh_region = NULL;
+               g_clear_object (&ce->priv->refresh_region);
        }
 
        ce->priv->buffer = buffer;
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index f536891..0835397 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -498,11 +498,7 @@ get_last_subregion (GtkSourceRegion *region,
 static void
 clear_task (GtkSourceSearchContext *search)
 {
-       if (search->priv->task_region != NULL)
-       {
-               g_object_unref (search->priv->task_region);
-               search->priv->task_region = NULL;
-       }
+       g_clear_object (&search->priv->task_region);
 
        if (search->priv->task != NULL)
        {
@@ -521,17 +517,8 @@ clear_task (GtkSourceSearchContext *search)
 static void
 clear_search (GtkSourceSearchContext *search)
 {
-       if (search->priv->scan_region != NULL)
-       {
-               g_object_unref (search->priv->scan_region);
-               search->priv->scan_region = NULL;
-       }
-
-       if (search->priv->high_priority_region != NULL)
-       {
-               g_object_unref (search->priv->high_priority_region);
-               search->priv->high_priority_region = NULL;
-       }
+       g_clear_object (&search->priv->scan_region);
+       g_clear_object (&search->priv->high_priority_region);
 
        if (search->priv->idle_scan_id != 0)
        {
@@ -1023,10 +1010,7 @@ smart_forward_search_async_step (GtkSourceSearchContext *search,
                GtkTextIter match_start;
                GtkTextIter match_end;
 
-               if (region != NULL)
-               {
-                       g_object_unref (region);
-               }
+               g_clear_object (&region);
 
                while (basic_forward_search (search, &iter, &match_start, &match_end, &limit))
                {
@@ -1067,11 +1051,7 @@ smart_forward_search_async_step (GtkSourceSearchContext *search,
                              task_data,
                              (GDestroyNotify)forward_backward_data_free);
 
-       if (search->priv->task_region != NULL)
-       {
-               g_object_unref (search->priv->task_region);
-       }
-
+       g_clear_object (&search->priv->task_region);
        search->priv->task_region = region;
 
        install_idle_scan (search);
@@ -1159,10 +1139,7 @@ smart_backward_search_async_step (GtkSourceSearchContext *search,
                GtkTextIter match_start;
                GtkTextIter match_end;
 
-               if (region != NULL)
-               {
-                       g_object_unref (region);
-               }
+               g_clear_object (&region);
 
                while (basic_backward_search (search, &iter, &match_start, &match_end, &limit))
                {
@@ -1203,11 +1180,7 @@ smart_backward_search_async_step (GtkSourceSearchContext *search,
                              task_data,
                              (GDestroyNotify)forward_backward_data_free);
 
-       if (search->priv->task_region != NULL)
-       {
-               g_object_unref (search->priv->task_region);
-       }
-
+       g_clear_object (&search->priv->task_region);
        search->priv->task_region = region;
 
        install_idle_scan (search);
@@ -1307,10 +1280,7 @@ adjust_subregion (GtkSourceSearchContext *search,
                                *start = tag_start;
                        }
 
-                       if (region != NULL)
-                       {
-                               g_object_unref (region);
-                       }
+                       g_clear_object (&region);
                }
        }
 
@@ -1360,10 +1330,7 @@ adjust_subregion (GtkSourceSearchContext *search,
                                *end = tag_end;
                        }
 
-                       if (region != NULL)
-                       {
-                               g_object_unref (region);
-                       }
+                       g_clear_object (&region);
                }
        }
 
@@ -1482,10 +1449,7 @@ remove_occurrences_in_range (GtkSourceSearchContext *search,
                                search->priv->occurrences_count--;
                        }
 
-                       if (region != NULL)
-                       {
-                               g_object_unref (region);
-                       }
+                       g_clear_object (&region);
                }
 
                iter = match_end;
@@ -1668,11 +1632,7 @@ resume_task (GtkSourceSearchContext *search)
        ForwardBackwardData *task_data = g_task_get_task_data (search->priv->task);
        GtkTextIter start_at;
 
-       if (search->priv->task_region != NULL)
-       {
-               g_object_unref (search->priv->task_region);
-               search->priv->task_region = NULL;
-       }
+       g_clear_object (&search->priv->task_region);
 
        gtk_text_buffer_get_iter_at_mark (search->priv->buffer,
                                          &start_at,
@@ -1720,9 +1680,7 @@ idle_scan_normal_search (GtkSourceSearchContext *search)
                 */
                scan_all_region (search, search->priv->high_priority_region);
 
-               g_object_unref (search->priv->high_priority_region);
-               search->priv->high_priority_region = NULL;
-
+               g_clear_object (&search->priv->high_priority_region);
                return G_SOURCE_CONTINUE;
        }
 
@@ -1740,12 +1698,7 @@ idle_scan_normal_search (GtkSourceSearchContext *search)
 
                g_object_notify (G_OBJECT (search), "occurrences-count");
 
-               if (search->priv->scan_region != NULL)
-               {
-                       g_object_unref (search->priv->scan_region);
-                       search->priv->scan_region = NULL;
-               }
-
+               g_clear_object (&search->priv->scan_region);
                return G_SOURCE_REMOVE;
        }
 
@@ -1806,7 +1759,7 @@ regex_search_handle_high_priority_region (GtkSourceSearchContext *search)
                gtk_source_region_iter_next (&region_iter);
        }
 
-       g_object_unref (region);
+       g_clear_object (&region);
 }
 
 /* Returns TRUE if the segment is finished, and FALSE on partial match. */
@@ -2024,10 +1977,7 @@ idle_scan_regex_search (GtkSourceSearchContext *search)
        if (search->priv->high_priority_region != NULL)
        {
                regex_search_handle_high_priority_region (search);
-
-               g_object_unref (search->priv->high_priority_region);
-               search->priv->high_priority_region = NULL;
-
+               g_clear_object (&search->priv->high_priority_region);
                return G_SOURCE_CONTINUE;
        }
 
@@ -2053,12 +2003,7 @@ idle_scan_regex_search (GtkSourceSearchContext *search)
 
                g_object_notify (G_OBJECT (search), "occurrences-count");
 
-               if (search->priv->scan_region != NULL)
-               {
-                       g_object_unref (search->priv->scan_region);
-                       search->priv->scan_region = NULL;
-               }
-
+               g_clear_object (&search->priv->scan_region);
                return G_SOURCE_REMOVE;
        }
 
@@ -2121,10 +2066,7 @@ smart_forward_search_step (GtkSourceSearchContext *search,
 
        if (gtk_source_region_is_empty (region))
        {
-               if (region != NULL)
-               {
-                       g_object_unref (region);
-               }
+               g_clear_object (&region);
 
                while (basic_forward_search (search, &iter, match_start, match_end, &limit))
                {
@@ -2152,7 +2094,7 @@ smart_forward_search_step (GtkSourceSearchContext *search,
                scan_region_forward (search, region);
        }
 
-       g_object_unref (region);
+       g_clear_object (&region);
 
        return FALSE;
 }
@@ -2220,10 +2162,7 @@ smart_backward_search_step (GtkSourceSearchContext *search,
 
        if (gtk_source_region_is_empty (region))
        {
-               if (region != NULL)
-               {
-                       g_object_unref (region);
-               }
+               g_clear_object (&region);
 
                while (basic_backward_search (search, &iter, match_start, match_end, &limit))
                {
@@ -2251,7 +2190,7 @@ smart_backward_search_step (GtkSourceSearchContext *search,
                scan_region_forward (search, region);
        }
 
-       g_object_unref (region);
+       g_clear_object (&region);
 
        return FALSE;
 }
@@ -3150,10 +3089,7 @@ gtk_source_search_context_get_occurrence_position (GtkSourceSearchContext *searc
 
                empty = gtk_source_region_is_empty (region);
 
-               if (region != NULL)
-               {
-                       g_object_unref (region);
-               }
+               g_clear_object (&region);
 
                if (!empty)
                {
@@ -3190,10 +3126,7 @@ gtk_source_search_context_get_occurrence_position (GtkSourceSearchContext *searc
 
                empty = gtk_source_region_is_empty (region);
 
-               if (region != NULL)
-               {
-                       g_object_unref (region);
-               }
+               g_clear_object (&region);
 
                if (!empty)
                {
@@ -3783,25 +3716,18 @@ _gtk_source_search_context_update_highlight (GtkSourceSearchContext *search,
 
        if (gtk_source_region_is_empty (region_to_highlight))
        {
-               if (region_to_highlight != NULL)
-               {
-                       g_object_unref (region_to_highlight);
-               }
-
+               g_clear_object (&region_to_highlight);
                return;
        }
 
        if (!synchronous)
        {
-               if (search->priv->high_priority_region != NULL)
-               {
-                       /* The high priority region is used to highlight the
-                        * region visible on screen. So if we are here, that
-                        * means that the visible region has changed. So we can
-                        * destroy the old high_priority_region.
-                        */
-                       g_object_unref (search->priv->high_priority_region);
-               }
+               /* The high priority region is used to highlight the region
+                * visible on screen. So if we are here, that means that the
+                * visible region has changed. So we can destroy the old
+                * high_priority_region.
+                */
+               g_clear_object (&search->priv->high_priority_region);
 
                search->priv->high_priority_region = region_to_highlight;
                install_idle_scan (search);
@@ -3824,6 +3750,6 @@ _gtk_source_search_context_update_highlight (GtkSourceSearchContext *search,
        else
        {
                scan_all_region (search, region_to_highlight);
-               g_object_unref (region_to_highlight);
+               g_clear_object (&region_to_highlight);
        }
 }
diff --git a/testsuite/test-region.c b/testsuite/test-region.c
index 8df4ec9..a2c178a 100644
--- a/testsuite/test-region.c
+++ b/testsuite/test-region.c
@@ -128,7 +128,7 @@ test_region (void)
                intersection = gtk_source_region_intersect (region, &iter1, &iter2);
                if (intersection) {
                        _gtk_source_region_debug_print (intersection);
-                       g_object_unref (intersection);
+                       g_clear_object (&intersection);
                } else {
                        g_print ("no intersection\n");
                }


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