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  {
87  }
88 
92  virtual ~ParameterSettable()
93  {
94  }
95 
105  virtual void save(ConfigurationParameters& params, QString prefix) = 0;
106 
107 //#ifdef DOXYGEN_RUNNING
118  static void describe( QString type ) { addTypeDescription( type, "Describe is missing" ); };
119 //#endif
127  virtual void postConfigureInitialization()
128  {
129  }
130 
134  virtual ParameterSettableUI* getUIManager() { return NULL; };
135 
148  template<class T>
149  void setRuntimeParameter( QString paramName, T newvalue ) {
150  QString paramFullPath = fullParameterDescriptionPath( type, paramName );
151  if ( !runtimeParameters.contains( paramFullPath ) ) {
152  throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
153  }
154  RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
155  rparam->set( this, newvalue );
156  foreach( RuntimeParameterObserver* obs, observers ) {
157  obs->onParameterChanges( this, paramName );
158  }
159  };
169  template<class T>
170  T getRuntimeParameter( QString paramName ) {
171  QString paramFullPath = fullParameterDescriptionPath( type, paramName );
172  if ( !runtimeParameters.contains( paramFullPath ) ) {
173  throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
174  }
175  RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
176  T ret;
177  rparam->get( this, ret );
178  return ret;
179  };
180 
184  void addObserver( RuntimeParameterObserver* obs );
185 
191  void removeObserver( RuntimeParameterObserver* obs );
192 
201  QString typeName() const {
202  return type;
203  };
204 
209  static QString fullParameterDescriptionPath( QString type, QString param );
214  static QString fullSubgroupDescriptionPath( QString type, QString sub );
215 
216 protected:
217 
220  class FARSA_CONF_API StringDescriptor {
221  public:
225  StringDescriptor( QString paramPath );
227  StringDescriptor& def( QString defaultValue );
229  StringDescriptor& props( Properties properties );
239  template<class T>
240  StringDescriptor& runtime( void (T::*setter)(QString), QString (T::*getter)()const ) {
241  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
242  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, QString>( paramPath, setter, getter );
243  return (*this);
244  };
253  StringDescriptor& help( QString shortHelp, QString longHelp=QString("") );
254  private:
256  QString paramPath;
257  };
258 
261  class FARSA_CONF_API IntDescriptor {
262  public:
266  IntDescriptor( QString paramPath );
268  IntDescriptor& def( int defaultValue );
270  IntDescriptor& props( Properties properties );
277  IntDescriptor& limits( int lower_bound, int upper_bound );
287  template<class T>
288  IntDescriptor& runtime( void (T::*setter)(int), int (T::*getter)()const ) {
289  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
290  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, int>( paramPath, setter, getter );
291  return (*this);
292  };
301  IntDescriptor& help( QString shortHelp, QString longHelp=QString("") );
302  private:
304  QString paramPath;
305  };
306 
309  class FARSA_CONF_API RealDescriptor {
310  public:
314  RealDescriptor( QString paramPath );
316  RealDescriptor& def( double defaultValue );
318  RealDescriptor& props( Properties properties );
325  RealDescriptor& limits( double lower_bound, double upper_bound );
335  template<class T>
336  RealDescriptor& runtime( void (T::*setter)(double), double (T::*getter)()const ) {
337  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
338  ParameterSettable::createParamDescription( paramPath, "precision", "double" );
339  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, double>( paramPath, setter, getter );
340  return (*this);
341  };
351  template<class T>
352  RealDescriptor& runtime( void (T::*setter)(float), float (T::*getter)()const ) {
353  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
354  ParameterSettable::createParamDescription( paramPath, "precision", "float" );
355  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, float>( paramPath, setter, getter );
356  return (*this);
357  };
366  RealDescriptor& help( QString shortHelp, QString longHelp=QString("") );
367  private:
369  QString paramPath;
370  };
371 
374  class FARSA_CONF_API BoolDescriptor {
375  public:
379  BoolDescriptor( QString paramPath );
381  BoolDescriptor& def( bool defaultValue );
383  BoolDescriptor& props( Properties properties );
393  template<class T>
394  BoolDescriptor& runtime( void (T::*setter)(bool), bool (T::*getter)()const ) {
395  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
396  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, bool>( paramPath, setter, getter );
397  return (*this);
398  };
407  BoolDescriptor& help( QString shortHelp, QString longHelp=QString("") );
408  private:
410  QString paramPath;
411  };
412 
415  class FARSA_CONF_API EnumDescriptor {
416  public:
420  EnumDescriptor( QString paramPath );
422  EnumDescriptor& def( QString defaultValue );
424  EnumDescriptor& props( Properties properties );
426  EnumDescriptor& values( QStringList allValues );
436  template<class T>
437  EnumDescriptor& runtime( void (T::*setter)(unsigned int), unsigned int (T::*getter)()const ) {
438  ParameterSettable::createParamDescription( paramPath, "isRuntime", "true" );
439  ParameterSettable::runtimeParameters[paramPath] = new MethodSetter<T, unsigned int>( paramPath, setter, getter );
440  return (*this);
441  };
450  EnumDescriptor& help( QString shortHelp, QString longHelp=QString("") );
451  private:
453  QString paramPath;
454  };
455 
458  class FARSA_CONF_API ObjectDescriptor {
459  public:
463  ObjectDescriptor( QString paramPath );
465  ObjectDescriptor& props( Properties properties );
467  ObjectDescriptor& type( QString className );
476  ObjectDescriptor& help( QString shortHelp, QString longHelp=QString("") );
477  private:
479  QString paramPath;
480  };
481 
484  class FARSA_CONF_API SubgroupDescriptor {
485  public:
489  SubgroupDescriptor( QString paramPath );
491  SubgroupDescriptor& props( Properties properties );
493  SubgroupDescriptor& type( QString className );
502  SubgroupDescriptor& help( QString shortHelp, QString longHelp=QString("") );
512  StringDescriptor describeString( QString parameter );
522  IntDescriptor describeInt( QString parameter );
532  RealDescriptor describeReal( QString parameter );
542  BoolDescriptor describeBool( QString parameter );
552  EnumDescriptor describeEnum( QString parameter );
562  ObjectDescriptor describeObject( QString parameter );
572  SubgroupDescriptor describeSubgroup( QString subgroup );
573  private:
575  QString subgroupPath;
576  };
577 
580  class FARSA_CONF_API Descriptor {
581  public:
589  Descriptor( QString type, QString shortHelp, QString longHelp );
601  StringDescriptor describeString( QString parameter );
613  IntDescriptor describeInt( QString parameter );
625  RealDescriptor describeReal( QString parameter );
637  BoolDescriptor describeBool( QString parameter );
649  EnumDescriptor describeEnum( QString parameter );
661  ObjectDescriptor describeObject( QString parameter );
673  SubgroupDescriptor describeSubgroup( QString subgroup );
674  private:
676  QString type;
677  };
678 
693  static Descriptor addTypeDescription( QString type, QString shortHelp, QString longHelp=QString("") );
694 
695 private:
696  // this friend allow to correctly set the typeName of a ParameterSettable only by the RealFactory
697  friend class RealFactory;
699  QString type;
701  void setTypeName( QString type ) {
702  this->type = type;
703  };
705  QList<RuntimeParameterObserver*> observers;
711  static QMap<QString, RuntimeParameterSetter*> runtimeParameters;
717  static void createParamDescription( QString paramPath, QString traitName, QString traitValue );
718 };
719 
730 class FARSA_CONF_TEMPLATE ParameterSettableInConstructor : public ParameterSettable
731 {
732 public:
740  {
741  }
742 
757  {
758  }
759 
764  {
765  }
766 };
767 
779 {
780 public:
785  {
786  }
787 
792  {
793  }
794 
805  virtual void configure(ConfigurationParameters& params, QString prefix) = 0;
806 };
807 
808 } // end namespace farsa
809 
810 Q_DECLARE_OPERATORS_FOR_FLAGS( farsa::ParameterSettable::Properties )
811 
812 #endif