Config.cpp
Go to the documentation of this file.
1 #include "Config.h"
2 
4 {
6  {
7  }
8 
9  bool
10  Config::getBool(const std::string name)
11  {
12  return get<bool>(name);
13  }
14 
15  float
16  Config::getFloat(const std::string name)
17  {
18  return get<float>(name);
19  }
20 
21  std::uint32_t
22  Config::getUint(const std::string name)
23  {
24  return get<std::uint32_t>(name);
25  }
26 
27  std::int32_t
28  Config::getInt(const std::string name)
29  {
30  return get<std::int32_t>(name);
31  }
32 
34  Config::getLinearConfig(const std::string name)
35  {
36  return get<types::LinearConfig>(name);
37  }
38 
40  Config::getModularConvertedValueConfig(const std::string& name)
41  {
42  return get<types::ModularConvertedValueConfig>(name);
43  }
44 
45  std::string
46  Config::getString(const std::string name)
47  {
48  return get<std::string>(name);
49  }
50 
51  std::list<bool>
52  Config::getBoolList(const std::string name)
53  {
54  return get<std::list<bool>>(name);
55  }
56 
57  std::list<float>
58  Config::getFloatList(const std::string name)
59  {
60  return get<std::list<float>>(name);
61  }
62 
63  std::list<std::uint32_t>
64  Config::getUintList(const std::string name)
65  {
66  return get<std::list<std::uint32_t>>(name);
67  }
68 
69  std::list<std::int32_t>
70  Config::getIntList(const std::string name)
71  {
72  return get<std::list<std::int32_t>>(name);
73  }
74 
75  bool
76  Config::checkAllItemsRead(std::vector<std::string>& errors) const
77  {
78  bool succ = true;
79  for (auto& i : items)
80  {
81  if (!i.second.isRead)
82  {
83  errors.push_back(createErrorMessageWithContext(
84  "Configuration element " + i.first +
85  " was not expected by the Device code but defined in HardwareConfig.",
86  *this));
87  }
88  }
89  return succ;
90  }
91 
92  // helper type for the visitor #4
93  template <class... Ts>
94  struct overloaded : Ts...
95  {
96  using Ts::operator()...;
97  };
98  // explicit deduction guide (not needed as of C++20)
99  template <class... Ts>
100  overloaded(Ts...) -> overloaded<Ts...>;
101 
102  template <class T>
103  void
104  printList(std::stringstream& s, std::list<T> list)
105  {
106  s << "{";
107  unsigned int i = 0;
108  for (auto& l : list)
109  {
110  i++;
111  s << l;
112  if (i != list.size())
113  {
114  s << ", ";
115  }
116  }
117  s << "}" << std::endl;
118  }
119 
120  void
121  Config::print(std::stringstream& s, int indention) const
122  {
123  for (auto& c : items)
124  {
125  for (int i = 0; i < indention; i++)
126  {
127  s << "\t";
128  }
129  s << c.first << " (";
130  std::visit(
131  overloaded{[&s](bool arg)
132  {
133  s << "Bool): ";
134  s << arg << std::endl;
135  },
136  [&s](float arg)
137  {
138  s << "Float): ";
139  s << arg << std::endl;
140  },
141  [&s](std::uint32_t arg)
142  {
143  s << "UInt): ";
144  s << arg << std::endl;
145  },
146  [&s](std::int32_t arg)
147  {
148  s << "Int): ";
149  s << arg << std::endl;
150  },
151  [&s](auto arg)
152  {
153  s << "?): ";
154  s << arg << std::endl;
155  },
156  [&s](types::LinearConfig arg)
157  {
158  s << "LinearConvertedValue): factor=" << arg.getFactor()
159  << " offset=" << arg.getOffset() << std::endl;
160  },
162  {
163  s << "ModularConvertedValueConfig): "
164  << "zeroOffset=" << arg.getZeroOffset() << std::endl;
165  },
166  [&s](std::string arg)
167  {
168  s << "String): ";
169  s << arg << std::endl;
170  },
171  [&s](std::list<bool> arg)
172  {
173  s << "BoolList): ";
174  printList(s, arg);
175  },
176  [&s](std::list<float> arg)
177  {
178  s << "FloatList): ";
179  printList(s, arg);
180  },
181  [&s](std::list<std::uint32_t> arg)
182  {
183  s << "UIntList): ";
184  printList(s, arg);
185  },
186  [&s](std::list<std::int32_t> arg)
187  {
188  s << "IntList): ";
189  printList(s, arg);
190  },
192  s << "Matrix<Float>):" << std::endl << arg << std::endl;
193  },
195  s << "Matrix<UInt>):" << std::endl << arg << std::endl;
196  },
198  s << "Matrix<Int>):" << std::endl << arg << std::endl;
199  }},
200  c.second.data);
201  } // namespace armarx::control::hardware_config
202  }
203 
204  void
205  Config::setBool(const std::string name, bool value, ConfigTag tag)
206  {
207  set<bool>(name, value, tag);
208  }
209 
210  void
211  Config::setFloat(const std::string name, float value, ConfigTag tag)
212  {
213  set<float>(name, value, tag);
214  }
215 
216  void
217  Config::setUint(const std::string name, std::uint32_t value, ConfigTag tag)
218  {
219  set<std::uint32_t>(name, value, tag);
220  }
221 
222  void
223  Config::setInt(const std::string name, std::int32_t value, ConfigTag tag)
224  {
225  set<std::int32_t>(name, value, tag);
226  }
227 
228  void
230  {
231  set<types::LinearConfig>(name, value, tag);
232  }
233 
234  void
237  ConfigTag tag)
238  {
239  set<types::ModularConvertedValueConfig>(name, value, tag);
240  }
241 
242  void
243  Config::setString(const std::string name, std::string value, ConfigTag tag)
244  {
245  set<std::string>(name, value, tag);
246  }
247 
248  void
249  Config::setBoolList(const std::string name, std::list<bool> value, ConfigTag tag)
250  {
251  set<std::list<bool>>(name, value, tag);
252  }
253 
254  void
255  Config::setFloatList(const std::string name, std::list<float> value, ConfigTag tag)
256  {
257  set<std::list<float>>(name, value, tag);
258  }
259 
260  void
261  Config::setUintList(const std::string name, std::list<std::uint32_t> value, ConfigTag tag)
262  {
263  set<std::list<std::uint32_t>>(name, value, tag);
264  }
265 
266  void
267  Config::setIntList(const std::string name, std::list<std::int32_t> value, ConfigTag tag)
268  {
269  set<std::list<std::int32_t>>(name, value, tag);
270  }
271 
272 
273  Config&
274  ControllerConfig::getControllerConfig(const std::string controllerName)
275  {
276  auto it = controllerConfigs.find(controllerName);
277  if (it != controllerConfigs.end())
278  {
279  return *it->second;
280  }
281  throw ConfigElementNotFoundError("Controller with name: " + controllerName);
282  }
283 
284  void
286  std::shared_ptr<Config> controllerConfig)
287  {
288  controllerConfigs[name] = controllerConfig;
289  }
290 
292  {
293  for (auto& pair : orig.controllerConfigs)
294  {
295  // Work around private copy constructor
296  Config* copied = new Config(*pair.second);
297  this->addControllerConfig(pair.first, std::shared_ptr<Config>(copied));
298  }
299  }
300 
301  std::string
303  {
304  switch (tag)
305  {
306  case TagProfile:
307  return "Profile";
308  case TagCustom:
309  return "Custom";
310  default:
311  return "";
312  }
313  }
314 
315  void
317  {
318  // Reset isRead tags
319  for (auto& i : items)
320  {
321  i.second.isRead = false;
322  }
323  }
324 
325  std::string
326  createErrorMessageWithContext(std::string message, const Config& context)
327  {
328  std::stringstream ss;
329  ss << message << std::endl << "Context:" << std::endl;
330  context.print(ss, 0);
331  return ss.str();
332  }
333 } // namespace armarx::control::hardware_config
armarx::control::hardware_config::printList
void printList(std::stringstream &s, std::list< T > list)
Definition: Config.cpp:104
armarx::control::hardware_config::Config::getIntList
std::list< std::int32_t > getIntList(const std::string name)
Get a IntList (std::list<std::int32_t>) typed Config attribute by name.
Definition: Config.cpp:70
armarx::control::hardware_config::Config::setFloatList
void setFloatList(const std::string name, std::list< float > value, ConfigTag tag)
Definition: Config.cpp:255
armarx::control::hardware_config::Config::setModularConvertedValueConfig
void setModularConvertedValueConfig(const std::string name, types::ModularConvertedValueConfig &&value, ConfigTag tag)
Definition: Config.cpp:235
armarx::control::hardware_config::ControllerConfig::getControllerConfig
Config & getControllerConfig(const std::string controllerName)
Definition: Config.cpp:274
armarx::control::hardware_config::Config::getFloatList
std::list< float > getFloatList(const std::string name)
Get a FloatList (std::list<float>) typed Config attribute by name.
Definition: Config.cpp:58
armarx::control::hardware_config::Config::Config
Config()
Definition: Config.cpp:5
armarx::control::hardware_config::ConfigElementNotFoundError
The ConfigElementNotFoundError class represents an error that is thrown when trying to get a config v...
Definition: Errors.h:24
armarx::control::hardware_config::types::LinearConfig
The LinearConfig class represents a linear conversion and has a factor and offset.
Definition: Types.h:17
armarx::control::hardware_config::Config::setBool
void setBool(const std::string name, bool value, ConfigTag tag)
Definition: Config.cpp:205
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
armarx::control::hardware_config::Config::setUintList
void setUintList(const std::string name, std::list< std::uint32_t > value, ConfigTag tag)
Definition: Config.cpp:261
armarx::control::hardware_config::Config::getLinearConfig
types::LinearConfig getLinearConfig(const std::string name)
Get a LinearConvertedValue typed Config attribute by name.
Definition: Config.cpp:34
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
armarx::control::hardware_config::overloaded
overloaded(Ts...) -> overloaded< Ts... >
armarx::control::hardware_config
Definition: Config.cpp:3
armarx::control::hardware_config::Config::setInt
void setInt(const std::string name, std::int32_t value, ConfigTag tag)
Definition: Config.cpp:223
armarx::control::hardware_config::Config::getInt
std::int32_t getInt(const std::string name)
Get a Int (std::int32_t) typed Config attribute by name.
Definition: Config.cpp:28
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::control::hardware_config::Config::setLinearConfig
void setLinearConfig(const std::string name, types::LinearConfig &&value, ConfigTag tag)
Definition: Config.cpp:229
armarx::control::hardware_config::ConfigTag
ConfigTag
The ConfigTag is used when setting a config value.
Definition: Config.h:61
armarx::control::hardware_config::createErrorMessageWithContext
std::string createErrorMessageWithContext(std::string message, const Config &context)
Create an error message that prints part of the current Config object to make it easiier to find the ...
Definition: Config.cpp:326
armarx::control::hardware_config::Config::getUintList
std::list< std::uint32_t > getUintList(const std::string name)
Get a UIntList (std::list<std::uint32_t>) typed Config attribute by name.
Definition: Config.cpp:64
armarx::control::hardware_config::Config::getModularConvertedValueConfig
types::ModularConvertedValueConfig getModularConvertedValueConfig(const std::string &name)
Get a ModularConvertedValue typed Config attribute by name.
Definition: Config.cpp:40
armarx::control::hardware_config::Config::setFloat
void setFloat(const std::string name, float value, ConfigTag tag)
Definition: Config.cpp:211
armarx::control::hardware_config::Config::getBool
bool getBool(const std::string name)
Get a Bool typed Config attribute by name.
Definition: Config.cpp:10
armarx::control::hardware_config::Config::getUint
std::uint32_t getUint(const std::string name)
Get a Uint (std::uint32_t) typed Config attribute by name.
Definition: Config.cpp:22
armarx::control::hardware_config::TagCustom
@ TagCustom
Definition: Config.h:65
armarx::control::hardware_config::tagName
std::string tagName(ConfigTag tag)
Definition: Config.cpp:302
armarx::control::hardware_config::types::ModularConvertedValueConfig
Definition: Types.h:68
armarx::control::hardware_config::Config::onParsingFinished
virtual void onParsingFinished()
This method is called when the config is completely read form the HardwareConfig file.
Definition: Config.cpp:316
armarx::control::hardware_config::Config::setBoolList
void setBoolList(const std::string name, std::list< bool > value, ConfigTag tag)
Definition: Config.cpp:249
armarx::control::hardware_config::Config::setString
void setString(const std::string name, std::string value, ConfigTag tag)
Definition: Config.cpp:243
armarx::control::hardware_config::Config::getBoolList
std::list< bool > getBoolList(const std::string name)
Get a BoolList (std::list<bool>) typed Config attribute by name.
Definition: Config.cpp:52
Config.h
armarx::control::hardware_config::Config::items
std::map< std::string, ConfigItemWithMetadata > items
Definition: Config.h:250
armarx::control::hardware_config::overloaded
Definition: Config.cpp:94
armarx::control::hardware_config::Config
The Config class is the base class of all specialized configurations that have a direct key -> value ...
Definition: Config.h:91
armarx::control::hardware_config::ControllerConfig::ControllerConfig
ControllerConfig()
Definition: Config.h:355
armarx::control::hardware_config::Config::setUint
void setUint(const std::string name, std::uint32_t value, ConfigTag tag)
Definition: Config.cpp:217
armarx::aron::data::visit
requires isVisitor< VisitorImplementation, typename VisitorImplementation::Input > void visit(VisitorImplementation &v, typename VisitorImplementation::Input &o)
Definition: Visitor.h:124
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic >
armarx::control::hardware_config::Config::getString
std::string getString(const std::string name)
Get a String typed Config attribute by name.
Definition: Config.cpp:46
armarx::control::hardware_config::TagProfile
@ TagProfile
Definition: Config.h:64
armarx::control::hardware_config::Config::setIntList
void setIntList(const std::string name, std::list< std::int32_t > value, ConfigTag tag)
Definition: Config.cpp:267
armarx::control::hardware_config::ControllerConfig::controllerConfigs
std::map< std::string, std::shared_ptr< Config > > controllerConfigs
Definition: Config.h:372
armarx::control::hardware_config::Config::getFloat
float getFloat(const std::string name)
Get a Float typed Config attribute by name.
Definition: Config.cpp:16
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::control::hardware_config::ControllerConfig
Config with additional Config objects for controllers.
Definition: Config.h:349
armarx::control::hardware_config::Config::checkAllItemsRead
virtual bool checkAllItemsRead(std::vector< std::string > &errors) const
This method is called once the device has read the configuration data it needs.
Definition: Config.cpp:76
armarx::control::hardware_config::Config::print
virtual void print(std::stringstream &s, int indention) const
Print this configuration.
Definition: Config.cpp:121
armarx::control::hardware_config::ControllerConfig::addControllerConfig
void addControllerConfig(const std::string controllerName, std::shared_ptr< Config > controllerConfig)
Add a Config for a controller with name.
Definition: Config.cpp:285