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_ADD_PRIVATE (GtkTreeModelRefCount) |
59 | G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL, |
60 | gtk_tree_model_ref_count_tree_model_init)) |
61 | |
62 | static void |
63 | row_removed (GtkTreeModelRefCount *ref_model, |
64 | GtkTreePath *path) |
65 | { |
66 | GHashTableIter iter; |
67 | GtkTreeIter tree_iter; |
68 | |
69 | if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ref_model), iter: &tree_iter)) |
70 | { |
71 | g_hash_table_remove_all (hash_table: ref_model->priv->node_hash); |
72 | return; |
73 | } |
74 | |
75 | g_hash_table_iter_init (iter: &iter, hash_table: ref_model->priv->node_hash); |
76 | |
77 | while (g_hash_table_iter_next (iter: &iter, key: &tree_iter.user_data, NULL)) |
78 | { |
79 | if (!gtk_tree_store_iter_is_valid (GTK_TREE_STORE (ref_model), iter: &tree_iter)) |
80 | g_hash_table_iter_remove (iter: &iter); |
81 | } |
82 | } |
83 | |
84 | static void |
85 | gtk_tree_model_ref_count_init (GtkTreeModelRefCount *ref_model) |
86 | { |
87 | ref_model->priv = gtk_tree_model_ref_count_get_instance_private (self: ref_model); |
88 | |
89 | ref_model->priv->node_hash = g_hash_table_new_full (hash_func: g_direct_hash, |
90 | key_equal_func: g_direct_equal, |
91 | NULL, |
92 | value_destroy_func: (GDestroyNotify)node_info_free); |
93 | |
94 | g_signal_connect (ref_model, "row-deleted" , G_CALLBACK (row_removed), NULL); |
95 | } |
96 | |
97 | static void |
98 | gtk_tree_model_ref_count_class_init (GtkTreeModelRefCountClass *ref_model_class) |
99 | { |
100 | G_OBJECT_CLASS (ref_model_class)->finalize = gtk_tree_model_ref_count_finalize; |
101 | } |
102 | |
103 | static void |
104 | gtk_tree_model_ref_count_tree_model_init (GtkTreeModelIface *iface) |
105 | { |
106 | iface->ref_node = gtk_tree_model_ref_count_ref_node; |
107 | iface->unref_node = gtk_tree_model_ref_count_unref_node; |
108 | } |
109 | |
110 | static void |
111 | gtk_tree_model_ref_count_finalize (GObject *object) |
112 | { |
113 | GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (object); |
114 | |
115 | if (ref_model->priv->node_hash) |
116 | { |
117 | g_hash_table_destroy (hash_table: ref_model->priv->node_hash); |
118 | ref_model->priv->node_hash = NULL; |
119 | } |
120 | |
121 | G_OBJECT_CLASS (gtk_tree_model_ref_count_parent_class)->finalize (object); |
122 | } |
123 | |
124 | |
125 | static NodeInfo * |
126 | node_info_new (void) |
127 | { |
128 | NodeInfo *info = g_slice_new (NodeInfo); |
129 | info->ref_count = 0; |
130 | |
131 | return info; |
132 | } |
133 | |
134 | static void |
135 | node_info_free (NodeInfo *info) |
136 | { |
137 | g_slice_free (NodeInfo, info); |
138 | } |
139 | |
140 | static void |
141 | gtk_tree_model_ref_count_ref_node (GtkTreeModel *model, |
142 | GtkTreeIter *iter) |
143 | { |
144 | NodeInfo *info; |
145 | GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (model); |
146 | |
147 | info = g_hash_table_lookup (hash_table: ref_model->priv->node_hash, key: iter->user_data); |
148 | if (!info) |
149 | { |
150 | info = node_info_new (); |
151 | |
152 | g_hash_table_insert (hash_table: ref_model->priv->node_hash, key: iter->user_data, value: info); |
153 | } |
154 | |
155 | info->ref_count++; |
156 | } |
157 | |
158 | static void |
159 | gtk_tree_model_ref_count_unref_node (GtkTreeModel *model, |
160 | GtkTreeIter *iter) |
161 | { |
162 | NodeInfo *info; |
163 | GtkTreeModelRefCount *ref_model = GTK_TREE_MODEL_REF_COUNT (model); |
164 | |
165 | info = g_hash_table_lookup (hash_table: ref_model->priv->node_hash, key: iter->user_data); |
166 | g_assert_nonnull (info); |
167 | g_assert_cmpint (info->ref_count, >, 0); |
168 | |
169 | info->ref_count--; |
170 | } |
171 | |
172 | |
173 | GtkTreeModel * |
174 | gtk_tree_model_ref_count_new (void) |
175 | { |
176 | GtkTreeModel *retval; |
177 | |
178 | retval = g_object_new (object_type: gtk_tree_model_ref_count_get_type (), NULL); |
179 | |
180 | return retval; |
181 | } |
182 | |
183 | static void |
184 | dump_iter (GtkTreeModelRefCount *ref_model, |
185 | GtkTreeIter *iter) |
186 | { |
187 | char *path_str; |
188 | NodeInfo *info; |
189 | GtkTreePath *path; |
190 | |
191 | path = gtk_tree_model_get_path (GTK_TREE_MODEL (ref_model), iter); |
192 | path_str = gtk_tree_path_to_string (path); |
193 | gtk_tree_path_free (path); |
194 | |
195 | info = g_hash_table_lookup (hash_table: ref_model->priv->node_hash, key: iter->user_data); |
196 | if (!info) |
197 | g_print (format: "%-16s ref_count=0\n" , path_str); |
198 | else |
199 | g_print (format: "%-16s ref_count=%d\n" , path_str, info->ref_count); |
200 | |
201 | g_free (mem: path_str); |
202 | } |
203 | |
204 | static void |
205 | gtk_tree_model_ref_count_dump_recurse (GtkTreeModelRefCount *ref_model, |
206 | GtkTreeIter *iter) |
207 | { |
208 | do |
209 | { |
210 | GtkTreeIter child; |
211 | |
212 | dump_iter (ref_model, iter); |
213 | |
214 | if (gtk_tree_model_iter_children (GTK_TREE_MODEL (ref_model), |
215 | iter: &child, parent: iter)) |
216 | gtk_tree_model_ref_count_dump_recurse (ref_model, iter: &child); |
217 | } |
218 | while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ref_model), iter)); |
219 | } |
220 | |
221 | void |
222 | gtk_tree_model_ref_count_dump (GtkTreeModelRefCount *ref_model) |
223 | { |
224 | GtkTreeIter iter; |
225 | |
226 | if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ref_model), iter: &iter)) |
227 | return; |
228 | |
229 | gtk_tree_model_ref_count_dump_recurse (ref_model, iter: &iter); |
230 | } |
231 | |
232 | static gboolean |
233 | check_iter (GtkTreeModelRefCount *ref_model, |
234 | GtkTreeIter *iter, |
235 | int expected_ref_count, |
236 | gboolean may_assert) |
237 | { |
238 | NodeInfo *info; |
239 | |
240 | if (may_assert) |
241 | g_assert_true (gtk_tree_store_iter_is_valid (GTK_TREE_STORE (ref_model), iter)); |
242 | |
243 | info = g_hash_table_lookup (hash_table: ref_model->priv->node_hash, key: iter->user_data); |
244 | if (!info) |
245 | { |
246 | if (expected_ref_count == 0) |
247 | return TRUE; |
248 | else |
249 | { |
250 | if (may_assert) |
251 | g_error ("Expected ref count %d, but node has never been referenced." , expected_ref_count); |
252 | return FALSE; |
253 | } |
254 | } |
255 | |
256 | if (may_assert) |
257 | { |
258 | if (expected_ref_count == 0) |
259 | g_assert_cmpint (expected_ref_count, ==, info->ref_count); |
260 | else |
261 | g_assert_cmpint (expected_ref_count, <=, info->ref_count); |
262 | } |
263 | |
264 | return expected_ref_count == info->ref_count; |
265 | } |
266 | |
267 | gboolean |
268 | gtk_tree_model_ref_count_check_level (GtkTreeModelRefCount *ref_model, |
269 | GtkTreeIter *parent, |
270 | int expected_ref_count, |
271 | gboolean recurse, |
272 | gboolean may_assert) |
273 | { |
274 | GtkTreeIter iter; |
275 | |
276 | if (!gtk_tree_model_iter_children (GTK_TREE_MODEL (ref_model), |
277 | iter: &iter, parent)) |
278 | return TRUE; |
279 | |
280 | do |
281 | { |
282 | if (!check_iter (ref_model, iter: &iter, expected_ref_count, may_assert)) |
283 | return FALSE; |
284 | |
285 | if (recurse && |
286 | gtk_tree_model_iter_has_child (GTK_TREE_MODEL (ref_model), iter: &iter)) |
287 | { |
288 | if (!gtk_tree_model_ref_count_check_level (ref_model, parent: &iter, |
289 | expected_ref_count, |
290 | recurse, may_assert)) |
291 | return FALSE; |
292 | } |
293 | } |
294 | while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ref_model), iter: &iter)); |
295 | |
296 | return TRUE; |
297 | } |
298 | |
299 | gboolean |
300 | gtk_tree_model_ref_count_check_node (GtkTreeModelRefCount *ref_model, |
301 | GtkTreeIter *iter, |
302 | int expected_ref_count, |
303 | gboolean may_assert) |
304 | { |
305 | return check_iter (ref_model, iter, expected_ref_count, may_assert); |
306 | } |
307 | |