Parcourir la source

Cleanup ModuleDescription

Instance ctkModuleParameter and ctkModuleParameterGroup on the heap instead
of on the stack.
Julien Finet il y a 14 ans
Parent
commit
bba19d433b

+ 2 - 3
Libs/ModuleDescription/Testing/Cpp/ctkModuleDescriptionTest.cpp

@@ -80,9 +80,8 @@ int ctkModuleDescriptionTest(int argc, char * argv [] )
   module[ "AlternativeTarget" ] = "MyAlternativeTarget";
   module[ "Location" ] = "MyLocation";
 
-
-  group.addParameter( param );
-  module.addParameterGroup( group );
+  group.addParameter( new ctkModuleParameter(param) );
+  module.addParameterGroup( new ctkModuleParameterGroup(group) );
 
   QTextStream(stdout)<< module;
 

+ 43 - 99
Libs/ModuleDescription/ctkModuleDescription.cpp

@@ -24,55 +24,28 @@ limitations under the License.
 #include "QTextStream"
 
 //----------------------------------------------------------------------------
-ctkModuleDescription::ctkModuleDescription()
+void ctkModuleDescription::addParameterGroup( ctkModuleParameterGroup* group )
 {
+  Q_ASSERT(group);
+	this->ParameterGroups.push_back(group);
 }
-
-//----------------------------------------------------------------------------
-ctkModuleDescription::ctkModuleDescription(const ctkModuleDescription &md)
-  : QHash<QString, QString>( QHash<QString, QString>( md ) )
-{
-  this->ParameterGroups = md.ParameterGroups;
-  this->Icon = md.Icon;
-}
-
-//----------------------------------------------------------------------------
-void ctkModuleDescription::operator=(const ctkModuleDescription &md)
-{
- QHash<QString, QString>::operator=(md);
-	this->ParameterGroups = md.ParameterGroups;
-	this->Icon = md.Icon;
-}
-
+/*
 //----------------------------------------------------------------------------
-QTextStream & operator<<(QTextStream &os, const ctkModuleDescription &module)
+const QVector<ctkModuleParameterGroup*>& ctkModuleDescription::parameterGroups() const
 {
-  os << QHash<QString, QString>(module);
-  //os << "Icon: " << module.icon() << endl;
-
-  os << "ParameterGroups: " << endl;
-  foreach( const ctkModuleParameterGroup& it, module.parameterGroups())
-    {
-    os << it;
-    }
-  return os;
-}
-
+	return this->ParameterGroups;
+}*/
 
 //----------------------------------------------------------------------------
 bool ctkModuleDescription::hasReturnParameters() const
 {
   // iterate over each parameter group
-  foreach( const ctkModuleParameterGroup& group, this->ParameterGroups)
+  foreach( const ctkModuleParameterGroup* group, this->ParameterGroups)
     {
-    // iterate over each parameter in this group
-    foreach( const ctkModuleParameter& param, group.parameters())
+    if (group->hasReturnParameters())
       {
-      if (param.isReturnParameter())
-        {
-        return true;
-        }
-      }    
+      return true;
+      }
     }
 
   return false;
@@ -91,32 +64,31 @@ bool ctkModuleDescription::setParameterDefaultValue(const QString& name, const Q
   return false;
 }
 
+//----------------------------------------------------------------------------
+ctkModuleParameterGroup* ctkModuleDescription::parameterGroup(const QString& parameterName)const
+{
+  // iterate over each parameter group
+  foreach( ctkModuleParameterGroup* group, this->ParameterGroups)
+    {
+    ctkModuleParameter* param = group->parameter(parameterName);
+    if (param)
+      {
+      return group;
+      }    
+    }
+  return 0;
+}
 
 //----------------------------------------------------------------------------
