[niepce] importer: UIResult now has a run() function to spawn the task on a thread



commit 7d2cffb6b9b066dfa08fb7dab9df67a90cbf9ec5
Author: Hubert Figuière <hub figuiere net>
Date:   Tue May 16 00:47:33 2017 -0400

    importer: UIResult now has a run() function to spawn the task on a thread

 src/fwk/toolkit/Makefile.am            |    2 +-
 src/fwk/toolkit/uiresult.cpp           |   32 ++++++++++++++++++++++++++
 src/fwk/toolkit/uiresult.hpp           |    6 +++-
 src/niepce/ui/dialogs/importdialog.cpp |   39 ++++++++++++++-----------------
 4 files changed, 55 insertions(+), 24 deletions(-)
---
diff --git a/src/fwk/toolkit/Makefile.am b/src/fwk/toolkit/Makefile.am
index f153d95..a663de0 100644
--- a/src/fwk/toolkit/Makefile.am
+++ b/src/fwk/toolkit/Makefile.am
@@ -30,7 +30,7 @@ libniepceframework_a_SOURCES = configuration.hpp configuration.cpp \
        frame.hpp frame.cpp \
        controller.hpp controller.cpp \
        uicontroller.hpp uicontroller.cpp \
-       uiresult.hpp \
+       uiresult.hpp uiresult.cpp \
        thumbnail.hpp thumbnail.cpp \
        mapcontroller.hpp mapcontroller.cpp \
        notification.hpp \
diff --git a/src/fwk/toolkit/uiresult.cpp b/src/fwk/toolkit/uiresult.cpp
new file mode 100644
index 0000000..d433d92
--- /dev/null
+++ b/src/fwk/toolkit/uiresult.cpp
@@ -0,0 +1,32 @@
+/* -*- mode: C++; tab-width: 2; c-basic-offset: 2; indent-tabs-mode:nil; -*- */
+/*
+ * niepce - fwk/toolkit/uiresult.hpp
+ *
+ * Copyright (C) 2017 Hubert Figuière
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <thread>
+
+#include "uiresult.hpp"
+
+namespace fwk {
+
+void UIResult::run(std::function<void ()>&& f) {
+  std::thread t(f);
+  t.detach();
+}
+
+}
diff --git a/src/fwk/toolkit/uiresult.hpp b/src/fwk/toolkit/uiresult.hpp
index e1cbdb3..4a49297 100644
--- a/src/fwk/toolkit/uiresult.hpp
+++ b/src/fwk/toolkit/uiresult.hpp
@@ -18,8 +18,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-
-
 #pragma once
 
 #include <deque>
@@ -31,6 +29,7 @@
 
 namespace fwk {
 
+/** @brief Fetch a "result" asynchronously */
 class UIResult
 {
 public:
@@ -39,6 +38,8 @@ public:
   sigc::connection connect(sigc::slot<void>&& slot) {
     return m_notifier.connect(std::move(slot));
   }
+
+  void run(std::function<void ()>&& f);
 protected:
   Glib::Dispatcher m_notifier;
   std::mutex m_data_mutex;
@@ -70,6 +71,7 @@ private:
   T m_data;
 };
 
+/** @brief Fetch many "results" asynchronously */
 template<class T>
 class UIResults
   : public UIResult
diff --git a/src/niepce/ui/dialogs/importdialog.cpp b/src/niepce/ui/dialogs/importdialog.cpp
index a67a3bc..54ab5e1 100644
--- a/src/niepce/ui/dialogs/importdialog.cpp
+++ b/src/niepce/ui/dialogs/importdialog.cpp
@@ -149,22 +149,19 @@ void ImportDialog::set_to_import(const Glib::ustring & f)
         m_importer = std::make_shared<eng::DirectoryImporter>();
     }
 
-    eng::IImporter::SourceContentReady source_content_ready =
-      [this] (std::list<eng::ImportedFile::Ptr>&& list_to_import) {
-        this->m_files_to_import.send_data(std::move(list_to_import));
-      };
-
-
     m_images_list_model->clear();
     m_images_list_map.clear();
     m_files_to_import.clear();
 
     auto importer = m_importer;
-    auto source_content =
-      std::async(std::launch::async,
-                 [f, importer, source_content_ready] () {
-                   return importer->list_source_content(f, source_content_ready);
-                 });
+    m_files_to_import.run(
+      [this, f, importer] () {
+        return importer->list_source_content(
+          f,
+          [this] (std::list<eng::ImportedFile::Ptr>&& list_to_import) {
+            this->m_files_to_import.send_data(std::move(list_to_import));
+          });
+      });
 
     m_folder_path_source = f;
     m_destination_folder->set_text(fwk::path_basename(f));
@@ -179,7 +176,7 @@ void ImportDialog::append_files_to_import()
     ERR_OUT("No image list model");
     return;
   }
-  // request the previews to the importer.tn.pixmap.pixbuf()
+  // request the previews to the importer.
   std::list<std::string> paths;
   for(const auto & f : files_to_import) {
     DBG_OUT("selected %s", f->name().c_str());
@@ -190,17 +187,17 @@ void ImportDialog::append_files_to_import()
     iter->set_value(m_grid_columns.file, std::move(f));
   }
 
-  eng::IImporter::PreviewReady preview_ready = [this] (const std::string& path,
-                                                       const fwk::Thumbnail& thumbnail) {
-    this->m_previews_to_import.send_data(std::make_pair(path, thumbnail));
-  };
-
   auto importer = m_importer;
   auto source = m_folder_path_source.raw();
-  std::async(std::launch::async,
-             [importer, source, paths, preview_ready] () {
-               return importer->get_previews_for(source, paths, preview_ready);
-             });
+  m_previews_to_import.run(
+    [this, importer, source, paths] () {
+      return importer->get_previews_for(
+        source, paths,
+        [this] (const std::string& path, const fwk::Thumbnail& thumbnail) {
+          this->m_previews_to_import.send_data(
+            std::make_pair(path, thumbnail));
+        });
+    });
 }
 
 void ImportDialog::preview_received()


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