iterator patch



Hi Philip / others,

Instead of complaining, here 's a patch making the iterators a bit
easier to use. I've tried to keep the changes minimal, mostly because
I'm lazy :-) Therefore, I've just added the '_is_done' method to
all iterators, which makes writing loops using iterators much nicer.

I've done some testing, and it seems to work... please have a look.

Can I assume that when I create an iterator, it will point at the first
element at the start?

Best wishes,
Dirk.
 
--
Dirk-Jan C. Binnema, Project Manager
Nokia Multimedia, Open Source Software Operations

Index: libtinymail-camel/tny-msg-folder-list-iterator.c
===================================================================
--- libtinymail-camel/tny-msg-folder-list-iterator.c	(revision 562)
+++ libtinymail-camel/tny-msg-folder-list-iterator.c	(working copy)
@@ -92,9 +92,26 @@
 	me->current = g_list_next (me->current);
 	g_mutex_unlock (me->model->iterator_lock);
 
-	return me->current->data;
+	return me->current ? me->current->data : NULL;
 }
 
+
+
+
+static gboolean 
+tny_msg_folder_list_iterator_is_done (TnyIteratorIface *self)
+{
+	TnyMsgFolderListIterator *me = (TnyMsgFolderListIterator*) self;
+
+	if (G_UNLIKELY (!me || !me->model))
+		return TRUE;
+
+	return me->current != NULL;
+}
+
+
+
+
 static gpointer 
 tny_msg_folder_list_iterator_prev (TnyIteratorIface *self)
 {
@@ -231,7 +248,8 @@
 	klass->has_next_func = tny_msg_folder_list_iterator_has_next;
 	klass->has_first_func = tny_msg_folder_list_iterator_has_first;
 	klass->get_list_func = tny_msg_folder_list_iterator_get_list;
-
+	klass->is_done = tny_msg_folder_list_iterator_is_done;
+	
 	return;
 }
 
Index: libtinymailui-gtk/tny-msg-header-list-iterator.c
===================================================================
--- libtinymailui-gtk/tny-msg-header-list-iterator.c	(revision 562)
+++ libtinymailui-gtk/tny-msg-header-list-iterator.c	(working copy)
@@ -108,7 +108,7 @@
 	me->current = g_list_next (me->current);
 	g_mutex_unlock (me->model->iterator_lock);
 
-	return me->current->data;
+	return me->current ? me->current->data : NULL;
 }
 
 gpointer 
@@ -135,6 +135,19 @@
 	return me->current->data;
 }
 
+
+static gboolean 
+tny_msg_header_list_iterator_is_done (TnyIteratorIface *self)
+{
+	TnyMsgHeaderListIterator *me = (TnyMsgHeaderListIterator*) self;
+
+	if (G_UNLIKELY (!me  || !me->model))
+		return TRUE;
+
+	return me->current != NULL;
+}
+
+
 gpointer 
 _tny_msg_header_list_iterator_first_nl (TnyMsgHeaderListIterator *me)
 {
@@ -283,7 +296,8 @@
 	klass->has_first_func = tny_msg_header_list_iterator_has_first;
 	klass->has_next_func = tny_msg_header_list_iterator_has_next;
 	klass->get_list_func = tny_msg_header_list_iterator_get_list;
-
+	klass->is_done = tny_msg_header_list_iterator_is_done;
+	
 	return;
 }
 
Index: libtinymailui-gtk/tny-account-tree-model-iterator.c
===================================================================
--- libtinymailui-gtk/tny-account-tree-model-iterator.c	(revision 562)
+++ libtinymailui-gtk/tny-account-tree-model-iterator.c	(working copy)
@@ -86,7 +86,7 @@
 	me->current = g_list_next (me->current);
 	g_mutex_unlock (me->model->iterator_lock);
 
-	return me->current->data;
+	return me->current ? me->current->data : NULL;
 }
 
 static gpointer 
@@ -106,6 +106,20 @@
 	return me->current->data;
 }
 
