ctkCmdLineModuleBackendFunctionPointer.cpp 9.0 KB

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