InControl.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef _InControl_
00023 #define _InControl_
00024 
00025 #include <string>
00026 #include <list>
00027 #include <typeinfo>
00028 #include <sstream>
00029 #include "TypeInfo.hxx"
00030 #include "InControlBase.hxx"
00031 #include "OutControlBase.hxx"
00032 
00033 namespace CLAM {
00034         class Processing;
00035         class OutControlBase;
00036 
00037         template<class ControlDataType>
00038         class OutControl;
00039 
00045         template<class ControlDataType>
00046         class InControl : public InControlBase
00047         {
00048                 class Callback;
00049         private:
00050                 Callback * _callback;
00051         protected:
00052                 ControlDataType mLastValue;
00053                 
00054         public:
00056                 InControl(const std::string &name = "unnamed in control", Processing * proc = 0)
00057                         : InControlBase(name,proc)
00058                         , _callback(new NullCallback)
00059                 {
00060                 }
00062                 template <typename ProcessingType, typename ParameterType>
00063                 InControl(const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(const ParameterType&))
00064                         : InControlBase(name,proc)
00065                         , _callback(new MethodCallback<ProcessingType,ParameterType>(proc, callback))
00066                 {
00067                 }
00069                 template <typename ProcessingType, typename ParameterType>
00070                 InControl(unsigned id, const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(unsigned, const ParameterType&))
00071                         : InControlBase(name,proc)
00072                         , _callback(new MethodCallbackWithId<ProcessingType,ParameterType>(proc, callback, id))
00073                 {
00074                 }
00076                 template <typename ProcessingType, typename ParameterType>
00077                 InControl(const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(ParameterType))
00078                         : InControlBase(name,proc)
00079                         , _callback(new MethodCallbackByCopy<ProcessingType,ParameterType>(proc, callback))
00080                 {
00081                 }
00083                 template <typename ProcessingType, typename ParameterType>
00084                 InControl(unsigned id, const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(unsigned, ParameterType))
00085                         : InControlBase(name,proc)
00086                         , _callback(new MethodCallbackByCopyWithId<ProcessingType,ParameterType>(proc, callback, id))
00087                 {
00088                 }
00089 
00090 
00091                 virtual ~InControl()
00092                 {
00093                         delete _callback;
00094                 }
00095 
00100                 virtual void DoControl(const ControlDataType& val) 
00101                 {
00102                         mLastValue = val;
00103                         _hasBeenRead=false;
00104                         _callback->DoControl(val);
00105                 };
00106 
00108                 virtual const ControlDataType& GetLastValue() const 
00109                 {
00110                         _hasBeenRead=true;
00111                         return mLastValue; 
00112                 };
00116                 const std::string GetLastValueAsString() // TODO: Use plugins as soon we start to use non streamable types
00117                 {
00118                         return GetLastValueAsString((TokenIsStorableAsLeaf*)0);
00119                 }
00120                 // For the typed linking check
00121                 virtual const std::type_info& GetTypeId() const { return typeid(ControlDataType); };
00122         private:
00123                 std::string GetLastValueAsString(StaticFalse* /*isStreamable*/) const
00124                 {
00125                         return "Not printable";
00126                 }
00128                 std::string GetLastValueAsString(StaticTrue* /*isStreamable*/) const
00129                 {
00130                         std::ostringstream valueStream;
00131                         valueStream << GetLastValue();
00132                         return valueStream.str();
00133                 }
00134 
00135         private:
00136                 // Callback wrappers
00137 
00139                 typedef typename TypeInfo<ControlDataType>::StorableAsLeaf TokenIsStorableAsLeaf;
00140                 class Callback
00141                 {
00142                         public:
00143                                 virtual ~Callback() {}
00144                                 virtual void DoControl(const ControlDataType & val) =0;
00145                 };
00146 
00148                 class NullCallback : public Callback
00149                 {
00150                         public:
00151                                 virtual void DoControl(const ControlDataType & val) {}
00152                 };
00153 
00156                 template <typename ProcessingType, typename ValueParameterType>
00157                 class MethodCallback : public Callback
00158                 {
00159                 protected:
00160                         ProcessingType * _processing;
00161                         void (ProcessingType::*_method)(const ValueParameterType& );
00162                 public:
00163                         MethodCallback(ProcessingType * processing, void (ProcessingType::*method)(const ValueParameterType &) )
00164                                 : _processing(processing)
00165                                 , _method(method)
00166                         {
00167                         }
00168                         virtual void DoControl(const ControlDataType & value)
00169                         {
00170                                 (_processing->*_method)(value);
00171                         }
00172                 };
00173 
00178                 template <typename ProcessingType, typename ValueParameterType>
00179                 class MethodCallbackWithId : public Callback
00180                 {
00181                         ProcessingType * _processing;
00182                         void (ProcessingType::*_method)(unsigned, const ValueParameterType &);
00183                         unsigned _id;
00184                 public:
00185                         MethodCallbackWithId(ProcessingType * processing, void (ProcessingType::*method)(unsigned,const ValueParameterType &), unsigned id )
00186                                 : _processing(processing)
00187                                 , _method(method)
00188                                 , _id(id)
00189                         {
00190                         }
00191                         virtual void DoControl(const ControlDataType & value)
00192                         {
00193                                 (_processing->*_method)(_id, value);
00194                         }
00195                 };
00196 
00200                 template <typename ProcessingType, typename ValueParameterType>
00201                 class MethodCallbackByCopy : public Callback
00202                 {
00203                 protected:
00204                         ProcessingType * _processing;
00205                         void (ProcessingType::*_method)(ValueParameterType);
00206                 public:
00207                         MethodCallbackByCopy(ProcessingType * processing, void (ProcessingType::*method)(ValueParameterType) )
00208                                 : _processing(processing)
00209                                 , _method(method)
00210                         {
00211                         }
00212                         virtual void DoControl(const ControlDataType & value)
00213                         {
00214                                 (_processing->*_method)(value);
00215                         }
00216                 };
00217 
00223                 template <typename ProcessingType, typename ValueParameterType>
00224                 class MethodCallbackByCopyWithId : public Callback
00225                 {
00226                         ProcessingType * _processing;
00227                         void (ProcessingType::*_method)(unsigned, ValueParameterType);
00228                         unsigned _id;
00229                 public:
00230                         MethodCallbackByCopyWithId(ProcessingType * processing, void (ProcessingType::*method)(unsigned,ValueParameterType), unsigned id )
00231                                 : _processing(processing)
00232                                 , _method(method)
00233                                 , _id(id)
00234                         {
00235                         }
00236                         virtual void DoControl(const ControlDataType & value)
00237                         {
00238                                 (_processing->*_method)(_id, value);
00239                         }
00240                 };
00241         };
00242         
00244 typedef InControl<float> FloatInControl;
00245 
00246 } // End namespace CLAM
00247 #endif //_InControl_
00248 
Generated by  doxygen 1.6.3