bpp-core  2.2.0
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 
287  virtual void setConstraint(Constraint* constraint, bool attach = false);
288 
298  virtual void addParameterListener(ParameterListener* listener, bool attachListener = true)
299  {
300  listeners_.push_back(listener);
301  listenerAttach_.push_back(attachListener);
302  }
303 
309  virtual void removeParameterListener(const std::string& listenerId);
310 
317  virtual bool hasParameterListener(const std::string& listenerId);
318 
319  protected:
321  {
322  for(std::vector<ParameterListener *>::iterator it = listeners_.begin(); it != listeners_.end(); it++)
323  (*it)->parameterNameChanged(event);
324  }
326  {
327  for(std::vector<ParameterListener *>::iterator it = listeners_.begin(); it != listeners_.end(); it++)
328  (*it)->parameterValueChanged(event);
329  }
330 
331  public:
338  };
339 
340 } //end of namespace bpp.
341 
342 #endif //_PARAMETER_H_
343 
virtual void parameterNameChanged(ParameterEvent &event)=0
Notify a renaming action.
const Parameter * getParameter() const
Definition: Parameter.h:81
double precision_
Definition: Parameter.h:141
The parameter listener interface.
Definition: Parameter.h:92
static const IntervalConstraint R_MINUS_STAR
Definition: Parameter.h:335
virtual const Constraint * removeConstraint()
Remove the constraint associated to this parameter.
Definition: Parameter.cpp:163
Parameter * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:199
virtual const Constraint * getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:255
Parameter()
Default contructor. Creates a parameter with no name, no constraint, and a value of 0...
Definition: Parameter.h:152
ParameterEvent(Parameter *parameter)
Definition: Parameter.cpp:53
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:135
This class allows to perform a correspondence analysis.
std::vector< ParameterListener * > listeners_
Definition: Parameter.h:144
void fireParameterValueChanged(ParameterEvent &event)
Definition: Parameter.h:325
static const IntervalConstraint PROP_CONSTRAINT_EX
Definition: Parameter.h:337
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void addParameterListener(ParameterListener *listener, bool attachListener=true)
Add a new listener to this parameter.
Definition: Parameter.h:298
static const IntervalConstraint R_PLUS_STAR
Definition: Parameter.h:333
Parameter * parameter_
Definition: Parameter.h:61
ParameterEvent(const ParameterEvent &pe)
Definition: Parameter.h:66
void setPrecision(double precision)
Set the precision of this parameter.
Definition: Parameter.cpp:136
virtual const std::string & getId() const =0
virtual bool hasConstraint() const
Tells if this parameter has a constraint.
Definition: Parameter.h:269
std::string name_
Definition: Parameter.h:139
virtual void removeParameterListener(const std::string &listenerId)
Remove all listeners with a given id from this parameter.
Definition: Parameter.cpp:172
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:123
Parameter * getParameter()
Definition: Parameter.h:82
ParameterEvent * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:78
static const IntervalConstraint R_MINUS
Definition: Parameter.h:334
virtual void setConstraint(Constraint *constraint, bool attach=false)
Set a constraint to this parameter.
Definition: Parameter.cpp:143
The constraint interface.
Definition: Constraints.h:62
virtual void parameterValueChanged(ParameterEvent &event)=0
Notify a value change.
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:234
double value_
Definition: Parameter.h:140
Constraint * constraint_
Definition: Parameter.h:142
virtual void setName(const std::string &name)
Set the name of this parameter.
Definition: Parameter.h:208
virtual bool hasParameterListener(const std::string &listenerId)
Tell is there is a listener with a given id from this parameter.
Definition: Parameter.cpp:187
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:99
static const IntervalConstraint PROP_CONSTRAINT_IN
Definition: Parameter.h:336
ParameterListener * clone() const =0
Create a copy of this object and send a pointer to it.
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:241
Exception thrown when a value do not match a given constraint.
std::vector< bool > listenerAttach_
Definition: Parameter.h:145
static const IntervalConstraint R_PLUS
Definition: Parameter.h:332
void fireParameterNameChanged(ParameterEvent &event)
Definition: Parameter.h:320
virtual double getPrecision() const
Get the precision of this parameter.
Definition: Parameter.h:248
ParameterEvent & operator=(const ParameterEvent &pe)
Definition: Parameter.h:67
virtual Constraint * getConstraint()
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:262