-ctkModuleParameter* ctkModuleDescription::parameter(const QString& name)
+ctkModuleParameter* ctkModuleDescription::parameter(const QString& name)const
 {
   // iterate over each parameter group
-  QVector<ctkModuleParameterGroup>::iterator pgbeginit
-    = this->ParameterGroups.begin();
-  QVector<ctkModuleParameterGroup>::iterator pgendit
-    = this->ParameterGroups.end();
-  QVector<ctkModuleParameterGroup>::iterator pgit;
-  
-  for (pgit = pgbeginit; pgit != pgendit; ++pgit)
+  foreach( const ctkModuleParameterGroup* group, this->ParameterGroups)
     {
-    // iterate over each parameter in this group
-    QVector<ctkModuleParameter>::iterator pbeginit
-      = (*pgit).parameters().begin();
-    QVector<ctkModuleParameter>::iterator pendit
-      = (*pgit).parameters().end();
-    QVector<ctkModuleParameter>::iterator pit;
-
-    for (pit = pbeginit; pit != pendit; ++pit)
+    ctkModuleParameter* param = group->parameter(name);
+    if (param)
       {
-      if ((*pit)["Name"] == name)
-        {
-        return &(*pit);
-        }
+      return param;
       }    
     }
   return 0;
@@ -184,7 +156,7 @@ bool ctkModuleDescription ::readParameterFile(const QString& filename)
 
 //----------------------------------------------------------------------------
 bool ctkModuleDescription::
-writeParameterFile(const QString& filename, bool withHandlesToBulkParameters)
+writeParameterFile(const QString& filename, bool withHandlesToBulkParameters)const
 {
   QFile rtp(filename);
 
@@ -196,52 +168,24 @@ writeParameterFile(const QString& filename, bool withHandlesToBulkParameters)
 
   QTextStream in(&rtp);
   // iterate over each parameter group
-  foreach(const ctkModuleParameterGroup& group, this->ParameterGroups)
+  foreach(const ctkModuleParameterGroup* group, this->ParameterGroups)
     {
-    // iterate over each parameter in this group
-    foreach(const ctkModuleParameter& param, group.parameters())
-      {
-      // write out all parameters or just the ones that are not bulk parameters
-      if (withHandlesToBulkParameters
-          || (!withHandlesToBulkParameters 
-              && (param[ "Tag" ] != "image"
-              && param[ "Tag" ] != "geometry"
-              && param[ "Tag" ] != "transform"
-              && param[ "Tag" ] != "table"
-              && param[ "Tag" ] != "measurement"
-              && param[ "Tag" ] != "point"  // point and region are special
-              && param[ "Tag" ] != "region")))
-        {
-        in << param[ "Name" ] << " = " << param[ "Default" ] << endl;
-
-        // multiple="true" may have to be handled differently
-        }
-      }
+    group->writeParameterFile(in, withHandlesToBulkParameters);
     }
 
   return true;
 }
 
 //----------------------------------------------------------------------------
-void ctkModuleDescription::addParameterGroup( const ctkModuleParameterGroup &group )
-{
-	this->ParameterGroups.push_back(group);
-}
-
-//----------------------------------------------------------------------------
-const QVector<ctkModuleParameterGroup>& ctkModuleDescription::parameterGroups() const
-{
-	return this->ParameterGroups;
-}
-
-//----------------------------------------------------------------------------
-QVector<ctkModuleParameterGroup>& ctkModuleDescription::parameterGroups()
+QTextStream & operator<<(QTextStream &os, const ctkModuleDescription &module)
 {
-	return this->ParameterGroups;
-}
+  os << QHash<QString, QString>(module);
+  os << "Icon: " << !module.icon().isNull() << endl;
 
-//----------------------------------------------------------------------------
-void ctkModuleDescription::setParameterGroups( const QVector<ctkModuleParameterGroup>& groups )
-{
-	this->ParameterGroups = groups;
-}
+  os << "ParameterGroups: " << endl;
+  foreach(const ctkModuleParameterGroup* group, module.ParameterGroups)
+    {
+    os << *group;
+    }
+  return os;
+}

+ 13 - 17
Libs/ModuleDescription/ctkModuleDescription.h