+
+static gboolean 
+tny_account_tree_model_iterator_is_done (TnyIteratorIface *self)
+{
+	TnyAccountTreeModelIterator *me = (TnyAccountTreeModelIterator*) self;
+	
+	if (G_UNLIKELY (!me || !me->model))
+		return TRUE;
+
+	return me->current == NULL;
+}
+
+
+
 static gpointer 
 tny_account_tree_model_iterator_first (TnyIteratorIface *self)
 {
@@ -228,7 +242,8 @@
 	klass->has_first_func = tny_account_tree_model_iterator_has_first;
 	klass->has_next_func = tny_account_tree_model_iterator_has_next;
 	klass->get_list_func = tny_account_tree_model_iterator_get_list;
-
+	klass->is_done  = tny_account_tree_model_iterator_is_done;
+	
 	return;
 }
 
Index: libtinymail/tny-iterator-iface.c
===================================================================
--- libtinymail/tny-iterator-iface.c	(revision 562)
+++ libtinymail/tny-iterator-iface.c	(working copy)
@@ -125,7 +125,31 @@
 }
 
 
+
 /**
+ * tny_iterator_iface_is_done:
+ * @self: A #TnyIteratorIface instance
+ *
+ * Does the iterator point to some valid list item
+ *
+ * Return value: TRUE if it points to a valid list item, FALSE otherwise
+ *
+ **/
+gboolean
+tny_iterator_iface_is_done (TnyIteratorIface *self)
+{
+#ifdef DEBUG
+	if (!TNY_ITERATOR_IFACE_GET_CLASS (self)->is_done)
+		g_critical ("You must implement tny_iterator_iface_is_done\n");
+#endif
+
+	return TNY_ITERATOR_IFACE_GET_CLASS (self)->is_done (self);
+}
+
+
+
+
+/**
  * tny_iterator_iface_has_first:
  * @self: A #TnyIteratorIface instance
  *
Index: libtinymail/tny-iterator-iface.h
===================================================================
--- libtinymail/tny-iterator-iface.h	(revision 562)
+++ libtinymail/tny-iterator-iface.h	(working copy)
@@ -48,6 +48,8 @@
 	gboolean (*has_first_func) (TnyIteratorIface *self);
 	gboolean (*has_next_func) (TnyIteratorIface *self);
 
+	gboolean (*is_done) (TnyIteratorIface *self);
+	
 	TnyListIface* (*get_list_func) (TnyIteratorIface *self);
 };
 
@@ -61,6 +63,9 @@
 gpointer tny_iterator_iface_current (TnyIteratorIface *self);
 gboolean tny_iterator_iface_has_first (TnyIteratorIface *self);
 gboolean tny_iterator_iface_has_next (TnyIteratorIface *self);
+
+gboolean tny_iterator_iface_is_done (TnyIteratorIface *self);
+
 TnyListIface* tny_iterator_iface_get_list (TnyIteratorIface *self);
 
 G_END_DECLS
Index: libtinymail/tny-list-iterator.c
===================================================================
--- libtinymail/tny-list-iterator.c	(revision 562)
+++ libtinymail/tny-list-iterator.c	(working copy)
@@ -94,7 +94,7 @@
 	me->current = g_list_next (me->current);
 	g_mutex_unlock (lpriv->iterator_lock);
 
-	return me->current->data;
+	return me->current ? me->current->data : NULL;
 }
 
 static gpointer 
@@ -134,6 +134,16 @@
 }
 
 
+static gboolean 
+tny_list_iterator_is_done (TnyIteratorIface *self)
+{
+	TnyListIterator *me = (TnyListIterator*) self;
+
+	return me->current == NULL;
+}
+
+
+
 static gpointer 
 tny_list_iterator_nth (TnyIteratorIface *self, guint nth)
 {
@@ -240,7 +250,8 @@
 	klass->has_first_func = tny_list_iterator_has_first;
 	klass->has_next_func = tny_list_iterator_has_next;
 	klass->get_list_func = tny_list_iterator_get_list;
-
+	klass->is_done = tny_list_iterator_is_done;
+	
 	return;
 }
 


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