1 /* gtktreemodelrefcount.c
2 * Copyright (C) 2011 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtktreemodelrefcount.h"
24 /* The purpose of this GtkTreeModel is to keep record of the reference count
25 * of each node. The reference count does not effect the functioning of
26 * the model in any way. Because this model is a subclass of GtkTreeStore,
27 * the GtkTreeStore API should be used to add to and remove nodes from
28 * this model. We depend on the iter format of GtkTreeStore, which means
29 * that this model needs to be revised in case the iter format of
30 * GtkTreeStore is modified. Currently, we make use of the fact that
31 * the value stored in the user_data field is unique for each node.
34 struct _GtkTreeModelRefCountPrivate
36 GHashTable *node_hash;
46 static void gtk_tree_model_ref_count_tree_model_init (GtkTreeModelIface *iface);
47 static void gtk_tree_model_ref_count_finalize (GObject *object);
49 static NodeInfo *node_info_new (void);
50 static void node_info_free (NodeInfo *info);
52 /* GtkTreeModel interface */
53 static void gtk_tree_model_ref_count_ref_node (GtkTreeModel *model,
55 static void gtk_tree_model_ref_count_unref_node (GtkTreeModel *model,
59 G_DEFINE_TYPE_WITH_CODE (GtkTreeModelRefCount, gtk_tree_model_ref_count, GTK_TYPE_TREE_STORE,
60 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
61 gtk_tree_model_ref_count_tree_model_init))
64 gtk_tree_model_ref_count_init (GtkTreeModelRefCount *ref_model)
66 ref_model->priv = G_TYPE_INSTANCE_GET_PRIVATE (ref_model,
67 GTK_TYPE_TREE_MODEL_REF_COUNT,
68 GtkTreeModelRefCountPrivate);
70 ref_model->priv->node_hash = g_hash_table_new_full (g_direct_hash,
73 (GDestroyNotify)node_info_free);
77 gtk_tree_model_ref_count_class_init (GtkTreeModelRefCountClass *ref_model_class)
79 GObjectClass *object_class;
81 object_class = (GObjectClass *) ref_model_class;
83 object_class->finalize = gtk_tree_model_ref_count_finalize;
85 g_type_class_add_private (object_class, sizeof (GtkTreeModelRefCountPrivate));
89 gtk_tree_model_ref_count_tree_model_init (GtkTreeModelIface *iface)
91 iface->ref_node = gtk_tree_model_ref_count_ref_node;
92 iface->unref_node = gtk_tree_model_ref_count_unref_node;
96 gtk_tree_model_ref_count_finalize (GObject *object)
98 GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (object);
100 if (ref_model->priv->node_hash)
102 g_hash_table_destroy (ref_model->priv->node_hash);
103 ref_model->priv->node_hash = NULL;
106 G_OBJECT_CLASS (gtk_tree_model_ref_count_parent_class)->finalize (object);
113 NodeInfo *info = g_slice_new (NodeInfo);
120 node_info_free (NodeInfo *info)
122 g_slice_free (NodeInfo, info);
126 gtk_tree_model_ref_count_ref_node (GtkTreeModel *model,
130 GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (model);
132 info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
135 info = node_info_new ();
137 g_hash_table_insert (ref_model->priv->node_hash, iter->user_data, info);
144 gtk_tree_model_ref_count_unref_node (GtkTreeModel *model,
148 GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (model);
150 info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
151 g_assert (info != NULL);
152 g_assert (info->ref_count > 0);
159 gtk_tree_model_ref_count_new (void)
161 GtkTreeModel *retval;
163 retval = g_object_new (gtk_tree_model_ref_count_get_type (), NULL);
169 dump_iter (GtkTreeModelRefCount *ref_model,
176 path = gtk_tree_model_get_path (GTK_TREE_MODEL (ref_model), iter);
177 path_str = gtk_tree_path_to_string (path);
178 gtk_tree_path_free (path);
180 info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
182 g_print ("%-16s ref_count=0\n", path_str);
184 g_print ("%-16s ref_count=%d\n", path_str, info->ref_count);
190 gtk_tree_model_ref_count_dump_recurse (GtkTreeModelRefCount *ref_model,
197 dump_iter (ref_model, iter);
199 if (gtk_tree_model_iter_children (GTK_TREE_MODEL (ref_model),
201 gtk_tree_model_ref_count_dump_recurse (ref_model, &child);
203 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ref_model), iter));
207 gtk_tree_model_ref_count_dump (GtkTreeModelRefCount *ref_model)
211 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ref_model), &iter))
214 gtk_tree_model_ref_count_dump_recurse (ref_model, &iter);
218 check_iter (GtkTreeModelRefCount *ref_model,
220 gint expected_ref_count,
226 g_assert (gtk_tree_store_iter_is_valid (GTK_TREE_STORE (ref_model), iter));
228 info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
231 if (expected_ref_count == 0)
236 g_error ("Expected ref count %d, but node has never been referenced.\n", expected_ref_count);
242 g_assert_cmpint (expected_ref_count, ==, info->ref_count);
244 return expected_ref_count == info->ref_count;
248 gtk_tree_model_ref_count_check_level (GtkTreeModelRefCount *ref_model,
250 gint expected_ref_count,
256 if (!gtk_tree_model_iter_children (GTK_TREE_MODEL (ref_model),
262 if (!check_iter (ref_model, &iter, expected_ref_count, may_assert))
266 gtk_tree_model_iter_has_child (GTK_TREE_MODEL (ref_model), &iter))
268 if (!gtk_tree_model_ref_count_check_level (ref_model, &iter,
270 recurse, may_assert))
274 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ref_model), &iter));
280 gtk_tree_model_ref_count_check_node (GtkTreeModelRefCount *ref_model,
282 gint expected_ref_count,
285 return check_iter (ref_model, iter, expected_ref_count, may_assert);