bpp-core  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends
AbstractParametrizable.h
Go to the documentation of this file.
1 //
2 // File: AbstractParametrizable.h
3 // Created by: Julien Dutheil
4 // Created on: Sun Mar 29 09:10 2009
5 // Created from file Parametrizable.h
6 //
7 
8 /*
9 Copyright or © or Copr. Bio++ Development Team, (November 19, 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 _ABSTRACTPARAMETRIZABLE_H_
42 #define _ABSTRACTPARAMETRIZABLE_H_
43 
44 #include "Parametrizable.h"
45 
46 //From the STL:
47 #include <map>
48 
49 namespace bpp
50 {
51 
63  public virtual Parametrizable
64 {
65  private:
67  std::string prefix_;
68 
69  public:
70  AbstractParametrizable(const std::string& prefix) : parameters_(), prefix_(prefix) {}
71 
73 
74  public:
75  bool hasParameter(const std::string& name) const { return parameters_.hasParameter(prefix_ + name); }
76 
77  const ParameterList& getParameters() const { return parameters_; }
78 
79  const Parameter& getParameter(const std::string& name) const throw (ParameterNotFoundException)
80  {
81  return parameters_.getParameter(prefix_ + name);
82  }
83 
84  double getParameterValue(const std::string& name) const
86  {
87  return getParameter(name).getValue();
88  }
89 
90  void setAllParametersValues(const ParameterList & parameters)
92  {
94  fireParameterChanged(parameters);
95  }
96 
97  void setParameterValue(const std::string& name, double value)
99  {
100  parameters_.setParameterValue(prefix_ + name, value);
102  }
103 
104  void setParametersValues(const ParameterList& parameters)
106  {
107  parameters_.setParametersValues(parameters);
108  fireParameterChanged(parameters);
109  }
110 
111  bool matchParametersValues(const ParameterList& parameters)
112  throw (ConstraintException)
113  {
114  bool test = parameters_.matchParametersValues(parameters);
115  if (test)
116  fireParameterChanged(parameters);
117  return test;
118  }
119 
120  size_t getNumberOfParameters() const { return parameters_.size(); }
121 
122  void setNamespace(const std::string& prefix);
123 
124  std::string getNamespace() const { return prefix_; }
125 
126  std::string getParameterNameWithoutNamespace(const std::string& name) const;
127 
133  virtual void fireParameterChanged(const ParameterList& parameters) = 0;
134 
135  protected:
136  void addParameter_(Parameter* parameter)
137  {
138  if (parameter)
139  parameters_.addParameter(parameter);
140  }
141 
142  void addParameters_(const ParameterList& parameters)
143  {
144  parameters_.addParameters(parameters);
145  }
146 
147  void deleteParameter_(size_t index) throw (IndexOutOfBoundsException)
148  {
149  if (index >= parameters_.size())
150  throw IndexOutOfBoundsException("AbstractParametrizable::deleteParameter_.", index, 0, parameters_.size() - 1);
152  }
153 
155  {
156  parameters_.reset();
157  }
158 
164  Parameter& getParameter_(const std::string& name) throw (ParameterNotFoundException)
165  {
166  return parameters_.getParameter(prefix_ + name);
167  }
168 
175  {
176  return getParameter_(name);
177  }
183  const Parameter& getParameterWithNamespace_(const std::string& name) const throw (ParameterNotFoundException)
184  {
185  return getParameter(name);
186  }
187 
189  {
190  if (index >= parameters_.size())
191  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
192  return parameters_[index];
193  }
194  const Parameter& getParameter_(size_t index) const throw (IndexOutOfBoundsException)
195  {
196  if(index >= parameters_.size())
197  throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
198  return parameters_[index];
199  }
200 
202 };
203 
204 } //end of namespace bpp.
205 
206 #endif //_ABSTRACTPARAMETRIZABLE_H_
207