ctkCmdLineModuleBackendFunctionPointer.cpp 8.7 KB

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