|
bpp-core
2.1.0
|
00001 // 00002 // File: Parameter.h 00003 // Created by: Julien Dutheil 00004 // Created on: Wed Oct 15 15:40:47 2003 00005 // 00006 00007 /* 00008 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004) 00009 00010 This software is a computer program whose purpose is to provide classes 00011 for numerical calculus. 00012 00013 This software is governed by the CeCILL license under French law and 00014 abiding by the rules of distribution of free software. You can use, 00015 modify and/ or redistribute the software under the terms of the CeCILL 00016 license as circulated by CEA, CNRS and INRIA at the following URL 00017 "http://www.cecill.info". 00018 00019 As a counterpart to the access to the source code and rights to copy, 00020 modify and redistribute granted by the license, users are provided only 00021 with a limited warranty and the software's author, the holder of the 00022 economic rights, and the successive licensors have only limited 00023 liability. 00024 00025 In this respect, the user's attention is drawn to the risks associated 00026 with loading, using, modifying and/or developing or reproducing the 00027 software by the user in light of its specific status of free software, 00028 that may mean that it is complicated to manipulate, and that also 00029 therefore means that it is reserved for developers and experienced 00030 professionals having in-depth computer knowledge. Users are therefore 00031 encouraged to load and test the software's suitability as regards their 00032 requirements in conditions enabling the security of their systems and/or 00033 data to be ensured and, more generally, to use and operate it in the 00034 same conditions as regards security. 00035 00036 The fact that you are presently reading this means that you have had 00037 knowledge of the CeCILL license and that you accept its terms. 00038 */ 00039 00040 #ifndef _PARAMETER_H_ 00041 #define _PARAMETER_H_ 00042 00043 #include "ParameterExceptions.h" 00044 #include "Constraints.h" 00045 #include "../Clonable.h" 00046 00047 // From the STL: 00048 #include <string> 00049 #include <iostream> 00050 #include <vector> 00051 00052 namespace bpp 00053 { 00054 00055 class Parameter; 00056 00057 class ParameterEvent: 00058 public virtual Clonable 00059 { 00060 protected: 00061 Parameter* parameter_; 00062 00063 public: 00064 ParameterEvent(Parameter* parameter); 00065 00066 ParameterEvent(const ParameterEvent& pe): parameter_(pe.parameter_) {} 00067 ParameterEvent& operator=(const ParameterEvent& pe) 00068 { 00069 parameter_ = pe.parameter_; 00070 return *this; 00071 } 00072 00073 #ifndef NO_VIRTUAL_COV 00074 ParameterEvent* 00075 #else 00076 Clonable* 00077 #endif 00078 clone() const { return new ParameterEvent(*this); } 00079 00080 public: 00081 const Parameter* getParameter() const { return parameter_; } 00082 Parameter* getParameter() { return parameter_; } 00083 }; 00084 00092 class ParameterListener: 00093 public virtual Clonable 00094 { 00095 public: 00096 #ifndef NO_VIRTUAL_COV 00097 ParameterListener* 00098 #else 00099 Clonable* 00100 #endif 00101 clone() const = 0; 00102 00103 public: 00104 00108 virtual const std::string& getId() const = 0; 00109 00115 virtual void parameterNameChanged(ParameterEvent& event) = 0; 00116 00122 virtual void parameterValueChanged(ParameterEvent& event) = 0; 00123 }; 00124 00135 class Parameter: 00136 public virtual Clonable 00137 { 00138 protected: 00139 std::string name_; //Parameter name 00140 double value_; //Parameter value 00141 double precision_; // Precision needed for Parameter value 00142 Constraint* constraint_; //A constraint on the value 00143 bool attach_; // Tells if the constraint is attached to the Parameter 00144 std::vector<ParameterListener*> listeners_; 00145 std::vector<bool> listenerAttach_; 00146 00147 public: // Class constructors and destructors: 00148 00152 Parameter(): name_(""), value_(0), precision_(0), constraint_(0), attach_(true), listeners_(), listenerAttach_() {} 00166 Parameter(const std::string& name, double value, Constraint* constraint, bool attachConstraint, double precision=0) 00167 throw (ConstraintException); 00168 00178 Parameter(const std::string& name, double value, const Constraint* constraint = 0, double precision=0) 00179 throw (ConstraintException); 00180 00181 00185 Parameter(const Parameter& param); 00186 00190 Parameter& operator=(const Parameter& param); 00191 00192 virtual ~Parameter(); 00193 00194 #ifndef NO_VIRTUAL_COV 00195 Parameter* 00196 #else 00197 Clonable* 00198 #endif 00199 clone() const { return new Parameter(*this); } 00200 00201 public: 00202 00208 virtual void setName(const std::string & name) 00209 { 00210 name_ = name; 00211 ParameterEvent event(this); 00212 fireParameterNameChanged(event); 00213 } 00214 00220 virtual void setValue(double value) throw (ConstraintException); 00221 00227 void setPrecision(double precision); 00228 00234 virtual const std::string& getName() const { return name_; } 00235 00241 virtual double getValue() const { return value_; } 00242 00248 virtual double getPrecision() const { return precision_; } 00249 00255 virtual const Constraint* getConstraint() const { return constraint_; } 00256 00262 virtual Constraint* getConstraint() { return constraint_; } 00263 00269 virtual bool hasConstraint() const { return constraint_ != 0; } 00270 00278 virtual const Constraint* removeConstraint(); 00279 00288 virtual void setConstraint(Constraint* constraint, bool attach = false); 00289 00299 virtual void addParameterListener(ParameterListener* listener, bool attachListener = true) 00300 { 00301 listeners_.push_back(listener); 00302 listenerAttach_.push_back(attachListener); 00303 } 00304 00310 virtual void removeParameterListener(const std::string& listenerId); 00311 00318 virtual bool hasParameterListener(const std::string& listenerId); 00319 00320 protected: 00321 void fireParameterNameChanged(ParameterEvent& event) 00322 { 00323 for(std::vector<ParameterListener *>::iterator it = listeners_.begin(); it != listeners_.end(); it++) 00324 (*it)->parameterNameChanged(event); 00325 } 00326 void fireParameterValueChanged(ParameterEvent& event) 00327 { 00328 for(std::vector<ParameterListener *>::iterator it = listeners_.begin(); it != listeners_.end(); it++) 00329 (*it)->parameterValueChanged(event); 00330 } 00331 00332 public: 00333 static const IntervalConstraint R_PLUS; 00334 static const IntervalConstraint R_PLUS_STAR; 00335 static const IntervalConstraint R_MINUS; 00336 static const IntervalConstraint R_MINUS_STAR; 00337 static const IntervalConstraint PROP_CONSTRAINT_IN; 00338 static const IntervalConstraint PROP_CONSTRAINT_EX; 00339 }; 00340 00341 } //end of namespace bpp. 00342 00343 #endif //_PARAMETER_H_ 00344