configuration/include/runtimeparametersetters.h

00001 /********************************************************************************
00002  *  FARSA - Total99                                                             *
00003  *  Copyright (C) 2008-2012 Tomassino Ferrauto <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 Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef RUNTIME_PARAMETER_SETTERS_H
00021 #define RUNTIME_PARAMETER_SETTERS_H
00022 
00023 #include "configurationconfig.h"
00024 
00025 namespace farsa {
00026 
00030 class FARSA_CONF_API RuntimeParameterSetter {
00031 public:
00033     RuntimeParameterSetter( QString name, int ) : name(name), type("Int") { /* nothing to do */ };
00035     RuntimeParameterSetter( QString name, unsigned int ) : name(name), type("Unsigned Int") { /* nothing to do */ };
00037     RuntimeParameterSetter( QString name, double ) : name(name), type("Double") { /* nothing to do */ };
00039     RuntimeParameterSetter( QString name, float ) : name(name), type("Float") { /* nothing to do */ };
00041     RuntimeParameterSetter( QString name, bool ) : name(name), type("Boolean") { /* nothing to do */ };
00043     virtual ~RuntimeParameterSetter() { /* nothing to do */ };
00048     virtual int set( int newvalue );
00053     virtual unsigned int set( unsigned int newvalue );
00058     virtual double set( double newvalue );
00063     virtual float set( float newvalue );
00068     virtual bool set( bool newvalue );
00070     QString getName() { return name; };
00072     QString getType() { return type; };
00073 protected:
00075     const QString name;
00077     const QString type;
00078 };
00079 
00083 template<typename H>
00084 class FARSA_CONF_TEMPLATE PointerSetter : public RuntimeParameterSetter {
00085 public:
00087     PointerSetter( H* pointer, QString name )
00088         : RuntimeParameterSetter( name, H() ),
00089           pparam(pointer) { /* nothing else to do */ };
00091     H set( H newvalue ) {
00092         (*pparam) = newvalue;
00093         return *pparam;
00094     };
00095 private:
00097     H* pparam;
00098 };
00099 
00102 template<class T, typename H>
00103 class FARSA_CONF_TEMPLATE MethodSetter : public RuntimeParameterSetter {
00104 public:
00106     MethodSetter( T* owner, void (T::*setter)(H), H (T::*getter)()const, QString name )
00107         : RuntimeParameterSetter( name, H() ),
00108           owner(owner),
00109           setter(setter) { /*nothing else to do*/ };
00111     H set( H newvalue ) {
00112         (owner->*setter)( newvalue );
00113         return (owner->*getter)();
00114     };
00115 private:
00117     T* owner;
00119     void (T::*setter)(H);
00121     H (T::*getter)()const;
00122 };
00123 
00124 } // end namespace farsa
00125 
00126 #endif