bpp-core  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends
Parameter.h
Go to the documentation of this file.
1 //
2 // File: Parameter.h
3 // Created by: Julien Dutheil
4 // Created on: Wed Oct 15 15:40:47 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _PARAMETER_H_
41 #define _PARAMETER_H_
42 
43 #include "ParameterExceptions.h"
44 #include "Constraints.h"
45 #include "../Clonable.h"
46 
47 // From the STL:
48 #include <string>
49 #include <iostream>
50 #include <vector>
51 
52 namespace bpp
53 {
54 
55  class Parameter;
56 
58  public virtual Clonable
59  {
60  protected:
62 
63  public:
64  ParameterEvent(Parameter* parameter);
65 
68  {
70  return *this;
71  }
72 
73 #ifndef NO_VIRTUAL_COV
75 #else
76  Clonable*
77 #endif
78  clone() const { return new ParameterEvent(*this); }
79 
80  public:
81  const Parameter* getParameter() const { return parameter_; }
83  };
84 
93  public virtual Clonable
94  {
95  public:
96 #ifndef NO_VIRTUAL_COV
98 #else
99  Clonable*
100 #endif
101  clone() const = 0;
102 
103  public:
104 
108  virtual const std::string& getId() const = 0;
109 
115  virtual void parameterNameChanged(ParameterEvent& event) = 0;
116 
122  virtual void parameterValueChanged(ParameterEvent& event) = 0;
123  };
124 
135  class Parameter:
136  public virtual Clonable
137  {
138  protected:
139  std::string name_; //Parameter name
140  double value_; //Parameter value
141  double precision_; // Precision needed for Parameter value
142  Constraint* constraint_; //A constraint on the value
143  bool attach_; // Tells if the constraint is attached to the Parameter
144  std::vector<ParameterListener*> listeners_;
145  std::vector<bool> listenerAttach_;
146 
147  public: // Class constructors and destructors:
148 
166  Parameter(const std::string& name, double value, Constraint* constraint, bool attachConstraint, double precision=0)
167  throw (ConstraintException);
168 
178  Parameter(const std::string& name, double value, const Constraint* constraint = 0, double precision=0)
179  throw (ConstraintException);
180 
181 
185  Parameter(const Parameter& param);
186 
190  Parameter& operator=(const Parameter& param);
191 
192  virtual ~Parameter();
193 
194 #ifndef NO_VIRTUAL_COV
195  Parameter*
196 #else
197  Clonable*
198 #endif
199  clone() const { return new Parameter(*this); }
200 
201  public:
202 
208  virtual void setName(const std::string & name)
209  {
210  name_ = name;
211  ParameterEvent event(this);
213  }
214 
220  virtual void setValue(double value) throw (ConstraintException);
221 
227  void setPrecision(double precision);
228 
234  virtual const std::string& getName() const { return name_; }
235 
241  virtual double getValue() const { return value_; }
242 
248  virtual double getPrecision() const { return precision_; }
249 
255  virtual const Constraint* getConstraint() const { return constraint_; }
256 
262  virtual Constraint* getConstraint() { return constraint_; }
263 
269  virtual bool hasConstraint() const { return constraint_ != 0; }
270 
278  virtual const Constraint* removeConstraint();
279 
288  virtual void setConstraint(Constraint* constraint, bool attach = false);
289 
299  virtual void addParameterListener(ParameterListener* listener, bool attachListener = true)
300  {
301  listeners_.push_back(listener);
302  listenerAttach_.push_back(attachListener);
303  }
304 
310  virtual void removeParameterListener(const std::string& listenerId);
311 
318  virtual bool hasParameterListener(const std::string& listenerId);
319 
320  protected:
322  {
323  for(std::vector<ParameterListener *>::iterator it = listeners_.begin(); it != listeners_.end(); it++)
324  (*it)->parameterNameChanged(event);
325  }
327  {
328  for(std::vector<ParameterListener *>::iterator it = listeners_.begin(); it != listeners_.end(); it++)
329  (*it)->parameterValueChanged(event);
330  }
331 
332  public:
339  };
340 
341 } //end of namespace bpp.
342 
343 #endif //_PARAMETER_H_
344