[gtk+/nth-child: 16/33] widgetpath: Make structure refcounted



commit 79c867202f8dadd8fa287b44cda946a3eb88679f
Author: Benjamin Otte <otte redhat com>
Date:   Fri May 27 16:36:07 2011 +0200

    widgetpath: Make structure refcounted
    
    I want to use widget paths in a way that make a lot more sense with a
    refcounted structure. See the following patches.

 docs/reference/gtk/gtk3-sections.txt |    2 +
 gtk/gtk.symbols                      |    2 +
 gtk/gtkwidgetpath.c                  |   52 +++++++++++++++++++++++++++++++--
 gtk/gtkwidgetpath.h                  |    2 +
 4 files changed, 54 insertions(+), 4 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 291b219..45f1773 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -5417,6 +5417,8 @@ GTK_CHECK_VERSION
 GtkWidgetPath
 gtk_widget_path_append_type
 gtk_widget_path_copy
+gtk_widget_path_ref
+gtk_widget_path_unref
 gtk_widget_path_free
 gtk_widget_path_get_object_type
 gtk_widget_path_has_parent
diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols
index 51e07da..4948e92 100644
--- a/gtk/gtk.symbols
+++ b/gtk/gtk.symbols
@@ -3578,6 +3578,8 @@ gtk_widget_override_symbolic_color
 gtk_widget_path
 gtk_widget_path_append_type
 gtk_widget_path_copy
+gtk_widget_path_ref
+gtk_widget_path_unref
 gtk_widget_path_free
 gtk_widget_path_get_type
 gtk_widget_path_get_object_type
diff --git a/gtk/gtkwidgetpath.c b/gtk/gtkwidgetpath.c
index 384cd5f..52ca7e0 100644
--- a/gtk/gtkwidgetpath.c
+++ b/gtk/gtkwidgetpath.c
@@ -97,6 +97,8 @@ struct GtkPathElement
 
 struct _GtkWidgetPath
 {
+  volatile guint ref_count;
+
   GArray *elems; /* First element contains the described widget */
 };
 
@@ -116,6 +118,7 @@ gtk_widget_path_new (void)
 
   path = g_slice_new0 (GtkWidgetPath);
   path->elems = g_array_new (FALSE, TRUE, sizeof (GtkPathElement));
+  path->ref_count = 1;
 
   return path;
 }
@@ -174,20 +177,44 @@ gtk_widget_path_copy (const GtkWidgetPath *path)
 }
 
 /**
- * gtk_widget_path_free:
+ * gtk_widget_path_ref:
  * @path: a #GtkWidgetPath
  *
- * Frees a #GtkWidgetPath.
+ * Increments the reference count on @path.
  *
- * Since: 3.0
+ * Returns: @path itself.
+ *
+ * Since: 3.2
+ **/
+GtkWidgetPath *
+gtk_widget_path_ref (GtkWidgetPath *path)
+{
+  g_return_val_if_fail (path != NULL, path);
+
+  g_atomic_int_add (&path->ref_count, 1);
+
+  return path;
+}
+
+/**
+ * gtk_widget_path_unref:
+ * @path: a #GtkWidgetPath
+ *
+ * Decrements the reference count on @path, freeing the structure
+ * if the reference count reaches 0.
+ *
+ * Since: 3.2
  **/
 void
-gtk_widget_path_free (GtkWidgetPath *path)
+gtk_widget_path_unref (GtkWidgetPath *path)
 {
   guint i;
 
   g_return_if_fail (path != NULL);
 
+  if (!g_atomic_int_dec_and_test (&path->ref_count))
+    return;
+
   for (i = 0; i < path->elems->len; i++)
     {
       GtkPathElement *elem;
@@ -206,6 +233,23 @@ gtk_widget_path_free (GtkWidgetPath *path)
 }
 
 /**
+ * gtk_widget_path_free:
+ * @path: a #GtkWidgetPath
+ *
+ * Decrements the reference count on @path, freeing the structure
+ * if the reference count reaches 0.
+ *
+ * Since: 3.0
+ **/
+void
+gtk_widget_path_free (GtkWidgetPath *path)
+{
+  g_return_if_fail (path != NULL);
+
+  gtk_widget_path_unref (path);
+}
+
+/**
  * gtk_widget_path_length:
  * @path: a #GtkWidgetPath
  *
diff --git a/gtk/gtkwidgetpath.h b/gtk/gtkwidgetpath.h
index e44824f..12822a8 100644
--- a/gtk/gtkwidgetpath.h
+++ b/gtk/gtkwidgetpath.h
@@ -41,6 +41,8 @@ GType           gtk_widget_path_get_type            (void) G_GNUC_CONST;
 GtkWidgetPath * gtk_widget_path_new                 (void);
 
 GtkWidgetPath * gtk_widget_path_copy                (const GtkWidgetPath *path);
+GtkWidgetPath * gtk_widget_path_ref                 (GtkWidgetPath       *path);
+void            gtk_widget_path_unref               (GtkWidgetPath       *path);
 void            gtk_widget_path_free                (GtkWidgetPath       *path);
 
 char *          gtk_widget_path_to_string           (const GtkWidgetPath *path);



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