Explorar o código

Enhanced to function pointer backend to handle custom parameter types.

Sascha Zelzer %!s(int64=12) %!d(string=hai) anos
pai
achega
9bd490aab4

+ 31 - 0
Libs/CommandLineModules/Backend/FunctionPointer/ctkCmdLineModuleBackendFPTypeTraits.h

@@ -86,6 +86,37 @@ public:
                                      typename Select<isReference, typename UnConst<ReferenceType>::Result, typename UnConst<T>::Result>::Result >::Result RawType;
                                      typename Select<isReference, typename UnConst<ReferenceType>::Result, typename UnConst<T>::Result>::Result >::Result RawType;
 };
 };
 
 
+template<bool C, typename T = void>
+struct EnableIf
+{
+  typedef T Type;
+};
+
+template<typename T>
+struct EnableIf<false, T> {};
+
+template<typename, typename>
+struct IsSame
+{
+  static bool const value = false;
+};
+
+template<typename A>
+struct IsSame<A, A>
+{
+  static bool const value = true;
+};
+
+template<typename B, typename D>
+struct IsBaseOf
+{
+  static D* MakeD();
+  static char (& Test(B*))[1];
+  static char (& Test(...))[2];
+  static bool const value = sizeof Test(MakeD()) == 1 &&
+      !IsSame<B volatile const, void volatile const>::value;
+};
+
 
 
 }
 }
 }
 }

+ 4 - 2
Libs/CommandLineModules/Backend/FunctionPointer/ctkCmdLineModuleBackendFPUtil_p.h

@@ -22,6 +22,8 @@
 #ifndef CTKCMDLINEMODULEBACKENDFPUTIL_P_H
 #ifndef CTKCMDLINEMODULEBACKENDFPUTIL_P_H
 #define CTKCMDLINEMODULEBACKENDFPUTIL_P_H
 #define CTKCMDLINEMODULEBACKENDFPUTIL_P_H
 
 
+#include "ctkCommandLineModulesBackendFunctionPointerExport.h"
+
 #include <QVariant>
 #include <QVariant>
 
 
 class ctkCmdLineModuleBackendFunctionPointer;
 class ctkCmdLineModuleBackendFunctionPointer;
