runtimeparametersetters.h
1 /********************************************************************************
2  * FARSA - Total99 *
3  * Copyright (C) 2008-2012 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 RUNTIME_PARAMETER_SETTERS_H
21 #define RUNTIME_PARAMETER_SETTERS_H
22 
23 #include "configurationconfig.h"
24 #include <QString>
25 #include <QStringList>
26 
27 namespace farsa {
28 
29 class ParameterSettable;
30 
33 class FARSA_CONF_API RuntimeParameterObserver {
34 public:
35  virtual ~RuntimeParameterObserver() { /*nothing to do*/ };
38  virtual void onParameterChanges( ParameterSettable* object, QString paramName ) = 0;
39 };
40 
44 class FARSA_CONF_API RuntimeParameterSetter {
45 public:
47  RuntimeParameterSetter( QString descrPath ) {
48  this->descrPath = descrPath;
49  this->name = descrPath.split( '/', QString::SkipEmptyParts ).last();
50  };
52  virtual ~RuntimeParameterSetter() { /* nothing to do */ };
58  virtual QString set( ParameterSettable* object, QString newvalue );
64  virtual int set( ParameterSettable* object, int newvalue );
70  virtual unsigned int set( ParameterSettable* object, unsigned int newvalue );
76  virtual double set( ParameterSettable* object, double newvalue );
82  virtual float set( ParameterSettable* object, float newvalue );
88  virtual bool set( ParameterSettable* object, bool newvalue );
93  virtual void get( ParameterSettable* object, QString& ret );
98  virtual void get( ParameterSettable* object, int& ret );
103  virtual void get( ParameterSettable* object, unsigned int& ret );
108  virtual void get( ParameterSettable* object, double& ret );
113  virtual void get( ParameterSettable* object, float& ret );
118  virtual void get( ParameterSettable* object, bool& ret );
120  QString getName() { return name; };
122  QString getDescriptionPath() { return descrPath; };
123 protected:
125  QString name;
127  QString descrPath;
128 };
129 
133 // template<typename H>
134 // class FARSA_CONF_TEMPLATE PointerSetter : public RuntimeParameterSetter {
135 // public:
136 // /*! the constructor */
137 // PointerSetter( H* pointer, QString name )
138 // : RuntimeParameterSetter( name, H() ),
139 // pparam(pointer) { /* nothing else to do */ };
140 // /*! set the parameter changing directly the value of the int parameter */
141 // H set( H newvalue ) {
142 // (*pparam) = newvalue;
143 // return *pparam;
144 // };
145 // private:
146 // /*! the pointer to the parameter */
147 // H* pparam;
148 // };
149 
152 template<class T, typename H>
153 class FARSA_CONF_TEMPLATE MethodSetter : public RuntimeParameterSetter {
154 public:
156  MethodSetter( QString paramPath, void (T::*setter)(H), H (T::*getter)()const )
157  : RuntimeParameterSetter( paramPath ),
158  setter(setter),
159  getter(getter) { /*nothing else to do*/ };
161  H set( ParameterSettable* object, H newvalue ) {
162  T* robject = static_cast<T*>( object );
163  (robject->*setter)( newvalue );
164  return (robject->*getter)();
165  };
167  void get( ParameterSettable* object, H& retvar ) {
168  T* robject = static_cast<T*>( object );
169  retvar = (robject->*getter)();
170  };
171 private:
173  void (T::*setter)(H);
175  H (T::*getter)()const;
176 };
177 
178 } // end namespace farsa
179 
180 #endif