ctkCmdLineModuleConcurrentHelpers.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleConcurrentHelpers.h"
  16. #include "ctkCmdLineModuleManager.h"
  17. #include "ctkCmdLineModuleRunException.h"
  18. #include <qtconcurrentexception.h>
  19. #include <QUrl>
  20. #include <QDebug>
  21. //----------------------------------------------------------------------------
  22. ctkCmdLineModuleConcurrentRegister::ctkCmdLineModuleConcurrentRegister(ctkCmdLineModuleManager* manager,
  23. bool debug)
  24. : ModuleManager(manager), Debug(debug)
  25. {}
  26. //----------------------------------------------------------------------------
  27. ctkCmdLineModuleReferenceResult ctkCmdLineModuleConcurrentRegister::operator()(const QString& moduleLocation)
  28. {
  29. return this->operator ()(QUrl::fromLocalFile(moduleLocation));
  30. }
  31. //----------------------------------------------------------------------------
  32. ctkCmdLineModuleReferenceResult ctkCmdLineModuleConcurrentRegister::operator()(const QUrl& moduleUrl)
  33. {
  34. try
  35. {
  36. ctkCmdLineModuleReference reference = this->ModuleManager->registerModule(moduleUrl);
  37. return ctkCmdLineModuleReferenceResult(reference);
  38. }
  39. catch (const ctkException& e)
  40. {
  41. if (this->Debug)
  42. {
  43. qDebug() << e.message();
  44. }
  45. return ctkCmdLineModuleReferenceResult(moduleUrl, e.message());
  46. }
  47. catch (const QtConcurrent::Exception& e)
  48. {
  49. if (this->Debug)
  50. {
  51. qDebug() << e.what();
  52. }
  53. return ctkCmdLineModuleReferenceResult(moduleUrl, e.what());
  54. }
  55. catch (...)
  56. {
  57. QString errorMessage = QObject::tr("Module %1 failed with an unknown exception.").arg(moduleUrl.toString());
  58. if (this->Debug)
  59. {
  60. qDebug() << errorMessage;
  61. }
  62. return ctkCmdLineModuleReferenceResult(moduleUrl, errorMessage);
  63. }
  64. return ctkCmdLineModuleReferenceResult(moduleUrl);
  65. }
  66. //----------------------------------------------------------------------------
  67. ctkCmdLineModuleConcurrentUnRegister::ctkCmdLineModuleConcurrentUnRegister(ctkCmdLineModuleManager* manager)
  68. : ModuleManager(manager)
  69. {}
  70. //----------------------------------------------------------------------------
  71. bool ctkCmdLineModuleConcurrentUnRegister::operator()(const QString& moduleLocation)
  72. {
  73. return this->operator ()(QUrl::fromLocalFile(moduleLocation));
  74. }
  75. //----------------------------------------------------------------------------
  76. bool ctkCmdLineModuleConcurrentUnRegister::operator()(const QUrl& moduleUrl)
  77. {
  78. return this->operator ()(this->ModuleManager->moduleReference(moduleUrl));
  79. }
  80. //----------------------------------------------------------------------------
  81. bool ctkCmdLineModuleConcurrentUnRegister::operator()(const ctkCmdLineModuleReference& moduleRef)
  82. {
  83. if (moduleRef)
  84. {
  85. this->ModuleManager->unregisterModule(moduleRef);
  86. return true;
  87. }
  88. return false;
  89. }