@@ -29,7 +31,7 @@ class ctkCmdLineModuleBackendFunctionPointer;
 namespace ctk {
 namespace ctk {
 namespace CmdLineModuleBackendFunctionPointer {
 namespace CmdLineModuleBackendFunctionPointer {
 
 
-struct FunctionPointerHolderBase
+struct CTK_CMDLINEMODULEBACKENDFP_EXPORT FunctionPointerHolderBase
 {
 {
   virtual ~FunctionPointerHolderBase();
   virtual ~FunctionPointerHolderBase();
 
 
@@ -84,7 +86,7 @@ struct FunctionPointerHolder2 : public FunctionPointerHolderBase
   FunctionPointerType Fp;
   FunctionPointerType Fp;
 };
 };
 
 
-struct FunctionPointerProxy
+struct CTK_CMDLINEMODULEBACKENDFP_EXPORT FunctionPointerProxy
 {
 {
   FunctionPointerProxy();
   FunctionPointerProxy();
   ~FunctionPointerProxy();
   ~FunctionPointerProxy();

+ 13 - 1
Libs/CommandLineModules/Backend/FunctionPointer/ctkCmdLineModuleBackendFunctionPointer.cpp

@@ -177,6 +177,11 @@ ctkCmdLineModuleBackendFunctionPointer::ctkCmdLineModuleBackendFunctionPointer()
 }
 }
 
 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
+ctkCmdLineModuleBackendFunctionPointer::~ctkCmdLineModuleBackendFunctionPointer()
+{
+}
+
+//----------------------------------------------------------------------------
 QString ctkCmdLineModuleBackendFunctionPointer::name() const
 QString ctkCmdLineModuleBackendFunctionPointer::name() const
 {
 {
   return "Function Pointer (experimental)";
   return "Function Pointer (experimental)";
@@ -206,6 +211,7 @@ qint64 ctkCmdLineModuleBackendFunctionPointer::timeStamp(const QUrl &location) c
 QByteArray ctkCmdLineModuleBackendFunctionPointer::rawXmlDescription(const QUrl& location)
 QByteArray ctkCmdLineModuleBackendFunctionPointer::rawXmlDescription(const QUrl& location)
 {
 {
   if (!d->UrlToFpDescription.contains(location)) return QByteArray();
   if (!d->UrlToFpDescription.contains(location)) return QByteArray();
+  qDebug() << d->UrlToFpDescription[location].d->xmlDescription();
   return QByteArray(qPrintable(d->UrlToFpDescription[location].d->xmlDescription()));
   return QByteArray(qPrintable(d->UrlToFpDescription[location].d->xmlDescription()));
 }
 }
 
 
@@ -215,7 +221,7 @@ ctkCmdLineModuleFuture ctkCmdLineModuleBackendFunctionPointer::run(ctkCmdLineMod
   QUrl url = frontend->location();
   QUrl url = frontend->location();
 
 
   const Description& descr = d->UrlToFpDescription[url];
   const Description& descr = d->UrlToFpDescription[url];
-  QList<QVariant> args = frontend->values().values();
+  QList<QVariant> args = this->arguments(frontend);
 
 
   // Instances of ctkCmdLineModuleFunctionPointerTask are auto-deleted by the
   // Instances of ctkCmdLineModuleFunctionPointerTask are auto-deleted by the
   // thread pool
   // thread pool
@@ -224,6 +230,12 @@ ctkCmdLineModuleFuture ctkCmdLineModuleBackendFunctionPointer::run(ctkCmdLineMod
 }
 }
 
 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
+QList<QVariant> ctkCmdLineModuleBackendFunctionPointer::arguments(ctkCmdLineModuleFrontend *frontend) const
+{
+  return frontend->values().values();
+}
+
+//----------------------------------------------------------------------------
 QList<QUrl> ctkCmdLineModuleBackendFunctionPointer::registeredFunctionPointers() const
 QList<QUrl> ctkCmdLineModuleBackendFunctionPointer::registeredFunctionPointers() const
 {
 {
   return d->UrlToFpDescription.keys();
   return d->UrlToFpDescription.keys();

+ 65 - 19
Libs/CommandLineModules/Backend/FunctionPointer/ctkCmdLineModuleBackendFunctionPointer.h

@@ -26,6 +26,7 @@
 
 
 #include "ctkCommandLineModulesBackendFunctionPointerExport.h"
 #include "ctkCommandLineModulesBackendFunctionPointerExport.h"
 #include "ctkCmdLineModuleBackendFPTypeTraits.h"
 #include "ctkCmdLineModuleBackendFPTypeTraits.h"
+#include "ctkCmdLineModuleBackendFPUtil_p.h"
 
 
 #include <QScopedPointer>
 #include <QScopedPointer>
 #include <QSharedPointer>
 #include <QSharedPointer>
@@ -44,6 +45,45 @@ struct FunctionPointerProxy;
 template<typename T>
 template<typename T>
 QString GetParameterTypeName();
 QString GetParameterTypeName();
 
 
+struct ImageType {};
+
+// default parameter description
+template<typename T, typename Enable = void>
+struct CreateXmlFor
+{
+  static QString parameter(int index, const QString& typeName, const QString& label = QString(), const QString& description = QString())
+  {
+    QString xmlParameter;
+    QTextStream str(&xmlParameter);
+    str << "    <" << typeName << ">\n";
+    str << "      <name>" << QString("param%1").arg(index) << "</name>\n";
+    str << "      <index>" << index << "</index>\n";
+    str << "      <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
+    str << "      <label>" << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "</label>\n";
+    str << "    </" << typeName << ">\n";
+    return xmlParameter;
+  }
+};
+
+// specialization for input image types
+template<typename T>
+struct CreateXmlFor<T, typename EnableIf<IsBaseOf<ImageType, T>::value >::Type >
+{
+  static QString parameter(int index, const QString& typeName, const QString& label = QString(), const QString& description = QString())
+  {
+    QString xmlParameter;
+    QTextStream str(&xmlParameter);
+    str << "    <" << typeName << ">\n";
+    str << "      <name>" << QString("param%1").arg(index) << "</name>\n";
+    str << "      <index>" << index << "</index>\n";
+    str << "      <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
+    str << "      <label>" << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "</label>\n";
+    str << "      <channel>input</channel>\n";
+    str << "    </" << typeName << ">\n";
+    return xmlParameter;
+  }
+};
+
 }
 }
 }
 }
 
 
@@ -93,6 +133,7 @@ public:
   };
   };
 
 
   ctkCmdLineModuleBackendFunctionPointer();
   ctkCmdLineModuleBackendFunctionPointer();
+  ~ctkCmdLineModuleBackendFunctionPointer();
 
 
   virtual QString name() const;
   virtual QString name() const;
   virtual QString description() const;
   virtual QString description() const;
@@ -103,16 +144,19 @@ public:
 
 
   virtual QByteArray rawXmlDescription(const QUrl& location);
   virtual QByteArray rawXmlDescription(const QUrl& location);
 
 
-  virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend *frontend);
-
   QList<QUrl> registeredFunctionPointers() const;
   QList<QUrl> registeredFunctionPointers() const;
 
 
   template<typename A>
   template<typename A>
   Description* registerFunctionPointer(const QString& title, void (*fp)(A),
   Description* registerFunctionPointer(const QString& title, void (*fp)(A),
                                        const QString& paramLabel = QString(), const QString& paramDescr = QString())
                                        const QString& paramLabel = QString(), const QString& paramDescr = QString())
   {
   {
+    typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<A>::RawType RawTypeA;
+
     QList<QString> params;
     QList<QString> params;
-    params << CreateXmlForParameter<A>(0, paramLabel, paramDescr);
+    params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeA>::
+              parameter(0,
+                        ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeA>(),
+                        paramLabel, paramDescr);
     return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
     return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
   }
   }
 
 
@@ -121,35 +165,37 @@ public:
                                        const QString& paramLabel0 = QString(), const QString& paramDescr0 = QString(),
                                        const QString& paramLabel0 = QString(), const QString& paramDescr0 = QString(),
                                        const QString& paramLabel1 = QString(), const QString& paramDescr1 = QString())
                                        const QString& paramLabel1 = QString(), const QString& paramDescr1 = QString())
   {
   {
+    typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<A>::RawType RawTypeA;
+    typedef typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<B>::RawType RawTypeB;
+
     QList<QString> params;
     QList<QString> params;
-    params << CreateXmlForParameter<A>(0, paramLabel0, paramDescr0);
-    params << CreateXmlForParameter<B>(1, paramLabel1, paramDescr1);
+    params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeA>::
+              parameter(0,
+                        ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeA>(),
+                        paramLabel0, paramDescr0);
+    params << ctk::CmdLineModuleBackendFunctionPointer::CreateXmlFor<RawTypeB>::
+              parameter(1,
+                        ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<RawTypeB>(),
+                        paramLabel1, paramDescr1);
     return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
     return this->registerFunctionPointerProxy(title, ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy(fp), params);
   }
   }
 
 
