CLAM-Development  1.4.0
InControl.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _InControl_
23 #define _InControl_
24 
25 #include <string>
26 #include <list>
27 #include <typeinfo>
28 #include <sstream>
29 #include "TypeInfo.hxx"
30 #include "InControlBase.hxx"
31 #include "OutControlBase.hxx"
32 
33 namespace CLAM {
34  class Processing;
35  class OutControlBase;
36 
37  template<class ControlDataType>
38  class OutControl;
39 
45  template<class ControlDataType>
46  class InControl : public InControlBase
47  {
48  class Callback;
49  private:
50  Callback * _callback;
51  protected:
52  ControlDataType mLastValue;
53 
54  public:
56  InControl(const std::string &name = "unnamed in control", Processing * proc = 0)
57  : InControlBase(name,proc)
58  , _callback(new NullCallback)
59  {
60  }
62  template <typename ProcessingType, typename ParameterType>
63  InControl(const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(const ParameterType&))
64  : InControlBase(name,proc)
65  , _callback(new MethodCallback<ProcessingType,ParameterType>(proc, callback))
66  {
67  }
69  template <typename ProcessingType, typename ParameterType>
70  InControl(unsigned id, const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(unsigned, const ParameterType&))
71  : InControlBase(name,proc)
72  , _callback(new MethodCallbackWithId<ProcessingType,ParameterType>(proc, callback, id))
73  {
74  }
76  template <typename ProcessingType, typename ParameterType>
77  InControl(const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(ParameterType))
78  : InControlBase(name,proc)
79  , _callback(new MethodCallbackByCopy<ProcessingType,ParameterType>(proc, callback))
80  {
81  }
83  template <typename ProcessingType, typename ParameterType>
84  InControl(unsigned id, const std::string &name, ProcessingType * proc, void (ProcessingType::*callback)(unsigned, ParameterType))
85  : InControlBase(name,proc)
86  , _callback(new MethodCallbackByCopyWithId<ProcessingType,ParameterType>(proc, callback, id))
87  {
88  }
89 
90 
91  virtual ~InControl()
92  {
93  delete _callback;
94  }
95 
100  virtual void DoControl(const ControlDataType& val)
101  {
102  mLastValue = val;
103  _hasBeenRead=false;
104  _callback->DoControl(val);
105  };
106 
108  virtual const ControlDataType& GetLastValue() const
109  {
110  _hasBeenRead=true;
111  return mLastValue;
112  };
116  const std::string GetLastValueAsString() // TODO: Use plugins as soon we start to use non streamable types
117  {
119  }
120  // For the typed linking check
121  virtual const std::type_info& GetTypeId() const { return typeid(ControlDataType); };
122  private:
123  std::string GetLastValueAsString(StaticFalse* /*isStreamable*/) const
124  {
125  return "Not printable";
126  }
128  std::string GetLastValueAsString(StaticTrue* /*isStreamable*/) const
129  {
130  std::ostringstream valueStream;
131  valueStream << GetLastValue();
132  return valueStream.str();
133  }
134 
135  private:
136  // Callback wrappers
137 
139  typedef typename TypeInfo<ControlDataType>::StorableAsLeaf TokenIsStorableAsLeaf;
140  class Callback
141  {
142  public:
143  virtual ~Callback() {}
144  virtual void DoControl(const ControlDataType & val) =0;
145  };
146 
148  class NullCallback : public Callback
149  {
150  public:
151  virtual void DoControl(const ControlDataType & val) {}
152  };
153 
156  template <typename ProcessingType, typename ValueParameterType>
157  class MethodCallback : public Callback
158  {
159  protected:
160  ProcessingType * _processing;
161  void (ProcessingType::*_method)(const ValueParameterType& );
162  public:
163  MethodCallback(ProcessingType * processing, void (ProcessingType::*method)(const ValueParameterType &) )
164  : _processing(processing)
165  , _method(method)
166  {
167  }
168  virtual void DoControl(const ControlDataType & value)
169  {
170  (_processing->*_method)(value);
171  }
172  };
173 
178  template <typename ProcessingType, typename ValueParameterType>
179  class MethodCallbackWithId : public Callback
180  {
181  ProcessingType * _processing;
182  void (ProcessingType::*_method)(unsigned, const ValueParameterType &);
183  unsigned _id;
184  public:
185  MethodCallbackWithId(ProcessingType * processing, void (ProcessingType::*method)(unsigned,const ValueParameterType &), unsigned id )
186  : _processing(processing)
187  , _method(method)
188  , _id(id)
189  {
190  }
191  virtual void DoControl(const ControlDataType & value)
192  {
193  (_processing->*_method)(_id, value);
194  }
195  };
196 
200  template <typename ProcessingType, typename ValueParameterType>
201  class MethodCallbackByCopy : public Callback
202  {
203  protected:
204  ProcessingType * _processing;
205  void (ProcessingType::*_method)(ValueParameterType);
206  public:
207  MethodCallbackByCopy(ProcessingType * processing, void (ProcessingType::*method)(ValueParameterType) )
208  : _processing(processing)
209  , _method(method)
210  {
211  }
212  virtual void DoControl(const ControlDataType & value)
213  {
214  (_processing->*_method)(value);
215  }
216  };
217 
223  template <typename ProcessingType, typename ValueParameterType>
224  class MethodCallbackByCopyWithId : public Callback
225  {
226  ProcessingType * _processing;
227  void (ProcessingType::*_method)(unsigned, ValueParameterType);
228  unsigned _id;
229  public:
230  MethodCallbackByCopyWithId(ProcessingType * processing, void (ProcessingType::*method)(unsigned,ValueParameterType), unsigned id )
231  : _processing(processing)
232  , _method(method)
233  , _id(id)
234  {
235  }
236  virtual void DoControl(const ControlDataType & value)
237  {
238  (_processing->*_method)(_id, value);
239  }
240  };
241  };
242 
245 
246 } // End namespace CLAM
247 #endif //_InControl_
248