[niepce] Use lambda for FileBundle::filter_bundles()



commit cc435aa542ac160b6aa17c72098a1d2775297042
Author: Hubert Figuière <hub figuiere net>
Date:   Fri Jun 7 18:55:34 2013 -0400

    Use lambda for FileBundle::filter_bundles()

 src/engine/db/filebundle.cpp      |   38 +++++++++++++++++++-----------------
 src/engine/db/test_filebundle.cpp |    6 ++--
 2 files changed, 23 insertions(+), 21 deletions(-)
---
diff --git a/src/engine/db/filebundle.cpp b/src/engine/db/filebundle.cpp
index 192f80a..a5c6c6e 100644
--- a/src/engine/db/filebundle.cpp
+++ b/src/engine/db/filebundle.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - engine/db/filebundle.cpp
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2013 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <algorithm>
 
 #include "filebundle.hpp"
 #include "fwk/base/debug.hpp"
@@ -80,23 +81,24 @@ FileBundle::filter_bundles(const fwk::FileList::Ptr & files)
 
     files->sort();
 
-    for(auto iter = files->begin();
-        iter != files->end(); ++iter)
-    {
-        std::string basename = fwk::path_stem(*iter);
-
-        if(basename != current_base) {
-            FileBundle::Ptr new_bundle(new FileBundle());
-            if(new_bundle->add(*iter)) {
-                bundles->push_back(new_bundle);
-                current_bundle = new_bundle;
-                current_base = basename;
-            }
-        }
-        else {
-            current_bundle->add(*iter);
-        }
-    }
+    std::for_each(files->begin(), files->end(),
+                  [&current_base, &bundles, &current_bundle]
+                  (const std::string & f) {
+                      std::string basename = fwk::path_stem(f);
+
+                      if(basename != current_base) {
+                          FileBundle::Ptr new_bundle(new FileBundle());
+                          if(new_bundle->add(f)) {
+                              bundles->push_back(new_bundle);
+                              current_bundle = new_bundle;
+                              current_base = basename;
+                          }
+                      }
+                      else {
+                          current_bundle->add(f);
+                      }
+                  }
+        );
 
     return bundles;
 }
diff --git a/src/engine/db/test_filebundle.cpp b/src/engine/db/test_filebundle.cpp
index 8abdfa7..3cb17ab 100644
--- a/src/engine/db/test_filebundle.cpp
+++ b/src/engine/db/test_filebundle.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - engine/db/test_filebundle.cpp
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2013 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -47,8 +47,8 @@ int test_main(int, char *[])
         eng::FileBundle::filter_bundles(thelist);
 
     BOOST_CHECK(bundles_list->size() == 2);
-    eng::FileBundle::List::const_iterator iter = bundles_list->begin();
-    eng::FileBundle::Ptr b = *iter;
+    auto iter = bundles_list->begin();
+    auto b = *iter;
     BOOST_CHECK(b->main_file() == "/foo/bar/dcs_0001.nef");
     BOOST_CHECK(b->jpeg() == "/foo/bar/dcs_0001.jpg");
     BOOST_CHECK(b->sidecar() == "/foo/bar/dcs_0001.xmp");


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