bpp-core3  3.0.0
Parameter.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_PARAMETER_H
6 #define BPP_NUMERIC_PARAMETER_H
7 
8 
9 #include "../Clonable.h"
10 #include "Constraints.h"
11 #include "ParameterExceptions.h"
12 
13 // From the STL:
14 #include <string>
15 #include <iostream>
16 #include <vector>
17 #include <memory>
18 
19 namespace bpp
20 {
21 class Parameter;
22 
24  public virtual Clonable
25 {
26 protected:
28 
29 public:
31 
32  ParameterEvent(const ParameterEvent& pe) : parameter_(pe.parameter_) {}
34  {
35  parameter_ = pe.parameter_;
36  return *this;
37  }
38 
39  ParameterEvent* clone() const { return new ParameterEvent(*this); }
40 
41 public:
42  const Parameter* parameter() const { return parameter_; }
44 };
45 
54  public virtual Clonable
55 {
56 public:
57  ParameterListener* clone() const = 0;
58 
59 public:
63  virtual const std::string& getId() const = 0;
64 
70  virtual void parameterNameChanged(ParameterEvent& event) = 0;
71 
77  virtual void parameterValueChanged(ParameterEvent& event) = 0;
78 
84  virtual void parameterConstraintChanged(ParameterEvent& event) = 0;
85 };
86 
97 class Parameter :
98  public virtual Clonable
99 {
100 protected:
101  std::string name_; // Parameter name
102  double value_; // Parameter value
103  double precision_; // Precision needed for Parameter value
104  std::shared_ptr<ConstraintInterface> constraint_;
105  std::vector< std::shared_ptr<ParameterListener>> listeners_;
106 
107 public:
108  // Class constructors and destructors:
109 
113  Parameter() : name_(""), value_(0), precision_(0), constraint_(0), listeners_() {}
114 
124  Parameter(const std::string& name, double value, std::shared_ptr<ConstraintInterface> constraint = 0, double precision = 0);
125 
126  Parameter(const std::string& name, double value, std::shared_ptr<ConstraintInterface> constraint, bool precision) = delete;
127 
131  Parameter(const Parameter& param);
132 
136  Parameter& operator=(const Parameter& param);
137 
138  virtual ~Parameter();
139 
140  Parameter* clone() const { return new Parameter(*this); }
141 
142 public:
148  virtual void setName(const std::string& name)
149  {
150  name_ = name;
151  ParameterEvent event(this);
152  fireParameterNameChanged(event);
153  }
154 
160  virtual void setValue(double value);
161 
167  void setPrecision(double precision);
168 
174  virtual const std::string& getName() const { return name_; }
175 
181  virtual double getValue() const { return value_; }
182 
188  virtual double getPrecision() const { return precision_; }
189 
196  virtual std::shared_ptr<const ConstraintInterface> getConstraint() const { return constraint_; }
197 
203  virtual const ConstraintInterface& constraint() const
204  {
205  if (constraint_)
206  return *constraint_;
207  else
208  throw NullPointerException("Parameter::constraint(). No contraint is associated to this parameter.");
209  }
210 
218  virtual std::shared_ptr<ConstraintInterface> getConstraint() { return constraint_; }
219 
226  {
227  if (constraint_)
228  return *constraint_;
229  else
230  throw NullPointerException("Parameter::constraint(). No contraint is associated to this parameter.");
231  }
232 
238  virtual bool hasConstraint() const { return constraint_ != nullptr; }
239 
247  virtual std::shared_ptr<ConstraintInterface> removeConstraint();
248 
254  virtual void setConstraint(std::shared_ptr<ConstraintInterface> constraint);
255 
261  virtual void addParameterListener(std::shared_ptr<ParameterListener> listener)
262  {
263  listeners_.push_back(listener);
264  }
265 
271  virtual void removeParameterListener(const std::string& listenerId);
272 
279  virtual bool hasParameterListener(const std::string& listenerId);
280 
281 protected:
283  {
284  for (auto listener : listeners_)
285  {
286  listener->parameterNameChanged(event);
287  }
288  }
289 
291  {
292  for (auto listener : listeners_)
293  {
294  listener->parameterValueChanged(event);
295  }
296  }
297 
299  {
300  for (auto listener : listeners_)
301  {
302  listener->parameterConstraintChanged(event);
303  }
304  }
305 
306 public:
307  static const std::shared_ptr<IntervalConstraint> R_PLUS;
308  static const std::shared_ptr<IntervalConstraint> R_PLUS_STAR;
309  static const std::shared_ptr<IntervalConstraint> R_MINUS;
310  static const std::shared_ptr<IntervalConstraint> R_MINUS_STAR;
311  static const std::shared_ptr<IntervalConstraint> PROP_CONSTRAINT_IN;
312  static const std::shared_ptr<IntervalConstraint> PROP_CONSTRAINT_EX;
313 };
314 } // end of namespace bpp.
315 #endif // BPP_NUMERIC_PARAMETER_H
double precision_
Definition: Parameter.h:103
static const std::shared_ptr< IntervalConstraint > R_PLUS
Definition: Parameter.h:307
The parameter listener interface.
Definition: Parameter.h:53
Parameter * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:140
Parameter()
Default contructor. Creates a parameter with no name, no constraint, and a value of 0...
Definition: Parameter.h:113
virtual std::shared_ptr< ConstraintInterface > getConstraint()
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:218
virtual void addParameterListener(std::shared_ptr< ParameterListener > listener)
Add a new listener to this parameter.
Definition: Parameter.h:261
std::vector< std::shared_ptr< ParameterListener > > listeners_
Definition: Parameter.h:105
ParameterEvent(Parameter *parameter)
Definition: Parameter.cpp:20
The constraint interface.
Definition: Constraints.h:28
Parameter * parameter()
Definition: Parameter.h:43
void fireParameterValueChanged(ParameterEvent &event)
Definition: Parameter.h:290
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
Parameter * parameter_
Definition: Parameter.h:27
ParameterEvent(const ParameterEvent &pe)
Definition: Parameter.h:32
const Parameter * parameter() const
Definition: Parameter.h:42
virtual bool hasConstraint() const
Tells if this parameter has a constraint.
Definition: Parameter.h:238
std::string name_
Definition: Parameter.h:101
virtual std::shared_ptr< const ConstraintInterface > getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:196
static const std::shared_ptr< IntervalConstraint > R_MINUS
Definition: Parameter.h:309
static const std::shared_ptr< IntervalConstraint > R_MINUS_STAR
Definition: Parameter.h:310
ParameterEvent * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:39
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_EX
Definition: Parameter.h:312
void fireParameterConstraintChanged(ParameterEvent &event)
Definition: Parameter.h:298
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_IN
Definition: Parameter.h:311
The base class exception for NULL pointer error. This exception may be thrown when an unexpected NULL...
Definition: Exceptions.h:56
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:174
double value_
Definition: Parameter.h:102
virtual void setName(const std::string &name)
Set the name of this parameter.
Definition: Parameter.h:148
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
virtual ConstraintInterface & constraint()
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:225
std::shared_ptr< ConstraintInterface > constraint_
Definition: Parameter.h:104
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:181
virtual const ConstraintInterface & constraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:203
void fireParameterNameChanged(ParameterEvent &event)
Definition: Parameter.h:282
virtual double getPrecision() const
Get the precision of this parameter.
Definition: Parameter.h:188
ParameterEvent & operator=(const ParameterEvent &pe)
Definition: Parameter.h:33
static const std::shared_ptr< IntervalConstraint > R_PLUS_STAR
Definition: Parameter.h:308