@@ -53,30 +53,26 @@
 class CTK_MODULDESC_EXPORT ctkModuleDescription : public QHash<QString, QString>
 {
 public:
-  ctkModuleDescription();
-  ctkModuleDescription(const ctkModuleDescription &md);
-
-  void operator=(const ctkModuleDescription &md);
-
+  // Optional icon associated to the module
   void setIcon(const QIcon& logo);
   const QIcon& icon() const;
   
-  void addParameterGroup(const ctkModuleParameterGroup &group);
+  void addParameterGroup(ctkModuleParameterGroup* group);
 
-  const QVector<ctkModuleParameterGroup>& parameterGroups() const;
-
-  QVector<ctkModuleParameterGroup>& parameterGroups();
+  //const QVector<ctkModuleParameterGroup*>& parameterGroups() const;
+  
+  // Return the group that contain the parameter associated to the name
+  ctkModuleParameterGroup* parameterGroup(const QString& parameterName) const;
+  // Return the first parameter corresponding to the name from any group
+  ctkModuleParameter* parameter(const QString& parameterName) const;
   
-  void setParameterGroups(const QVector<ctkModuleParameterGroup>& groups);
-
   // Does the module have any simple (primitive) return types?
   bool hasReturnParameters() const;
 
-  bool setParameterDefaultValue(const QString& name,
+  /// TODO: move to ctkModuleParameter
+  bool setParameterDefaultValue(const QString& parameterName,
                                 const QString& value);
 
-  ctkModuleParameter* parameter(const QString& name);
-
   ///
   /// Read a parameter file. Syntax of file is "name: value" for each
   /// parameter. Returns a bool indicating whether any parameter value
@@ -88,12 +84,12 @@ public:
   /// the parameters.  The "withHandlesToBulkParameters" parameter
   /// controls whether the handles to the bulk parameters (image,
   /// geometry, etc.) are written to the file.
-  bool writeParameterFile(const QString& filename, bool withHandlesToBulkParameters = true);
+  bool writeParameterFile(const QString& filename, bool withHandlesToBulkParameters = true)const;
 
 private:
-
+  friend CTK_MODULDESC_EXPORT QTextStream & operator<<(QTextStream &os, const ctkModuleDescription &module);
   /// Groups of parameters
-  QVector<ctkModuleParameterGroup> ParameterGroups;
+  QVector<ctkModuleParameterGroup*> ParameterGroups;
   /// Icon of the module
   QIcon Icon;
 };

+ 7 - 14
Libs/ModuleDescription/ctkModuleDescriptionReader.cpp

@@ -18,29 +18,22 @@ limitations under the License.
 
 =============================================================================*/
 
-
 #include "ctkModuleDescriptionReader.h"
 
 //----------------------------------------------------------------------------
 ctkModuleDescriptionReader::ctkModuleDescriptionReader() 
 {
-
+  this->Device = 0;
 }
 
-//----------------------------------------------------------------------------
-ctkModuleDescriptionReader::~ctkModuleDescriptionReader()
+//-----------------------------------------------------------------------------
+void ctkModuleDescriptionReader::setInput(QIODevice * device)
 {
-
+  this->Device = device;
 }
 
-//----------------------------------------------------------------------------
-void ctkModuleDescriptionReader::setXmlContent( const QString &val )
-{
-  this->XmlContent = val;
-}
-
-//----------------------------------------------------------------------------
-const ctkModuleDescription &ctkModuleDescriptionReader::moduleDescription() const
+//-----------------------------------------------------------------------------
+QIODevice* ctkModuleDescriptionReader::input()const
 {
-  return this->ModuleDescription;
+  return this->Device;
 }

+ 9 - 12
Libs/ModuleDescription/ctkModuleDescriptionReader.h

@@ -24,30 +24,27 @@ limitations under the License.
 #include "CTKModuleDescriptionExport.h"
 #include "ctkModuleDescriptionReaderInterface.h"
 
+class QIODevice;
 /** 
- * \brief Base Reader of ModuleDescription
+ * \brief Base XML Reader of ModuleDescription
  *
- * This is the base interface
+ * This specialized ctkModuleDescriptionReaderInterface can initialize
+ * a module description from an XML file. update() must be reimplemented
+ * in inherited classes.
  */
 class CTK_MODULDESC_EXPORT ctkModuleDescriptionReader :
-  virtual public ctkModuleDescriptionReaderInterface
+  public ctkModuleDescriptionReaderInterface
 {
   Q_OBJECT
 public:
   ctkModuleDescriptionReader();
-  ~ctkModuleDescriptionReader();
 
   //! xml is the content of the XML file
-  void setXmlContent(const QString &val);
-
-  //!
-  const ctkModuleDescription &moduleDescription() const;
+  void setInput(QIODevice * device);
+  QIODevice* input()const;
 
 protected:
-  ///
-  QString XmlContent;
-  ///
-  ctkModuleDescription ModuleDescription;
+  QIODevice*           Device;
 };
 
 #endif

+ 28 - 0
Libs/ModuleDescription/ctkModuleDescriptionReaderInterface.cpp

@@ -0,0 +1,28 @@
+/*=============================================================================
+
+Library: CTK
+
+Copyright (c) 2010 Brigham and Women's Hospital (BWH) All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+=============================================================================*/
+
+// CTK includes
+#include "ctkModuleDescriptionReaderInterface.h"
+
+//-----------------------------------------------------------------------------
+const ctkModuleDescription& ctkModuleDescriptionReaderInterface::moduleDescription()const
+{
+  return this->ModuleDescription;
+}

+ 11 - 8
Libs/ModuleDescription/ctkModuleDescriptionReaderInterface.h

@@ -21,28 +21,31 @@ limitations under the License.
 #ifndef __ctkModuleDescriptionReaderInterface_h
 #define __ctkModuleDescriptionReaderInterface_h
 
+// Qt includes
+#include <QObject>
 #include <QString>
 
-#include "CTKModuleDescriptionExport.h"
+// CTK includes
 #include "ctkModuleDescription.h"
+#include "CTKModuleDescriptionExport.h"
 
 /** 
  * \brief Interface of ModuleDescription reader
  */
 class CTK_MODULDESC_EXPORT ctkModuleDescriptionReaderInterface : public QObject
 {
-    Q_OBJECT
+  Q_OBJECT
 public:
 
-  //! xml is the content of the XML file
-  virtual void setXmlContent(const QString &val) = 0;
-
-  //!
-  virtual const ctkModuleDescription &moduleDescription() const = 0;
-
   //!
   virtual void update( ) = 0;
 
+  //! Returns the populated module description.
+  //! Note: the module description is set after update() is called
+  const ctkModuleDescription &moduleDescription() const;
+
+protected:
+  ctkModuleDescription ModuleDescription;
 };
 
 #endif

+ 3 - 18
Libs/ModuleDescription/ctkModuleParameter.cpp

@@ -22,26 +22,12 @@ limitations under the License.
 #include "QStringList"
 
 //----------------------------------------------------------------------------
-ctkModuleParameter::ctkModuleParameter()
-{
-}
-
-//----------------------------------------------------------------------------
-ctkModuleParameter::ctkModuleParameter(const ctkModuleParameter& parameter)
-  : QHash<QString, QString>( QHash<QString, QString>( parameter ) )
-{
-}
-
-//----------------------------------------------------------------------------
 bool ctkModuleParameter::isReturnParameter() const
 {
   // could check for tag == float, int, float-vector, ...
-  if ( (*this)["Channel"] == "output" 
-    && !this->isFlagParameter() && !this->isIndexParameter())
-  {
-    return true;
-  }
-  return false;
+  return (*this)["Channel"] == "output" 
+         && !this->isFlagParameter()
+         && !this->isIndexParameter();
 }
 
 //----------------------------------------------------------------------------
@@ -84,7 +70,6 @@ QTextStream & operator<<(QTextStream &os, const ctkModuleParameter &parameter)
   return os;
 }
 
