[gnome-menus] GMenuTreeItem: Register boxed types, drop user data



commit 14c163c8a8d35fe2daa9c0973495fb4462a3ddec
Author: Colin Walters <walters verbum org>
Date:   Sun Apr 17 08:46:14 2011 -0400

    GMenuTreeItem: Register boxed types, drop user data
    
    This was a hack for the static Python bindings, no longer necessary
    after we register proper boxed types for the structures.
    
    Convert the refcount to an atomic integer too; the _unref may be
    called from a garbage collector thread in bindings.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=647968

 libmenu/gmenu-tree.c |  101 +++++++++++++++++++++++++++++++++----------------
 libmenu/gmenu-tree.h |   12 +++--
 2 files changed, 75 insertions(+), 38 deletions(-)
---
diff --git a/libmenu/gmenu-tree.c b/libmenu/gmenu-tree.c
index ec499bf..129b3aa 100644
--- a/libmenu/gmenu-tree.c
+++ b/libmenu/gmenu-tree.c
@@ -70,10 +70,7 @@ struct GMenuTreeItem
 
   GMenuTreeDirectory *parent;
   
-  gpointer       user_data;
-  GDestroyNotify dnotify;
-
-  guint refcount;
+  gint refcount;
 };
 
 struct GMenuTreeDirectory
@@ -1145,7 +1142,7 @@ gmenu_tree_item_ref (gpointer itemp)
   g_return_val_if_fail (item != NULL, NULL);
   g_return_val_if_fail (item->refcount > 0, NULL);
 
-  item->refcount++;
+  g_atomic_int_inc (&item->refcount);
 
   return item;
 }
@@ -1160,7 +1157,7 @@ gmenu_tree_item_unref (gpointer itemp)
   g_return_if_fail (item != NULL);
   g_return_if_fail (item->refcount > 0);
 
-  if (--item->refcount == 0)
+  if (g_atomic_int_dec_and_test (&(item->refcount)))
     {
       switch (item->type)
 	{
@@ -1188,11 +1185,6 @@ gmenu_tree_item_unref (gpointer itemp)
 	  break;
 	}
 
-      if (item->dnotify)
-	item->dnotify (item->user_data);
-      item->user_data = NULL;
-      item->dnotify   = NULL;
-
       item->parent = NULL;
 
       g_free (item);
@@ -1212,28 +1204,6 @@ gmenu_tree_item_unref_and_unset_parent (gpointer itemp)
   gmenu_tree_item_unref (item);
 }
 
-void
-gmenu_tree_item_set_user_data (GMenuTreeItem  *item,
-			       gpointer        user_data,
-			       GDestroyNotify  dnotify)
-{
-  g_return_if_fail (item != NULL);
-
-  if (item->dnotify != NULL)
-    item->dnotify (item->user_data);
-
-  item->dnotify   = dnotify;
-  item->user_data = user_data;
-}
-
-gpointer
-gmenu_tree_item_get_user_data (GMenuTreeItem *item)
-{
-  g_return_val_if_fail (item != NULL, NULL);
-
-  return item->user_data;
-}
-
 static inline const char *
 gmenu_tree_item_compare_get_name_helper (GMenuTreeItem    *item,
 					 GMenuTreeFlags    flags)
@@ -4174,6 +4144,71 @@ gmenu_tree_force_rebuild (GMenuTree *tree)
 }
 
 GType
+gmenu_tree_directory_get_type (void)
+{
+  static GType gtype = G_TYPE_INVALID;
+  if (gtype == G_TYPE_INVALID)
+    {
+      gtype = g_boxed_type_register_static ("GMenuTreeDirectory",
+          (GBoxedCopyFunc)gmenu_tree_item_ref,
+          (GBoxedFreeFunc)gmenu_tree_item_unref);
+    }
+  return gtype;
+}
+
+GType
+gmenu_tree_entry_get_type (void)
+{
+  static GType gtype = G_TYPE_INVALID;
+  if (gtype == G_TYPE_INVALID)
+    {
+      gtype = g_boxed_type_register_static ("GMenuTreeEntry",
+          (GBoxedCopyFunc)gmenu_tree_item_ref,
+          (GBoxedFreeFunc)gmenu_tree_item_unref);
+    }
+  return gtype;
+}
+
+GType
+gmenu_tree_separator_get_type (void)
+{
+  static GType gtype = G_TYPE_INVALID;
+  if (gtype == G_TYPE_INVALID)
+    {
+      gtype = g_boxed_type_register_static ("GMenuTreeSeparator",
+          (GBoxedCopyFunc)gmenu_tree_item_ref,
+          (GBoxedFreeFunc)gmenu_tree_item_unref);
+    }
+  return gtype;
+}
+
+GType
+gmenu_tree_header_get_type (void)
+{
+  static GType gtype = G_TYPE_INVALID;
+  if (gtype == G_TYPE_INVALID)
+    {
+      gtype = g_boxed_type_register_static ("GMenuTreeHeader",
+          (GBoxedCopyFunc)gmenu_tree_item_ref,
+          (GBoxedFreeFunc)gmenu_tree_item_unref);
+    }
+  return gtype;
+}
+
+GType
+gmenu_tree_alias_get_type (void)
+{
+  static GType gtype = G_TYPE_INVALID;
+  if (gtype == G_TYPE_INVALID)
+    {
+      gtype = g_boxed_type_register_static ("GMenuTreeAlias",
+          (GBoxedCopyFunc)gmenu_tree_item_ref,
+          (GBoxedFreeFunc)gmenu_tree_item_unref);
+    }
+  return gtype;
+}
+
+GType
 gmenu_tree_flags_get_type (void)
 {
   static GType enum_type_id = 0;
diff --git a/libmenu/gmenu-tree.h b/libmenu/gmenu-tree.h
index 7539466..c297aea 100644
--- a/libmenu/gmenu-tree.h
+++ b/libmenu/gmenu-tree.h
@@ -69,6 +69,13 @@ typedef enum
 #define GMENU_TREE_HEADER(i)    ((GMenuTreeHeader *)(i))
 #define GMENU_TREE_ALIAS(i)     ((GMenuTreeAlias *)(i))
 
+/* Explicitly skip item, it's a "hidden" base class */
+GType gmenu_tree_directory_get_type (void);
+GType gmenu_tree_entry_get_type (void);
+GType gmenu_tree_separator_get_type (void);
+GType gmenu_tree_header_get_type (void);
+GType gmenu_tree_alias_get_type (void);
+
 typedef enum
 {
   GMENU_TREE_FLAGS_NONE                = 0,
@@ -92,11 +99,6 @@ GMenuTreeDirectory *gmenu_tree_get_directory_from_path (GMenuTree  *tree,
 gpointer gmenu_tree_item_ref   (gpointer item);
 void     gmenu_tree_item_unref (gpointer item);
 
-void     gmenu_tree_item_set_user_data (GMenuTreeItem   *item,
-					gpointer        user_data,
-					GDestroyNotify  dnotify);
-gpointer gmenu_tree_item_get_user_data (GMenuTreeItem   *item);
-
 GMenuTreeItemType   gmenu_tree_item_get_item_type   (GMenuTreeItem *item);
 GMenuTreeDirectory *gmenu_tree_item_get_parent (GMenuTreeItem *item);
 



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