00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "MarControl.h"
00024 #include "MarControlValue.h"
00025 #include "MarSystem.h"
00026
00027
00028 using std::ostringstream;
00029 using std::vector;
00030 using std::pair;
00031
00032
00033 namespace Marsyas {
00034
00035
00036 #ifdef TRACECONTROLS
00037 std::set<MarControl*> MarControlPtr::controlTracer;
00038
00039 void
00040 MarControlPtr::printControlTracer()
00041 {
00042 std::set<MarControl*>::iterator it;
00043 if (MarControlPtr::controlTracer.size() > 0)
00044 {
00045 cout << "#############################################################" << endl;
00046 cout << "++ Existing MarControls: " << MarControlPtr::controlTracer.size() << endl;
00047 for (it=MarControlPtr::controlTracer.begin(); it!=MarControlPtr::controlTracer.end(); it++)
00048 {
00049 cout << (*it)->getMarSystem()->getPrefix() << (*it)->getName()
00050 << " | ref.count: " << (*it)->getRefCount() << endl;
00051 }
00052 cout << "#############################################################" << endl;
00053 }
00054 }
00055 #endif
00056
00057 MarControlPtr::MarControlPtr()
00058 {
00059 control_ = NULL;
00060 }
00061
00062 MarControlPtr::~MarControlPtr()
00063 {
00064 if (control_)
00065 {
00066 TRACE_REMCONTROL;
00067 control_->unref();
00068 control_ = NULL;
00069 }
00070 }
00071
00072
00073
00074
00075
00076
00077 WAS_INLINE void MarControl::ref()
00078 {
00079 refCount_++;
00080 }
00081 WAS_INLINE void MarControl::unref()
00082 {
00083 if (--refCount_ <= 0)
00084 delete this;
00085 }
00086 int MarControl::getRefCount() const
00087 {
00088 return refCount_;
00089 }
00090
00091
00092 MarControl*
00093 MarControl::clone()
00094 {
00095 return new MarControl(*this);
00096 }
00097
00098 void
00099 MarControl::setMarSystem(MarSystem* msys)
00100 {
00101 msys_ = msys;
00102 }
00103
00104 MarSystem*
00105 MarControl::getMarSystem()
00106 {
00107 return msys_;
00108 }
00109
00110 void
00111 MarControl::setName(std::string cname)
00112 {
00113 cname_ = cname;
00114 }
00115
00116 std::string
00117 MarControl::getName() const
00118 {
00119 return cname_;
00120 }
00121
00122 void
00123 MarControl::setState(bool state)
00124 {
00125 state_ = state;
00126 }
00127
00128 bool
00129 MarControl::hasState() const
00130 {
00131 return state_;
00132 }
00133
00134 mrs_string
00135 MarControl::getType() const
00136 {
00137 return value_->getType();
00138 }
00139
00140 void
00141 MarControl::callMarSystemUpdate()
00142 {
00143 if (state_ && msys_)
00144 {
00145 MarSystem* msys = msys_;
00146 msys->update(this);
00147 return;
00148 }
00149 }
00150
00151 bool
00152 MarControl::linkTo(MarControlPtr ctrl, bool update)
00153 {
00154 if (ctrl.isInvalid())
00155 {
00156 ostringstream oss;
00157 oss << "MarControl::linkTo() - Linking to an invalid control ";
00158 oss << "(" << ctrl->cname_ << " with " << cname_ << ").";
00159 MRSWARN(oss.str());
00160 return false;
00161 }
00162
00163
00164
00165 if(value_ == ctrl->value_)
00166 {
00167 return true;
00168 }
00169
00170 if (ctrl->value_->type_ != value_->type_)
00171 {
00172 ostringstream oss;
00173 oss << "MarControl::linkTo() - Linking controls of different types ";
00174 oss << "(" << ctrl->cname_ << " with " << cname_ << ").";
00175 MRSWARN(oss.str());
00176 return false;
00177 }
00178
00179
00180
00181 this->unlinkFromTarget();
00182
00183
00184
00185 MarControlValue* oldvalue = value_;
00186
00187 MarControlValue* newvalue = ctrl->value_;
00188
00189
00190
00191 vector<pair<MarControl*, MarControl*> >::iterator lit;
00192 for(lit=oldvalue->links_.begin(); lit!=oldvalue->links_.end(); ++lit)
00193 {
00194
00195 lit->first->value_ = newvalue;
00196
00197
00198 if(lit->first == lit->second)
00199 {
00200
00201 newvalue->links_.push_back(pair<MarControl*, MarControl*>(lit->first, ctrl()));
00202 }
00203 else
00204 newvalue->links_.push_back(*lit);
00205 }
00206
00207
00208 delete oldvalue;
00209
00210
00211 if(update)
00212 value_->callMarSystemsUpdate();
00213
00214 return true;
00215 }
00216
00217
00218 mrs_string
00219 MarControl::to_string() const
00220 {
00221 if(!this)
00222 {
00223 MRSERR("MarControl::to() - trying to get a value from a NULL MarControl! Returning invalid value...");
00224 return "";
00225 }
00226 const MarControlValueT<mrs_string> *ptr = dynamic_cast<const MarControlValueT<mrs_string>*>(value_);
00227 if(ptr)
00228 {
00229 return ptr->get();
00230 }
00231 else
00232 {
00233 MRSERR("MarControl::to() - Incompatible type requested - " << "expected " << value_->getType() << " for control " << this->getName()) ;
00234 return "";
00235 }
00236 }
00237
00238 mrs_natural
00239 MarControl::to_natural() const
00240 {
00241 if(!this)
00242 {
00243 MRSERR("MarControl::to() - trying to get a value from a NULL MarControl! Returning invalid value...");
00244 return 0;
00245 }
00246 const MarControlValueT<mrs_natural> *ptr = dynamic_cast<const MarControlValueT<mrs_natural>*>(value_);
00247 if(ptr)
00248 {
00249 return ptr->get();
00250 }
00251 else
00252 {
00253 MRSERR("MarControl::to() - Incompatible type requested - " << "expected " << value_->getType() << " for control " << this->getName()) ;
00254 return 0;
00255 }
00256 }
00257
00258
00259 mrs_bool
00260 MarControl::to_bool() const
00261 {
00262 if(!this)
00263 {
00264 MRSERR("MarControl::to() - trying to get a value from a NULL MarControl! Returning invalid value...");
00265 return 0;
00266 }
00267 const MarControlValueT<mrs_bool> *ptr = dynamic_cast<const MarControlValueT<mrs_bool>*>(value_);
00268 if(ptr)
00269 {
00270 return ptr->get();
00271 }
00272 else
00273 {
00274 MRSERR("MarControl::to() - Incompatible type requested - " << "expected " << value_->getType() << " for control " << this->getName()) ;
00275 return 0;
00276 }
00277 }
00278
00279 mrs_real
00280 MarControl::to_real() const
00281 {
00282 if(!this)
00283 {
00284 MRSERR("MarControl::to() - trying to get a value from a NULL MarControl! Returning invalid value...");
00285 return 0.0;
00286 }
00287 const MarControlValueT<mrs_real> *ptr = dynamic_cast<const MarControlValueT<mrs_real>*>(value_);
00288 if(ptr)
00289 {
00290 return ptr->get();
00291 }
00292 else
00293 {
00294 MRSERR("MarControl::to() - Incompatible type requested - " << "expected " << value_->getType() << " for control " << this->getName()) ;
00295 return 0.0;
00296 }
00297 }
00298
00299 mrs_realvec
00300 MarControl::to_realvec() const
00301 {
00302 if(!this)
00303 {
00304 MRSERR("MarControl::to() - trying to get a value from a NULL MarControl! Returning invalid value...");
00305 realvec s;
00306 return s;
00307 }
00308 const MarControlValueT<mrs_realvec> *ptr = dynamic_cast<const MarControlValueT<mrs_realvec>*>(value_);
00309 if(ptr)
00310 {
00311 return ptr->get();
00312 }
00313 else
00314 {
00315 MRSERR("MarControl::to() - Incompatible type requested - " << "expected " << value_->getType() << " for control " << this->getName()) ;
00316 realvec s;
00317 return s;
00318 }
00319 }
00320
00321
00322
00323 void
00324 MarControl::unlinkFromAll()
00325 {
00326
00327
00328 this->unlinkFromTarget();
00329
00330
00331 vector<pair<MarControl*, MarControl*> >::iterator lit;
00332 vector<MarControl*> linkedControls;
00333 for(lit=value_->links_.begin(); lit!=value_->links_.end(); ++lit)
00334 {
00335 if(lit->second == this && lit->first != lit->second)
00336 linkedControls.push_back(lit->first);
00337 }
00338
00339 for(mrs_natural i=0; i < (mrs_natural)linkedControls.size(); ++i)
00340 linkedControls[i]->unlinkFromTarget();
00341 }
00342
00343 void
00344 MarControl::unlinkFromTarget()
00345 {
00346 vector<pair<MarControl*, MarControl*> >::iterator lit;
00347
00348
00349
00350
00351
00352 if(value_->links_.size() <= 1)
00353 return;
00354
00355 MarControlValue* oldvalue = value_;
00356 MarControlValue* newvalue = oldvalue->clone();
00357
00358 vector<pair<MarControl*, MarControl*> >* inSet = new vector<pair<MarControl*, MarControl*> >;
00359 vector<pair<MarControl*, MarControl*> >* outSet = new vector<pair<MarControl*, MarControl*> >;
00360
00361 mrs_natural toProcess = oldvalue->links_.size();
00362 bool* processed = new bool[oldvalue->links_.size()];
00363 for(mrs_natural i=0; i < (mrs_natural)oldvalue->links_.size(); ++i)
00364 processed[i] = false;
00365
00366
00367 MarControl* oldRootLink = NULL;
00368
00369 lit = oldvalue->links_.begin();
00370 mrs_natural idx = 0;
00371 while(toProcess > 0)
00372 {
00373
00374 if(!processed[idx])
00375 {
00376
00377 if(lit->first == lit->second)
00378 {
00379 oldRootLink = lit->first;
00380 outSet->push_back(*lit);
00381 toProcess --;
00382 processed[idx]=true;
00383 }
00384
00385
00386 else if(lit->first == this)
00387 {
00388 lit->first->value_ = newvalue;
00389 inSet->push_back(pair<MarControl*, MarControl*>(lit->first, lit->first));
00390 toProcess--;
00391 processed[idx]=true;
00392 }
00393
00394
00395 else if(lit->second == oldRootLink)
00396 {
00397 outSet->push_back(*lit);
00398 toProcess --;
00399 processed[idx]=true;
00400 }
00401
00402 else if(lit->second == this)
00403 {
00404 lit->first->value_ = newvalue;
00405 inSet->push_back(*lit);
00406 toProcess--;
00407 processed[idx]=true;
00408 }
00409
00410
00411
00412 else
00413 {
00414 bool found = false;
00415 vector<pair<MarControl*, MarControl*> >::iterator sit;
00416
00417
00418
00419 vector<pair<MarControl*, MarControl*> > inSet2 = *inSet;
00420
00421 for(sit = inSet2.begin(); sit != inSet2.end(); ++sit)
00422 {
00423 if(lit->second == sit->first)
00424 {
00425 lit->first->value_ = newvalue;
00426 inSet->push_back(*lit);
00427 toProcess--;
00428 processed[idx]=true;
00429 found = true;
00430 }
00431 }
00432
00433 if(!found)
00434 {
00435 vector<pair<MarControl*, MarControl*> > outSet2 = *outSet;
00436 for(sit = outSet2.begin(); sit != outSet2.end(); ++sit)
00437 {
00438 if(lit->second == sit->first)
00439 {
00440 outSet->push_back(*lit);
00441 toProcess--;
00442 processed[idx]=true;
00443 }
00444 }
00445 }
00446 }
00447 }
00448
00449
00450 if(lit!=oldvalue->links_.end())
00451 {
00452 lit++;
00453 idx++;
00454 }
00455 else
00456 {
00457 lit=oldvalue->links_.begin();
00458 idx = 0;
00459 }
00460 }
00461
00462 delete [] processed;
00463
00464
00465 oldvalue->links_ = *outSet;
00466 newvalue->links_ = *inSet;
00467
00468 delete inSet;
00469 delete outSet;
00470
00471
00472
00473 if(newvalue->links_.size() == 0)
00474 delete newvalue;
00475 }
00476
00477 bool
00478 MarControl::isLinked() const
00479 {
00480
00481
00482
00483 if(value_->links_.size()-1 == 0)
00484 return false;
00485 else
00486 return true;
00487 }
00488
00489 vector<pair<MarControlPtr, MarControlPtr> >
00490 MarControl::getLinks()
00491 {
00492 vector<pair<MarControlPtr, MarControlPtr> > res;
00493 vector<pair<MarControl*, MarControl*> >::const_iterator lit;
00494 for(lit=value_->links_.begin(); lit != value_->links_.end(); ++lit)
00495 {
00496 res.push_back(pair<MarControlPtr, MarControlPtr>(MarControlPtr(lit->first),MarControlPtr(lit->second)));
00497 }
00498 return res;
00499 }
00500
00501
00502
00503 WAS_INLINE MarControlPtr::MarControlPtr(MarControl control)
00504 {
00505 control_ = new MarControl(control);
00506 control_->ref();
00507 TRACE_ADDCONTROL;
00508 }
00509
00510 WAS_INLINE MarControlPtr::MarControlPtr(MarControlValue *value)
00511 {
00512 control_ = new MarControl(value);
00513 control_->ref();
00514 TRACE_ADDCONTROL;
00515 }
00516
00517 WAS_INLINE MarControlPtr::MarControlPtr(int ne)
00518 {
00519 control_ = new MarControl((mrs_natural)ne);
00520 control_->ref();
00521 TRACE_ADDCONTROL;
00522 }
00523
00524 WAS_INLINE MarControlPtr::MarControlPtr(unsigned int ne)
00525 {
00526 control_ = new MarControl((mrs_natural)ne);
00527 control_->ref();
00528 TRACE_ADDCONTROL;
00529 }
00530
00531 WAS_INLINE MarControlPtr::MarControlPtr(float ne)
00532 {
00533 control_ = new MarControl(ne);
00534 control_->ref();
00535 TRACE_ADDCONTROL;
00536 }
00537
00538
00539 WAS_INLINE MarControlPtr::MarControlPtr(mrs_natural ne)
00540 {
00541 control_ = new MarControl(ne);
00542 control_->ref();
00543 TRACE_ADDCONTROL;
00544 }
00545
00546 WAS_INLINE MarControlPtr::MarControlPtr(double re)
00547 {
00548 control_ = new MarControl(re);
00549 control_->ref();
00550 TRACE_ADDCONTROL;
00551 }
00552
00553 WAS_INLINE MarControlPtr::MarControlPtr(const char *c)
00554 {
00555 control_ = new MarControl(std::string(c));
00556 control_->ref();
00557 TRACE_ADDCONTROL;
00558 }
00559
00560 WAS_INLINE MarControlPtr::MarControlPtr(std::string st)
00561 {
00562 control_ = new MarControl(st);
00563 control_->ref();
00564 TRACE_ADDCONTROL;
00565 }
00566
00567 WAS_INLINE MarControlPtr::MarControlPtr(mrs_bool be)
00568 {
00569 control_ = new MarControl(be);
00570 control_->ref();
00571 TRACE_ADDCONTROL;
00572 }
00573
00574 WAS_INLINE MarControlPtr::MarControlPtr(realvec ve)
00575 {
00576 control_ = new MarControl(ve);
00577 control_->ref();
00578 TRACE_ADDCONTROL;
00579 }
00580
00581 WAS_INLINE std::ostream& operator<<(std::ostream& os, const MarControlPtr& ctrl)
00582 {
00583 return (os << (*ctrl.control_));
00584 }
00585
00586 WAS_INLINE bool operator==(const MarControlPtr& v1, const MarControlPtr& v2)
00587 {
00588 return (*v1.control_) == (*v2.control_);
00589 }
00590
00591 WAS_INLINE bool operator!=(const MarControlPtr& v1, const MarControlPtr& v2)
00592 {
00593 return (*v1.control_) != (*v2.control_);
00594 }
00595
00596 WAS_INLINE mrs_real operator+(const MarControlPtr& v1, const mrs_real& v2)
00597 {
00598 return (*v1.control_)+v2;
00599 }
00600
00601 WAS_INLINE mrs_real operator+(const mrs_real& v1, const MarControlPtr& v2)
00602 {
00603 return v1+(*v2.control_);
00604 }
00605
00606 WAS_INLINE mrs_real operator-(const MarControlPtr& v1, const mrs_real& v2)
00607 {
00608 return (*v1.control_)-v2;
00609 }
00610
00611 WAS_INLINE mrs_real operator-(const mrs_real& v1, const MarControlPtr& v2)
00612 {
00613 return v1-(*v2.control_);
00614 }
00615
00616 WAS_INLINE mrs_real operator*(const MarControlPtr& v1, const mrs_real& v2)
00617 {
00618 return (*v1.control_)*v2;
00619 }
00620 WAS_INLINE mrs_real operator*(const mrs_real& v1, const MarControlPtr& v2)
00621 {
00622 return v1*(*v2.control_);
00623 }
00624
00625 WAS_INLINE mrs_real operator/(const MarControlPtr& v1, const mrs_real& v2)
00626 {
00627 return (*v1.control_)/v2;
00628 }
00629
00630 WAS_INLINE mrs_real operator/(const mrs_real& v1, const MarControlPtr& v2)
00631 {
00632 return v1/(*v2.control_);
00633 }
00634
00635 WAS_INLINE MarControlPtr operator+(const MarControlPtr& v1, const MarControlPtr& v2)
00636 {
00637 return (*v1.control_)+(*v2.control_);
00638 }
00639
00640 WAS_INLINE MarControlPtr operator-(const MarControlPtr& v1, const MarControlPtr& v2)
00641 {
00642 return (*v1.control_)-(*v2.control_);
00643 }
00644
00645 WAS_INLINE MarControlPtr operator*(const MarControlPtr& v1, const MarControlPtr& v2)
00646 {
00647 return (*v1.control_)*(*v2.control_);
00648 }
00649
00650 WAS_INLINE MarControlPtr operator/(const MarControlPtr& v1, const MarControlPtr& v2)
00651 {
00652 return (*v1.control_)/(*v2.control_);
00653 }
00654
00655 bool operator<(const MarControlPtr& v1, const MarControlPtr& v2)
00656 {
00657 return v1.control_ < v2.control_;
00658 }
00659
00660
00661 MarControlPtr::MarControlPtr(const MarControlPtr& a)
00662 {
00663 control_ = a.control_;
00664 if (control_)
00665 {
00666 control_->ref();
00667 TRACE_ADDCONTROL;
00668 }
00669 }
00670
00671 WAS_INLINE
00672 MarControlPtr::MarControlPtr(MarControl *control)
00673 {
00674 control_ = control;
00675 if (control_)
00676 {
00677 control_->ref();
00678 TRACE_ADDCONTROL;
00679 }
00680 }
00681
00682 WAS_INLINE
00683 MarControlPtr&
00684 MarControlPtr::operator=(const MarControlPtr& a)
00685 {
00686 if (control_)
00687 {
00688 TRACE_REMCONTROL;
00689 control_->unref();
00690 }
00691 control_ = a.control_;
00692 if (control_)
00693 {
00694 control_->ref();
00695 TRACE_ADDCONTROL;
00696 }
00697 return *this;
00698 }
00699
00700 WAS_INLINE
00701 bool
00702 MarControlPtr::isInvalid() const
00703 {
00704 return (control_== NULL);
00705 }
00706
00707 WAS_INLINE
00708 bool
00709 MarControlPtr::isEqual(const MarControlPtr& p)
00710 {
00711 return (control_ == p.control_);
00712 }
00713
00714
00715
00716
00717 MarControl::MarControl() :
00718 refCount_(0),
00719 value_(NULL),
00720 msys_(NULL),
00721 state_(false)
00722 {
00723
00724
00725 }
00726
00727
00728 WAS_INLINE
00729 MarControl::MarControl(const MarControl& a)
00730 {
00731 refCount_ = 0;
00732 msys_ = a.msys_;
00733 cname_ = a.cname_;
00734 state_ = a.state_;
00735 desc_ = a.desc_;
00736 value_ = a.value_->clone();
00737 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00738 }
00739
00740 WAS_INLINE
00741 MarControl::MarControl(MarControlValue *value, std::string cname, MarSystem* msys, bool state)
00742 {
00743 refCount_ = 0;
00744 msys_ = msys;
00745 cname_ = cname;
00746 state_ = state;
00747 desc_ = "";
00748 value_ = value->clone();
00749 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00750 }
00751
00752 WAS_INLINE
00753 MarControl::MarControl(double re, std::string cname, MarSystem* msys, bool state)
00754 {
00755 refCount_ = 0;
00756 msys_ = msys;
00757 cname_ = cname;
00758 state_ = state;
00759 desc_ = "";
00760 value_ = new MarControlValueT<mrs_real>(re);
00761 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00762 }
00763
00764
00765 WAS_INLINE
00766 MarControl::MarControl(float re, std::string cname, MarSystem* msys, bool state)
00767 {
00768 refCount_ = 0;
00769 msys_ = msys;
00770 cname_ = cname;
00771 state_ = state;
00772 desc_ = "";
00773 value_ = new MarControlValueT<mrs_real>(re);
00774 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00775 }
00776
00777
00778 WAS_INLINE
00779 MarControl::MarControl(mrs_natural ne, std::string cname, MarSystem* msys, bool state)
00780 {
00781 refCount_ = 0;
00782 msys_ = msys;
00783 cname_ = cname;
00784 state_ = state;
00785 desc_ = "";
00786 value_ = new MarControlValueT<mrs_natural>(ne);
00787 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00788 }
00789
00790 WAS_INLINE
00791 MarControl::MarControl(std::string st, std::string cname, MarSystem* msys, bool state)
00792 {
00793 refCount_ = 0;
00794 msys_ = msys;
00795 cname_ = cname;
00796 state_ = state;
00797 desc_ = "";
00798 value_ = new MarControlValueT<std::string>(st);
00799 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00800 }
00801
00802 WAS_INLINE
00803 MarControl::MarControl(mrs_bool be, std::string cname, MarSystem* msys, bool state)
00804 {
00805 refCount_ = 0;
00806 msys_ = msys;
00807 cname_ = cname;
00808 state_ = state;
00809 desc_ = "";
00810 value_ = new MarControlValueT<bool>(be);
00811 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00812 }
00813
00814 WAS_INLINE
00815 MarControl::MarControl(realvec& ve, std::string cname, MarSystem* msys, bool state)
00816 {
00817 refCount_ = 0;
00818 msys_ = msys;
00819 cname_ = cname;
00820 state_ = state;
00821 desc_ = "";
00822 value_ = new MarControlValueT<realvec>(ve);
00823 value_->links_.push_back(std::pair<MarControl*, MarControl*>(this, this));
00824 }
00825
00826 WAS_INLINE
00827 MarControl::~MarControl()
00828 {
00829
00830 this->unlinkFromAll();
00831
00832 delete value_;
00833 value_ = NULL;
00834 }
00835
00836 WAS_INLINE
00837 MarControl&
00838 MarControl::operator=(const MarControl& a)
00839 {
00840 if (this != &a)
00841 this->setValue(a.value_);
00842
00843 return *this;
00844 }
00845
00846
00847
00848 WAS_INLINE
00849 bool
00850 MarControl::setValue(MarControlPtr mc, bool update)
00851 {
00852 if (value_->type_ != mc->value_->type_)
00853 {
00854 std::ostringstream sstr;
00855 sstr << "MarControl::setValue() - Trying to set value of incompatible type "
00856 << "(expected " << value_->type_ << ", given " << mc->value_->type_ << ")";
00857 MRSWARN(sstr.str());
00858 return false;
00859 }
00860
00861 if (MarControlPtr(this) == mc)
00862 {
00863 return true;
00864 }
00865
00866 value_->copyValue(*(mc->value_));
00867
00868 #ifdef MARSYAS_TRACECONTROLS
00869 value_->setDebugValue();
00870 #endif
00871
00872
00873 if(update)
00874 value_->callMarSystemsUpdate();
00875
00876 return true;
00877 }
00878
00879 WAS_INLINE
00880 bool
00881 MarControl::setValue(MarControlValue *mcv, bool update)
00882 {
00883 if (value_->type_ != mcv->type_)
00884 {
00885 std::ostringstream sstr;
00886 sstr << "MarControl::setValue() - Trying to set value of incompatible type "
00887 << "(expected " << value_->type_ << ", given " << mcv->type_ << ")";
00888 MRSWARN(sstr.str());
00889 return false;
00890 }
00891
00892 if (!mcv->isNotEqual(value_))
00893 {
00894 return true;
00895 }
00896
00897 value_->copyValue(*(mcv));
00898
00899 #ifdef MARSYAS_TRACECONTROLS
00900 value_->setDebugValue();
00901 #endif
00902
00903
00904 if(update)
00905 value_->callMarSystemsUpdate();
00906
00907 return true;
00908 }
00909
00910 WAS_INLINE
00911 bool
00912 MarControl::setValue(const char *t, bool update)
00913 {
00914 return this->setValue(std::string(t), update);
00915 }
00916
00917 WAS_INLINE
00918 bool
00919 MarControl::setValue(const int t, bool update)
00920 {
00921 return this->setValue((mrs_natural)t, update);
00922 }
00923
00924
00925
00926 WAS_INLINE
00927 std::ostream&
00928 operator<<(std::ostream& os, const MarControl& ctrl)
00929 {
00930 return ctrl.value_->serialize(os);
00931 }
00932
00933 WAS_INLINE
00934 bool
00935 operator==(const MarControl& v1, const MarControl& v2)
00936 {
00937 return !(v1.value_->isNotEqual(v2.value_));
00938 }
00939
00940 WAS_INLINE
00941 bool
00942 operator!=(const MarControl& v1, const MarControl& v2)
00943 {
00944 return v1.value_->isNotEqual(v2.value_);
00945 }
00946
00947 WAS_INLINE
00948 mrs_real
00949 operator+(const MarControl& v1, const mrs_real& v2)
00950 {
00951 mrs_real r1;
00952 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v1.value_);
00953 if(ptr)
00954 {
00955 r1 = ptr->get();
00956 }
00957 else
00958 {
00959 std::ostringstream sstr;
00960 sstr << "MarControl::operator + : Trying to get value of incompatible type "
00961 << "(expected " << v1.getType() << ", given " << typeid(mrs_real).name() << ")";
00962 MRSWARN(sstr.str());
00963 return false;
00964 }
00965 return r1 + v2;
00966 }
00967
00968 WAS_INLINE
00969 mrs_real
00970 operator+(const mrs_real& v1, const MarControl& v2)
00971 {
00972 mrs_real r2;
00973 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v2.value_);
00974 if(ptr)
00975 {
00976 r2 = ptr->get();
00977 }
00978 else
00979 {
00980 std::ostringstream sstr;
00981 sstr << "MarControl::operator + : Trying to get value of incompatible type "
00982 << "(expected " << v2.getType() << ", given " << typeid(mrs_real).name() << ")";
00983 MRSWARN(sstr.str());
00984 return false;
00985 }
00986 return v1 + r2;
00987 }
00988
00989 WAS_INLINE
00990 mrs_real
00991 operator-(const MarControl& v1, const mrs_real& v2)
00992 {
00993 mrs_real r1;
00994 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v1.value_);
00995 if(ptr)
00996 {
00997 r1 = ptr->get();
00998 }
00999 else
01000 {
01001 std::ostringstream sstr;
01002 sstr << "[MarControl::setValue] Trying to get value of incompatible type "
01003 << "(expected " << v1.getType() << ", given " << typeid(mrs_real).name() << ")";
01004 MRSWARN(sstr.str());
01005 return false;
01006 }
01007 return r1 - v2;
01008 }
01009
01010 WAS_INLINE
01011 mrs_real
01012 operator-(const mrs_real& v1, const MarControl& v2)
01013 {
01014 mrs_real r2;
01015 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v2.value_);
01016 if(ptr)
01017 {
01018 r2 = ptr->get();
01019 }
01020 else
01021 {
01022 std::ostringstream sstr;
01023 sstr << "[MarControl::setValue] Trying to get value of incompatible type "
01024 << "(expected " << v2.getType() << ", given " << typeid(mrs_real).name() << ")";
01025 MRSWARN(sstr.str());
01026 return false;
01027 }
01028 return v1 - r2;
01029 }
01030
01031 WAS_INLINE
01032 mrs_real
01033 operator*(const MarControl& v1, const mrs_real& v2)
01034 {
01035 mrs_real r1;
01036 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v1.value_);
01037 if(ptr)
01038 {
01039 r1 = ptr->get();
01040 }
01041 else
01042 {
01043 std::ostringstream sstr;
01044 sstr << "[MarControl::setValue] Trying to get value of incompatible type "
01045 << "(expected " << v1.getType() << ", given " << typeid(mrs_real).name() << ")";
01046 MRSWARN(sstr.str());
01047 return false;
01048 }
01049 return r1 * v2;
01050 }
01051
01052 WAS_INLINE
01053 mrs_real
01054 operator*(const mrs_real& v1, const MarControl& v2)
01055 {
01056 mrs_real r2;
01057 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v2.value_);
01058 if(ptr)
01059 {
01060 r2 = ptr->get();
01061 }
01062 else
01063 {
01064 std::ostringstream sstr;
01065 sstr << "[MarControl::setValue] Trying to get value of incompatible type "
01066 << "(expected " << v2.getType() << ", given " << typeid(mrs_real).name() << ")";
01067 MRSWARN(sstr.str());
01068 return false;
01069 }
01070 return v1 * r2;
01071 }
01072
01073 WAS_INLINE
01074 mrs_real
01075 operator/(const MarControl& v1, const mrs_real& v2)
01076 {
01077 mrs_real r1;
01078 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v1.value_);
01079 if(ptr)
01080 {
01081 r1 = ptr->get();
01082 }
01083 else
01084 {
01085 std::ostringstream sstr;
01086 sstr << "[MarControl::setValue] Trying to get value of incompatible type "
01087 << "(expected " << v1.getType() << ", given " << typeid(mrs_real).name() << ")";
01088 MRSWARN(sstr.str());
01089 return false;
01090 }
01091 return r1 / v2;
01092 }
01093
01094 WAS_INLINE
01095 mrs_real
01096 operator/(const mrs_real& v1, const MarControl& v2)
01097 {
01098 mrs_real r2;
01099 MarControlValueT<mrs_real> *ptr = dynamic_cast<MarControlValueT<mrs_real>*>(v2.value_);
01100 if(ptr)
01101 {
01102 r2 = ptr->get();
01103 }
01104 else
01105 {
01106 std::ostringstream sstr;
01107 sstr << "[MarControl::setValue] Trying to get value of incompatible type "
01108 << "(expected " << v2.getType() << ", given " << typeid(mrs_real).name() << ")";
01109 MRSWARN(sstr.str());
01110 return false;
01111 }
01112 return v1 / r2;
01113 }
01114
01115 WAS_INLINE
01116 MarControl
01117 operator+(const MarControl& v1, const MarControl& v2)
01118 {
01119 MarControlValue *val = v1.value_->sum(v2.value_);
01120 MarControl ret(val);
01121 delete val;
01122 return ret;
01123 }
01124
01125 WAS_INLINE
01126 MarControl
01127 operator-(const MarControl& v1, const MarControl& v2)
01128 {
01129 MarControlValue *val = v1.value_->subtract(v2.value_);
01130 MarControl ret(val);
01131 delete val;
01132 return ret;
01133 }
01134
01135 WAS_INLINE
01136 MarControl
01137 operator*(const MarControl& v1, const MarControl& v2)
01138 {
01139 MarControlValue *val = v1.value_->multiply(v2.value_);
01140 MarControl ret(val);
01141 delete val;
01142 return ret;
01143 }
01144
01145 WAS_INLINE
01146 MarControl
01147 operator/(const MarControl& v1, const MarControl& v2)
01148 {
01149 MarControlValue *val = v1.value_->divide(v2.value_);
01150 MarControl ret(val);
01151 delete val;
01152 return ret;
01153 }
01154
01155
01156 WAS_INLINE
01157 bool
01158 MarControl::isTrue()
01159 {
01160 MarControlValueT<bool> *ptr = dynamic_cast<MarControlValueT<bool>*>(value_);
01161 if(ptr)
01162 {
01163 return ptr->get();
01164 }
01165 else
01166 {
01167 std::ostringstream sstr;
01168 sstr << "MarControl::isTrue() - Trying to get use bool-specific method with " << value_->getType();
01169 MRSWARN(sstr.str());
01170 return false;
01171 }
01172 }
01173
01174
01175 }