+protected:
+
+  virtual ctkCmdLineModuleFuture run(ctkCmdLineModuleFrontend* frontend);
+
+  virtual QList<QVariant> arguments(ctkCmdLineModuleFrontend* frontend) const;
+
 private:
 private:
 
 
   Description* registerFunctionPointerProxy(const QString &title,
   Description* registerFunctionPointerProxy(const QString &title,
                                             const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& proxy,
                                             const ctk::CmdLineModuleBackendFunctionPointer::FunctionPointerProxy& proxy,
                                             const QList<QString>& params);
                                             const QList<QString>& params);
 
 
-  template<typename T>
-  QString CreateXmlForParameter(int index, const QString& label = QString(), const QString& description = QString())
-  {
-    QString xmlParameter;
-    QTextStream str(&xmlParameter);
-    QString typeName = ctk::CmdLineModuleBackendFunctionPointer::GetParameterTypeName<typename ctk::CmdLineModuleBackendFunctionPointer::TypeTraits<T>::RawType>();
-    str << "    <" << typeName << ">\n";
-    str << "      <name>" << QString("param%1").arg(index) << "</name>\n";
-    str << "      <index>" << index << "</index>\n";
-    str << "      <description>" << (description.isEmpty() ? "Description not available." : description) << "</description>\n";
-    str << "      <label>" << (label.isEmpty() ? QString("Parameter %1").arg(index) : label) << "</label>\n";
-    str << "    </" << typeName << ">\n";
-    return xmlParameter;
-  }
 
 
   QScopedPointer<ctkCmdLineModuleBackendFunctionPointerPrivate> d;
   QScopedPointer<ctkCmdLineModuleBackendFunctionPointerPrivate> d;
 
 
 };
 };
 
 
+
 #endif // CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H
 #endif // CTKCMDLINEMODULEBACKENDFUNCTIONPOINTER_H