]> Pileus Git - ~andy/gtk/blob - gtk/tests/gtktreemodelrefcount.c
appchooserdialog: remove redundant checks
[~andy/gtk] / gtk / tests / gtktreemodelrefcount.c
1 /* gtktreemodelrefcount.c
2  * Copyright (C) 2011  Kristian Rietveld <kris@gtk.org>
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19 #include "gtktreemodelrefcount.h"
20
21
22 /* The purpose of this GtkTreeModel is to keep record of the reference count
23  * of each node.  The reference count does not effect the functioning of
24  * the model in any way.  Because this model is a subclass of GtkTreeStore,
25  * the GtkTreeStore API should be used to add to and remove nodes from
26  * this model.  We depend on the iter format of GtkTreeStore, which means
27  * that this model needs to be revised in case the iter format of
28  * GtkTreeStore is modified.  Currently, we make use of the fact that
29  * the value stored in the user_data field is unique for each node.
30  */
31
32 struct _GtkTreeModelRefCountPrivate
33 {
34   GHashTable *node_hash;
35 };
36
37 typedef struct
38 {
39   int ref_count;
40 }
41 NodeInfo;
42
43
44 static void      gtk_tree_model_ref_count_tree_model_init (GtkTreeModelIface *iface);
45 static void      gtk_tree_model_ref_count_finalize        (GObject           *object);
46
47 static NodeInfo *node_info_new                            (void);
48 static void      node_info_free                           (NodeInfo          *info);
49
50 /* GtkTreeModel interface */
51 static void      gtk_tree_model_ref_count_ref_node        (GtkTreeModel      *model,
52                                                            GtkTreeIter       *iter);
53 static void      gtk_tree_model_ref_count_unref_node      (GtkTreeModel      *model,
54                                                            GtkTreeIter       *iter);
55
56
57 G_DEFINE_TYPE_WITH_CODE (GtkTreeModelRefCount, gtk_tree_model_ref_count, GTK_TYPE_TREE_STORE,
58                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
59                                                 gtk_tree_model_ref_count_tree_model_init))
60
61 static void
62 row_removed (GtkTreeModelRefCount *ref_model,
63              GtkTreePath *path)
64 {
65   GHashTableIter iter;
66   GtkTreeIter tree_iter;
67
68   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ref_model), &tree_iter))
69     {
70       g_hash_table_remove_all (ref_model->priv->node_hash);
71       return;
72     }
73
74   g_hash_table_iter_init (&iter, ref_model->priv->node_hash);
75
76   while (g_hash_table_iter_next (&iter, &tree_iter.user_data, NULL))
77     {
78       if (!gtk_tree_store_iter_is_valid (GTK_TREE_STORE (ref_model), &tree_iter))
79         g_hash_table_iter_remove (&iter);
80     }
81 }
82
83 static void
84 gtk_tree_model_ref_count_init (GtkTreeModelRefCount *ref_model)
85 {
86   ref_model->priv = G_TYPE_INSTANCE_GET_PRIVATE (ref_model,
87                                                  GTK_TYPE_TREE_MODEL_REF_COUNT,
88                                                  GtkTreeModelRefCountPrivate);
89
90   ref_model->priv->node_hash = g_hash_table_new_full (g_direct_hash,
91                                                       g_direct_equal,
92                                                       NULL,
93                                                       (GDestroyNotify)node_info_free);
94
95   g_signal_connect (ref_model, "row-deleted", G_CALLBACK (row_removed), NULL);
96 }
97
98 static void
99 gtk_tree_model_ref_count_class_init (GtkTreeModelRefCountClass *ref_model_class)
100 {
101   GObjectClass *object_class;
102
103   object_class = (GObjectClass *) ref_model_class;
104
105   object_class->finalize = gtk_tree_model_ref_count_finalize;
106
107   g_type_class_add_private (object_class, sizeof (GtkTreeModelRefCountPrivate));
108 }
109
110 static void
111 gtk_tree_model_ref_count_tree_model_init (GtkTreeModelIface *iface)
112 {
113   iface->ref_node = gtk_tree_model_ref_count_ref_node;
114   iface->unref_node = gtk_tree_model_ref_count_unref_node;
115 }
116
117 static void
118 gtk_tree_model_ref_count_finalize (GObject *object)
119 {
120   GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (object);
121
122   if (ref_model->priv->node_hash)
123     {
124       g_hash_table_destroy (ref_model->priv->node_hash);
125       ref_model->priv->node_hash = NULL;
126     }
127
128   G_OBJECT_CLASS (gtk_tree_model_ref_count_parent_class)->finalize (object);
129 }
130
131
132 static NodeInfo *
133 node_info_new (void)
134 {
135   NodeInfo *info = g_slice_new (NodeInfo);
136   info->ref_count = 0;
137
138   return info;
139 }
140
141 static void
142 node_info_free (NodeInfo *info)
143 {
144   g_slice_free (NodeInfo, info);
145 }
146
147 static void
148 gtk_tree_model_ref_count_ref_node (GtkTreeModel *model,
149                                    GtkTreeIter  *iter)
150 {
151   NodeInfo *info;
152   GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (model);
153
154   info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
155   if (!info)
156     {
157       info = node_info_new ();
158
159       g_hash_table_insert (ref_model->priv->node_hash, iter->user_data, info);
160     }
161
162   info->ref_count++;
163 }
164
165 static void
166 gtk_tree_model_ref_count_unref_node (GtkTreeModel *model,
167                                      GtkTreeIter  *iter)
168 {
169   NodeInfo *info;
170   GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (model);
171
172   info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
173   g_assert (info != NULL);
174   g_assert (info->ref_count > 0);
175
176   info->ref_count--;
177 }
178
179
180 GtkTreeModel *
181 gtk_tree_model_ref_count_new (void)
182 {
183   GtkTreeModel *retval;
184
185   retval = g_object_new (gtk_tree_model_ref_count_get_type (), NULL);
186
187   return retval;
188 }
189
190 static void
191 dump_iter (GtkTreeModelRefCount *ref_model,
192            GtkTreeIter          *iter)
193 {
194   gchar *path_str;
195   NodeInfo *info;
196   GtkTreePath *path;
197
198   path = gtk_tree_model_get_path (GTK_TREE_MODEL (ref_model), iter);
199   path_str = gtk_tree_path_to_string (path);
200   gtk_tree_path_free (path);
201
202   info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
203   if (!info)
204     g_print ("%-16s ref_count=0\n", path_str);
205   else
206     g_print ("%-16s ref_count=%d\n", path_str, info->ref_count);
207
208   g_free (path_str);
209 }
210
211 static void
212 gtk_tree_model_ref_count_dump_recurse (GtkTreeModelRefCount *ref_model,
213                                        GtkTreeIter          *iter)
214 {
215   do
216     {
217       GtkTreeIter child;
218
219       dump_iter (ref_model, iter);
220
221       if (gtk_tree_model_iter_children (GTK_TREE_MODEL (ref_model),
222                                         &child, iter))
223         gtk_tree_model_ref_count_dump_recurse (ref_model, &child);
224     }
225   while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ref_model), iter));
226 }
227
228 void
229 gtk_tree_model_ref_count_dump (GtkTreeModelRefCount *ref_model)
230 {
231   GtkTreeIter iter;
232
233   if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ref_model), &iter))
234     return;
235
236   gtk_tree_model_ref_count_dump_recurse (ref_model, &iter);
237 }
238
239 static gboolean
240 check_iter (GtkTreeModelRefCount *ref_model,
241             GtkTreeIter          *iter,
242             gint                  expected_ref_count,
243             gboolean              may_assert)
244 {
245   NodeInfo *info;
246
247   if (may_assert)
248     g_assert (gtk_tree_store_iter_is_valid (GTK_TREE_STORE (ref_model), iter));
249
250   info = g_hash_table_lookup (ref_model->priv->node_hash, iter->user_data);
251   if (!info)
252     {
253       if (expected_ref_count == 0)
254         return TRUE;
255       else
256         {
257           if (may_assert)
258             g_error ("Expected ref count %d, but node has never been referenced.\n", expected_ref_count);
259           return FALSE;
260         }
261     }
262
263   if (may_assert)
264     {
265       if (expected_ref_count == 0)
266         g_assert_cmpint (expected_ref_count, ==, info->ref_count);
267       else
268         g_assert_cmpint (expected_ref_count, <=, info->ref_count);
269     }
270
271   return expected_ref_count == info->ref_count;
272 }
273
274 gboolean
275 gtk_tree_model_ref_count_check_level (GtkTreeModelRefCount *ref_model,
276                                       GtkTreeIter          *parent,
277                                       gint                  expected_ref_count,
278                                       gboolean              recurse,
279                                       gboolean              may_assert)
280 {
281   GtkTreeIter iter;
282
283   if (!gtk_tree_model_iter_children (GTK_TREE_MODEL (ref_model),
284                                      &iter, parent))
285     return TRUE;
286
287   do
288     {
289       if (!check_iter (ref_model, &iter, expected_ref_count, may_assert))
290         return FALSE;
291
292       if (recurse &&
293           gtk_tree_model_iter_has_child (GTK_TREE_MODEL (ref_model), &iter))
294         {
295           if (!gtk_tree_model_ref_count_check_level (ref_model, &iter,
296                                                      expected_ref_count,
297                                                      recurse, may_assert))
298             return FALSE;
299         }
300     }
301   while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ref_model), &iter));
302
303   return TRUE;
304 }
305
306 gboolean
307 gtk_tree_model_ref_count_check_node (GtkTreeModelRefCount *ref_model,
308                                      GtkTreeIter          *iter,
309                                      gint                  expected_ref_count,
310                                      gboolean              may_assert)
311 {
312   return check_iter (ref_model, iter, expected_ref_count, may_assert);
313 }