Belle II Software  release-05-02-19
ModuleParamList.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/core/ModuleParam.h>
14 
15 #include <map>
16 #include <vector>
17 #include <string>
18 #include <memory>
19 
20 namespace boost {
21  namespace python {
22  class list;
23  namespace api {
24  class object;
25  }
26  using api::object;
27  }
28 }
29 
30 namespace Belle2 {
35  //------------------------------------------------------
36  // Define convenient typdefs
37  //------------------------------------------------------
38 
40  using ModuleParamPtr = std::shared_ptr<ModuleParamBase>;
41 
47 
48  private:
54  [[noreturn]] static void throwNotFoundError(const std::string& name);
55 
63  [[noreturn]] static void throwTypeError(const std::string& name,
64  const std::string& expectedTypeInfo,
65  const std::string& typeInfo);
66  public:
71 
76 
89  template<typename T>
90  void addParameter(const std::string& name, T& paramVariable, const std::string& description, const T& defaultValue);
91 
104  template<typename T>
105  void addParameter(const std::string& name, T& paramVariable, const std::string& description);
106 
115  template<typename T>
116  void setParameter(const std::string& name, const T& value);
117 
119  void setParameters(const ModuleParamList& params);
120 
126  std::vector<std::string> getParameterNames() const;
127 
136  std::string getParameterDescription(const std::string& name) const;
137 
146  std::string getParameterTypeInfo(const std::string& name) const;
147 
157  template<typename T>
158  ModuleParam<T>& getParameter(const std::string& name) const;
159 
161  std::vector<std::string> getUnsetForcedParams() const;
162 
163  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164  // Python API
165  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166 
176  std::shared_ptr<boost::python::list> getParamInfoListPython() const;
177 
188  template<typename PythonObject>
189  void setParamPython(const std::string& name, const PythonObject& pyObj);
190 
200  template<typename PythonObject>
201  void getParamValuesPython(const std::string& name, PythonObject& pyOutput, bool defaultValues) const;
202 
203  private:
212  ModuleParamPtr getParameterPtr(const std::string& name) const;
213 
214  private:
216  std::map<std::string, ModuleParamPtr> m_paramMap;
217  };
218 
219  // These frequent template instantiations are provided by the library and do not require including the details header
220  extern template void ModuleParamList::addParameter(const std::string&, bool&, const std::string&, const bool&);
221  extern template void ModuleParamList::addParameter(const std::string&, double&, const std::string&, const double&);
222  extern template void ModuleParamList::addParameter(const std::string&, float&, const std::string&, const float&);
223  extern template void ModuleParamList::addParameter(const std::string&, int&, const std::string&, const int&);
224  extern template void ModuleParamList::addParameter(const std::string&, std::string&, const std::string&, const std::string&);
225 
227 } //end of Belle2 namespace
Belle2::ModuleParamList::getParameterPtr
ModuleParamPtr getParameterPtr(const std::string &name) const
Returns a ModuleParamPtr to a parameter.
Definition: ModuleParamList.cc:126
Belle2::ModuleParamList::m_paramMap
std::map< std::string, ModuleParamPtr > m_paramMap
Stores the module parameters together with a string name as key.
Definition: ModuleParamList.h:216
Belle2::ModuleParamList::throwTypeError
static void throwTypeError(const std::string &name, const std::string &expectedTypeInfo, const std::string &typeInfo)
Throws an error for a requested parameter that exists but was request with the wrong type.
Definition: ModuleParamList.cc:40
Belle2::ModuleParamList::getParameter
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
Definition: ModuleParamList.templateDetails.h:90
Belle2::ModuleParamList::getUnsetForcedParams
std::vector< std::string > getUnsetForcedParams() const
Returns list of unset parameters (if they are required to have a value.
Definition: ModuleParamList.cc:56
Belle2::ModuleParamList::setParameters
void setParameters(const ModuleParamList &params)
Set values for parameters from other parameter list.
Definition: ModuleParamList.cc:88
Belle2::ModuleParamList::ModuleParamList
ModuleParamList()
Constructor.
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ModuleParamList::~ModuleParamList
~ModuleParamList()
Destructor.
Definition: ModuleParamList.cc:50
Belle2::ModuleParam
A single parameter of the module.
Definition: DisplayUI.h:34
Belle2::ModuleParamList::setParamPython
void setParamPython(const std::string &name, const PythonObject &pyObj)
Implements a method for setting boost::python objects.
Definition: ModuleParamList.templateDetails.h:114
Belle2::ModuleParamList::getParameterTypeInfo
std::string getParameterTypeInfo(const std::string &name) const
Returns the type info of a parameter given by its name.
Definition: ModuleParamList.cc:112
Belle2::ModuleParamList::throwNotFoundError
static void throwNotFoundError(const std::string &name)
Throws an error for a requested parameter that does not exist.
Definition: ModuleParamList.cc:35
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::ModuleParamList::getParameterDescription
std::string getParameterDescription(const std::string &name) const
Returns the description of a parameter given by its name.
Definition: ModuleParamList.cc:107
Belle2::ModuleParamPtr
std::shared_ptr< ModuleParamBase > ModuleParamPtr
Defines a pointer to a module parameter as a boost shared pointer. *‍/.
Definition: ModuleParamList.h:40
Belle2::ModuleParamList::setParameter
void setParameter(const std::string &name, const T &value)
Sets the value of a parameter given by its name.
Definition: ModuleParamList.templateDetails.h:79
Belle2::ModuleParamList::getParamInfoListPython
std::shared_ptr< boost::python::list > getParamInfoListPython() const
Returns a python list of all parameters.
Definition: ModuleParamList.cc:66
Belle2::ModuleParamList::getParamValuesPython
void getParamValuesPython(const std::string &name, PythonObject &pyOutput, bool defaultValues) const
Returns a python object containing the value or default value of the given parameter.
Definition: ModuleParamList.templateDetails.h:122
Belle2::ModuleParamList::getParameterNames
std::vector< std::string > getParameterNames() const
Returns the names of all parameters in this parameter list.
Definition: ModuleParamList.cc:97