configuration/src/parametersettable.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2008-2011 by Tomassino Ferrauto                         *
00003  *   t_ferrauto@yahoo.it                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "parametersettable.h"
00022 #include "factory.h"
00023 #include "configurationparameters.h"
00024 #include "configurationhelper.h"
00025 
00026 // helper function for setting the properties of a parameter or a type
00027 void setProperties( farsa::ConfigurationParameters& typeDescr, QString group, farsa::ParameterSettable::Properties props ) {
00028     if ( props.testFlag( farsa::ParameterSettable::IsList ) ) {
00029         typeDescr.createParameter( group, "isList", "true" );
00030     } else {
00031         typeDescr.createParameter( group, "isList", "false" );
00032     }
00033     if ( props.testFlag( farsa::ParameterSettable::IsMandatory ) ) {
00034         typeDescr.createParameter( group, "isMandatory", "true" );
00035     } else {
00036         typeDescr.createParameter( group, "isMandatory", "false" );
00037     }
00038     if ( props.testFlag( farsa::ParameterSettable::AllowMultiple ) ) {
00039         typeDescr.createParameter( group, "allowMultiple", "true" );
00040     } else {
00041         typeDescr.createParameter( group, "allowMultiple", "false" );
00042     }
00043 };
00044 
00045 //helper function for creating the full path to a parameter
00046 QString fullParameterPath( QString type, QString param, QString parent ) {
00047     QString ret = type + "/";
00048     if ( parent != "" ) {
00049         QStringList groups = parent.split( "/", QString::SkipEmptyParts );
00050         foreach( QString parent, groups ) {
00051             ret += "Subgroups/"+parent+"/";
00052         }
00053     }
00054     ret += "Parameters/"+param;
00055     return ret;
00056 }
00057 
00058 //helper function for creating the full path to a subgroup
00059 QString fullSubgroupPath( QString type, QString sub, QString parent ) {
00060     QString ret = type + "/";
00061     if ( parent != "" ) {
00062         QStringList groups = parent.split( "/", QString::SkipEmptyParts );
00063         foreach( QString parent, groups ) {
00064             ret += "Subgroups/"+parent+"/";
00065         }
00066     }
00067     ret += "Subgroups/"+sub;
00068     return ret;
00069 }
00070 
00071 namespace farsa {
00072 
00073 const double ParameterSettable::Infinity = std::numeric_limits<double>::infinity();
00074 const int ParameterSettable::MaxInteger = std::numeric_limits<int>::max();
00075 const int ParameterSettable::MinInteger = std::numeric_limits<int>::min();
00076 
00077 void ParameterSettable::setRuntimeParameter( QString paramFullPath, int newvalue ) {
00078     if ( !runtimeParameters.contains( paramFullPath ) ) {
00079         throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
00080         return;
00081     }
00082     RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
00083     rparam->set( newvalue );
00084 }
00085 
00086 void ParameterSettable::setRuntimeParameter( QString paramFullPath, unsigned int newvalue ) {
00087     if ( !runtimeParameters.contains( paramFullPath ) ) {
00088         throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
00089         return;
00090     }
00091     RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
00092     rparam->set( newvalue );
00093 }
00094 
00095 void ParameterSettable::setRuntimeParameter( QString paramFullPath, double newvalue ) {
00096     if ( !runtimeParameters.contains( paramFullPath ) ) {
00097         throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
00098         return;
00099     }
00100     RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
00101     rparam->set( newvalue );
00102 }
00103 
00104 void ParameterSettable::setRuntimeParameter( QString paramFullPath, float newvalue ) {
00105     if ( !runtimeParameters.contains( paramFullPath ) ) {
00106         throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
00107         return;
00108     }
00109     RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
00110     rparam->set( newvalue );
00111 }
00112 
00113 void ParameterSettable::setRuntimeParameter( QString paramFullPath, bool newvalue ) {
00114     if ( !runtimeParameters.contains( paramFullPath ) ) {
00115         throw NoRuntimeModifiableParameter( paramFullPath.toAscii().data() );
00116         return;
00117     }
00118     RuntimeParameterSetter* rparam = runtimeParameters[paramFullPath];
00119     rparam->set( newvalue );
00120 }
00121 
00122 QList<RuntimeParameterSetter*> ParameterSettable::runtimeParameterList() {
00123     return runtimeParameters.values();
00124 }
00125 
00126 void ParameterSettable::markParameterAsRuntime( QString paramName, int* pointer ) {
00127     markParameterAsRuntime( new PointerSetter<int>( pointer, paramName ) );
00128 }
00129 
00130 void ParameterSettable::markParameterAsRuntime( QString paramName, double* pointer ) {
00131     markParameterAsRuntime( new PointerSetter<double>( pointer, paramName ) );
00132 }
00133 
00134 void ParameterSettable::markParameterAsRuntime( QString paramName, bool* pointer ) {
00135     markParameterAsRuntime( new PointerSetter<bool>( pointer, paramName ) );
00136 }
00137 
00138 void ParameterSettable::markParameterAsRuntime( RuntimeParameterSetter* setter ) {
00139     QString name = setter->getName();
00140     runtimeParameters[name] = setter;
00141 }
00142 
00143 ParameterSettable::Descriptor ParameterSettable::addTypeDescription( QString type, QString shortHelp, QString longHelp ) {
00144     return Descriptor( type, shortHelp, longHelp );
00145 }
00146 
00147 ParameterSettable::Descriptor::Descriptor( QString type, QString shortHelp, QString longHelp ) {
00148     this->type = type;
00149     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00150     typeDescr.createGroup( type );
00151     typeDescr.createParameter( type, "shortHelp", shortHelp );
00152     if ( longHelp != "" ) {
00153         typeDescr.createParameter( type, "longHelp", longHelp );
00154     }
00155 }
00156 
00157 ParameterSettable::StringDescriptor ParameterSettable::Descriptor::describeString( QString parameter ) {
00158     return StringDescriptor( type + "/Parameters/" + parameter );
00159 }
00160 
00161 ParameterSettable::IntDescriptor ParameterSettable::Descriptor::describeInt( QString parameter ) {
00162     return IntDescriptor( type + "/Parameters/" + parameter );
00163 }
00164 
00165 ParameterSettable::RealDescriptor ParameterSettable::Descriptor::describeReal( QString parameter ) {
00166     return RealDescriptor( type + "/Parameters/" + parameter );
00167 }
00168 
00169 ParameterSettable::BoolDescriptor ParameterSettable::Descriptor::describeBool( QString parameter ) {
00170     return BoolDescriptor( type + "/Parameters/" + parameter );
00171 }
00172 
00173 ParameterSettable::EnumDescriptor ParameterSettable::Descriptor::describeEnum( QString parameter ) {
00174     return EnumDescriptor( type + "/Parameters/" + parameter );
00175 }
00176 
00177 ParameterSettable::ObjectDescriptor ParameterSettable::Descriptor::describeObject( QString parameter ) {
00178     return ObjectDescriptor( type + "/Parameters/" + parameter );
00179 }
00180 
00181 ParameterSettable::SubgroupDescriptor ParameterSettable::Descriptor::describeSubgroup( QString subgroup ) {
00182     return SubgroupDescriptor( type + "/Subgroups/" + subgroup );
00183 }
00184 
00185 ParameterSettable::StringDescriptor::StringDescriptor( QString paramPath ) {
00186     this->paramPath = paramPath;
00187     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00188     typeDescr.createGroup( paramPath );
00189     typeDescr.createParameter( paramPath, "type", "string" );
00190 }
00191 
00192 ParameterSettable::StringDescriptor& ParameterSettable::StringDescriptor::def( QString defaultValue ) {
00193     Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
00194     return (*this);
00195 }
00196 
00197 ParameterSettable::StringDescriptor& ParameterSettable::StringDescriptor::props( Properties properties ) {
00198     setProperties( Factory::getTypeDescriptions(), paramPath, properties );
00199     return (*this);
00200 }
00201 
00202 ParameterSettable::StringDescriptor& ParameterSettable::StringDescriptor::help( QString shortHelp, QString longHelp ) {
00203     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00204     typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
00205     if ( longHelp != "" ) {
00206         typeDescr.createParameter( paramPath, "longHelp", longHelp );
00207     }
00208     return (*this);
00209 }
00210 
00211 ParameterSettable::IntDescriptor::IntDescriptor( QString paramPath ) {
00212     this->paramPath = paramPath;
00213     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00214     typeDescr.createGroup( paramPath );
00215     typeDescr.createParameter( paramPath, "type", "int" );
00216 }
00217 
00218 ParameterSettable::IntDescriptor& ParameterSettable::IntDescriptor::def( int defaultValue ) {
00219     Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
00220     return (*this);
00221 }
00222 
00223 ParameterSettable::IntDescriptor& ParameterSettable::IntDescriptor::props( Properties properties ) {
00224     setProperties( Factory::getTypeDescriptions(), paramPath, properties );
00225     return (*this);
00226 }
00227 
00228 ParameterSettable::IntDescriptor& ParameterSettable::IntDescriptor::limits( int lower_bound, int upper_bound ) {
00229     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00230     if ( lower_bound != MinInteger ) {
00231         typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
00232     }
00233     if ( upper_bound != MaxInteger ) {
00234         typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
00235     }
00236     return (*this);
00237 }
00238 
00239 ParameterSettable::IntDescriptor& ParameterSettable::IntDescriptor::help( QString shortHelp, QString longHelp ) {
00240     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00241     typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
00242     if ( longHelp != "" ) {
00243         typeDescr.createParameter( paramPath, "longHelp", longHelp );
00244     }
00245     return (*this);
00246 }
00247 
00248 ParameterSettable::RealDescriptor::RealDescriptor( QString paramPath ) {
00249     this->paramPath = paramPath;
00250     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00251     typeDescr.createGroup( paramPath );
00252     typeDescr.createParameter( paramPath, "type", "real" );
00253 }
00254 
00255 ParameterSettable::RealDescriptor& ParameterSettable::RealDescriptor::def( double defaultValue ) {
00256     Factory::getTypeDescriptions().createParameter( paramPath, "default", QString::number(defaultValue) );
00257     return (*this);
00258 }
00259 
00260 ParameterSettable::RealDescriptor& ParameterSettable::RealDescriptor::props( Properties properties ) {
00261     setProperties( Factory::getTypeDescriptions(), paramPath, properties );
00262     return (*this);
00263 }
00264 
00265 ParameterSettable::RealDescriptor& ParameterSettable::RealDescriptor::limits( double lower_bound, double upper_bound ) {
00266     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00267     if ( lower_bound != -Infinity ) {
00268         typeDescr.createParameter( paramPath, "lowerBound", QString::number(lower_bound) );
00269     }
00270     if ( upper_bound != +Infinity ) {
00271         typeDescr.createParameter( paramPath, "upperBound", QString::number(upper_bound) );
00272     }
00273     return (*this);
00274 }
00275 
00276 ParameterSettable::RealDescriptor& ParameterSettable::RealDescriptor::help( QString shortHelp, QString longHelp ) {
00277     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00278     typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
00279     if ( longHelp != "" ) {
00280         typeDescr.createParameter( paramPath, "longHelp", longHelp );
00281     }
00282     return (*this);
00283 }
00284 
00285 ParameterSettable::BoolDescriptor::BoolDescriptor( QString paramPath ) {
00286     this->paramPath = paramPath;
00287     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00288     typeDescr.createGroup( paramPath );
00289     typeDescr.createParameter( paramPath, "type", "bool" );
00290 }
00291 
00292 ParameterSettable::BoolDescriptor& ParameterSettable::BoolDescriptor::def( bool defaultValue ) {
00293     Factory::getTypeDescriptions().createParameter( paramPath, "default", (defaultValue ? "true" : "false") );
00294     return (*this);
00295 }
00296 
00297 ParameterSettable::BoolDescriptor& ParameterSettable::BoolDescriptor::props( Properties properties ) {
00298     setProperties( Factory::getTypeDescriptions(), paramPath, properties );
00299     return (*this);
00300 }
00301 
00302 ParameterSettable::BoolDescriptor& ParameterSettable::BoolDescriptor::help( QString shortHelp, QString longHelp ) {
00303     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00304     typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
00305     if ( longHelp != "" ) {
00306         typeDescr.createParameter( paramPath, "longHelp", longHelp );
00307     }
00308     return (*this);
00309 }
00310 
00311 ParameterSettable::EnumDescriptor::EnumDescriptor( QString paramPath ) {
00312     this->paramPath = paramPath;
00313     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00314     typeDescr.createGroup( paramPath );
00315     typeDescr.createParameter( paramPath, "type", "enum" );
00316 }
00317 
00318 ParameterSettable::EnumDescriptor& ParameterSettable::EnumDescriptor::def( QString defaultValue ) {
00319     Factory::getTypeDescriptions().createParameter( paramPath, "default", defaultValue );
00320     return (*this);
00321 }
00322 
00323 ParameterSettable::EnumDescriptor& ParameterSettable::EnumDescriptor::props( Properties properties ) {
00324     setProperties( Factory::getTypeDescriptions(), paramPath, properties );
00325     return (*this);
00326 }
00327 
00328 ParameterSettable::EnumDescriptor& ParameterSettable::EnumDescriptor::values( QStringList allValues ) {
00329     Factory::getTypeDescriptions().createParameter( paramPath, "values", allValues.join(" ") );
00330     return (*this);
00331 }
00332 
00333 ParameterSettable::EnumDescriptor& ParameterSettable::EnumDescriptor::help( QString shortHelp, QString longHelp ) {
00334     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00335     typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
00336     if ( longHelp != "" ) {
00337         typeDescr.createParameter( paramPath, "longHelp", longHelp );
00338     }
00339     return (*this);
00340 }
00341 
00342 ParameterSettable::ObjectDescriptor::ObjectDescriptor( QString paramPath ) {
00343     this->paramPath = paramPath;
00344     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00345     typeDescr.createGroup( paramPath );
00346     typeDescr.createParameter( paramPath, "type", "object" );
00347 }
00348 
00349 ParameterSettable::ObjectDescriptor& ParameterSettable::ObjectDescriptor::props( Properties properties ) {
00350     setProperties( Factory::getTypeDescriptions(), paramPath, properties );
00351     return (*this);
00352 }
00353 
00354 ParameterSettable::ObjectDescriptor& ParameterSettable::ObjectDescriptor::type( QString className ) {
00355     Factory::getTypeDescriptions().createParameter( paramPath, "class", className );
00356     return (*this);
00357 }
00358 
00359 ParameterSettable::ObjectDescriptor& ParameterSettable::ObjectDescriptor::help( QString shortHelp, QString longHelp ) {
00360     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00361     typeDescr.createParameter( paramPath, "shortHelp", shortHelp );
00362     if ( longHelp != "" ) {
00363         typeDescr.createParameter( paramPath, "longHelp", longHelp );
00364     }
00365     return (*this);
00366 }
00367 
00368 ParameterSettable::SubgroupDescriptor::SubgroupDescriptor( QString subgroupPath ) {
00369     this->subgroupPath = subgroupPath;
00370     Factory::getTypeDescriptions().createGroup( subgroupPath );
00371 }
00372 
00373 ParameterSettable::SubgroupDescriptor& ParameterSettable::SubgroupDescriptor::props( Properties properties ) {
00374     setProperties( Factory::getTypeDescriptions(), subgroupPath, properties );
00375     return (*this);
00376 }
00377 
00378 ParameterSettable::SubgroupDescriptor& ParameterSettable::SubgroupDescriptor::type( QString className ) {
00379     Factory::getTypeDescriptions().createParameter( subgroupPath, "type", "object" );
00380     Factory::getTypeDescriptions().createParameter( subgroupPath, "class", className );
00381     return (*this);
00382 }
00383 
00384 ParameterSettable::SubgroupDescriptor& ParameterSettable::SubgroupDescriptor::help( QString shortHelp, QString longHelp ) {
00385     ConfigurationParameters& typeDescr = Factory::getTypeDescriptions();
00386     typeDescr.createParameter( subgroupPath, "shortHelp", shortHelp );
00387     if ( longHelp != "" ) {
00388         typeDescr.createParameter( subgroupPath, "longHelp", longHelp );
00389     }
00390     return (*this);
00391 }
00392 
00393 ParameterSettable::StringDescriptor ParameterSettable::SubgroupDescriptor::describeString( QString parameter ) {
00394     return StringDescriptor( subgroupPath + "/Parameters/" + parameter );
00395 }
00396 
00397 ParameterSettable::IntDescriptor ParameterSettable::SubgroupDescriptor::describeInt( QString parameter ) {
00398     return IntDescriptor( subgroupPath + "/Parameters/" + parameter );
00399 }
00400 
00401 ParameterSettable::RealDescriptor ParameterSettable::SubgroupDescriptor::describeReal( QString parameter ) {
00402     return RealDescriptor( subgroupPath + "/Parameters/" + parameter );
00403 }
00404 
00405 ParameterSettable::BoolDescriptor ParameterSettable::SubgroupDescriptor::describeBool( QString parameter ) {
00406     return BoolDescriptor( subgroupPath + "/Parameters/" + parameter );
00407 }
00408 
00409 ParameterSettable::EnumDescriptor ParameterSettable::SubgroupDescriptor::describeEnum( QString parameter ) {
00410     return EnumDescriptor( subgroupPath + "/Parameters/" + parameter );
00411 }
00412 
00413 ParameterSettable::ObjectDescriptor ParameterSettable::SubgroupDescriptor::describeObject( QString parameter ) {
00414     return ObjectDescriptor( subgroupPath + "/Parameters/" + parameter );
00415 }
00416 
00417 ParameterSettable::SubgroupDescriptor ParameterSettable::SubgroupDescriptor::describeSubgroup( QString subgroup ) {
00418     return SubgroupDescriptor( subgroupPath + "/Subgroups/" + subgroup );
00419 }
00420 
00421 } // end namespace farsa
00422