00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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()
00117 {
00118 return GetLastValueAsString((TokenIsStorableAsLeaf*)0);
00119 }
00120
00121 virtual const std::type_info& GetTypeId() const { return typeid(ControlDataType); };
00122 private:
00123 std::string GetLastValueAsString(StaticFalse* ) const
00124 {
00125 return "Not printable";
00126 }
00128 std::string GetLastValueAsString(StaticTrue* ) const
00129 {
00130 std::ostringstream valueStream;
00131 valueStream << GetLastValue();
00132 return valueStream.str();
00133 }
00134
00135 private:
00136
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 }
00247 #endif //_InControl_
00248