ctkCmdLineModuleBackendFunctionPointer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "ctkCmdLineModuleBackendFunctionPointer.h"
  16. #include "ctkCmdLineModuleBackendFPUtil_p.h"
  17. #include "ctkCmdLineModuleBackendFPDescriptionPrivate.h"
  18. #include "ctkCmdLineModuleFunctionPointerTask_p.h"
  19. #include "ctkCmdLineModuleFuture.h"
  20. #include "ctkCmdLineModuleFrontend.h"
  21. #include <QByteArray>
  22. #include <QString>
  23. #include <QList>
  24. #include <QHash>
  25. #include <QUrl>
  26. namespace ctk {
  27. namespace CmdLineModuleBackendFunctionPointer {
  28. template<>
  29. CTK_CMDLINEMODULEBACKENDFP_EXPORT QString GetParameterTypeName<int>()
  30. {
  31. return "integer";
  32. }
  33. template<>
  34. CTK_CMDLINEMODULEBACKENDFP_EXPORT QString GetParameterTypeName<QList<int> >()
  35. {
  36. return "integer-vector";
  37. }
  38. }
  39. }
  40. void CalculateFibonacciNumbers(int level) //, QList<int>* result)
  41. {
  42. qDebug() << "Number: 0";
  43. if (level > 0)
  44. {
  45. sleep(1);
  46. qDebug() << "Number: 1";
  47. if (level == 1) return;
  48. }
  49. int first = 0;
  50. int second = 1;
  51. for (int i = 1; i < level; ++i)
  52. {
  53. int tmp = first;
  54. first = second;
  55. second = first + tmp;
  56. sleep(1);
  57. qDebug() << "Number:" << second;
  58. }
  59. }
  60. ctkCmdLineModuleBackendFunctionPointer::Description::Description()
  61. : d(new ctkCmdLineModuleBackendFunctionPointer::DescriptionPrivate)
  62. {
  63. }
  64. ctkCmdLineModuleBackendFunctionPointer::Description::~Description()
  65. {
  66. }
  67. QUrl ctkCmdLineModuleBackendFunctionPointer::Description::moduleLocation() const
  68. {
  69. return d->ModuleLocation;
  70. }
  71. QString ctkCmdLineModuleBackendFunctionPointer::Description::moduleCategory() const
  72. {
  73. return d->ModuleCategory;
  74. }
  75. void ctkCmdLineModuleBackendFunctionPointer::Description::setModuleCategory(const QString& category)
  76. {
  77. d->ModuleCategory = category;
  78. }
  79. QString ctkCmdLineModuleBackendFunctionPointer::Description::moduleTitle() const
  80. {
  81. return d->ModuleTitle;
  82. }
  83. void ctkCmdLineModuleBackendFunctionPointer::Description::setModuleTitle(const QString &title)
  84. {
  85. d->ModuleTitle = title;
  86. }
  87. QString ctkCmdLineModuleBackendFunctionPointer::Description::moduleDescription() const
  88. {
  89. return d->ModuleDescription;
  90. }
  91. void ctkCmdLineModuleBackendFunctionPointer::Description::setModuleDescription(const QString &description)
  92. {
  93. d->ModuleDescription = description;
  94. }
  95. QString ctkCmdLineModuleBackendFunctionPointer::Description::moduleVersion() const
  96. {
  97. return d->ModuleVersion;
  98. }
  99. void ctkCmdLineModuleBackendFunctionPointer::Description::setModuleVersion(const QString &version)
  100. {
  101. d->ModuleVersion = version;
  102. }
  103. QString ctkCmdLineModuleBackendFunctionPointer::Description::moduleContributor() const
  104. {
  105. return d->ModuleContributor;
  106. }
  107. void ctkCmdLineModuleBackendFunctionPointer::Description::setModuleContributor(const QString &contributor)
  108. {
  109. d->ModuleContributor = contributor;
  110. }
  111. ctkCmdLineModuleBackendFunctionPointer::Description::Description(const QUrl &location,
  112. const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy &fpProxy)
  113. : d(new ctkCmdLineModuleBackendFunctionPointer::DescriptionPrivate)
  114. {
  115. d->ModuleLocation = location;
  116. d->FpProxy = fpProxy;
  117. }
  118. struct ctkCmdLineModuleBackendFunctionPointerPrivate
  119. {
  120. QHash<QUrl, ctkCmdLineModuleBackendFunctionPointer::Description> UrlToFpDescription;
  121. };
  122. ctkCmdLineModuleBackendFunctionPointer::ctkCmdLineModuleBackendFunctionPointer()
  123. : d(new ctkCmdLineModuleBackendFunctionPointerPrivate)
  124. {
  125. this->registerFunctionPointer("Fibonacci Number", CalculateFibonacciNumbers, "Count");
  126. }
  127. QString ctkCmdLineModuleBackendFunctionPointer::name() const
  128. {
  129. return "Function Pointer (experimental)";
  130. }
  131. QString ctkCmdLineModuleBackendFunctionPointer::description() const
  132. {
  133. return "Calls a previously registered function pointer.";
  134. }
  135. QList<QString> ctkCmdLineModuleBackendFunctionPointer::schemes() const
  136. {
  137. static QList<QString> supportedSchemes = QList<QString>() << "fp";
  138. return supportedSchemes;
  139. }
  140. qint64 ctkCmdLineModuleBackendFunctionPointer::timeStamp(const QUrl &location) const
  141. {
  142. Q_UNUSED(location)
  143. return 0;
  144. }
  145. QByteArray ctkCmdLineModuleBackendFunctionPointer::rawXmlDescription(const QUrl& location)
  146. {
  147. if (!d->UrlToFpDescription.contains(location)) return QByteArray();
  148. return QByteArray(qPrintable(d->UrlToFpDescription[location].d->xmlDescription()));
  149. }
  150. ctkCmdLineModuleFuture ctkCmdLineModuleBackendFunctionPointer::run(ctkCmdLineModuleFrontend *frontend)
  151. {
  152. QUrl url = frontend->location();
  153. const Description& descr = d->UrlToFpDescription[url];
  154. QList<QVariant> args = frontend->values().values();
  155. // Instances of ctkCmdLineModuleFunctionPointerTask are auto-deleted by the
  156. // thread pool
  157. ctkCmdLineModuleFunctionPointerTask* fpTask = new ctkCmdLineModuleFunctionPointerTask(descr, args);
  158. return fpTask->start();
  159. }
  160. QList<QUrl> ctkCmdLineModuleBackendFunctionPointer::registeredFunctionPointers() const
  161. {
  162. return d->UrlToFpDescription.keys();
  163. }
  164. ctkCmdLineModuleBackendFunctionPointer::Description*
  165. ctkCmdLineModuleBackendFunctionPointer::registerFunctionPointerProxy(const QString& title,
  166. const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& proxy,
  167. const QList<QString>& params)
  168. {
  169. QUrl url(QString("fp://0x%1").arg(reinterpret_cast<quintptr>(proxy.FpHolder)));
  170. d->UrlToFpDescription[url] = Description(url, proxy);
  171. Description& fpDescr = d->UrlToFpDescription[url];
  172. fpDescr.setModuleTitle(title);
  173. fpDescr.d->paramDescriptions = params;
  174. return &fpDescr;
  175. }