-
 //----------------------------------------------------------------------------
 QTextStream & operator<<(QTextStream &os, const QStringList &list)
 {

+ 3 - 7
Libs/ModuleDescription/ctkModuleParameter.h

@@ -37,19 +37,15 @@
 class CTK_MODULDESC_EXPORT ctkModuleParameter : public QHash<QString, QString>
 {
 public:
-  ctkModuleParameter();
-  ctkModuleParameter(const ctkModuleParameter& parameter);
-
   /// Simple return types are parameters on output channel with no
   /// flags and without a specified index 
-  virtual bool isReturnParameter() const;
+  bool isReturnParameter() const;
   
   /// Has a flag or a long flag?
-  virtual bool isFlagParameter() const;
+  bool isFlagParameter() const;
 
   /// Is an index type?
-  virtual bool isIndexParameter() const;
-
+  bool isIndexParameter() const;
 };
 
 CTK_MODULDESC_EXPORT QTextStream & operator<<(QTextStream &os, const ctkModuleParameter &parameter);

+ 54 - 23
Libs/ModuleDescription/ctkModuleParameterGroup.cpp

@@ -21,44 +21,73 @@ limitations under the License.
 #include "ctkModuleParameterGroup.h"
 
 //----------------------------------------------------------------------------
-ctkModuleParameterGroup::ctkModuleParameterGroup()
+ctkModuleParameterGroup::~ctkModuleParameterGroup()
 {
+  foreach(ctkModuleParameter* param, this->Parameters)
+    {
+    delete param;
+    }
+  this->Parameters.clear();
 }
 
 //----------------------------------------------------------------------------
-ctkModuleParameterGroup
-::ctkModuleParameterGroup(const ctkModuleParameterGroup &parameters)
-  : QHash<QString, QString>( QHash<QString, QString>( parameters ) )
+void ctkModuleParameterGroup::addParameter( ctkModuleParameter* parameter )
 {
-  this->Parameters = parameters.Parameters;
-}
-
-//----------------------------------------------------------------------------
-/* this is done automatically no?
-void ctkModuleParameterGroup
-::operator=(const ctkModuleParameterGroup &parameters)
-{
-  QHash<QString, QString>::operator=(parameters);
-  this->Parameters = parameters.Parameters;
+  Q_ASSERT(parameter);
+  this->Parameters.push_back(parameter);
 }
-*/
 
 //----------------------------------------------------------------------------
-void ctkModuleParameterGroup::addParameter( const ctkModuleParameter &parameter )
+ctkModuleParameter* ctkModuleParameterGroup::parameter( const QString& parameterName )const
 {
-  this->Parameters.push_back(parameter);
+  foreach(ctkModuleParameter* param, this->Parameters)
+    {
+    if ((*param)["Name"] == parameterName)
+      {
+      return param;
+      }
+    }
+  return 0;
 }
 
 //----------------------------------------------------------------------------
-const QVector<ctkModuleParameter>& ctkModuleParameterGroup::parameters() const
+bool ctkModuleParameterGroup::hasReturnParameters() const
 {
-  return this->Parameters;
+  // iterate over each parameter in this group
+  foreach(const ctkModuleParameter* param, this->Parameters)
+    {
+    if (param->isReturnParameter())
+      {
+      return true;
+      }
+    }
+  return false;
 }
 
 //----------------------------------------------------------------------------
-QVector<ctkModuleParameter>& ctkModuleParameterGroup::parameters()
+bool ctkModuleParameterGroup::
+writeParameterFile(QTextStream& in, bool withHandlesToBulkParameters)const
 {
-  return this->Parameters;
+  // iterate over each parameter in this group
+  foreach(const ctkModuleParameter* moduleParameter, this->Parameters)
+    {
+    const ctkModuleParameter& param = *moduleParameter;
+    // write out all parameters or just the ones that are not bulk parameters
+    if (withHandlesToBulkParameters
+        || (!withHandlesToBulkParameters 
+            && (param[ "Tag" ] != "image"
+            && param[ "Tag" ] != "geometry"
+            && param[ "Tag" ] != "transform"
+            && param[ "Tag" ] != "table"
+            && param[ "Tag" ] != "measurement"
+            && param[ "Tag" ] != "point"  // point and region are special
+            && param[ "Tag" ] != "region")))
+      {
+      in << param[ "Name" ] << " = " << param[ "Default" ] << endl;
+      // multiple="true" may have to be handled differently
+      }
+    }
+  return true;
 }
 
 //----------------------------------------------------------------------------
@@ -67,7 +96,9 @@ QTextStream & operator<<(QTextStream &os, const ctkModuleParameterGroup &group)
   os << QHash<QString, QString>(group);
 
   os << "  Parameters: " << endl;
-  foreach( const ctkModuleParameter& it, group.parameters())
-  { os << it; }
+  foreach (const ctkModuleParameter* it, group.Parameters)
+    {
+    os << *it;
+    }
   return os;
 }

