parametersettable.cpp
1 /***************************************************************************
2  * Copyright (C) 2008-2011 by Tomassino Ferrauto *
3  * 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 *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "parametersettable.h"
22 #include "factory.h"
23 #include "configurationparameters.h"
24 #include "configurationhelper.h"
25 
26 // helper function for setting the properties of a parameter or a type
27 void setProperties( farsa::ConfigurationParameters& typeDescr, QString group, farsa::ParameterSettable::Properties props ) {
28  if ( props.testFlag( farsa::ParameterSettable::IsList ) ) {
29  typeDescr.createParameter( group, "isList", "true" );
30  } else {
31  typeDescr.createParameter( group, "isList", "false" );
32  }
33  if ( props.testFlag( farsa::ParameterSettable::IsMandatory ) ) {
34  typeDescr.createParameter( group, "isMandatory", "true" );
35  } else {
36  typeDescr.createParameter( group, "isMandatory", "false" );
37  }
38  if ( props.testFlag( farsa::ParameterSettable::AllowMultiple ) ) {
39  typeDescr.createParameter( group, "allowMultiple", "true" );
40  } else {
41  typeDescr.createParameter( group, "allowMultiple", "false" );
42  }
43 };
44 
45 namespace farsa {
46 
47 const double ParameterSettable::Infinity = std::numeric_limits<double>::infinity();
48 const int ParameterSettable::MaxInteger = std::numeric_limits<int>::max();
49 const int ParameterSettable::MinInteger = std::numeric_limits<int>::min();
50 
51 QMap<QString, RuntimeParameterSetter*> ParameterSettable::runtimeParameters;
52 
54 {
55 }
56 
58 {
59  foreach( RuntimeParameterObserver* obs, observers ) {
60  obs->onObjectDestruction( this );
61  }
62 }
63 
65  if ( !observers.contains( obs ) ) {
66  observers.append( obs );
67  }
68 }
69 
71  observers.removeAll( obs );
72 }
73 
74 void ParameterSettable::notifyChangesToParam( QString paramName ) {
75  foreach( RuntimeParameterObserver* obs, observers ) {
76  obs->onParameterChanges( this, paramName );
77  }
78 }
79 
80 QString ParameterSettable::fullParameterDescriptionPath( QString type, QString param ) {
81  QString ret = type + "/";
82  QStringList groups = param.split( "/", QString::SkipEmptyParts );
83  for( int i=0; i<groups.size()-1; i++ ) {
84  ret += "Subgroups/"+groups[i]+"/";
85  }
86  ret += "Parameters/"+groups.last();
87  return ret;
88 }
89 
90 QString ParameterSettable::fullSubgroupDescriptionPath( QString type, QString sub ) {
91  QString ret = type + "/";
92  QStringList groups = sub.split( "/", QString::SkipEmptyParts );
93  foreach( QString agroup, groups ) {
94  ret += "Subgroups/"+agroup+"/";
95  }
96  return ret;
97 }
98 
99 void ParameterSettable::createParamDescription( QString paramPath, QString traitName, QString traitValue ) {
100  Factory::getTypeDescriptions().createParameter( paramPath, traitName, traitValue );
101 }
102 
103 ParameterSettable::Descriptor ParameterSettable::addTypeDescription( QString type, QString shortHelp, QString longHelp ) {
104  return Descriptor( type, shortHelp, longHelp );
105 }
106 
107 ParameterSettable::Descriptor::Descriptor( QString type, QString shortHelp, QString longHelp ) {
108  this->type = type;
110  typeDescr.createGroup( type );
111  typeDescr.createParameter( type, "shortHelp", shortHelp );
112  if ( longHelp != "" ) {
113  typeDescr.createParameter( type, "longHelp", longHelp );
114  }
115 }
116 
118  return StringDescriptor( type + "/Parameters/" + parameter );
119 }
120 
122  return IntDescriptor( type + "/Parameters/" + parameter );
123 }
124 
126  return RealDescriptor( type + "/Parameters/" + parameter );
127 }
128 
130  return BoolDescriptor( type + "/Parameters/" + parameter );
131 }
132 
134  return EnumDescriptor( type + "/Parameters/" + parameter );
135 }
136 
138  return ObjectDescriptor( type + "/Parameters/" + parameter );
139 }
140 
142  return SubgroupDescriptor( type + "/Subgroups/" + subgroup );
143 }
144 
146  this->paramPath = paramPath;
148  typeDescr.createGroup( paramPath );
149  typeDescr.createParameter( paramPath, "type", "string" );
150 }
151 
153  Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
154  return (*this);
155 }
156 
158  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
159  return (*this);
160 }
161 
164  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
165  if ( longHelp != "" ) {
166  typeDescr.createParameter( paramPath, "longHelp", longHelp );
167  }
168  return (*this);
169 }
170 
172  this->paramPath = paramPath;
174  typeDescr.createGroup( paramPath );
175  typeDescr.createParameter( paramPath, "type", "int" );
176 }
177 
179  Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
180  return (*this);
181 }
182 
184  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
185  return (*this);
186 }
187 
190  if ( lower_bound != MinInteger ) {
191  typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
192  }
193  if ( upper_bound != MaxInteger ) {
194  typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
195  }
196  return (*this);
197 }
198 
201  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
202  if ( longHelp != "" ) {
203  typeDescr.createParameter( paramPath, "longHelp", longHelp );
204  }
205  return (*this);
206 }
207 
209  this->paramPath = paramPath;
211  typeDescr.createGroup( paramPath );
212  typeDescr.createParameter( paramPath, "type", "real" );
213 }
214 
216  Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
217  return (*this);
218 }
219 
221  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
222  return (*this);
223 }
224 
227  if ( lower_bound != -Infinity ) {
228  typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
229  }
230  if ( upper_bound != +Infinity ) {
231  typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
232  }
233  return (*this);
234 }
235 
238  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
239  if ( longHelp != "" ) {
240  typeDescr.createParameter( paramPath, "longHelp", longHelp );
241  }
242  return (*this);
243 }
244 
246  this->paramPath = paramPath;
248  typeDescr.createGroup( paramPath );
249  typeDescr.createParameter( paramPath, "type", "bool" );
250 }
251 
253  Factory::getTypeDescriptions().createParameter( paramPath, "default", (defaultValue ? "true" : "false") );
254  return (*this);
255 }
256 
258  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
259  return (*this);
260 }
261 
264  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
265  if ( longHelp != "" ) {
266  typeDescr.createParameter( paramPath, "longHelp", longHelp );
267  }
268  return (*this);
269 }
270 
272  this->paramPath = paramPath;
274  typeDescr.createGroup( paramPath );
275  typeDescr.createParameter( paramPath, "type", "enum" );
276 }
277 
279  Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
280  return (*this);
281 }
282 
284  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
285  return (*this);
286 }
287 
289  Factory::getTypeDescriptions().createParameter( paramPath, "values", allValues.join(" ") );
290  return (*this);
291 }
292 
295  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
296  if ( longHelp != "" ) {
297  typeDescr.createParameter( paramPath, "longHelp", longHelp );
298  }
299  return (*this);
300 }
301 
303  this->paramPath = paramPath;
305  typeDescr.createGroup( paramPath );
306  typeDescr.createParameter( paramPath, "type", "object" );
307 }
308 
310  setProperties( Factory::getTypeDescriptions(), paramPath, properties );
311  return (*this);
312 }
313 
315  Factory::getTypeDescriptions().createParameter( paramPath, "class", className );
316  return (*this);
317 }
318 
321  typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
322  if ( longHelp != "" ) {
323  typeDescr.createParameter( paramPath, "longHelp", longHelp );
324  }
325  return (*this);
326 }
327 
329  this->subgroupPath = subgroupPath;
330  Factory::getTypeDescriptions().createGroup( subgroupPath );
331 }
332 
334  setProperties( Factory::getTypeDescriptions(), subgroupPath, properties );
335  return (*this);
336 }
337 
339  Factory::getTypeDescriptions().createParameter( subgroupPath, "type", "object" );
340  Factory::getTypeDescriptions().createParameter( subgroupPath, "class", className );
341  return (*this);
342 }
343 
346  typeDescr.createParameter( subgroupPath, "shortHelp", shortHelp );
347  if ( longHelp != "" ) {
348  typeDescr.createParameter( subgroupPath, "longHelp", longHelp );
349  }
350  return (*this);
351 }
352 
354  return StringDescriptor( subgroupPath + "/Parameters/" + parameter );
355 }
356 
358  return IntDescriptor( subgroupPath + "/Parameters/" + parameter );
359 }
360 
362  return RealDescriptor( subgroupPath + "/Parameters/" + parameter );
363 }
364 
366  return BoolDescriptor( subgroupPath + "/Parameters/" + parameter );
367 }
368 
370  return EnumDescriptor( subgroupPath + "/Parameters/" + parameter );
371 }
372 
374  return ObjectDescriptor( subgroupPath + "/Parameters/" + parameter );
375 }
376 
378  return SubgroupDescriptor( subgroupPath + "/Subgroups/" + subgroup );
379 }
380 
381 } // end namespace farsa
382 
SubgroupDescriptor describeSubgroup(QString subgroup)
Add/Modify the description of a subgroup This method add (if there was no previous description of the...
EnumDescriptor & props(Properties properties)
set the Properties of this parameter (see Property documentation)
static QString fullSubgroupDescriptionPath(QString type, QString sub)
helper function for creating the full path to a subgroup description
BoolDescriptor(QString paramPath)
Add (if not exists) the description of a Boolean parameter.
EnumDescriptor & help(QString shortHelp, QString longHelp=QString(""))
Add a short help comment and a complete help comment.
static ConfigurationParameters & getTypeDescriptions()
Returns the only instance of the typeDescriptions object.
Definition: factory.cpp:34
IntDescriptor & limits(int lower_bound, int upper_bound)
set the lower and the upper bounds for the parameter's value If you don't want to limit the lower_bou...
Utility Class for customize the description of Enum-like parameter.
ObjectDescriptor & props(Properties properties)
set the Properties of this parameter (see Property documentation)
Utility Class for customize the description of an object parameter.
SubgroupDescriptor(QString paramPath)
Add (if not exists) the description of a subgroup parameter.
Utility Class for customize the description of String parameter.
EnumDescriptor describeEnum(QString parameter)
Add/Modify the description of a parameter which can take value from a fixed set of values This method...
IntDescriptor & props(Properties properties)
set the Properties of this parameter (see Property documentation)
StringDescriptor(QString paramPath)
Add (if not exists) the description of a String parameter.
virtual void onObjectDestruction(ParameterSettable *object)=0
called when the object is going to be destroyed
IntDescriptor(QString paramPath)
Add (if not exists) the description of a Int parameter.
ObjectDescriptor & help(QString shortHelp, QString longHelp=QString(""))
Add a short help comment and a complete help comment.
RealDescriptor describeReal(QString parameter)
Add/Modify the description of a real value parameter to this subgroup This method add (if there was n...
EnumDescriptor & def(QString defaultValue)
set the default value when the parameter is not present into the configuration
ObjectDescriptor & type(QString className)
set the class of the object (it includes also all subclasses)
Utility Class for customize the description of Real valued parameter.
static const double Infinity
Shortcut variable for indicate an infinity double number.
IntDescriptor describeInt(QString parameter)
Add/Modify the description of a int parameter This method add (if there was no previous description o...
virtual void onParameterChanges(ParameterSettable *object, QString paramName)=0
called when a runtime parameter changes its value
BoolDescriptor describeBool(QString parameter)
Add/Modify the description of a boolean parameter This method add (if there was no previous descripti...
void removeObserver(RuntimeParameterObserver *obs)
remove the RuntimeParameterObserver to this object
The class containing configuration parameters.
SubgroupDescriptor & props(Properties properties)
set the Properties of this parameter (see Property documentation)
virtual ~ParameterSettable()
Destructor.
BoolDescriptor & help(QString shortHelp, QString longHelp=QString(""))
Add a short help comment and a complete help comment.
static QString fullParameterDescriptionPath(QString type, QString param)
helper function for creating the full path to a parameter description
EnumDescriptor & values(QStringList allValues)
set all the possible values acceptable for this parameter
the interface for any observer who needs to be notified when a runtime parameter changes ...
means that the parameter accepts a list of values
RealDescriptor & props(Properties properties)
set the Properties of this parameter (see Property documentation)
EnumDescriptor(QString paramPath)
Add (if not exists) the description of a Enum-like parameter.
StringDescriptor describeString(QString parameter)
Add/Modify the description of a string parameter This method add (if there was no previous descriptio...
RealDescriptor & help(QString shortHelp, QString longHelp=QString(""))
Add a short help comment and a complete help comment.
Utility Class for customize the description of a subgroup.
SubgroupDescriptor & type(QString className)
the name of the class of the object corresponding to the subgroup (it includes also all subclasses) ...
BoolDescriptor & def(bool defaultValue)
set the default value when the parameter is not present into the configuration
Utility Class for describe the parameters of a ParameterSettable.
Descriptor(QString type, QString shortHelp, QString longHelp)
Add an entry for the type into the descriptions of all available types.
EnumDescriptor describeEnum(QString parameter)
Add/Modify the description of a parameter which can take value from a fixed set of values This method...
BoolDescriptor describeBool(QString parameter)
Add/Modify the description of a boolean parameter to this subgroup This method add (if there was no p...
StringDescriptor describeString(QString parameter)
Add/Modify the description of a string parameter to this subgroup This method add (if there was no pr...
StringDescriptor & def(QString defaultValue)
set the default value when the parameter is not present into the configuration
void createGroup(QString groupPath)
Create a group in the path specified.
StringDescriptor & help(QString shortHelp, QString longHelp=QString(""))
Add a short help comment and a complete help comment.
Utility Class for customize the description of Integer parameter.
IntDescriptor & def(int defaultValue)
set the default value when the parameter is not present into the configuration
ObjectDescriptor describeObject(QString parameter)
Add/Modify the description of a object parameter This method add (if there was no previous descriptio...
BoolDescriptor & props(Properties properties)
set the Properties of this parameter (see Property documentation)
RealDescriptor & def(double defaultValue)
set the default value when the parameter is not present into the configuration
SubgroupDescriptor describeSubgroup(QString subgroup)
Add/Modify the description of a subgroup to this subgroup This method add (if there was no previous d...
void notifyChangesToParam(QString paramName)
notify observers about a changing to a runtime parameter
SubgroupDescriptor & help(QString shortHelp, QString longHelp=QString(""))
Add a short help comment and a complete help comment.
static const int MinInteger
Shortcut variable for indicate the minimum int value allowed.
ObjectDescriptor describeObject(QString parameter)
Add/Modify the description of a object parameter to this subgroup This method add (if there was no pr...
IntDescriptor & help(QString shortHelp, QString longHelp=QString(""))
Add a short help comment and a complete help comment.
static Descriptor addTypeDescription(QString type, QString shortHelp, QString longHelp=QString(""))
Add an entry for the type into the descriptions of all available types.
void addObserver(RuntimeParameterObserver *obs)
add a RuntimeParameterObserver to this object
RealDescriptor describeReal(QString parameter)
Add/Modify the description of a real value parameter This method add (if there was no previous descri...
static const int MaxInteger
Shortcut variable for indicate the maximum int value allowed.
ObjectDescriptor(QString paramPath)
Add (if not exists) the description of a object parameter.
means that more than one of this parameter can exists; there are numbered using the ':' syntax; i...
RealDescriptor(QString paramPath)
Add (if not exists) the description of a Real valued parameter.
Utility Class for customize the description of Boolean parameter.
StringDescriptor & props(Properties properties)
set the Properties of this parameter (see Property documentation)
void createParameter(QString groupPath, QString parameter)
Create a parameter in the group path specified.
RealDescriptor & limits(double lower_bound, double upper_bound)
set the lower and the upper bounds for the parameter's value If you don't want to limit the lower_bou...
IntDescriptor describeInt(QString parameter)
Add/Modify the description of a int parameter to this subgroup This method add (if there was no previ...
if the parameter is mandatory