ctkCmdLineModuleBackendFunctionPointer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H
  16. #define CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H
  17. #include "ctkCmdLineModuleBackend.h"
  18. #include "ctkCommandLineModulesBackendFunctionPointerExport.h"
  19. #include "ctkCmdLineModuleBackendFPTypeTraits.h"
  20. #include "ctkCmdLineModuleBackendFPUtil_p.h"
  21. #include <QScopedPointer>
  22. #include <QSharedPointer>
  23. #include <QString>
  24. #include <QUrl>
  25. #include <QMetaType>
  26. #include <QDebug>
  27. namespace ctk {
  28. namespace CmdLineModuleBackendFunctionPointer {
  29. struct FunctionPointerProxy;
  30. template<typename T>
  31. QString GetParameterTypeName();
  32. struct ImageType {};
  33. // default parameter description
  34. template<typename T, typename Enable = void>
  35. struct CreateXmlFor
  36. {
  37. static QString parameter(int index, const QString& typeName, const QString& label = QString(), const QString& description = QString())
  38. {
  39. QString xmlParameter;
  40. QTextStream str(&xmlParameter);
  41. str << " <" << typeName << ">\n";
  42. str << " <name>" << QString("param%1").arg(index) << "</name>\n";
  43. str << " <index>" << index << "</index>\n";
  44. str << " <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
  45. str << " <label>" << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "</label>\n";
  46. str << " </" << typeName << ">\n";
  47. return xmlParameter;
  48. }
  49. };
  50. // specialization for input image types
  51. template<typename T>
  52. struct CreateXmlFor<T, typename EnableIf<IsBaseOf<ImageType, T>::value >::Type >
  53. {
  54. static QString parameter(int index, const QString& typeName, const QString& label = QString(), const QString& description = QString())
  55. {
  56. QString xmlParameter;
  57. QTextStream str(&xmlParameter);
  58. str << " <" << typeName << ">\n";
  59. str << " <name>" << QString("param%1").arg(index) << "</name>\n";
  60. str << " <index>" << index << "</index>\n";
  61. str << " <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
  62. str << " <label>" << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "</label>\n";
  63. str << " <channel>input</channel>\n";
  64. str << " </" << typeName << ">\n";
  65. return xmlParameter;
  66. }
  67. };
  68. }
  69. }
  70. Q_DECLARE_METATYPE(QList<int>*)
  71. struct ctkCmdLineModuleBackendFunctionPointerPrivate;
  72. /**
  73. * \class ctkCmdLineModuleBackendFunctionPointer
  74. * \brief Provides a back-end implementation to enable directly calling a function pointer.
  75. * \ingroup CommandLineModulesBackendFunctionPointer_API
  76. *
  77. * \warning This back-end is highly experimental and will not work for most function pointers when
  78. * trying to register them via registerFunctionPointer().
  79. */
  80. class CTK_CMDLINEMODULEBACKENDFP_EXPORT ctkCmdLineModuleBackendFunctionPointer : public ctkCmdLineModuleBackend
  81. {
  82. public:
  83. class DescriptionPrivate;
  84. class Description
  85. {
  86. public:
  87. Description();
  88. ~Description();
  89. QUrl moduleLocation() const;
  90. QString moduleCategory() const;
  91. void setModuleCategory(const QString &category);
  92. QString moduleTitle() const;
  93. void setModuleTitle(const QString& title);
  94. QString moduleDescription() const;
  95. void setModuleDescription(const QString& description);
  96. QString moduleVersion() const;
  97. void setModuleVersion(const QString& version);
  98. QString moduleContributor() const;
  99. void setModuleContributor(const QString& contributor);
  100. private:
  101. friend class ctkCmdLineModuleBackendFunctionPointer;
  102. friend class ctkCmdLineModuleFunctionPointerTask;
  103. Description(const QUrl& location, const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& fpProxy);
  104. QSharedPointer<DescriptionPrivate> d;
  105. };
  106. ctkCmdLineModuleBackendFunctionPointer();
  107. ~ctkCmdLineModuleBackendFunctionPointer();
  108. virtual QString name() const;
  109. virtual QString description() const;
  110. virtual QList<QString> schemes() const;
  111. virtual qint64 timeStamp(const QUrl &location) const;
  112. virtual QByteArray rawXmlDescription(const QUrl& location);
  113. QList<QUrl> registeredFunctionPointers() const;
  114. template<typename A>
  115. Description* registerFunctionPointer(const QString& title, void (*fp)(A),
  116. const QString& paramLabel = QString(), const QString& paramDescr = QString())
  117. {
  118. typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<A>::RawType RawTypeA;
  119. QList<QString> params;
  120. params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeA>::
  121. parameter(0,
  122. ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeA>(),
  123. paramLabel, paramDescr);
  124. return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
  125. }
  126. template<typename A, typename B>
  127. Description* registerFunctionPointer(const QString& title, void (*fp)(A,B),
  128. const QString& paramLabel0 = QString(), const QString& paramDescr0 = QString(),
  129. const QString& paramLabel1 = QString(), const QString& paramDescr1 = QString())
  130. {
  131. typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<A>::RawType RawTypeA;
  132. typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<B>::RawType RawTypeB;
  133. QList<QString> params;
  134. params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeA>::
  135. parameter(0,
  136. ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeA>(),
  137. paramLabel0, paramDescr0);
  138. params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeB>::
  139. parameter(1,
  140. ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeB>(),
  141. paramLabel1, paramDescr1);
  142. return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
  143. }
  144. protected:
  145. virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend* frontend);
  146. virtual QList<QVariant> arguments(ctkCmdLineModuleFrontend* frontend) const;
  147. private:
  148. Description* registerFunctionPointerProxy(const QString &title,
  149. const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& proxy,
  150. const QList<QString>& params);
  151. QScopedPointer<ctkCmdLineModuleBackendFunctionPointerPrivate> d;
  152. };
  153. #endif // CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H