ctkCmdLineModuleBackendFunctionPointer.cpp 9.1 KB

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