Belle II Software development
ModuleParamList.templateDetails.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <framework/core/ModuleParamList.h>
12#include <framework/core/ModuleParam.h>
13#include <framework/logging/Logger.h>
14
15#include <map>
16#include <string>
17
18namespace Belle2 {
23 //======================================================
24 // Implementation of template based methods
25 //======================================================
26
27 template <typename T>
28 void ModuleParamList::addParameter(const std::string& name,
29 T& paramVariable,
30 const std::string& description,
31 const T& defaultValue)
32 {
33 ModuleParamPtr newParam(new ModuleParam<T>(paramVariable, description, false));
34
35 // Check if a parameter with the given name already exists
36 std::map<std::string, ModuleParamPtr>::iterator mapIter;
37 mapIter = m_paramMap.find(name);
38
39 if (mapIter == m_paramMap.end()) {
40 m_paramMap.insert(std::make_pair(name, newParam));
41 ModuleParam<T>* explModParam = static_cast<ModuleParam<T>*>(newParam.get());
42 explModParam->setDefaultValue(defaultValue);
43 } else {
44 B2ERROR("A parameter with the name '" + name +
45 "' already exists! The name of a module parameter must be unique within a module.");
46 }
47 }
48
49 template <typename T>
50 void ModuleParamList::addParameter(const std::string& name,
51 T& paramVariable,
52 const std::string& description)
53 {
54 ModuleParamPtr newParam(new ModuleParam<T>(paramVariable, description, true));
55
56 // Check if a parameter with the given name already exists
57 std::map<std::string, ModuleParamPtr>::iterator mapIter;
58 mapIter = m_paramMap.find(name);
59
60 if (mapIter == m_paramMap.end()) {
61 m_paramMap.insert(std::make_pair(name, newParam));
62 } else {
63 B2ERROR("A parameter with the name '" + name +
64 "' already exists! The name of a module parameter must be unique within a module.");
65 }
66 }
67
68 template <typename T>
69 void ModuleParamList::setParameter(const std::string& name, const T& value)
70 {
71 try {
72 ModuleParam<T>& explModParam = getParameter<T>(name);
73 explModParam.setValue(value);
74 } catch (std::runtime_error& exc) {
75 B2ERROR(exc.what());
76 }
77 }
78
79 template <typename T>
80 ModuleParam<T>& ModuleParamList::getParameter(const std::string& name) const
81 {
82 // Check if a parameter with the given name exists
83 std::map<std::string, ModuleParamPtr>::const_iterator mapIter;
84 mapIter = m_paramMap.find(name);
85
86 if (mapIter != m_paramMap.end()) {
87 ModuleParamPtr moduleParam = mapIter->second;
88
89 // Check the type of the stored parameter (currently done using the type identifier string)
90 if (moduleParam->getTypeInfo() == ModuleParam<T>::TypeInfo()) {
91 ModuleParam<T>* explModParam = static_cast<ModuleParam<T>*>(moduleParam.get());
92 return *explModParam;
93 } else
94 throwTypeError(name, moduleParam->getTypeInfo(), ModuleParam<T>::TypeInfo());
95 } else
97 }
98
99 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100 // Python API
101 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
103 template <typename PythonObject>
104 void ModuleParamList::setParamPython(const std::string& name, const PythonObject& pyObj)
105 {
106
108 p->setValueFromPythonObject(pyObj);
109 }
110
111 template <typename PythonObject>
112 void ModuleParamList::getParamValuesPython(const std::string& name,
113 PythonObject& pyOutput,
114 bool defaultValues) const
115 {
116 try {
118 p->setValueToPythonObject(pyOutput, defaultValues);
119 } catch (std::runtime_error& exc) {
120 B2ERROR(exc.what());
121 }
122 }
123
125} // end of Belle2 namespace
std::map< std::string, ModuleParamPtr > m_paramMap
Stores the module parameters together with a string name as key.
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.
ModuleParamPtr getParameterPtr(const std::string &name) const
Returns a ModuleParamPtr to a parameter.
static void throwNotFoundError(const std::string &name)
Throws an error for a requested parameter that does not exist.
A single parameter of the module.
Definition: ModuleParam.h:34
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
void setParameter(const std::string &name, const T &value)
Sets the value of a parameter given by its name.
std::shared_ptr< ModuleParamBase > ModuleParamPtr
Defines a pointer to a module parameter as a boost shared pointer. *‍/.
void setParamPython(const std::string &name, const PythonObject &pyObj)
Implements a method for setting boost::python objects.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
void setDefaultValue(const T &defaultValue)
Sets the default value of a parameter.
void setValue(const T &value)
Sets the value of a parameter.
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.
Abstract base class for different kinds of events.