00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __MARCONTROLVALUE__
00020 #define __MARCONTROLVALUE__
00021
00022 #include <string>
00023 #include <vector>
00024 #include <utility>
00025 #include <typeinfo>
00026
00027 #include "common_header.h"
00028 #include "realvec.h"
00029
00030
00031 namespace Marsyas
00032 {
00039 class marsyas_EXPORT MarControl;
00040
00041 class marsyas_EXPORT MarControlValue
00042 {
00043 friend class MarControl;
00044 friend class MarControlAccessor;
00045
00046 protected:
00047 std::string type_;
00048 MarControl* current_link_;
00049
00050 std::string value_debug_;
00051
00052
00053
00054 std::vector<std::pair<MarControl*, MarControl*> > links_;
00055 std::vector<std::pair<MarControl*, MarControl*> >::iterator lit_;
00056
00057 protected:
00058 MarControlValue() {}
00059 MarControlValue(const MarControlValue& a)
00060 {
00061 type_ = a.type_;
00062 };
00063
00064
00065 void setDebugValue();
00066
00067 void current_link_update();
00068
00069
00070 public:
00071 virtual ~MarControlValue() {}
00072
00073 virtual MarControlValue* clone() = 0;
00074 virtual void copyValue(MarControlValue& value) = 0;
00075 virtual void callMarSystemsUpdate() = 0;
00076 virtual MarControlValue* create() = 0;
00077
00078 virtual std::string getTypeID() = 0;
00079
00080 std::string getRegisteredType();
00081
00082 virtual std::string getType() const ;
00083
00084
00085 virtual void createFromStream(std::istream&) = 0;
00086
00087 virtual std::ostream& serialize(std::ostream& os) = 0;
00088
00089 virtual bool isNotEqual(MarControlValue *v) = 0;
00090
00091 virtual MarControlValue* sum(MarControlValue *v) = 0;
00092 virtual MarControlValue* subtract(MarControlValue *v) = 0;
00093 virtual MarControlValue* multiply(MarControlValue *v) = 0;
00094 virtual MarControlValue* divide(MarControlValue *v) = 0;
00095 };
00096
00098
00099 template<class T>
00100 class MarControlValueT : public MarControlValue
00101 {
00102 friend class MarControl;
00103 friend class MarControlAccessor;
00104
00105 protected:
00106 T value_;
00107 T tempValue_;
00108
00109 public:
00110 static T invalidValue;
00111
00112 public:
00113 MarControlValueT();
00114 MarControlValueT(T value);
00115 MarControlValueT(const MarControlValueT& a);
00116
00117 virtual ~MarControlValueT() {}
00118
00119 MarControlValueT& operator=(const MarControlValueT& a);
00120
00121 virtual MarControlValue* clone();
00122 virtual void copyValue(MarControlValue& value);
00123 virtual void callMarSystemsUpdate();
00124 virtual MarControlValue* create();
00125
00126 virtual std::string getTypeID();
00127
00128
00129 void set(MarControlValue *val, bool update);
00130 void set(T &re, bool update);
00131
00132
00133 const T& get() const;
00134
00135 virtual void createFromStream(std::istream&);
00136 virtual std::ostream& serialize(std::ostream& os);
00137 virtual bool isNotEqual(MarControlValue *v);
00138 virtual MarControlValue* sum(MarControlValue *v);
00139 virtual MarControlValue* subtract(MarControlValue *v);
00140 virtual MarControlValue* multiply(MarControlValue *v);
00141 virtual MarControlValue* divide(MarControlValue *v);
00142 bool isInvalid() { return &value_ == &invalidValue; }
00143 };
00144
00145 template<>
00146 class marsyas_EXPORT MarControlValueT<realvec> : public MarControlValue
00147 {
00148 friend class MarControl;
00149 friend class MarControlAccessor;
00150
00151 protected:
00152 realvec value_;
00153 realvec tempValue_;
00154
00155 public:
00156 static realvec invalidValue;
00157
00158 public:
00159 MarControlValueT(realvec value);
00160 MarControlValueT(const MarControlValueT& a);
00161
00162 virtual ~MarControlValueT() {}
00163
00164 MarControlValueT& operator=(const MarControlValueT& a);
00165
00166 virtual MarControlValue* clone();
00167 virtual void copyValue(MarControlValue& value);
00168 virtual void callMarSystemsUpdate();
00169 virtual MarControlValue* create();
00170
00171 virtual std::string getTypeID();
00172
00173
00174 inline void set(MarControlValue *val, bool update);
00175 inline void set(realvec &re, bool update);
00176
00177
00178 const realvec& get() const;
00179 realvec& getRef();
00180
00181 virtual void createFromStream(std::istream&);
00182 virtual std::ostream& serialize(std::ostream& os);
00183 virtual bool isNotEqual(MarControlValue *v);
00184 virtual MarControlValue* sum(MarControlValue *v);
00185 virtual MarControlValue* subtract(MarControlValue *v);
00186 virtual MarControlValue* multiply(MarControlValue *v);
00187 virtual MarControlValue* divide(MarControlValue *v);
00188 bool isInvalid() { return &value_ == &invalidValue; }
00189 };
00190
00191
00192 template<>
00193 class marsyas_EXPORT MarControlValueT<bool> : public MarControlValue
00194 {
00195 friend class MarControl;
00196 friend class MarControlAccessor;
00197
00198 protected:
00199 bool value_;
00200 bool tempValue_;
00201
00202 public:
00203 static bool invalidValue;
00204
00205 public:
00206 MarControlValueT(bool value);
00207 MarControlValueT(const MarControlValueT& a);
00208
00209 virtual ~MarControlValueT() {}
00210
00211 MarControlValueT& operator=(const MarControlValueT& a);
00212
00213 virtual std::string getTypeID();
00214
00215 virtual MarControlValue* clone();
00216 virtual void copyValue(MarControlValue& value);
00217 virtual void callMarSystemsUpdate();
00218 virtual MarControlValue* create();
00219
00220
00221 inline void set(MarControlValue *val, bool update);
00222 inline void set(bool &re, bool update);
00223
00224
00225 const bool& get() const;
00226
00227 virtual void createFromStream(std::istream&);
00228 virtual std::ostream& serialize(std::ostream& os);
00229 virtual bool isNotEqual(MarControlValue *v);
00230 virtual MarControlValue* sum(MarControlValue *v);
00231 virtual MarControlValue* subtract(MarControlValue *v);
00232 virtual MarControlValue* multiply(MarControlValue *v);
00233 virtual MarControlValue* divide(MarControlValue *v);
00234 bool isInvalid() { return &value_ == &invalidValue; }
00235 };
00236
00237
00238 inline std::string operator-(std::string& , std::string&) { return ""; }
00239 inline std::string operator*(std::string& , std::string&) { return ""; }
00240 inline std::string operator/(std::string& , std::string&) { return ""; }
00241
00242
00243
00244
00245 template<class T>
00246 T MarControlValueT<T>::invalidValue;
00247
00248 template<class T>
00249 MarControlValueT<T>::MarControlValueT()
00250 {
00251 value_ = T();
00252 tempValue_ = T();
00253
00254
00255 if (typeid(T) == typeid(mrs_real))
00256 type_ = "mrs_real";
00257 else if (typeid(T) == typeid(mrs_natural))
00258 type_ = "mrs_natural";
00259 else if (typeid(T) == typeid(std::string))
00260 type_ = "mrs_string";
00261 else if (typeid(T) == typeid(realvec))
00262 type_ = "mrs_realvec";
00263 else if (typeid(T) == typeid(bool))
00264 type_ = "mrs_bool";
00265 else
00266 {
00267 type_ = this->getRegisteredType();
00268 }
00269 }
00270
00271 template<class T>
00272 MarControlValueT<T>::MarControlValueT(T value)
00273 {
00274 value_ = value;
00275
00276 setDebugValue();
00277
00278
00279 if (typeid(T) == typeid(mrs_real))
00280 type_ = "mrs_real";
00281 else if (typeid(T) == typeid(mrs_natural))
00282 type_ = "mrs_natural";
00283 else if (typeid(T) == typeid(std::string))
00284 type_ = "mrs_string";
00285 else if (typeid(T) == typeid(realvec))
00286 type_ = "mrs_realvec";
00287 else if (typeid(T) == typeid(bool))
00288 type_ = "mrs_bool";
00289 else
00290 {
00291 type_ = this->getRegisteredType();
00292 }
00293 }
00294
00295 template<class T>
00296 MarControlValueT<T>::MarControlValueT(const MarControlValueT& a):MarControlValue(a)
00297 {
00298 value_ = a.value_;
00299 type_ = a.type_;
00300
00301 setDebugValue();
00302 }
00303
00304 template<class T>
00305 MarControlValueT<T>&
00306 MarControlValueT<T>::operator=(const MarControlValueT& a)
00307 {
00308 if (this != &a)
00309 {
00310 value_ = a.value_;
00311 type_ = a.type_;
00312
00313 setDebugValue();
00314
00315
00316
00317 }
00318 return *this;
00319 }
00320
00321 template<class T>
00322 MarControlValue*
00323 MarControlValueT<T>::clone()
00324 {
00325 return new MarControlValueT<T>(*this);
00326 }
00327
00328 template<class T>
00329 void
00330 MarControlValueT<T>::copyValue(MarControlValue& value)
00331 {
00332 MarControlValueT<T> &v = dynamic_cast<MarControlValueT<T>&>(value);
00333 value_ = v.value_;
00334 }
00335
00336 template<class T>
00337 void
00338 MarControlValueT<T>::callMarSystemsUpdate()
00339 {
00340
00341
00342
00343
00344
00345
00346 tempValue_ = value_;
00347
00348
00349
00350 for(lit_ = links_.begin(); lit_ != links_.end(); ++lit_)
00351 {
00352 value_ = tempValue_;
00353 current_link_ = lit_->first;
00354 current_link_update();
00355
00356 }
00357
00358
00359 }
00360
00361 template<class T>
00362 MarControlValue*
00363 MarControlValueT<T>::create()
00364 {
00365 return new MarControlValueT<T>(T());
00366 }
00367
00368 template<class T>
00369 std::string
00370 MarControlValueT<T>::getTypeID()
00371 {
00372 return typeid(T).name();
00373 }
00374
00375 inline
00376 std::string
00377 MarControlValueT<realvec>::getTypeID()
00378 {
00379 return typeid(realvec).name();
00380 }
00381
00382 inline
00383 std::string
00384 MarControlValueT<bool>::getTypeID()
00385 {
00386 return typeid(bool).name();
00387 }
00388
00389 template<class T>
00390 void
00391 MarControlValueT<T>::set(T &val, bool update)
00392 {
00393 value_ = val;
00394
00395 setDebugValue();
00396
00397 if(update)
00398 {
00399 callMarSystemsUpdate();
00400 }
00401 }
00402
00403 inline
00404 void
00405 MarControlValueT<realvec>::set(realvec &val, bool update)
00406 {
00407 value_ = val;
00408
00409 setDebugValue();
00410
00411 if(update)
00412 {
00413 callMarSystemsUpdate();
00414 }
00415 }
00416
00417 inline
00418 void
00419 MarControlValueT<bool>::set(bool &val, bool update)
00420 {
00421 value_ = val;
00422
00423 setDebugValue();
00424
00425
00426 if(update)
00427 callMarSystemsUpdate();
00428 }
00429
00430 template<class T>
00431 const T&
00432 MarControlValueT<T>::get() const
00433 {
00434 return value_;
00435 }
00436
00437
00438
00439
00440 template<class T>
00441 bool
00442 MarControlValueT<T>::isNotEqual(MarControlValue *v)
00443 {
00444 if(this != v)
00445 {
00446 if (type_ != v->getType())
00447 {
00448 std::ostringstream sstr;
00449 sstr << "MarControlValueT::isNotEqual() - Trying to compare different types of MarControlValue. "
00450 << "(" << this->getType() << " with " << v->getType() << ")";
00451 MRSWARN(sstr.str());
00452 return false;
00453 }
00454
00455 return (value_ != dynamic_cast<MarControlValueT<T>*>(v)->get());
00456 }
00457 else
00458 return false;
00459 }
00460
00461 template<class T>
00462 MarControlValue*
00463 MarControlValueT<T>::sum(MarControlValue *v)
00464 {
00465 MarControlValueT<T> *ptr = dynamic_cast<MarControlValueT<T>*>(v);
00466 if(!ptr)
00467 {
00468 std::ostringstream sstr;
00469 sstr << "MarControlValueT::sum() - Trying to sum different types of MarControlValue. "
00470 << "(" << this->getType() << " with " << v->getType() << ")";
00471 MRSWARN(sstr.str());
00472 return NULL;
00473 }
00474 return new MarControlValueT<T>(value_+ptr->value_);
00475 }
00476
00477 template<class T>
00478 MarControlValue*
00479 MarControlValueT<T>::subtract(MarControlValue *v)
00480 {
00481 MarControlValueT<T> *ptr = dynamic_cast<MarControlValueT<T>*>(v);
00482 if(!ptr)
00483 {
00484 std::ostringstream sstr;
00485 sstr << "MarControlValueT::subtract() - Trying to subtract different types of MarControlValue. "
00486 << "(" << this->getType() << " with " << v->getType() << ")";
00487 MRSWARN(sstr.str());
00488 return NULL;
00489 }
00490 return new MarControlValueT<T>(value_-ptr->value_);
00491 }
00492
00493 template<class T>
00494 MarControlValue*
00495 MarControlValueT<T>::multiply(MarControlValue *v)
00496 {
00497 MarControlValueT<T> *ptr = dynamic_cast<MarControlValueT<T>*>(v);
00498 if(!ptr)
00499 {
00500 std::ostringstream sstr;
00501 sstr << "MarControlValueT::multiply() - Trying to multiply different types of MarControlValue. "
00502 << "(" << this->getType() << " with " << v->getType() << ")";
00503 MRSWARN(sstr.str());
00504 return NULL;
00505 }
00506 return new MarControlValueT<T>(value_*ptr->value_);
00507 }
00508
00509 template<class T>
00510 MarControlValue*
00511 MarControlValueT<T>::divide(MarControlValue *v)
00512 {
00513 MarControlValueT<T> *ptr = dynamic_cast<MarControlValueT<T>*>(v);
00514 if(!ptr)
00515 {
00516 std::ostringstream sstr;
00517 sstr << "[MarControlValueT::divide] Trying to divide different types of MarControlValue. "
00518 << "(" << this->getType() << " with " << v->getType() << ")";
00519 MRSWARN(sstr.str());
00520 return NULL;
00521 }
00522 return new MarControlValueT<T>(value_/ptr->value_);
00523 }
00524
00525
00526
00527
00528
00529 template<class T>
00530 void
00531 MarControlValueT<T>::createFromStream(std::istream& in)
00532 {
00533 in >> value_;
00534
00535
00536 setDebugValue();
00537
00538
00539
00540 }
00541
00542
00543 template<class T>
00544 std::ostream&
00545 MarControlValueT<T>::serialize(std::ostream& os)
00546 {
00547 os << value_;
00548 return os;
00549 }
00550
00551 }
00552
00553 #endif