[ekiga/gnome-2-26] Removed all uses of iterators, as they're my prime suspects for crashes



commit aca40cd87aca7fb6a11823ef77c088d8136a77d7
Author: Julien Puydt <jpuydt src gnome org>
Date:   Sun Mar 29 08:38:21 2009 +0000

    Removed all uses of iterators, as they're my prime suspects for crashes
    
    svn path=/trunk/; revision=7830
---
 lib/engine/components/avahi/avahi-heap.cpp         |   51 +++++--
 lib/engine/components/evolution/evolution-book.cpp |   85 +++++++++----
 .../components/evolution/evolution-source.cpp      |   53 +++++---
 lib/engine/components/local-roster/local-heap.cpp  |  135 +++++++++++++++-----
 4 files changed, 233 insertions(+), 91 deletions(-)

diff --git a/lib/engine/components/avahi/avahi-heap.cpp b/lib/engine/components/avahi/avahi-heap.cpp
index 19c500e..6a3a651 100644
--- a/lib/engine/components/avahi/avahi-heap.cpp
+++ b/lib/engine/components/avahi/avahi-heap.cpp
@@ -225,6 +225,33 @@ Avahi::Heap::BrowserCallback (AvahiServiceBrowser *browser,
   }
 }
 
+class resolver_callback_helper
+{
+public:
+
+  resolver_callback_helper (const std::string name_): name(name_)
+  {}
+
+  bool test (gmref_ptr<Ekiga::URIPresentity> presentity_)
+  {
+    bool result;
+
+    if (presentity_->get_name () == name) {
+
+      presentity = presentity_;
+      result = false;
+    }
+    return result;
+  }
+
+  gmref_ptr<Ekiga::URIPresentity> found_presentity () const
+  { return presentity; }
+
+private:
+  gmref_ptr<Ekiga::URIPresentity> presentity;
+  const std::string name;
+};
+
 void
 Avahi::Heap::ResolverCallback (AvahiServiceResolver *resolver,
 			       AvahiIfIndex /*interface*/,
@@ -243,13 +270,12 @@ Avahi::Heap::ResolverCallback (AvahiServiceResolver *resolver,
   std::string software;
   std::string presence;
   std::string status;
-  bool already_known = false;
   gchar *url = NULL;
   AvahiStringList *txt_tmp = NULL;
 
   switch (event) {
 
-  case AVAHI_RESOLVER_FOUND:
+  case AVAHI_RESOLVER_FOUND: {
     name = name_;
     for (txt_tmp = txt;  txt_tmp != NULL; txt_tmp = txt_tmp->next) {
 
@@ -274,19 +300,14 @@ Avahi::Heap::ResolverCallback (AvahiServiceResolver *resolver,
       }
     }
 
-    for (iterator iter = begin ();
-	 iter != end ();
-	 iter++) {
+    resolver_callback_helper helper(name);
+    visit_presentities (sigc::mem_fun (helper, &resolver_callback_helper::test));
+    if (helper.found_presentity ()) {
 
-      if ((*iter)->get_name () == name) {
-
-	/* known contact has been updated */
-	presence_received.emit ((*iter)->get_uri (), presence);
-	status_received.emit ((*iter)->get_uri (), status);
-	already_known = true;
-      }
-    }
-    if (already_known == false) {
+      /* known contact has been updated */
+      presence_received.emit (helper.found_presentity ()->get_uri (), presence);
+      status_received.emit (helper.found_presentity ()->get_uri (), status);
+    } else {
 
       /* ok, this is a new contact */
       gchar** broken = NULL;
@@ -305,7 +326,7 @@ Avahi::Heap::ResolverCallback (AvahiServiceResolver *resolver,
       }
       g_strfreev (broken);
     }
-    break;
+    break;}
   case AVAHI_RESOLVER_FAILURE:
 
     avahi_service_resolver_free (resolver);
diff --git a/lib/engine/components/evolution/evolution-book.cpp b/lib/engine/components/evolution/evolution-book.cpp
index 7017a84..9054ef3 100644
--- a/lib/engine/components/evolution/evolution-book.cpp
+++ b/lib/engine/components/evolution/evolution-book.cpp
@@ -89,18 +89,38 @@ on_view_contacts_removed_c (EBook */*ebook*/,
   ((Evolution::Book *)data)->on_view_contacts_removed (ids);
 }
 
+class contacts_removed_helper
+{
+public:
+
+  contacts_removed_helper (const std::string id_): id(id_)
+  {}
+
+  bool test (Evolution::ContactPtr contact)
+  {
+    bool result;
+
+    if (contact->get_id () == id) {
+
+      contact->removed.emit ();
+      result = false;
+    }
+
+    return result;
+  }
+
+private:
+  const std::string id;
+};
+
 void
 Evolution::Book::on_view_contacts_removed (GList *ids)
 {
-  for (; ids != NULL; ids = g_list_next (ids))
-    for (iterator iter = begin ();
-	 iter != end ();
-	 iter++)
-      if ((*iter)->get_id () == (gchar *)ids->data) {
-
-	remove_contact (*iter);
-	break; // will do the loop on ids, but stop using iter which is invalid
-      }
+  for (; ids != NULL; ids = g_list_next (ids)) {
+
+    contacts_removed_helper helper((gchar*)ids->data);
+    visit_contacts (sigc::mem_fun (helper, &contacts_removed_helper::test));
+  }
 }
 
 static void
@@ -111,22 +131,41 @@ on_view_contacts_changed_c (EBook */*ebook*/,
   ((Evolution::Book*)data)->on_view_contacts_changed (econtacts);
 }
 
+class contact_updated_helper
+{
+public:
+
+  contact_updated_helper (EContact* econtact_): econtact(econtact_)
+  {
+    id = (const gchar*)e_contact_get_const (econtact, E_CONTACT_UID);
+  }
+
+  bool test (Evolution::ContactPtr contact)
+  {
+    bool result = true;
+
+    if (contact->get_id () == id) {
+
+      contact->update_econtact (econtact);
+      result = false;
+    }
+
+    return result;
+  }
+
+private:
+  EContact* econtact;
+  std::string id;
+};
+
 void
 Evolution::Book::on_view_contacts_changed (GList *econtacts)
 {
-  EContact *econtact = NULL;
-
   for (; econtacts != NULL; econtacts = g_list_next (econtacts)) {
 
-    econtact = E_CONTACT (econtacts->data);
+    contact_updated_helper helper (E_CONTACT (econtacts->data));
 
-    for (iterator iter = begin ();
-	 iter != end ();
-	 iter++)
-
-      if ((*iter)->get_id()
-	  == (const gchar *)e_contact_get_const (econtact, E_CONTACT_UID))
-	(*iter)->update_econtact (econtact);
+    visit_contacts (sigc::mem_fun (helper, &contact_updated_helper::test));
   }
 }
 
@@ -262,13 +301,7 @@ Evolution::Book::get_status () const
 void
 Evolution::Book::refresh ()
 {
-  /* we flush */
-  iterator iter = begin ();
-  while (iter != end ()) {
-
-    remove_contact (*iter);
-    iter = begin ();
-  }
+  remove_all_objects ();
 
   /* we go */
   if (e_book_is_opened (book)) 
diff --git a/lib/engine/components/evolution/evolution-source.cpp b/lib/engine/components/evolution/evolution-source.cpp
index 76470fb..ccb2431 100644
--- a/lib/engine/components/evolution/evolution-source.cpp
+++ b/lib/engine/components/evolution/evolution-source.cpp
@@ -100,36 +100,49 @@ on_source_list_group_removed_c (ESourceList */*source_list*/,
   self->remove_group (group);
 }
 
-void
-Evolution::Source::remove_group (ESourceGroup *group)
+class remove_helper
 {
-  gboolean found = FALSE;
-  ESource *book_source = NULL;
-  ESourceGroup *book_group = NULL;
-  EBook *book_ebook = NULL;
+public :
 
-  do {
+  remove_helper (ESourceGroup* group_): group(group_)
+  { ready (); }
 
-    found = FALSE;
+  inline void ready ()
+  { found = false; }
 
-    for (iterator iter = begin ();
-	 iter != end ();
-	 iter++) {
+  bool test (Evolution::BookPtr book)
+  {
+    EBook *book_ebook = book->get_ebook ();
+    ESource *book_source = e_book_get_source (book_ebook);
+    ESourceGroup *book_group = e_source_peek_group (book_source);
 
-      book_ebook = (*iter)->get_ebook ();
+    if (book_group == group) {
 
-      book_source = e_book_get_source (book_ebook);
+      book->removed.emit ();
+      found = true;
+    }
+    return !found;
+  }
 
-      book_group = e_source_peek_group (book_source);
+  bool has_found () const
+  { return found; }
 
-      if (book_group == group) {
+private:
+  ESourceGroup* group;
+  bool found;
+};
 
-	found = TRUE;
-	(*iter)->removed.emit ();
-      }
-    }
+void
+Evolution::Source::remove_group (ESourceGroup *group)
+{
+  remove_helper helper (group);
+
+  do {
+
+    helper.ready ();
+    visit_books (sigc::mem_fun (helper, &remove_helper::test));
 
-  } while (found);
+  } while (helper.has_found ());
 }
 
 Evolution::Source::Source (Ekiga::ServiceCore &_services)
diff --git a/lib/engine/components/local-roster/local-heap.cpp b/lib/engine/components/local-roster/local-heap.cpp
index 4a26e8f..3dcf077 100644
--- a/lib/engine/components/local-roster/local-heap.cpp
+++ b/lib/engine/components/local-roster/local-heap.cpp
@@ -63,7 +63,7 @@ Local::Heap::Heap (Ekiga::ServiceCore &_core): core (_core), doc ()
     doc = std::tr1::shared_ptr<xmlDoc> (xmlRecoverMemory (raw.c_str (), raw.length ()), xmlFreeDoc);
     if ( !doc)
       doc = std::tr1::shared_ptr<xmlDoc> (xmlNewDoc (BAD_CAST "1.0"), xmlFreeDoc);
-    
+
     root = xmlDocGetRootElement (doc.get ());
     if (root == NULL) {
 
@@ -129,32 +129,60 @@ Local::Heap::populate_menu_for_group (const std::string name,
   return true;
 }
 
+struct has_presentity_with_uri_helper
+{
+  has_presentity_with_uri_helper (const std::string uri_): uri(uri_),
+							   found(false)
+  {}
+
+  const std::string uri;
+
+  bool found;
+
+  bool test (Local::PresentityPtr presentity)
+  {
+    if (presentity->get_uri () == uri) {
+
+      found = true;
+    }
+
+    return !found;
+  }
+};
 
 bool
 Local::Heap::has_presentity_with_uri (const std::string uri)
 {
-  bool result = false;
+  has_presentity_with_uri_helper helper(uri);
 
-  for (iterator iter = begin ();
-       iter != end () && result != true;
-       iter++)
-    result = ((*iter)->get_uri () == uri);
+  visit_presentities (sigc::mem_fun (helper, &has_presentity_with_uri_helper::test));
 
-  return result;
+  return helper.found;
 }
 
+struct existing_groups_helper
+{
+  std::set<std::string> groups;
+
+  bool test (Local::PresentityPtr presentity)
+  {
+    groups.insert (presentity->get_groups ().begin (),
+		   presentity->get_groups ().end ());
+
+    return true;
+  }
+};
 
 const std::set<std::string>
 Local::Heap::existing_groups ()
 {
   std::set<std::string> result;
 
-  for (iterator iter = begin ();
-       iter != end ();
-       iter++) {
+  {
+    existing_groups_helper helper;
 
-    std::set<std::string> groups = (*iter)->get_groups ();
-    result.insert (groups.begin (), groups.end ());
+    visit_presentities (sigc::mem_fun (helper, &existing_groups_helper::test));
+    result = helper.groups;
   }
 
   result.insert (_("Family"));
@@ -210,31 +238,64 @@ Local::Heap::new_presentity (const std::string name,
   }
 }
 
+struct push_presence_helper
+{
+  push_presence_helper (const std::string uri_,
+			const std::string presence_): uri(uri_),
+						      presence(presence_)
+  {}
+
+  bool test (Local::PresentityPtr presentity)
+  {
+    if (presentity->get_uri () == uri) {
+
+      presentity->set_presence (presence);
+    }
+
+    return true;
+  }
+
+  const std::string uri;
+  const std::string presence;
+};
 
 void
 Local::Heap::push_presence (const std::string uri,
 			    const std::string presence)
 {
-  for (iterator iter = begin ();
-       iter != end ();
-       ++iter) {
+  push_presence_helper helper(uri, presence);
 
-    if ((*iter)->get_uri () == uri)
-      (*iter)->set_presence (presence);
-  }
+  visit_presentities (sigc::mem_fun (helper, &push_presence_helper::test));
 }
 
+struct push_status_helper
+{
+  push_status_helper (const std::string uri_,
+			const std::string status_): uri(uri_),
+						    status(status_)
+  {}
+
+  bool test (Local::PresentityPtr presentity)
+  {
+    if (presentity->get_uri () == uri) {
+
+      presentity->set_status (status);
+    }
+
+    return true;
+  }
+
+  const std::string uri;
+  const std::string status;
+};
+
 void
 Local::Heap::push_status (const std::string uri,
 			  const std::string status)
 {
-  for (iterator iter = begin ();
-       iter != end ();
-       ++iter) {
+  push_status_helper helper(uri, status);
 
-    if ((*iter)->get_uri () == uri)
-      (*iter)->set_status (status);
-  }
+  visit_presentities (sigc::mem_fun (helper, &push_status_helper::test));
 }
 
 
@@ -373,6 +434,24 @@ Local::Heap::on_rename_group (std::string name)
   }
 }
 
+struct rename_group_form_submitted_helper
+{
+  rename_group_form_submitted_helper (const std::string old_name_,
+				      const std::string new_name_):
+    old_name(old_name_),
+    new_name(new_name_)
+  {}
+
+  const std::string old_name;
+  const std::string new_name;
+
+  bool rename_group (Local::PresentityPtr presentity)
+  {
+    presentity->rename_group (old_name, new_name);
+    return true;
+  }
+};
+
 void
 Local::Heap::rename_group_form_submitted (std::string old_name,
 					  bool submitted,
@@ -386,12 +465,8 @@ Local::Heap::rename_group_form_submitted (std::string old_name,
 
     if ( !new_name.empty () && new_name != old_name) {
 
-      for (iterator iter = begin ();
-	   iter != end ();
-	   ++iter) {
-
-	(*iter)->rename_group (old_name, new_name);
-      }
+      rename_group_form_submitted_helper helper (old_name, new_name);
+      visit_presentities (sigc::mem_fun (helper, &rename_group_form_submitted_helper::rename_group));
     }
   } catch (Ekiga::Form::not_found) {
 



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