[gnome-system-monitor] Use ProcInfo::all directly



commit e9173fbcef460ff947e3a01e1468c746d7249a3e
Author: Artem Vorotnikov <artem vorotnikov me>
Date:   Sat Dec 24 22:23:48 2016 +0300

    Use ProcInfo::all directly
    
    Signed-off-by: Artem Vorotnikov <artem vorotnikov me>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776653

 src/application.h |    4 ----
 src/lsof.cpp      |   10 ++++------
 src/proctable.cpp |   36 +++++++++++++++---------------------
 3 files changed, 19 insertions(+), 31 deletions(-)
---
diff --git a/src/application.h b/src/application.h
index 50550d6..7965fd8 100644
--- a/src/application.h
+++ b/src/application.h
@@ -143,13 +143,9 @@ class ProcInfo
     // by pid so this helps a lot when looking for the parent node
     // as ppid is nearly always < pid.
     typedef std::map<pid_t, ProcInfo*> List;
-    typedef List::iterator Iterator;
-
     static List all;
 
     static ProcInfo* find(pid_t pid);
-    static Iterator begin() { return ProcInfo::all.begin(); }
-    static Iterator end() { return ProcInfo::all.end(); }
 
 
     ProcInfo(pid_t pid);
diff --git a/src/lsof.cpp b/src/lsof.cpp
index 1b27eed..e04c448 100644
--- a/src/lsof.cpp
+++ b/src/lsof.cpp
@@ -133,7 +133,6 @@ namespace
         void search()
         {
             typedef std::set<string> MatchSet;
-            typedef MatchSet::const_iterator iterator;
 
             bool regex_error = false;
 
@@ -143,21 +142,20 @@ namespace
 
                 unsigned count = 0;
 
-                for (ProcInfo::Iterator it(ProcInfo::begin()); it != ProcInfo::end(); ++it) {
-                    const ProcInfo &info(*it->second);
-
+                for (const auto& v : ProcInfo::all) {
+                    const auto& info = *v.second;
                     MatchSet matches;
                     lsof.search(info, std::inserter(matches, matches.begin()));
                     count += matches.size();
 
-                    for (iterator it(matches.begin()), end(matches.end()); it != end; ++it) {
+                    for (const auto& match : matches) {
                         GtkTreeIter file;
                         gtk_list_store_append(this->model, &file);
                         gtk_list_store_set(this->model, &file,
                                            PROCMAN_LSOF_COL_PIXBUF, info.pixbuf->gobj(),
                                            PROCMAN_LSOF_COL_PROCESS, info.name,
                                            PROCMAN_LSOF_COL_PID, info.pid,
-                                           PROCMAN_LSOF_COL_FILENAME, it->c_str(),
+                                           PROCMAN_LSOF_COL_FILENAME, match.c_str(),
                                            -1);
                     }
                 }
diff --git a/src/proctable.cpp b/src/proctable.cpp
index 6682908..8233f5a 100644
--- a/src/proctable.cpp
+++ b/src/proctable.cpp
@@ -69,8 +69,7 @@ std::map<pid_t, guint64> ProcInfo::cpu_times;
 
 ProcInfo* ProcInfo::find(pid_t pid)
 {
-    Iterator it(ProcInfo::all.find(pid));
-    return (it == ProcInfo::all.end() ? NULL : it->second);
+    try { return all.at(pid); } catch (const std::out_of_range& e) { return nullptr; }
 }
 
 static void
@@ -199,8 +198,8 @@ cb_refresh_icons (GtkIconTheme *theme, gpointer data)
         g_source_remove (app->timeout);
     }
 
-    for (ProcInfo::Iterator it(ProcInfo::begin()); it != ProcInfo::end(); ++it) {
-        app->pretty_table->set_icon(*(it->second));
+    for (auto& v : ProcInfo::all) {
+        app->pretty_table->set_icon(*v.second);
     }
 
     cb_timeout(app);
@@ -983,7 +982,8 @@ refresh_list (GsmApplication *app, const pid_t* pid_list, const guint n)
 
         if (!info) {
             info = new ProcInfo(pid_list[i]);
-            ProcInfo::all[info->pid] = info;
+            ProcInfo::all.erase(info->pid);
+            ProcInfo::all.insert({info->pid, info});
             addition.push_back(info);
         }
 
@@ -997,23 +997,19 @@ refresh_list (GsmApplication *app, const pid_t* pid_list, const guint n)
 
     const std::set<pid_t> pids(pid_list, pid_list + n);
 
-    ProcInfo::Iterator it(ProcInfo::begin());
-
-    while (it != ProcInfo::end()) {
-        ProcInfo * const info = it->second;
-        ProcInfo::Iterator next(it);
-        ++next;
-
+    ProcInfo::List new_set;
+    for (const auto& v : ProcInfo::all) {
+        auto& info = v.second;
         if (pids.find(info->pid) == pids.end()) {
             procman_debug("ripping %d", info->pid);
             remove_info_from_tree(app, model, info, addition);
             addition.remove(info);
-            ProcInfo::all.erase(it);
             delete info;
+        } else {
+            new_set.insert({info->pid, info});
         }
-
-        it = next;
     }
+    ProcInfo::all = new_set;
 
     // INVARIANT
     // pid_list == ProcInfo::all + addition
@@ -1080,13 +1076,11 @@ refresh_list (GsmApplication *app, const pid_t* pid_list, const guint n)
     }
     else {
         // don't care of the tree
-        for (ProcList::iterator it(addition.begin()); it != addition.end(); ++it)
-            insert_info_to_tree(*it, app);
+        for (auto& v : addition) insert_info_to_tree(v, app);
     }
 
 
-    for (ProcInfo::Iterator it(ProcInfo::begin()); it != ProcInfo::end(); ++it)
-        update_info_mutable_cols(it->second);
+    for (auto& v : ProcInfo::all) update_info_mutable_cols(v.second);
 }
 
 void
@@ -1158,8 +1152,8 @@ proctable_update (GsmApplication *app)
 void
 proctable_free_table (GsmApplication * const app)
 {
-    for (ProcInfo::Iterator it(ProcInfo::begin()); it != ProcInfo::end(); ++it)
-        delete it->second;
+    for (auto& v : ProcInfo::all)
+        delete v.second;
 
     ProcInfo::all.clear();
 }


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