13 #include <unordered_map> 14 #include <yaml-cpp/yaml.h> 16 #include <boost/algorithm/string.hpp> 48 Parameter(
const std::string& name,
const std::string& type)
49 : name(name), type(type)
61 const std::string&
getName()
const {
return name; }
66 const std::string&
getType()
const {
return type; }
71 virtual const int getTypeByteSize()
const = 0;
76 virtual const bool isTypeSigned()
const = 0;
82 virtual const bool isTypeArithmetic()
const = 0;
87 void setValid(
const bool isParamValid) { this->isParamValid = isParamValid; }
92 const bool isValid()
const {
return isParamValid; }
171 parameters = std::unordered_map<std::string, std::shared_ptr<Parameter>>();
180 return parameters.find(name) != parameters.end();
186 const int size()
const {
return parameters.size(); }
194 std::stringstream ss = std::stringstream();
195 for (
auto item : parameters)
197 ss <<
"Name: " << item.first <<
", Type: " << item.second->getType()
200 ss <<
"List size: " << size();
218 template <
typename T>
220 const T& value,
const bool isValid =
true)
222 std::shared_ptr<Parameter> param =
226 parameters.insert(std::make_pair(name, param));
234 template <
typename T>
237 if (parameters.find(name) == parameters.end())
238 throw std::out_of_range(
"Key: \"" + name +
"\" not in Map!");
239 if (
sizeof(value) > parameters.at(name)->getTypeByteSize())
240 throw std::invalid_argument(
"Key: \"" + name +
"\" with Type: \"" +
241 parameters.at(name)->getType() +
242 "\" doesn't match given variable type.");
251 template <
typename T>
252 const std::shared_ptr<GenericParameter<T>>&
255 if (parameters.find(name) == parameters.end())
256 throw std::out_of_range(
"Key: \"" + name +
"\" not in Map!");
261 if (parameters.find(name) == parameters.end())
262 throw std::out_of_range(
"Key: \"" + name +
"\" not in Map!");
263 return parameters.at(name);
272 std::string& out)
const 274 if (parameters.find(name) == parameters.end())
275 throw std::out_of_range(
"Key: \"" + name +
"\" not in Map!");
276 const std::string&
type = parameters.at(name)->getType();
277 int size = getParameter(name)->getTypeByteSize();
278 bool isSigned = getParameter(name)->isTypeSigned();
279 bool isArithmetic = getParameter(name)->isTypeArithmetic();
280 if (type.compare(
"int8_t") == 0)
282 if (size > 1 || !isSigned || !isArithmetic)
283 throw std::invalid_argument(
"Parameter typename \"" + type +
284 "\" doesn't match given variable type!");
286 getParameterValue(name, value);
287 out = std::to_string(static_cast<int>(value));
289 else if (type.compare(
"uint8_t") == 0)
291 if (size > 1 || isSigned || !isArithmetic)
292 throw std::invalid_argument(
"Parameter typename \"" + type +
293 "\" doesn't match given variable type!");
295 getParameterValue(name, value);
296 out = std::to_string(static_cast<unsigned int>(value));
298 else if (type.compare(
"int16_t") == 0)
300 if (size > 2 || !isSigned || !isArithmetic)
301 throw std::invalid_argument(
"Parameter typename \"" + type +
302 "\" doesn't match given variable type!");
304 getParameterValue(name, value);
305 out = std::to_string(static_cast<int>(value));
307 else if (type.compare(
"uint16_t") == 0)
309 if (size > 2 || isSigned || !isArithmetic)
310 throw std::invalid_argument(
"Parameter typename \"" + type +
311 "\" doesn't match given variable type!");
312 unsigned short value;
313 getParameterValue(name, value);
314 out = std::to_string(static_cast<unsigned int>(value));
316 else if (type.compare(
"int32_t") == 0)
318 if (size > 4 || !isSigned || !isArithmetic)
319 throw std::invalid_argument(
"Parameter typename \"" + type +
320 "\" doesn't match given variable type!");
322 getParameterValue(name, value);
323 out = std::to_string(value);
325 else if (type.compare(
"uint32_t") == 0)
327 if (size > 4 || isSigned || !isArithmetic)
328 throw std::invalid_argument(
"Parameter typename \"" + type +
329 "\" doesn't match given variable type!");
331 getParameterValue(name, value);
332 out = std::to_string(value);
334 else if (type.compare(
"int64_t") == 0)
336 if (size > 8 || !isSigned || !isArithmetic)
337 throw std::invalid_argument(
"Parameter typename \"" + type +
338 "\" doesn't match given variable type!");
340 getParameterValue(name, value);
341 out = std::to_string(value);
343 else if (type.compare(
"uint64_t") == 0)
345 if (size > 8 || isSigned || !isArithmetic)
346 throw std::invalid_argument(
"Parameter typename \"" + type +
347 "\" doesn't match given variable type!");
349 getParameterValue(name, value);
350 out = std::to_string(value);
352 else if (type.compare(
"float32_t") == 0)
354 if (size > 4 || !isArithmetic)
355 throw std::invalid_argument(
"Parameter typename \"" + type +
356 "\" doesn't match given variable type!");
358 getParameterValue(name, value);
359 out = std::to_string(value);
361 else if (type.compare(
"float64_t") == 0)
363 if (size > 8 || !isArithmetic)
364 throw std::invalid_argument(
"Parameter typename \"" + type +
365 "\" doesn't match given variable type!");
367 getParameterValue(name, value);
368 out = std::to_string(value);
370 else if (type.compare(
"string_t") == 0)
374 throw std::invalid_argument(
"Parameter typename \"" + type +
375 "\" doesn't match given variable type!");
376 getParameterValue(name, out);
378 else if (type.compare(
"string_t[]") == 0)
381 throw std::invalid_argument(
"Parameter typename \"" + type +
382 "\" doesn't match given variable type!");
384 std::vector<std::string> sArray;
385 getParameterValue(name, sArray);
386 std::stringstream ss = std::stringstream();
387 for (std::string s : sArray)
391 out = ss.str().substr(1, std::string::npos);
395 throw std::invalid_argument(
"Parameter typename \"" + type +
396 "\" is an unsupported type!");
407 const std::string& input,
410 if (parameters.find(name) == parameters.end())
411 throw std::out_of_range(
"Key: \"" + name +
"\" not in Map!");
417 const std::string&
type = parameters.at(name)->getType();
418 int size = getParameter(name)->getTypeByteSize();
419 bool isSigned = getParameter(name)->isTypeSigned();
420 bool isArithmetic = getParameter(name)->isTypeArithmetic();
421 if (type.compare(
"int8_t") == 0)
423 if (size > 1 || !isSigned || !isArithmetic)
424 throw std::invalid_argument(
"Parameter typename \"" + type +
425 "\" doesn't match stored variable type!");
426 int value = std::stoi(input);
428 ->
setData(static_cast<char>(value));
430 else if (type.compare(
"uint8_t") == 0)
432 if (size > 1 || isSigned || !isArithmetic)
433 throw std::invalid_argument(
"Parameter typename \"" + type +
434 "\" doesn't match stored variable type!");
435 unsigned long value = std::stoul(input);
437 parameters[
name])->
setData(static_cast<unsigned char>(value));
439 else if (type.compare(
"int16_t") == 0)
441 if (size > 2 || !isSigned || !isArithmetic)
442 throw std::invalid_argument(
"Parameter typename \"" + type +
443 "\" doesn't match stored variable type!");
444 int value = std::stoi(input);
446 ->
setData(static_cast<short>(value));
448 else if (type.compare(
"uint16_t") == 0)
450 if (size > 2 || isSigned || !isArithmetic)
451 throw std::invalid_argument(
"Parameter typename \"" + type +
452 "\" doesn't match stored variable type!");
453 unsigned long value = std::stoul(input);
455 parameters[
name])->
setData(static_cast<unsigned short>(value));
457 else if (type.compare(
"int32_t") == 0)
459 if (size > 4 || !isSigned || !isArithmetic)
460 throw std::invalid_argument(
"Parameter typename \"" + type +
461 "\" doesn't match stored variable type!");
462 int value = std::stoi(input);
466 else if (type.compare(
"uint32_t") == 0)
468 if (size > 4 || isSigned || !isArithmetic)
469 throw std::invalid_argument(
"Parameter typename \"" + type +
470 "\" doesn't match stored variable type!");
471 unsigned long value = std::stoul(input);
475 else if (type.compare(
"int64_t") == 0)
477 if (size > 8 || !isSigned || !isArithmetic)
478 throw std::invalid_argument(
"Parameter typename \"" + type +
479 "\" doesn't match stored variable type!");
480 long value = std::stol(input);
484 else if (type.compare(
"uint64_t") == 0)
486 if (size > 8 || isSigned || !isArithmetic)
487 throw std::invalid_argument(
"Parameter typename \"" + type +
488 "\" doesn't match stored variable type!");
489 unsigned long value = std::stoul(input);
493 else if (type.compare(
"float32_t") == 0)
495 if (size > 4 || !isArithmetic)
496 throw std::invalid_argument(
"Parameter typename \"" + type +
497 "\" doesn't match stored variable type!");
498 float value = std::stof(input);
502 else if (type.compare(
"float64_t") == 0)
504 if (size > 8 || !isArithmetic)
505 throw std::invalid_argument(
"Parameter typename \"" + type +
506 "\" doesn't match stored variable type!");
507 double value = std::stod(input);
511 else if (type.compare(
"string_t") == 0)
514 throw std::invalid_argument(
"Parameter typename \"" + type +
515 "\" doesn't match stored variable type!");
519 else if (type.compare(
"string_t[]") == 0)
522 throw std::invalid_argument(
"Parameter typename \"" + type +
523 "\" doesn't match stored variable type!");
524 std::vector<std::string> split;
525 boost::split(split, input, boost::is_any_of(
" "));
532 throw std::invalid_argument(
"Parameter typename \"" + type +
533 "\" is an unsupported type!");
555 std::shared_ptr<Parameter> param;
557 if (type.compare(
"int8_t") == 0)
564 else if (type.compare(
"uint8_t") == 0)
566 unsigned char value = 0;
567 param = std::shared_ptr<Parameter>(
572 else if (type.compare(
"int16_t") == 0)
579 else if (type.compare(
"uint16_t") == 0)
581 unsigned short value = 0;
582 param = std::shared_ptr<Parameter>(
587 else if (type.compare(
"int32_t") == 0)
593 else if (type.compare(
"uint32_t") == 0)
595 unsigned int value = 0;
596 param = std::shared_ptr<Parameter>(
601 else if (type.compare(
"int64_t") == 0)
608 else if (type.compare(
"uint64_t") == 0)
610 unsigned long value = 0;
611 param = std::shared_ptr<Parameter>(
616 else if (type.compare(
"float32_t") == 0)
623 else if (type.compare(
"float64_t") == 0)
631 else if (type.compare(
"string_t") == 0)
633 param = std::shared_ptr<Parameter>(
638 else if (type.compare(
"string_t[]") == 0)
640 std::vector<std::string> value;
641 param = std::shared_ptr<Parameter>(
648 throw std::invalid_argument(
"Parameter typename \"" + type +
649 "\" is an unsupported type!");
653 parameters.insert(std::make_pair(name, param));
657 std::unordered_map<std::string, std::shared_ptr<Parameter>>
parameters;
661 #endif // PARAMETER_H ParameterMap()
Parameter::ParameterMap constructor.
const std::shared_ptr< GenericParameter< T > > & getDynamicParameter(const std::string &name) const
Get the dynamic typed parameter contained in this map with the given name.
const std::string & getName() const
Get the name of this parameter.
Classes in this Namespace implement dynamic type functionality.
std::string toString() const
Get the string representation of all parameters contained within this map.
void setData(const T &m_data)
Set the value this paramter contains.
void setValid(const bool isParamValid)
Defines whether this paramters value is NaN/invalid or not.
const bool isTypeArithmetic() const
Is this parameter an arithmetic type?
void getParameterValueAsString(const std::string &name, std::string &out) const
Get the string representation of a value from a parameter in this map with the given name...
virtual ~Parameter()
Parameter::Parameter destructor.
The Parameter::ParameterMap class provides a map functionality for Parameter::Parameter objects conta...
const T & getData() const
Get the value this paramter contains.
const bool isValid() const
Is this parameters value NaN/invalid?
The Parameter::GenericParameter class is a sub class of the Parameter::Parameter class.
const bool isParamInMap(const std::string &name) const
Is a parameter with the given name contained within this map?.
const std::string & getType() const
Get the type of this parameter.
Parameter(const std::string &name, const std::string &type)
Parameter::Parameter constructor.
void insertParameter(const std::string &name, const std::string &type, const bool isValid=true)
Insert a new parameter in this map, with the given name and type.
void insertParameter(const std::string &name, const std::string &type, const T &value, const bool isValid=true)
Insert a new parameter in this map, with the given name, type and value.
const std::shared_ptr< Parameter > & getParameter(const std::string &name) const
const int size() const
Get the amount of parameters contained within this map.
void setParameterValueAsString(const std::string &name, const std::string &input, const bool isValid=true)
Set the value of a parameter in this map with the given name from a string.
const bool isTypeSigned() const
Get the sign of this parameter.
const int getTypeByteSize() const
Get the byte size of this parameter.
void getParameterValue(const std::string &name, T &value) const
Get the value of a parameter in this map with the given name.
GenericParameter(const std::string &name, const std::string &type)
Parameter::GenericParameter constructor.
std::unordered_map< std::string, std::shared_ptr< Parameter > > parameters