[niepce] Use const_iterator when possible. And C++11 auto.



commit 6eb8b08e603e08a3d5b69a2385765228a17c5207
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Jun 20 22:52:16 2015 -0400

    Use const_iterator when possible. And C++11 auto.

 src/engine/db/filebundle.cpp             |    2 +-
 src/fwk/base/geometry.cpp                |    2 +-
 src/fwk/base/map.hpp                     |    6 +++---
 src/fwk/base/propertybag.hpp             |   13 +++++++++++--
 src/fwk/toolkit/controller.cpp           |    9 ++++-----
 src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp |    6 +++---
 src/fwk/utils/files.hpp                  |    8 ++++----
 src/fwk/utils/modulemanager.cpp          |   12 ++++++------
 src/fwk/utils/pathutils.cpp              |   26 ++++++++++++--------------
 src/niepce/ui/dialogs/importdialog.cpp   |    2 +-
 src/niepce/ui/workspacecontroller.cpp    |   12 ++++++------
 11 files changed, 52 insertions(+), 46 deletions(-)
---
diff --git a/src/engine/db/filebundle.cpp b/src/engine/db/filebundle.cpp
index 275029f..17eb195 100644
--- a/src/engine/db/filebundle.cpp
+++ b/src/engine/db/filebundle.cpp
@@ -81,7 +81,7 @@ FileBundle::filter_bundles(const fwk::FileList::Ptr & files)
 
     files->sort();
 
