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
41 {
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
229 Config::setLinearConfig(const std::string name, types::LinearConfig&& value, ConfigTag tag)
230 {
231 set<types::LinearConfig>(name, value, tag);
232 }
233
234 void
237 ConfigTag tag)
238 {
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 Config&
273 ControllerConfig::getControllerConfig(const std::string controllerName)
274 {
275 auto it = controllerConfigs.find(controllerName);
276 if (it != controllerConfigs.end())
277 {
278 return *it->second;
279 }
280 throw ConfigElementNotFoundError("Controller with name: " + controllerName);
281 }
282
283 void
285 std::shared_ptr<Config> controllerConfig)
286 {
287 controllerConfigs[name] = controllerConfig;
288 }
289
291 {
292 for (auto& pair : orig.controllerConfigs)
293 {
294 // Work around private copy constructor
295 Config* copied = new Config(*pair.second);
296 this->addControllerConfig(pair.first, std::shared_ptr<Config>(copied));
297 }
298 }
299
300 std::string
302 {
303 switch (tag)
304 {
305 case TagProfile:
306 return "Profile";
307 case TagCustom:
308 return "Custom";
309 default:
310 return "";
311 }
312 }
313
314 void
316 {
317 // Reset isRead tags
318 for (auto& i : items)
319 {
320 i.second.isRead = false;
321 }
322 }
323
324 std::string
325 createErrorMessageWithContext(std::string message, const Config& context)
326 {
327 std::stringstream ss;
328 ss << message << std::endl << "Context:" << std::endl;
329 context.print(ss, 0);
330 return ss.str();
331 }
332} // namespace armarx::control::hardware_config
constexpr T c
The ConfigElementNotFoundError class represents an error that is thrown when trying to get a config v...
Definition Errors.h:25
The Config class is the base class of all specialized configurations that have a direct key -> value ...
Definition Config.h:94
void setModularConvertedValueConfig(const std::string name, types::ModularConvertedValueConfig &&value, ConfigTag tag)
Definition Config.cpp:235
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
std::string getString(const std::string name)
Get a String typed Config attribute by name.
Definition Config.cpp:46
void setInt(const std::string name, std::int32_t value, ConfigTag tag)
Definition Config.cpp:223
virtual void print(std::stringstream &s, int indention) const
Print this configuration.
Definition Config.cpp:121
types::LinearConfig getLinearConfig(const std::string name)
Get a LinearConvertedValue typed Config attribute by name.
Definition Config.cpp:34
void setBool(const std::string name, bool value, ConfigTag tag)
Definition Config.cpp:205
float getFloat(const std::string name)
Get a Float typed Config attribute by name.
Definition Config.cpp:16
T get(const std::string name)
Definition Config.h:332
virtual void onParsingFinished()
This method is called when the config is completely read form the HardwareConfig file.
Definition Config.cpp:315
void setFloatList(const std::string name, std::list< float > value, ConfigTag tag)
Definition Config.cpp:255
void set(const std::string name, T value, ConfigTag tag)
Definition Config.h:272
std::list< bool > getBoolList(const std::string name)
Get a BoolList (std::list<bool>) typed Config attribute by name.
Definition Config.cpp:52
types::ModularConvertedValueConfig getModularConvertedValueConfig(const std::string &name)
Get a ModularConvertedValue typed Config attribute by name.
Definition Config.cpp:40
std::uint32_t getUint(const std::string name)
Get a Uint (std::uint32_t) typed Config attribute by name.
Definition Config.cpp:22
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
bool getBool(const std::string name)
Get a Bool typed Config attribute by name.
Definition Config.cpp:10
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
void setString(const std::string name, std::string value, ConfigTag tag)
Definition Config.cpp:243
std::map< std::string, ConfigItemWithMetadata > items
Definition Config.h:252
void setUint(const std::string name, std::uint32_t value, ConfigTag tag)
Definition Config.cpp:217
void setLinearConfig(const std::string name, types::LinearConfig &&value, ConfigTag tag)
Definition Config.cpp:229
std::list< float > getFloatList(const std::string name)
Get a FloatList (std::list<float>) typed Config attribute by name.
Definition Config.cpp:58
void setUintList(const std::string name, std::list< std::uint32_t > value, ConfigTag tag)
Definition Config.cpp:261
void setBoolList(const std::string name, std::list< bool > value, ConfigTag tag)
Definition Config.cpp:249
void setIntList(const std::string name, std::list< std::int32_t > value, ConfigTag tag)
Definition Config.cpp:267
void setFloat(const std::string name, float value, ConfigTag tag)
Definition Config.cpp:211
std::int32_t getInt(const std::string name)
Get a Int (std::int32_t) typed Config attribute by name.
Definition Config.cpp:28
void addControllerConfig(const std::string controllerName, std::shared_ptr< Config > controllerConfig)
Add a Config for a controller with name.
Definition Config.cpp:284
std::map< std::string, std::shared_ptr< Config > > controllerConfigs
Definition Config.h:376
Config & getControllerConfig(const std::string controllerName)
Definition Config.cpp:273
The LinearConfig class represents a linear conversion and has a factor and offset.
Definition Types.h:17
overloaded(Ts...) -> overloaded< Ts... >
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:325
void printList(std::stringstream &s, std::list< T > list)
Definition Config.cpp:104
ConfigTag
The ConfigTag is used when setting a config value.
Definition Config.h:62
std::string tagName(ConfigTag tag)
Definition Config.cpp:301
std::string Variant::get< std::string >() const
Definition Variant.cpp:284
void Variant::set< std::uint32_t >(const std::uint32_t &value)
Definition Variant.cpp:198
std::int32_t Variant::get< std::int32_t >() const
Definition Variant.cpp:94
void Variant::set< std::string >(const std::string &value)
Definition Variant.cpp:312
void Variant::set< std::int32_t >(const std::int32_t &value)
Definition Variant.cpp:104
std::uint32_t Variant::get< std::uint32_t >() const
Definition Variant.cpp:187