ctkCmdLineModuleBackendFunctionPointer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <QScopedPointer>
  21. #include <QSharedPointer>
  22. #include <QString>
  23. #include <QUrl>
  24. #include <QMetaType>
  25. #include <QDebug>
  26. namespace ctk {
  27. namespace CmdLineModuleBackendFunctionPointer {
  28. struct FunctionPointerProxy;
  29. template<typename T>
  30. QString GetParameterTypeName();
  31. }
  32. }
  33. Q_DECLARE_METATYPE(QList<int>*)
  34. struct ctkCmdLineModuleBackendFunctionPointerPrivate;
  35. class CTK_CMDLINEMODULEBACKENDFP_EXPORT ctkCmdLineModuleBackendFunctionPointer : public ctkCmdLineModuleBackend
  36. {
  37. public:
  38. class DescriptionPrivate;
  39. class Description
  40. {
  41. public:
  42. Description();
  43. ~Description();
  44. QUrl moduleLocation() const;
  45. QString moduleCategory() const;
  46. void setModuleCategory(const QString &category);
  47. QString moduleTitle() const;
  48. void setModuleTitle(const QString& title);
  49. QString moduleDescription() const;
  50. void setModuleDescription(const QString& description);
  51. QString moduleVersion() const;
  52. void setModuleVersion(const QString& version);
  53. QString moduleContributor() const;
  54. void setModuleContributor(const QString& contributor);
  55. private:
  56. friend class ctkCmdLineModuleBackendFunctionPointer;
  57. friend class ctkCmdLineModuleFunctionPointerTask;
  58. Description(const QUrl& location, const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& fpProxy);
  59. QSharedPointer<DescriptionPrivate> d;
  60. };
  61. ctkCmdLineModuleBackendFunctionPointer();
  62. virtual QString name() const;
  63. virtual QString description() const;
  64. virtual QList<QString> schemes() const;
  65. virtual qint64 timeStamp(const QUrl &location) const;
  66. virtual QByteArray rawXmlDescription(const QUrl& location);
  67. virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend *frontend);
  68. QList<QUrl> registeredFunctionPointers() const;
  69. template<typename A>
  70. Description* registerFunctionPointer(const QString& title, void (*fp)(A),
  71. const QString& paramLabel = QString(), const QString& paramDescr = QString())
  72. {
  73. QList<QString> params;
  74. params << CreateXmlForParameter<A>(0, paramLabel, paramDescr);
  75. return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
  76. }
  77. template<typename A, typename B>
  78. Description* registerFunctionPointer(const QString& title, void (*fp)(A,B),
  79. const QString& paramLabel0 = QString(), const QString& paramDescr0 = QString(),
  80. const QString& paramLabel1 = QString(), const QString& paramDescr1 = QString())
  81. {
  82. QList<QString> params;
  83. params << CreateXmlForParameter<A>(0, paramLabel0, paramDescr0);
  84. params << CreateXmlForParameter<B>(1, paramLabel1, paramDescr1);
  85. return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
  86. }
  87. private:
  88. Description* registerFunctionPointerProxy(const QString &title,
  89. const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& proxy,
  90. const QList<QString>& params);
  91. template<typename T>
  92. QString CreateXmlForParameter(int index, const QString& label = QString(), const QString& description = QString())
  93. {
  94. QString xmlParameter;
  95. QTextStream str(&xmlParameter);
  96. QString typeName = ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<T>::RawType>();
  97. str << " <" << typeName << ">\n";
  98. str << " <name>" << QString("param%1").arg(index) << "</name>\n";
  99. str << " <index>" << index << "</index>\n";
  100. str << " <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
  101. str << " <label>" << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "</label>\n";
  102. str << " </" << typeName << ">\n";
  103. return xmlParameter;
  104. }
  105. QScopedPointer<ctkCmdLineModuleBackendFunctionPointerPrivate> d;
  106. };
  107. #endif // CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H