70#include <ChimeraTK/SupportedUserTypes.h>
73#include <unordered_map>
79 struct ArrayFunctorFill;
80 struct FunctorSetValues;
81 struct FunctorSetValuesArray;
114 const std::unordered_set<std::string>& tags = {});
126 const T&
get(std::string variableName)
const;
133 const T&
get(std::string variableName,
const T& defaultValue)
const;
139 std::list<std::string>
getModules(
const std::string& path = {})
const;
143 void construct(
const std::string& fileName);
158 :
accessor(owner, name,
"unknown",
"Configuration variable"),
value(std::move(theValue)) {}
169 Array(
Module* owner,
const std::string& name,
const std::vector<T>& theValue)
170 :
accessor(owner, name,
"unknown", theValue.size(),
"Configuration array"),
value(theValue) {}
180 void createVar(
const std::string& name,
const std::string& value);
184 void createArray(
const std::string& name,
const std::map<size_t, std::string>& values);
188 [[nodiscard]]
bool checkVariable(std::string
const& name, std::string
const& type)
const;
192 [[nodiscard]]
bool checkArray(std::string
const& name, std::string
const& type)
const;
197 using MapOfVar = std::unordered_map<std::string, Var<T>>;
205 using MapOfArray = std::unordered_map<std::string, Array<T>>;
208 ChimeraTK::TemplateUserTypeMapNoVoid<MapOfArray>
_arrayMap;
211 ChimeraTK::SingleTypeUserTypeMapNoVoid<const char*>
_typeMap{
"int8",
"uint8",
"int16",
"uint16",
"int32",
"uint32",
212 "int64",
"uint64",
"float",
"double",
"string",
"boolean"};
220 std::optional<const T*>
getImpl(
const std::string& variableName, T*)
const;
223 std::optional<const std::vector<T>*>
getImpl(
const std::string& variableName, std::vector<T>*)
const;
237 if(variableName.starts_with(
"/")) {
238 variableName = variableName.substr(1);
240 auto result =
getImpl(variableName,
static_cast<T*
>(
nullptr));
241 return *(result.value_or(&defaultValue));
249 if(variableName.starts_with(
"/")) {
250 variableName = variableName.substr(1);
252 auto result =
getImpl(variableName,
static_cast<T*
>(
nullptr));
253 if(!result.has_value()) {
254 auto msg =
"ConfigReader: Cannot find configuration variable of the name '" + variableName +
255 "' in the config file '" +
_fileName +
"'.";
256 std::cerr << msg << std::endl;
257 throw(ChimeraTK::logic_error(msg));
259 return *(result.value());
271 return &(boost::fusion::at_key<T>(
_variableMap.table).at(variableName).value);
278 std::optional<const std::vector<T>*>
ConfigReader::getImpl(
const std::string& variableName, std::vector<T>*)
const {
283 return &(boost::fusion::at_key<T>(
_arrayMap.table).at(variableName).value);
Generic module to read an XML config file and provide the defined values as constant variables.
void mainLoop() override
To be implemented by the user: function called in a separate thread executing the main loop of the mo...
void createVar(const std::string &name, const std::string &value)
Create an instance of Var<T> and place it on the variableMap.
std::list< std::string > getModules(const std::string &path={}) const
Returns a list of names of modules which are direct children of path.
std::optional< const T * > getImpl(const std::string &variableName, T *) const
Implementation of get() which can be overloaded for scalars and vectors.
bool 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...
std::string _fileName
File name.
std::unique_ptr< ModuleTree > _moduleTree
List to hold VariableNodes corresponding to xml modules.
bool 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...
void prepare() override
Prepare the execution of the module.
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.
ChimeraTK::TemplateUserTypeMapNoVoid< MapOfArray > _arrayMap
Type-depending map of vectors of arrays.
void construct(const std::string &fileName)
Helper function to avoid code duplication in constructors.
ChimeraTK::SingleTypeUserTypeMapNoVoid< const char * > _typeMap
Map assigning string type identifyers to C++ types.
ChimeraTK::TemplateUserTypeMapNoVoid< MapOfVar > _variableMap
Type-depending map of vectors of variables.
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.
const T & get(std::string variableName) const
Get value for given configuration variable.
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.
void parsingError(const std::string &message) noexcept
throw a parsing error with more information
Base class for ApplicationModule and DeviceModule, to have a common interface for these module types.
InvalidityTracer application module.
Functor to fill variableMap for arrays.
Convenience class for output array accessors (always UpdateMode::push)
Class holding the values and the accessor for one configuration array.
ArrayOutput< T > accessor
Array(Module *owner, const std::string &name, const std::vector< T > &theValue)
Class holding the value and the accessor for one configuration variable.
Var(Module *owner, const std::string &name, T theValue)
ScalarOutput< T > accessor
Functor to fill variableMap.
Functor to set values to the array accessors.
Functor to set values to the scalar accessors.
Convenience class for output scalar accessors (always UpdateMode::push)