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;
42  virtual void onObjectDestruction( ParameterSettable* object ) = 0;
43 };
44 
48 class FARSA_CONF_API RuntimeParameterSetter {
49 public:
51  RuntimeParameterSetter( QString descrPath ) {
52  this->descrPath = descrPath;
53  this->name = descrPath.split( '/', QString::SkipEmptyParts ).last();
54  };
56  virtual ~RuntimeParameterSetter() { /* nothing to do */ };
62  virtual QString set( ParameterSettable* object, QString newvalue );
68  virtual int set( ParameterSettable* object, int newvalue );
74  virtual unsigned int set( ParameterSettable* object, unsigned int newvalue );
80  virtual double set( ParameterSettable* object, double newvalue );
86  virtual float set( ParameterSettable* object, float newvalue );
92  virtual bool set( ParameterSettable* object, bool newvalue );
97  virtual void get( ParameterSettable* object, QString& ret );
102  virtual void get( ParameterSettable* object, int& ret );
107  virtual void get( ParameterSettable* object, unsigned int& ret );
112  virtual void get( ParameterSettable* object, double& ret );
117  virtual void get( ParameterSettable* object, float& ret );
122  virtual void get( ParameterSettable* object, bool& ret );
124  QString getName() { return name; };
126  QString getDescriptionPath() { return descrPath; };
127 protected:
129  QString name;
131  QString descrPath;
132 };
133 
137 // template<typename H>
138 // class FARSA_CONF_TEMPLATE PointerSetter : public RuntimeParameterSetter {
139 // public:
140 // /*! the constructor */
141 // PointerSetter( H* pointer, QString name )
142 // : RuntimeParameterSetter( name, H() ),
143 // pparam(pointer) { /* nothing else to do */ };
144 // /*! set the parameter changing directly the value of the int parameter */
145 // H set( H newvalue ) {
146 // (*pparam) = newvalue;
147 // return *pparam;
148 // };
149 // private:
150 // /*! the pointer to the parameter */
151 // H* pparam;
152 // };
153 
156 template<class T, typename H>
157 class FARSA_CONF_TEMPLATE MethodSetter : public RuntimeParameterSetter {
158 public:
160  MethodSetter( QString paramPath, void (T::*setter)(H), H (T::*getter)()const )
161  : RuntimeParameterSetter( paramPath ),
162  setter(setter),
163  getter(getter) { /*nothing else to do*/ };
165  H set( ParameterSettable* object, H newvalue ) {
166  T* robject = static_cast<T*>( object );
167  (robject->*setter)( newvalue );
168  return (robject->*getter)();
169  };
171  void get( ParameterSettable* object, H& retvar ) {
172  T* robject = static_cast<T*>( object );
173  retvar = (robject->*getter)();
174  };
175 private:
177  void (T::*setter)(H);
179  H (T::*getter)()const;
180 };
181 
182 } // end namespace farsa
183 
184 #endif