+ 11 - 11
Libs/ModuleDescription/ctkModuleParameterGroup.h

@@ -32,20 +32,20 @@
 class CTK_MODULDESC_EXPORT ctkModuleParameterGroup : public QHash<QString, QString>
 {
 public:
-  ctkModuleParameterGroup();
-  ctkModuleParameterGroup(const ctkModuleParameterGroup &parameters);
-
-  //void operator=(const ctkModuleParameterGroup &parameters);
-
-  void addParameter(const ctkModuleParameter &parameter);
-
-  const QVector<ctkModuleParameter>& parameters() const;
-
-  QVector<ctkModuleParameter>& parameters();
+  virtual ~ctkModuleParameterGroup();
+  
+  /// Takes ownership of parameter
+  void addParameter(ctkModuleParameter* parameter);
+  /// Returns 0 if not found.
+  ctkModuleParameter* parameter(const QString& parameterName)const;
+  /// Returns true if at least 1 parameter is a return type
+  bool hasReturnParameters()const;
+  bool writeParameterFile(QTextStream& in, bool withHandlesToBulkParameters = true)const;
   
 private:
+  friend CTK_MODULDESC_EXPORT QTextStream & operator<<(QTextStream &os, const ctkModuleParameterGroup &group);
   ///
-  QVector<ctkModuleParameter> Parameters;
+  QVector<ctkModuleParameter*> Parameters;
 };
 
 CTK_MODULDESC_EXPORT QTextStream & operator<<(QTextStream &os, const ctkModuleParameterGroup &group);