-    std::for_each(files->begin(), files->end(),
+    std::for_each(files->cbegin(), files->cend(),
                   [&current_base, &bundles, &current_bundle]
                   (const std::string & f) {
                       std::string basename = fwk::path_stem(f);
diff --git a/src/fwk/base/geometry.cpp b/src/fwk/base/geometry.cpp
index 38a3065..5f88739 100644
--- a/src/fwk/base/geometry.cpp
+++ b/src/fwk/base/geometry.cpp
@@ -55,7 +55,7 @@ Rect::Rect(const std::string & s)
         throw std::bad_cast();
     }
     int i = 0;
-    for_each(v.begin(), v.end(),
+    for_each(v.cbegin(), v.cend(),
              [&i, this] (const std::string &s) {
                  try {
                      _r[i] = std::stoi(s);
diff --git a/src/fwk/base/map.hpp b/src/fwk/base/map.hpp
index d82840c..253de11 100644
--- a/src/fwk/base/map.hpp
+++ b/src/fwk/base/map.hpp
@@ -38,7 +38,7 @@ namespace fwk {
   void map_get_keys(const _Map & m, std::vector<typename _Map::key_type> & l)
   {
     l.clear();
-    std::for_each(m.begin(), m.end(),
+    std::for_each(m.cbegin(), m.cend(),
                   [&l] (const typename _Map::value_type & p) {
                       l.push_back(p.first);
                   }
@@ -51,7 +51,7 @@ namespace fwk {
   void map_get_values(const _Map & m, std::vector<typename _Map::mapped_type> & l)
   {
     l.clear();
-    std::for_each(m.begin(), m.end(),
+    std::for_each(m.cbegin(), m.cend(),
                   [&l] (const typename _Map::value_type & p) {
                       l.push_back(p.second);
                   }
@@ -63,7 +63,7 @@ namespace fwk {
   template <typename _Map>
   void map_delete_all_second(const _Map & m)
   {
-    std::for_each(m.begin(), m.end(),
+    std::for_each(m.cbegin(), m.cend(),
                   [] (const typename _Map::value_type & p) {
                       delete p.second;
                   }
diff --git a/src/fwk/base/propertybag.hpp b/src/fwk/base/propertybag.hpp
index e741909..74d2aa5 100644
--- a/src/fwk/base/propertybag.hpp
+++ b/src/fwk/base/propertybag.hpp
@@ -70,12 +70,21 @@ public:
 
     const_iterator begin() const
         {
-            return m_bag.begin();
+            return m_bag.cbegin();
         }
     const_iterator end() const
         {
-            return m_bag.end();
+            return m_bag.cend();
         }
+    const_iterator cbegin() const
+        {
+            return m_bag.cbegin();
+        }
+    const_iterator cend() const
+        {
+            return m_bag.cend();
+        }
+
 
     /** return true if a property was removed prior to insertion */
     bool set_value_for_property(PropertyIndex idx, const PropertyValue & value);
diff --git a/src/fwk/toolkit/controller.cpp b/src/fwk/toolkit/controller.cpp
index c2b4d94..f62d964 100644
--- a/src/fwk/toolkit/controller.cpp
+++ b/src/fwk/toolkit/controller.cpp
@@ -48,9 +48,8 @@ Controller::add(const Controller::Ptr & sub)
 
 void Controller::remove(const Ptr & sub)
 {
-    std::list<Ptr>::iterator iter = std::find(m_subs.begin(),
-                                              m_subs.end(), sub);
-    if(iter != m_subs.end()) {
+    auto iter = std::find(m_subs.cbegin(), m_subs.cend(), sub);
+    if(iter != m_subs.cend()) {
         (*iter)->clearParent();
         m_subs.erase(iter);
     }
@@ -65,7 +64,7 @@ void Controller::terminate()
 {
     DBG_OUT("terminating controller");
     using std::placeholders::_1;
-    std::for_each(m_subs.begin(), m_subs.end(),
+    std::for_each(m_subs.cbegin(), m_subs.cend(),
                   std::bind(&Controller::terminate, _1));
     clearParent();
     m_subs.clear();
@@ -79,7 +78,7 @@ void Controller::_ready()
 {
     using std::placeholders::_1;
 
-    std::for_each(m_subs.begin(), m_subs.end(),
+    std::for_each(m_subs.cbegin(), m_subs.cend(),
                   std::bind(&Controller::_ready, _1));
     on_ready();
 }
diff --git a/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp b/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp
index 7d704cc..ffe5bf4 100644
--- a/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp
+++ b/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp
@@ -296,9 +296,9 @@ namespace db { namespace sqlite {
                        return false ;
                  }
 
-                 const std::vector< SQLStatement::binder_t > & bindings = a_statement.bindings();
-                 for(std::vector< SQLStatement::binder_t >::const_iterator iter = bindings.begin();
-                         iter != bindings.end(); iter++) 
+                 const auto & bindings = a_statement.bindings();
+                 for(auto iter = bindings.cbegin();
+                         iter != bindings.cend(); iter++) 
                  {
                        ColumnType ctype = iter->get<0>();
                        int idx = iter->get<1>();
diff --git a/src/fwk/utils/files.hpp b/src/fwk/utils/files.hpp
index 0ef96f4..b8c04eb 100644
--- a/src/fwk/utils/files.hpp
+++ b/src/fwk/utils/files.hpp
@@ -57,10 +57,10 @@ namespace fwk {
                static Ptr getFilesFromDirectory(const value_type & dir,
                                                 std::function<bool (const Glib::RefPtr<Gio::FileInfo> &)> 
filter);
 
-               const_iterator begin() const
-                       { return _impltype_t::begin(); }
-               const_iterator end() const
-                       { return _impltype_t::end(); }
+               const_iterator cbegin() const
+                       { return _impltype_t::cbegin(); }
+               const_iterator cend() const
+                       { return _impltype_t::cend(); }
                size_type size() const
                        { return _impltype_t::size(); }
                void sort()
diff --git a/src/fwk/utils/modulemanager.cpp b/src/fwk/utils/modulemanager.cpp
index 045d203..8448528 100644
--- a/src/fwk/utils/modulemanager.cpp
+++ b/src/fwk/utils/modulemanager.cpp
@@ -45,8 +45,8 @@ namespace fwk {
 
   ModuleManager::~ModuleManager()
   {
-    for(ModuleList::const_iterator mod_iter = m_modules.begin();
-        mod_iter != m_modules.end(); ++mod_iter) {
+    for(ModuleList::const_iterator mod_iter = m_modules.cbegin();
+        mod_iter != m_modules.cend(); ++mod_iter) {
       delete *mod_iter;
     }
   }
@@ -65,14 +65,14 @@ namespace fwk {
 
     std::string ext = std::string(".") + G_MODULE_SUFFIX;
 
-    for(std::set<std::string>::const_iterator iter = m_dirs.begin();
-        iter != m_dirs.end(); ++iter) {
+    for(auto iter = m_dirs.cbegin();
+        iter != m_dirs.cend(); ++iter) {
 
       fwk::FileList::Ptr l;
       l = FileList::getFilesFromDirectory(*iter, std::bind(&fwk::filter_ext, _1, ext));
 
-      for(FileList::const_iterator mod_iter = l->begin();
-          mod_iter != l->end(); ++mod_iter) {
+      for(auto mod_iter = l->cbegin();
+          mod_iter != l->cend(); ++mod_iter) {
 
         Glib::Module module(*iter + "/" + path_basename(*mod_iter), 
                             Glib::MODULE_BIND_LOCAL);
diff --git a/src/fwk/utils/pathutils.cpp b/src/fwk/utils/pathutils.cpp
index c586ecd..a08d61a 100644
--- a/src/fwk/utils/pathutils.cpp
+++ b/src/fwk/utils/pathutils.cpp
@@ -32,21 +32,21 @@ void _path_remove_recursive(const Glib::RefPtr<Gio::File> & dir);
  */
 std::string path_basename(const std::string & path)
 {
-    std::string::size_type slash_idx = path.find_last_of("/");
+    auto slash_idx = path.find_last_of("/");
     if(slash_idx == std::string::npos) {
         return path;
     }
-    return std::string(path.begin() + slash_idx + 1, path.end());
+    return std::string(path.cbegin() + slash_idx + 1, path.cend());
 }
 
 static
 std::string::size_type _path_extension_pos(const std::string &path)
 {
-    std::string::size_type idx = path.find_last_of(".");
+    auto idx = path.find_last_of(".");
     if(idx == std::string::npos) {
         return std::string::npos;
     }
-    std::string::size_type slash_idx = path.find_last_of("/");
+    auto slash_idx = path.find_last_of("/");
     // if the '.' is not part of the last component
     if((slash_idx != std::string::npos) && (idx < slash_idx)) {
         return std::string::npos;
@@ -60,22 +60,20 @@ std::string::size_type _path_extension_pos(const std::string &path)
  */
 std::string path_stem(const std::string & path)
 {
-    std::string stem;
-    std::string::size_type idx;
-    idx = _path_extension_pos(path);
+    auto idx = _path_extension_pos(path);
     if(idx == std::string::npos) {
         return path;
     }
-    return std::string(path.begin(), path.begin() + idx);
+    return std::string(path.cbegin(), path.cbegin() + idx);
 }
 
 std::string path_dirname(const std::string & path)
 {
-    std::string::size_type slash_idx = path.find_last_of("/");
+    auto slash_idx = path.find_last_of("/");
     if(slash_idx == std::string::npos) {
         return "";
     }
-    return std::string(path.begin(), path.begin() + slash_idx + 1);
+    return std::string(path.cbegin(), path.cbegin() + slash_idx + 1);
 }
 
 /** return the extension of a path
@@ -85,9 +83,9 @@ std::string path_dirname(const std::string & path)
 std::string path_extension(const std::string & path)
 {
     std::string extension;
-    std::string::size_type idx = _path_extension_pos(path);
+    auto idx = _path_extension_pos(path);
     if(idx != std::string::npos) {
-        extension = std::string(path.begin() + idx, path.end());
+        extension = std::string(path.cbegin() + idx, path.cend());
     }
 
     return extension;
@@ -95,9 +93,9 @@ std::string path_extension(const std::string & path)
 
 
 std::string path_replace_extension(const std::string & path, const char * ext)
-{ 
+{
     std::string result = path;
-    std::string::size_type idx = _path_extension_pos(result);
+    auto idx = _path_extension_pos(result);
     if(idx != std::string::npos) {
         result.replace(result.begin() + idx, result.end(), ext);
     }
diff --git a/src/niepce/ui/dialogs/importdialog.cpp b/src/niepce/ui/dialogs/importdialog.cpp
index 1c255a4..62e1dba 100644
--- a/src/niepce/ui/dialogs/importdialog.cpp
+++ b/src/niepce/ui/dialogs/importdialog.cpp
@@ -115,7 +115,7 @@ void ImportDialog::set_to_import(const Glib::ustring & f)
     fwk::FileList::Ptr list_to_import
         = fwk::FileList::getFilesFromDirectory(f, &fwk::filter_xmp_out);
 
-    std::for_each(list_to_import->begin(), list_to_import->end(),
+    std::for_each(list_to_import->cbegin(), list_to_import->cend(),
                   [this] (const std::string & s) {
                       DBG_OUT("selected %s", s.c_str());
                       Gtk::TreeIter iter = m_imagesListModel->append();
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index 54ce65b..72cc4b2 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -93,7 +93,7 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
         eng::LibFolder::ListPtr l
             = boost::any_cast<eng::LibFolder::ListPtr>(ln.param);
         DBG_OUT("received added folders # %lu", l->size());
-        for_each(l->begin(), l->end(),
+        for_each(l->cbegin(), l->cend(),
                  std::bind(&WorkspaceController::add_folder_item,
                              this, _1));
         break;
@@ -111,7 +111,7 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
         eng::Keyword::ListPtr l
             = boost::any_cast<eng::Keyword::ListPtr>(ln.param);
         DBG_ASSERT(static_cast<bool>(l), "keyword list must not be NULL");
-        for_each(l->begin(), l->end(),
+        for_each(l->cbegin(), l->cend(),
                  std::bind(&WorkspaceController::add_keyword_item,
                              this, _1));
         break;
@@ -120,9 +120,9 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
     {
         std::pair<eng::library_id_t,int> count(boost::any_cast<std::pair<eng::library_id_t,int> >(ln.param));
         DBG_OUT("count for folder %Ld is %d", (long long)count.first, count.second);
-        std::map<eng::library_id_t, Gtk::TreeIter>::iterator iter
+        std::map<eng::library_id_t, Gtk::TreeIter>::const_iterator iter
             = m_folderidmap.find( count.first );
-        if(iter != m_folderidmap.end()) {
+        if(iter != m_folderidmap.cend()) {
             Gtk::TreeRow row = *(iter->second);
             row[m_librarycolumns.m_count_n] = count.second;
             row[m_librarycolumns.m_count] = std::to_string(count.second);
@@ -134,9 +134,9 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
     {
         std::pair<eng::library_id_t,int> count(boost::any_cast<std::pair<eng::library_id_t,int> >(ln.param));
         DBG_OUT("count change for folder %Ld is %d", (long long)count.first, count.second);
-        std::map<eng::library_id_t, Gtk::TreeIter>::iterator iter
+        std::map<eng::library_id_t, Gtk::TreeIter>::const_iterator iter
             = m_folderidmap.find( count.first );
-        if(iter != m_folderidmap.end()) {
+        if(iter != m_folderidmap.cend()) {
             Gtk::TreeRow row = *(iter->second);
             int new_count = row[m_librarycolumns.m_count_n] + count.second;
             row[m_librarycolumns.m_count_n] = new_count;


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