parametersettable.h
1 /********************************************************************************
2  * FARSA - Total99 *
3  * Copyright (C) 2008-2011 Tomassino Ferrauto <t_ferrauto@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef PARAMETER_SETTABLE_H
21 #define PARAMETER_SETTABLE_H
22 
23 #include "configurationconfig.h"
24 #include "configurationexceptions.h"
25 #include "runtimeparametersetters.h"
26 #include <QFlags>
27 #include <cmath>
28 #include <limits>
29 
30 namespace farsa {
31 
32 class ConfigurationHelper;
33 class ConfigurationParameters;
34 class RealFactory;
35 class ParameterSettableUI;
36 
51 class FARSA_CONF_API ParameterSettable
52 {
53 public:
60  enum Property {
62  Default = 0x0000,
64  IsList = 0x0001,
66  IsMandatory = 0x0002,
71  AllowMultiple = 0x0004
72  };
73  Q_DECLARE_FLAGS( Properties, Property )
74 
75 
76  static const double Infinity;
78  static const int MaxInteger;
80  static const int MinInteger;
81 
86 
90  virtual ~ParameterSettable();
91 
101  virtual void save(ConfigurationParameters& params, QString prefix) = 0;
102 
103 //#ifdef DOXYGEN_RUNNING
114  static void describe( QString type ) { addTypeDescription( type, "Describe is missing" ); };
115 //#endif
123  virtual void postConfigureInitialization()
124  {
125  }
126 
130  virtual ParameterSettableUI* getUIManager() { return NULL; };
131 
144  template<class T>
145  void setRuntimeParameter( QString paramName, T newvalue ) {
146  QString paramFullPath = fullParameterDescriptionPath( type, paramName );
147  if ( !runtimeParameters.contains( paramFullPath ) ) {
148  throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
149  }
150  RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
151  rparam->set( this, newvalue );
152  foreach( RuntimeParameterObserver* obs, observers ) {
153  obs->onParameterChanges( this, paramName );
154  }
155  };
165  template<class T>
166  T getRuntimeParameter( QString paramName ) {
167  QString paramFullPath = fullParameterDescriptionPath( type, paramName );
168  if ( !runtimeParameters.contains( paramFullPath ) ) {
169  throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
170  }
171  RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
172  T ret;
173  rparam->get( this, ret );
174  return ret;
175  };
176 
180  void addObserver( RuntimeParameterObserver* obs );
181 
187  void removeObserver( RuntimeParameterObserver* obs );
188 
197  QString typeName() const {
198  return type;
199  };
200 
205  static QString fullParameterDescriptionPath( QString type, QString param );
210  static QString fullSubgroupDescriptionPath( QString type, QString sub );
211 
212 protected:
219  void notifyChangesToParam( QString paramName );
220 
223  class FARSA_CONF_API StringDescriptor {
224  public:
228  StringDescriptor( QString paramPath );
230  StringDescriptor& def( QString defaultValue );
232  StringDescriptor& props( Properties properties );
242  template<class T>
243  StringDescriptor& runtime( void (T::*setter)(QString), QString (T::*getter)()const ) {
244  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
245  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, QString>( paramPath, setter, getter );
246  return (*this);
247  };
256  StringDescriptor& help( QString shortHelp, QString longHelp=QString("") );
257  private:
259  QString paramPath;
260  };
261 
264  class FARSA_CONF_API IntDescriptor {
265  public:
269  IntDescriptor( QString paramPath );
271  IntDescriptor& def( int defaultValue );
273  IntDescriptor& props( Properties properties );
280  IntDescriptor& limits( int lower_bound, int upper_bound );
290  template<class T>
291  IntDescriptor& runtime( void (T::*setter)(int), int (T::*getter)()const ) {
292  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
293  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, int>( paramPath, setter, getter );
294  return (*this);
295  };
304  IntDescriptor& help( QString shortHelp, QString longHelp=QString("") );
305  private:
307  QString paramPath;
308  };
309 
312  class FARSA_CONF_API RealDescriptor {
313  public:
317  RealDescriptor( QString paramPath );
319  RealDescriptor& def( double defaultValue );
321  RealDescriptor& props( Properties properties );
328  RealDescriptor& limits( double lower_bound, double upper_bound );
338  template<class T>
339  RealDescriptor& runtime( void (T::*setter)(double), double (T::*getter)()const ) {
340  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
341  ParameterSettable::createParamDescription( paramPath, "precision", "double" );
342  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, double>( paramPath, setter, getter );
343  return (*this);
344  };
354  template<class T>
355  RealDescriptor& runtime( void (T::*setter)(float), float (T::*getter)()const ) {
356  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
357  ParameterSettable::createParamDescription( paramPath, "precision", "float" );
358  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, float>( paramPath, setter, getter );
359  return (*this);
360  };
369  RealDescriptor& help( QString shortHelp, QString longHelp=QString("") );
370  private:
372  QString paramPath;
373  };
374 
377  class FARSA_CONF_API BoolDescriptor {
378  public:
382  BoolDescriptor( QString paramPath );
384  BoolDescriptor& def( bool defaultValue );
386  BoolDescriptor& props( Properties properties );
396  template<class T>
397  BoolDescriptor& runtime( void (T::*setter)(bool), bool (T::*getter)()const ) {
398  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
399  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, bool>( paramPath, setter, getter );
400  return (*this);
401  };
410  BoolDescriptor& help( QString shortHelp, QString longHelp=QString("") );
411  private:
413  QString paramPath;
414  };
415 
418  class FARSA_CONF_API EnumDescriptor {
419  public:
423  EnumDescriptor( QString paramPath );
425  EnumDescriptor& def( QString defaultValue );
427  EnumDescriptor& props( Properties properties );
429  EnumDescriptor& values( QStringList allValues );
439  template<class T>
440  EnumDescriptor& runtime( void (T::*setter)(unsigned int), unsigned int (T::*getter)()const ) {
441  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
442  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, unsigned int>( paramPath, setter, getter );
443  return (*this);
444  };
453  EnumDescriptor& help( QString shortHelp, QString longHelp=QString("") );
454  private:
456  QString paramPath;
457  };
458 
461  class FARSA_CONF_API ObjectDescriptor {
462  public:
466  ObjectDescriptor( QString paramPath );
468  ObjectDescriptor& props( Properties properties );
470  ObjectDescriptor& type( QString className );
479  ObjectDescriptor& help( QString shortHelp, QString longHelp=QString("") );
480  private:
482  QString paramPath;
483  };
484 
487  class FARSA_CONF_API SubgroupDescriptor {
488  public:
492  SubgroupDescriptor( QString paramPath );
494  SubgroupDescriptor& props( Properties properties );
496  SubgroupDescriptor& type( QString className );
505  SubgroupDescriptor& help( QString shortHelp, QString longHelp=QString("") );
515  StringDescriptor describeString( QString parameter );
525  IntDescriptor describeInt( QString parameter );
535  RealDescriptor describeReal( QString parameter );
545  BoolDescriptor describeBool( QString parameter );
555  EnumDescriptor describeEnum( QString parameter );
565  ObjectDescriptor describeObject( QString parameter );
575  SubgroupDescriptor describeSubgroup( QString subgroup );
576  private:
578  QString subgroupPath;
579  };
580 
583  class FARSA_CONF_API Descriptor {
584  public:
592  Descriptor( QString type, QString shortHelp, QString longHelp );
604  StringDescriptor describeString( QString parameter );
616  IntDescriptor describeInt( QString parameter );
628  RealDescriptor describeReal( QString parameter );
640  BoolDescriptor describeBool( QString parameter );
652  EnumDescriptor describeEnum( QString parameter );
664  ObjectDescriptor describeObject( QString parameter );
676  SubgroupDescriptor describeSubgroup( QString subgroup );
677  private:
679  QString type;
680  };
681 
696  static Descriptor addTypeDescription( QString type, QString shortHelp, QString longHelp=QString("") );
697 
703  template <class EditorType>
704  static void setGraphicalEditor( QString type );
705 
706 private:
707  // this friend allow to correctly set the typeName of a ParameterSettable only by the RealFactory
708  friend class RealFactory;
710  QString type;
712  void setTypeName( QString type ) {
713  this->type = type;
714  };
716  QList<RuntimeParameterObserver*> observers;
722  static QMap<QString, RuntimeParameterSetter*> runtimeParameters;
728  static void createParamDescription( QString paramPath, QString traitName, QString traitValue );
729 };
730 
741 class FARSA_CONF_TEMPLATE ParameterSettableInConstructor : public ParameterSettable
742 {
743 public:
751  {
752  }
753 
768  {
769  }
770 
775  {
776  }
777 };
778 
790 {
791 public:
796  {
797  }
798 
803  {
804  }
805 
816  virtual void configure(ConfigurationParameters& params, QString prefix) = 0;
817 };
818 
819 } // end namespace farsa
820 
821 Q_DECLARE_OPERATORS_FOR_FLAGS( farsa::ParameterSettable::Properties )
822 
823 // Implementation of template functions
824 // #include "factory.h"
825 //
826 // namespace farsa {
827 //
828 // template <class EditorType>
829 // void ParameterSettable::setGraphicalEditor( QString type )
830 // {
831 // Factory::registerEditorForType<EditorType>(type);
832 // }
833 //
834 // }
835 
836 #endif