Browse Source

Factored out helper classes for QtConcurrent mapped functions.

Sascha Zelzer 12 years ago
parent
commit
fd907351aa

+ 1 - 0
Libs/CommandLineModules/Core/CMakeLists.txt

@@ -17,6 +17,7 @@ set(KIT_SRCS
   ctkCmdLineModuleBackend.cpp
   ctkCmdLineModuleCache.cpp
   ctkCmdLineModuleCache_p.h
+  ctkCmdLineModuleConcurrentHelpers.cpp
   ctkCmdLineModuleDefaultPathBuilder.cpp
   ctkCmdLineModuleDescription.cpp
   ctkCmdLineModuleDescription_p.h

+ 93 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleConcurrentHelpers.cpp

@@ -0,0 +1,93 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#include "ctkCmdLineModuleConcurrentHelpers.h"
+
+#include "ctkCmdLineModuleManager.h"
+#include "ctkException.h"
+
+#include <QUrl>
+#include <QDebug>
+
+//----------------------------------------------------------------------------
+ctkCmdLineModuleConcurrentRegister::ctkCmdLineModuleConcurrentRegister(ctkCmdLineModuleManager* manager,
+                                                                       bool debug)
+  : ModuleManager(manager), Debug(debug)
+{}
+
+//----------------------------------------------------------------------------
+ctkCmdLineModuleReference ctkCmdLineModuleConcurrentRegister::operator()(const QString& moduleLocation)
+{
+  return this->operator ()(QUrl::fromLocalFile(moduleLocation));
+}
+
+//----------------------------------------------------------------------------
+ctkCmdLineModuleReference ctkCmdLineModuleConcurrentRegister::operator()(const QUrl& moduleUrl)
+{
+  try
+  {
+    return this->ModuleManager->registerModule(moduleUrl);
+  }
+  catch (const ctkException& e)
+  {
+    if (this->Debug)
+    {
+      qDebug() << e;
+    }
+    return ctkCmdLineModuleReference();
+  }
+  catch (...)
+  {
+    if (this->Debug)
+    {
+      qDebug() << "Registering module" << moduleUrl << "failed with an unknown exception.";
+    }
+    return ctkCmdLineModuleReference();
+  }
+}
+
+//----------------------------------------------------------------------------
+ctkCmdLineModuleConcurrentUnRegister::ctkCmdLineModuleConcurrentUnRegister(ctkCmdLineModuleManager* manager)
+  : ModuleManager(manager)
+{}
+
+//----------------------------------------------------------------------------
+bool ctkCmdLineModuleConcurrentUnRegister::operator()(const QString& moduleLocation)
+{
+  return this->operator ()(QUrl::fromLocalFile(moduleLocation));
+}
+
+//----------------------------------------------------------------------------
+bool ctkCmdLineModuleConcurrentUnRegister::operator()(const QUrl& moduleUrl)
+{
+  return this->operator ()(this->ModuleManager->moduleReference(moduleUrl));
+}
+
+//----------------------------------------------------------------------------
+bool ctkCmdLineModuleConcurrentUnRegister::operator()(const ctkCmdLineModuleReference& moduleRef)
+{
+  if (moduleRef)
+  {
+    this->ModuleManager->unregisterModule(moduleRef);
+    return true;
+  }
+  return false;
+}

+ 77 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleConcurrentHelpers.h

@@ -0,0 +1,77 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#ifndef CTKCMDLINEMODULECONCURRENTHELPERS_H
+#define CTKCMDLINEMODULECONCURRENTHELPERS_H
+
+#include "ctkCommandLineModulesCoreExport.h"
+
+#include "ctkCmdLineModuleReference.h"
+
+class ctkCmdLineModuleManager;
+
+/**
+ * \ingroup CommandLineModulesCore
+ *
+ * \brief A function object for concurrently adding modules.
+ */
+class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleConcurrentRegister
+{
+
+public:
+
+  typedef ctkCmdLineModuleReference result_type;
+
+  ctkCmdLineModuleConcurrentRegister(ctkCmdLineModuleManager* manager, bool debug = false);
+  ctkCmdLineModuleReference operator()(const QString& moduleLocation);
+  ctkCmdLineModuleReference operator()(const QUrl& moduleUrl);
+
+private:
+
+  ctkCmdLineModuleManager* ModuleManager;
+  bool Debug;
+};
+
+/**
+ * \ingroup CommandLineModulesCore
+ *
+ * \brief A function object for concurrently removing modules.
+ */
+class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleConcurrentUnRegister
+{
+
+public:
+
+  typedef bool result_type;
+
+  ctkCmdLineModuleConcurrentUnRegister(ctkCmdLineModuleManager* manager);
+
+  bool operator()(const QString& moduleLocation);
+  bool operator()(const QUrl& moduleUrl);
+  bool operator()(const ctkCmdLineModuleReference& moduleRef);
+
+private:
+
+  ctkCmdLineModuleManager* ModuleManager;
+};
+
+
+#endif // CTKCMDLINEMODULECONCURRENTHELPERS_H

+ 4 - 68
Libs/CommandLineModules/Core/ctkCmdLineModuleDirectoryWatcher.cpp

@@ -21,6 +21,7 @@
 #include "ctkCmdLineModuleDirectoryWatcher.h"
 #include "ctkCmdLineModuleDirectoryWatcher_p.h"
 #include "ctkCmdLineModuleManager.h"
+#include "ctkCmdLineModuleConcurrentHelpers.h"
 #include "ctkException.h"
 
 #include <QObject>
@@ -34,72 +35,6 @@
 
 #include <iostream>
 
-//-----------------------------------------------------------------------------
-// A function object for concurrently adding modules
-namespace {
-struct AddModule
-{
-  typedef ctkCmdLineModuleReference result_type;
-
-  AddModule(ctkCmdLineModuleManager* manager, bool debug = false)
-    : ModuleManager(manager), Debug(debug)
-  {}
-
-  ctkCmdLineModuleReference operator()(const QString& moduleLocation)
-  {
-    try
-    {
-      return this->ModuleManager->registerModule(QUrl::fromLocalFile(moduleLocation));
-    }
-    catch (const ctkException& e)
-    {
-      if (this->Debug)
-      {
-        qDebug() << e;
-      }
-      return ctkCmdLineModuleReference();
-    }
-    catch (...)
-    {
-      if (this->Debug)
-      {
-        qDebug() << "Registering module" << moduleLocation << "failed with an unknown exception.";
-      }
-      return ctkCmdLineModuleReference();
-    }
-  }
-
-  ctkCmdLineModuleManager* ModuleManager;
-  bool Debug;
-};
-}
-
-//-----------------------------------------------------------------------------
-// A function object for concurrently removing modules
-namespace {
-struct RemoveModule
-{
-  typedef bool result_type;
-
-  RemoveModule(ctkCmdLineModuleManager* manager)
-    : ModuleManager(manager)
-  {}
-
-  bool operator()(const QString& moduleLocation)
-  {
-    ctkCmdLineModuleReference ref = this->ModuleManager->moduleReference(QUrl::fromLocalFile(moduleLocation));
-    if (ref)
-    {
-      this->ModuleManager->unregisterModule(ref);
-      return true;
-    }
-    return false;
-  }
-
-  ctkCmdLineModuleManager* ModuleManager;
-};
-}
-
 
 //-----------------------------------------------------------------------------
 // ctkCmdLineModuleDirectoryWatcher methods
@@ -388,7 +323,8 @@ void ctkCmdLineModuleDirectoryWatcherPrivate::updateModuleReferences(const QStri
 //-----------------------------------------------------------------------------
 QList<ctkCmdLineModuleReference> ctkCmdLineModuleDirectoryWatcherPrivate::loadModules(const QStringList& executables)
 {
-  QList<ctkCmdLineModuleReference> refs = QtConcurrent::blockingMapped(executables, AddModule(this->ModuleManager, this->Debug));
+  QList<ctkCmdLineModuleReference> refs = QtConcurrent::blockingMapped(executables,
+                                                                       ctkCmdLineModuleConcurrentRegister(this->ModuleManager, this->Debug));
 
   for (int i = 0; i < executables.size(); ++i)
   {
@@ -404,7 +340,7 @@ QList<ctkCmdLineModuleReference> ctkCmdLineModuleDirectoryWatcherPrivate::loadMo
 //-----------------------------------------------------------------------------
 void ctkCmdLineModuleDirectoryWatcherPrivate::unloadModules(const QStringList& executables)
 {
-  QtConcurrent::blockingMapped(executables, RemoveModule(this->ModuleManager));
+  QtConcurrent::blockingMapped(executables, ctkCmdLineModuleConcurrentUnRegister(this->ModuleManager));
   foreach(QString executable, executables)
   {
     this->MapFileNameToReference.remove(executable);