bpp-core3  3.0.0
Parameter.h
Go to the documentation of this file.
1 //
2 // File: Parameter.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-10-15 15:40:47
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_PARAMETER_H
42 #define BPP_NUMERIC_PARAMETER_H
43 
44 
45 #include "../Clonable.h"
46 #include "Constraints.h"
47 #include "ParameterExceptions.h"
48 
49 // From the STL:
50 #include <string>
51 #include <iostream>
52 #include <vector>
53 #include <memory>
54 
55 namespace bpp
56 {
57 class Parameter;
58 
60  public virtual Clonable
61 {
62 protected:
64 
65 public:
66  ParameterEvent(Parameter* parameter);
67 
70  {
72  return *this;
73  }
74 
75  ParameterEvent* clone() const { return new ParameterEvent(*this); }
76 
77 public:
78  const Parameter* getParameter() const { return parameter_; }
80 };
81 
90  public virtual Clonable
91 {
92 public:
93  ParameterListener* clone() const = 0;
94 
95 public:
99  virtual const std::string& getId() const = 0;
100 
106  virtual void parameterNameChanged(ParameterEvent& event) = 0;
107 
113  virtual void parameterValueChanged(ParameterEvent& event) = 0;
114 
120  virtual void parameterConstraintChanged(ParameterEvent& event) = 0;
121 };
122 
133 class Parameter :
134  public virtual Clonable
135 {
136 protected:
137  std::string name_; // Parameter name
138  double value_; // Parameter value
139  double precision_; // Precision needed for Parameter value
140  std::shared_ptr<Constraint> constraint_;
141  std::vector<ParameterListener*> listeners_;
142  std::vector<bool> listenerAttach_;
143 
144 public:
145  // Class constructors and destructors:
146 
151 
161  Parameter(const std::string& name, double value, std::shared_ptr<Constraint> constraint = 0, double precision = 0);
162 
163  Parameter(const std::string& name, double value, std::shared_ptr<Constraint> constraint, bool precision) = delete;
164 
168  Parameter(const Parameter& param);
169 
173  Parameter& operator=(const Parameter& param);
174 
175  virtual ~Parameter();
176 
177  Parameter* clone() const { return new Parameter(*this); }
178 
179 public:
185  virtual void setName(const std::string& name)
186  {
187  name_ = name;
188  ParameterEvent event(this);
190  }
191 
197  virtual void setValue(double value);
198 
204  void setPrecision(double precision);
205 
211  virtual const std::string& getName() const { return name_; }
212 
218  virtual double getValue() const { return value_; }
219 
225  virtual double getPrecision() const { return precision_; }
226 
233  virtual const std::shared_ptr<Constraint> getConstraint() const { return constraint_; }
234 
242  virtual std::shared_ptr<Constraint> getConstraint() { return constraint_; }
243 
249  virtual bool hasConstraint() const { return constraint_ != 0; }
250 
259  virtual std::shared_ptr<Constraint> removeConstraint();
260 
267  virtual void setConstraint(std::shared_ptr<Constraint> constraint);
268 
278  virtual void addParameterListener(ParameterListener* listener, bool attachListener = true)
279  {
280  listeners_.push_back(listener);
281  listenerAttach_.push_back(attachListener);
282  }
283 
289  virtual void removeParameterListener(const std::string& listenerId);
290 
297  virtual bool hasParameterListener(const std::string& listenerId);
298 
299 protected:
301  {
302  for (std::vector<ParameterListener*>::iterator it = listeners_.begin(); it != listeners_.end(); it++)
303  {
304  (*it)->parameterNameChanged(event);
305  }
306  }
307 
309  {
310  for (std::vector<ParameterListener*>::iterator it = listeners_.begin(); it != listeners_.end(); it++)
311  {
312  (*it)->parameterValueChanged(event);
313  }
314  }
315 
317  {
318  for (std::vector<ParameterListener*>::iterator it = listeners_.begin(); it != listeners_.end(); it++)
319  {
320  (*it)->parameterConstraintChanged(event);
321  }
322  }
323 
324 public:
325  static const std::shared_ptr<IntervalConstraint> R_PLUS;
326  static const std::shared_ptr<IntervalConstraint> R_PLUS_STAR;
327  static const std::shared_ptr<IntervalConstraint> R_MINUS;
328  static const std::shared_ptr<IntervalConstraint> R_MINUS_STAR;
329  static const std::shared_ptr<IntervalConstraint> PROP_CONSTRAINT_IN;
330  static const std::shared_ptr<IntervalConstraint> PROP_CONSTRAINT_EX;
331 };
332 } // end of namespace bpp.
333 #endif // BPP_NUMERIC_PARAMETER_H
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
Parameter * parameter_
Definition: Parameter.h:63
ParameterEvent * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:75
ParameterEvent & operator=(const ParameterEvent &pe)
Definition: Parameter.h:69
ParameterEvent(const ParameterEvent &pe)
Definition: Parameter.h:68
const Parameter * getParameter() const
Definition: Parameter.h:78
ParameterEvent(Parameter *parameter)
Definition: Parameter.cpp:55
Parameter * getParameter()
Definition: Parameter.h:79
The parameter listener interface.
Definition: Parameter.h:91
ParameterListener * clone() const =0
Create a copy of this object and send a pointer to it.
virtual void parameterNameChanged(ParameterEvent &event)=0
Notify a renaming action.
virtual const std::string & getId() const =0
virtual void parameterValueChanged(ParameterEvent &event)=0
Notify a value change.
virtual void parameterConstraintChanged(ParameterEvent &event)=0
Notify a constraint change.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void removeParameterListener(const std::string &listenerId)
Remove all listeners with a given id from this parameter.
Definition: Parameter.cpp:149
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:110
virtual std::shared_ptr< Constraint > removeConstraint()
Remove the constraint associated to this parameter.
Definition: Parameter.cpp:140
std::vector< ParameterListener * > listeners_
Definition: Parameter.h:141
virtual const std::shared_ptr< Constraint > getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:233
static const std::shared_ptr< IntervalConstraint > R_PLUS
Definition: Parameter.h:325
static const std::shared_ptr< IntervalConstraint > R_MINUS
Definition: Parameter.h:327
std::vector< bool > listenerAttach_
Definition: Parameter.h:142
std::shared_ptr< Constraint > constraint_
Definition: Parameter.h:140
Parameter(const std::string &name, double value, std::shared_ptr< Constraint > constraint, bool precision)=delete
void fireParameterNameChanged(ParameterEvent &event)
Definition: Parameter.h:300
virtual std::shared_ptr< Constraint > getConstraint()
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:242
virtual bool hasParameterListener(const std::string &listenerId)
Tell is there is a listener with a given id from this parameter.
Definition: Parameter.cpp:165
virtual double getPrecision() const
Get the precision of this parameter.
Definition: Parameter.h:225
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_IN
Definition: Parameter.h:329
virtual ~Parameter()
Definition: Parameter.cpp:99
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:218
virtual void setName(const std::string &name)
Set the name of this parameter.
Definition: Parameter.h:185
Parameter * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:177
virtual void setConstraint(std::shared_ptr< Constraint > constraint)
Set a constraint to this parameter.
Definition: Parameter.cpp:131
void setPrecision(double precision)
Set the precision of this parameter.
Definition: Parameter.cpp:124
static const std::shared_ptr< IntervalConstraint > R_MINUS_STAR
Definition: Parameter.h:328
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_EX
Definition: Parameter.h:330
double precision_
Definition: Parameter.h:139
double value_
Definition: Parameter.h:138
std::string name_
Definition: Parameter.h:137
static const std::shared_ptr< IntervalConstraint > R_PLUS_STAR
Definition: Parameter.h:326
virtual void addParameterListener(ParameterListener *listener, bool attachListener=true)
Add a new listener to this parameter.
Definition: Parameter.h:278
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:211
Parameter & operator=(const Parameter &param)
Assignment operator.
Definition: Parameter.cpp:81
virtual bool hasConstraint() const
Tells if this parameter has a constraint.
Definition: Parameter.h:249
void fireParameterConstraintChanged(ParameterEvent &event)
Definition: Parameter.h:316
void fireParameterValueChanged(ParameterEvent &event)
Definition: Parameter.h:308
Parameter()
Default contructor. Creates a parameter with no name, no constraint, and a value of 0.
Definition: Parameter.h:150