bpp-core  2.1.0
AbstractParametrizable.h
Go to the documentation of this file.
00001 //
00002 // File: AbstractParametrizable.h
00003 // Created by: Julien Dutheil
00004 // Created on: Sun Mar 29 09:10 2009
00005 // Created from file Parametrizable.h
00006 //
00007 
00008 /*
00009 Copyright or © or Copr. Bio++ Development Team, (November 19, 2004)
00010 
00011 This software is a computer program whose purpose is to provide classes
00012 for numerical calculus.
00013 
00014 This software is governed by the CeCILL  license under French law and
00015 abiding by the rules of distribution of free software.  You can  use, 
00016 modify and/ or redistribute the software under the terms of the CeCILL
00017 license as circulated by CEA, CNRS and INRIA at the following URL
00018 "http://www.cecill.info". 
00019 
00020 As a counterpart to the access to the source code and  rights to copy,
00021 modify and redistribute granted by the license, users are provided only
00022 with a limited warranty  and the software's author,  the holder of the
00023 economic rights,  and the successive licensors  have only  limited
00024 liability. 
00025 
00026 In this respect, the user's attention is drawn to the risks associated
00027 with loading,  using,  modifying and/or developing or reproducing the
00028 software by the user in light of its specific status of free software,
00029 that may mean  that it is complicated to manipulate,  and  that  also
00030 therefore means  that it is reserved for developers  and  experienced
00031 professionals having in-depth computer knowledge. Users are therefore
00032 encouraged to load and test the software's suitability as regards their
00033 requirements in conditions enabling the security of their systems and/or 
00034 data to be ensured and,  more generally, to use and operate it in the 
00035 same conditions as regards security. 
00036 
00037 The fact that you are presently reading this means that you have had
00038 knowledge of the CeCILL license and that you accept its terms.
00039 */
00040 
00041 #ifndef _ABSTRACTPARAMETRIZABLE_H_
00042 #define _ABSTRACTPARAMETRIZABLE_H_
00043 
00044 #include "Parametrizable.h"
00045 
00046 //From the STL:
00047 #include <map>
00048 
00049 namespace bpp
00050 {
00051 
00062 class AbstractParametrizable:
00063   public virtual Parametrizable
00064 {
00065   private:
00066     ParameterList parameters_;
00067     std::string prefix_;
00068 
00069   public:
00070     AbstractParametrizable(const std::string& prefix) : parameters_(), prefix_(prefix) {}
00071 
00072     virtual ~AbstractParametrizable() {}
00073 
00074   public:
00075     bool hasParameter(const std::string& name) const { return parameters_.hasParameter(prefix_ + name); }
00076 
00077     const ParameterList& getParameters() const { return parameters_; }
00078     
00079     const Parameter& getParameter(const std::string& name) const throw (ParameterNotFoundException)
00080     {
00081       return parameters_.getParameter(prefix_ + name);
00082     }
00083   
00084     double getParameterValue(const std::string& name) const
00085       throw (ParameterNotFoundException)
00086     { 
00087       return getParameter(name).getValue();
00088     }
00089 
00090     void setAllParametersValues(const ParameterList & parameters) 
00091       throw (ParameterNotFoundException, ConstraintException)
00092     {
00093       parameters_.setAllParametersValues(parameters);
00094       fireParameterChanged(parameters);
00095     }
00096 
00097     void setParameterValue(const std::string& name, double value) 
00098       throw (ParameterNotFoundException, ConstraintException)
00099     {
00100       parameters_.setParameterValue(prefix_ + name, value);
00101       fireParameterChanged(parameters_.subList(prefix_ + name));
00102     }
00103 
00104     void setParametersValues(const ParameterList& parameters)
00105       throw (ParameterNotFoundException, ConstraintException)
00106     { 
00107       parameters_.setParametersValues(parameters);
00108       fireParameterChanged(parameters);
00109     }
00110 
00111     bool matchParametersValues(const ParameterList& parameters)
00112       throw (ConstraintException)
00113     {
00114       bool test = parameters_.matchParametersValues(parameters);
00115       if (test) 
00116         fireParameterChanged(parameters);
00117       return test;
00118     }
00119 
00120     size_t getNumberOfParameters() const { return parameters_.size(); }
00121      
00122     void setNamespace(const std::string& prefix);
00123     
00124     std::string getNamespace() const { return prefix_; }
00125     
00126     std::string getParameterNameWithoutNamespace(const std::string& name) const;
00127 
00133     virtual void fireParameterChanged(const ParameterList& parameters) = 0;
00134 
00135   protected:
00136     void addParameter_(Parameter* parameter)
00137     {
00138       if (parameter)
00139         parameters_.addParameter(parameter);
00140     }
00141 
00142     void addParameters_(const ParameterList& parameters)
00143     {
00144       parameters_.addParameters(parameters);
00145     }
00146 
00147     void deleteParameter_(size_t index) throw (IndexOutOfBoundsException)
00148     {
00149       if (index >= parameters_.size())
00150         throw IndexOutOfBoundsException("AbstractParametrizable::deleteParameter_.", index, 0, parameters_.size() - 1);
00151       parameters_.deleteParameter(index);
00152     }
00153 
00154     void resetParameters_()
00155     {
00156       parameters_.reset();
00157     }
00158 
00164     Parameter& getParameter_(const std::string& name) throw (ParameterNotFoundException)
00165     {
00166       return parameters_.getParameter(prefix_ + name);
00167     }
00168   
00174     Parameter& getParameterWithNamespace_(const std::string& name) throw (ParameterNotFoundException)
00175     {
00176       return getParameter_(name);
00177     }
00183     const Parameter& getParameterWithNamespace_(const std::string& name) const throw (ParameterNotFoundException)
00184     {
00185       return getParameter(name);
00186     }
00187 
00188     Parameter& getParameter_(size_t index) throw (IndexOutOfBoundsException)
00189     {
00190       if (index >= parameters_.size())
00191         throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
00192       return parameters_[index];
00193     }
00194     const Parameter& getParameter_(size_t index) const throw (IndexOutOfBoundsException)
00195     {
00196       if(index >= parameters_.size())
00197         throw IndexOutOfBoundsException("AbstractParametrizable::getParameter_.", index, 0, parameters_.size() - 1);
00198       return parameters_[index];
00199     }
00200     
00201     ParameterList& getParameters_() { return parameters_; }
00202 };
00203 
00204 } //end of namespace bpp.
00205 
00206 #endif //_ABSTRACTPARAMETRIZABLE_H_
00207 
 All Classes Namespaces Files Functions Variables Typedefs Friends