ctkCmdLineModuleFunctionPointerTask.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "ctkCmdLineModuleFunctionPointerTask_p.h"
  16. #include "ctkCmdLineModuleBackendFPDescriptionPrivate.h"
  17. #include "ctkCmdLineModuleFuture.h"
  18. #include "ctkCmdLineModuleRunException.h"
  19. //----------------------------------------------------------------------------
  20. ctkCmdLineModuleFunctionPointerTask::ctkCmdLineModuleFunctionPointerTask(const ctkCmdLineModuleBackendFunctionPointer::Description &fpDescr, const QList<QVariant> &paramValues)
  21. : FpDescription(fpDescr)
  22. , ParamValues(paramValues)
  23. {
  24. }
  25. //----------------------------------------------------------------------------
  26. ctkCmdLineModuleFuture ctkCmdLineModuleFunctionPointerTask::start()
  27. {
  28. this->setRunnable(this);
  29. this->setProgressRange(0,0);
  30. this->reportStarted();
  31. ctkCmdLineModuleFuture future = this->future();
  32. QThreadPool::globalInstance()->start(this, /*m_priority*/ 0);
  33. return future;
  34. }
  35. //----------------------------------------------------------------------------
  36. void ctkCmdLineModuleFunctionPointerTask::run()
  37. {
  38. if (this->isCanceled())
  39. {
  40. this->reportFinished();
  41. return;
  42. }
  43. // call the function pointer and catch any exceptions
  44. QString excMsg;
  45. try
  46. {
  47. FpDescription.d->FpProxy.call(ParamValues);
  48. }
  49. catch (const std::exception& e)
  50. {
  51. excMsg = e.what();
  52. }
  53. catch (...)
  54. {
  55. excMsg = "Unknown exception.";
  56. }
  57. if (!excMsg.isNull())
  58. {
  59. this->reportException(ctkCmdLineModuleRunException(FpDescription.moduleLocation(), 0, excMsg));
  60. }
  61. this->setProgressRange(0,1);
  62. this->setProgressValue(1);
  63. //this->reportResult(result);
  64. this->reportFinished();
  65. }