ChimeraTK-ApplicationCore  04.01.00
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConfigReader.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
68 #include "ApplicationModule.h"
69 #include "ArrayAccessor.h"
70 #include "ScalarAccessor.h"
71 #include <unordered_map>
72 
73 #include <ChimeraTK/SupportedUserTypes.h>
74 
75 #include <map>
76 #include <utility>
77 
78 namespace ChimeraTK {
79 
80  struct FunctorFill;
81  struct ArrayFunctorFill;
82  struct FunctorSetValues;
83  struct FunctorSetValuesArray;
84  class ModuleTree;
85 
114  ConfigReader(ModuleGroup* owner, const std::string& name, const std::string& fileName,
115  const std::unordered_set<std::string>& tags = {});
116  ~ConfigReader() override;
117 
118  void mainLoop() override {}
119  void prepare() override;
120 
126  template<typename T>
127  const T& get(const std::string& variableName) const;
128 
133  template<typename T>
134  const T& get(const std::string& variableName, const T& defaultValue) const;
135 
140  std::list<std::string> getModules(const std::string& path = {}) const;
141 
142  protected:
144  void construct(const std::string& fileName);
145 
147  std::string _fileName;
148 
150  std::unique_ptr<ModuleTree> _moduleTree;
151 
153  void parsingError(const std::string& message);
154 
156  template<typename T>
157  struct Var {
158  Var(Module* owner, const std::string& name, T theValue)
159  : accessor(owner, name, "unknown", "Configuration variable"), value(std::move(theValue)) {}
160 
161  Var() = default;
162 
164  T value{};
165  };
166 
168  template<typename T>
169  struct Array {
170  Array(Module* owner, const std::string& name, const std::vector<T>& theValue)
171  : accessor(owner, name, "unknown", theValue.size(), "Configuration array"), value(theValue) {}
172 
173  Array() = default;
174 
176  std::vector<T> value;
177  };
178 
180  template<typename T>
181  void createVar(const std::string& name, const std::string& value);
182 
184  template<typename T>
185  void createArray(const std::string& name, const std::map<size_t, std::string>& values);
186 
189  void checkVariable(std::string const& name, std::string const& type) const;
190 
193  void checkArray(std::string const& name, std::string const& type) const;
194 
197  template<typename T>
198  using MapOfVar = std::unordered_map<std::string, Var<T>>;
199 
201  ChimeraTK::TemplateUserTypeMapNoVoid<MapOfVar> _variableMap;
202 
205  template<typename T>
206  using MapOfArray = std::unordered_map<std::string, Array<T>>;
207 
209  ChimeraTK::TemplateUserTypeMapNoVoid<MapOfArray> _arrayMap;
210 
212  ChimeraTK::SingleTypeUserTypeMapNoVoid<const char*> _typeMap{"int8", "uint8", "int16", "uint16", "int32", "uint32",
213  "int64", "uint64", "float", "double", "string", "boolean"};
214 
218  template<typename T>
219  const T& getImpl(const std::string& variableName, T*) const;
220  template<typename T>
221  const std::vector<T>& getImpl(const std::string& variableName, std::vector<T>*) const;
222 
223  friend struct FunctorFill;
224  friend struct ArrayFunctorFill;
225  friend struct FunctorSetValues;
226  friend struct FunctorSetValuesArray;
227  friend struct FunctorGetTypeForName;
228  };
229 
230  /********************************************************************************************************************/
231  /********************************************************************************************************************/
232 
233  template<typename T>
234  const T& ConfigReader::get(const std::string& variableName, const T& defaultValue) const {
236  try {
237  return getImpl(variableName, static_cast<T*>(nullptr));
238  }
239  catch(ChimeraTK::logic_error&) {
240  return defaultValue;
241  }
242  }
243 
244  /********************************************************************************************************************/
245  /********************************************************************************************************************/
246 
247  template<typename T>
248  const T& ConfigReader::get(const std::string& variableName) const {
249  return getImpl(variableName, static_cast<T*>(nullptr));
250  }
251 
252  /********************************************************************************************************************/
253  /********************************************************************************************************************/
254 
255  template<typename T>
256  const T& ConfigReader::getImpl(const std::string& variableName, T*) const {
257  checkVariable(variableName, boost::fusion::at_key<T>(_typeMap));
258  return boost::fusion::at_key<T>(_variableMap.table).at(variableName).value;
259  }
260 
261  /********************************************************************************************************************/
262  /********************************************************************************************************************/
263 
264  template<typename T>
265  const std::vector<T>& ConfigReader::getImpl(const std::string& variableName, std::vector<T>*) const {
266  checkArray(variableName, boost::fusion::at_key<T>(_typeMap));
267  return boost::fusion::at_key<T>(_arrayMap.table).at(variableName).value;
268  }
269 
270 } // namespace ChimeraTK
ChimeraTK::ConfigReader::_moduleTree
std::unique_ptr< ModuleTree > _moduleTree
List to hold VariableNodes corresponding to xml modules.
Definition: ConfigReader.h:150
ChimeraTK::FunctorFill
Functor to fill variableMap.
Definition: ConfigReader.cc:101
ChimeraTK::ConfigReader::parsingError
void parsingError(const std::string &message)
throw a parsing error with more information
Definition: ConfigReader.cc:396
ChimeraTK::ConfigReader
Generic module to read an XML config file and provide the defined values as constant variables.
Definition: ConfigReader.h:113
ChimeraTK::ConfigReader::get
const T & get(const std::string &variableName) const
Get value for given configuration variable.
Definition: ConfigReader.h:248
ChimeraTK::ConfigReader::Array::value
std::vector< T > value
Definition: ConfigReader.h:176
ChimeraTK::ConfigReader::Var::accessor
ScalarOutput< T > accessor
Definition: ConfigReader.h:163
ChimeraTK::ModuleGroup
Definition: ModuleGroup.h:16
ChimeraTK::ConfigReader::Array::Array
Array()=default
ChimeraTK::ConfigReader::createVar
void createVar(const std::string &name, const std::string &value)
Create an instance of Var<T> and place it on the variableMap.
Definition: ConfigReader.cc:231
ChimeraTK::ConfigReader::ConfigReader
ConfigReader(ModuleGroup *owner, const std::string &name, const std::string &fileName, const std::unordered_set< std::string > &tags={})
Definition: ConfigReader.cc:279
ChimeraTK::ConfigReader::checkVariable
void checkVariable(std::string const &name, std::string const &type) const
Check if variable exists in the config and if type of var name in the config file matches the given t...
Definition: ConfigReader.cc:188
ChimeraTK::ConfigReader::createArray
void createArray(const std::string &name, const std::map< size_t, std::string > &values)
Create an instance of Array<T> and place it on the arrayMap.
Definition: ConfigReader.cc:248
ArrayAccessor.h
ChimeraTK::ConfigReader::Var::value
T value
Definition: ConfigReader.h:164
ChimeraTK::ConfigReader::_arrayMap
ChimeraTK::TemplateUserTypeMapNoVoid< MapOfArray > _arrayMap
Type-depending map of vectors of arrays.
Definition: ConfigReader.h:209
ChimeraTK::ConfigReader::Var::Var
Var()=default
ChimeraTK::ArrayFunctorFill
Functor to fill variableMap for arrays.
Definition: ConfigReader.cc:131
ChimeraTK::FunctorSetValues
Functor to set values to the scalar accessors.
Definition: ConfigReader.cc:419
ChimeraTK::ConfigReader::Var
Class holding the value and the accessor for one configuration variable.
Definition: ConfigReader.h:157
ChimeraTK::ConfigReader::mainLoop
void mainLoop() override
To be implemented by the user: function called in a separate thread executing the main loop of the mo...
Definition: ConfigReader.h:118
ChimeraTK::ApplicationModule
Definition: ApplicationModule.h:24
ChimeraTK::ConfigReader::Array::accessor
ArrayOutput< T > accessor
Definition: ConfigReader.h:175
ChimeraTK::ConfigReader::prepare
void prepare() override
Prepare the execution of the module.
Definition: ConfigReader.cc:466
ChimeraTK::ConfigReader::Array::Array
Array(Module *owner, const std::string &name, const std::vector< T > &theValue)
Definition: ConfigReader.h:170
ChimeraTK::ConfigReader::~ConfigReader
~ConfigReader() override
ChimeraTK::ConfigReader::getModules
std::list< std::string > getModules(const std::string &path={}) const
Returns a list of names of modules which are direct children of path.
Definition: ConfigReader.cc:402
ChimeraTK::ConfigReader::_fileName
std::string _fileName
File name.
Definition: ConfigReader.h:147
ChimeraTK::ArrayOutput< T >
ChimeraTK::ConfigReader::getImpl
const T & getImpl(const std::string &variableName, T *) const
Implementation of get() which can be overloaded for scalars and vectors.
Definition: ConfigReader.h:256
ChimeraTK::ConfigReader::_typeMap
ChimeraTK::SingleTypeUserTypeMapNoVoid< const char * > _typeMap
Map assigning string type identifyers to C++ types.
Definition: ConfigReader.h:212
ScalarAccessor.h
ChimeraTK::ConfigReader::_variableMap
ChimeraTK::TemplateUserTypeMapNoVoid< MapOfVar > _variableMap
Type-depending map of vectors of variables.
Definition: ConfigReader.h:201
ApplicationModule.h
ChimeraTK::ConfigReader::MapOfArray
std::unordered_map< std::string, Array< T > > MapOfArray
Define type for map of std::string to Array, so we can put it into the TemplateUserTypeMap.
Definition: ConfigReader.h:206
ChimeraTK::FunctorGetTypeForName
Definition: ConfigReader.cc:161
ChimeraTK::ConfigReader::Var::Var
Var(Module *owner, const std::string &name, T theValue)
Definition: ConfigReader.h:158
ChimeraTK::ConfigReader::Array
Class holding the values and the accessor for one configuration array.
Definition: ConfigReader.h:169
ChimeraTK::ConfigReader::construct
void construct(const std::string &fileName)
Helper function to avoid code duplication in constructors.
Definition: ConfigReader.cc:358
ChimeraTK::FunctorSetValuesArray
Functor to set values to the array accessors.
Definition: ConfigReader.cc:443
ChimeraTK
InvalidityTracer application module.
Definition: spec_dataValidityPropagation.dox:2
ChimeraTK::ConfigReader::checkArray
void checkArray(std::string const &name, std::string const &type) const
Check if array exists in the config and if type of array name in the config file matches the given ty...
Definition: ConfigReader.cc:210
ChimeraTK::ScalarOutput< T >
ChimeraTK::Module
Base class for ApplicationModule and DeviceModule, to have a common interface for these module types.
Definition: Module.h:21
ChimeraTK::ConfigReader::MapOfVar
std::unordered_map< std::string, Var< T > > MapOfVar
Define type for map of std::string to Var, so we can put it into the TemplateUserTypeMap.
Definition: ConfigReader.h:198