18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
36 #include <ignition/math.hh>
48 #pragma warning(disable: 4251)
54 inline namespace SDF_VERSION_NAMESPACE {
92 os << std::setprecision(std::numeric_limits<double>::max_digits10) << s.
val;
99 os << std::setprecision(std::numeric_limits<float>::max_digits10) << s.
val;
103 template<
class... Ts>
107 std::visit([&os](
auto const &v)
126 public:
Param(
const std::string &_key,
const std::string &_typeName,
127 const std::string &_default,
bool _required,
128 const std::string &_description =
"");
140 public:
Param(
const std::string &_key,
const std::string &_typeName,
141 const std::string &_default,
bool _required,
142 const std::string &_minValue,
const std::string &_maxValue,
143 const std::string &_description =
"");
152 public:
Param(
Param &&_param) noexcept =
default;
158 public:
Param &operator=(
const Param &_param);
163 public:
Param &operator=(
Param &&_param) noexcept =
default;
166 public:
virtual ~
Param();
171 public: std::string GetAsString(
177 public: std::string GetDefaultAsString(
185 public: std::optional<std::string> GetMinValueAsString(
193 public: std::optional<std::string> GetMaxValueAsString(
201 public:
bool SetFromString(
const std::string &_value,
202 bool _ignoreParentAttributes);
206 public:
bool SetFromString(
const std::string &_value);
218 public:
bool SetParentElement(
ElementPtr _parentElement);
221 public:
void Reset();
234 public:
bool Reparse();
238 public:
const std::string &GetKey()
const;
243 public:
template<
typename Type>
248 public:
const std::string &GetTypeName()
const;
252 public:
bool GetRequired()
const;
256 public:
bool GetSet()
const;
262 public:
bool IgnoresParentElementAttribute()
const;
271 public:
template<
typename T>
272 void SetUpdateFunc(T _updateFunc);
276 public:
void Update();
283 public:
template<
typename T>
284 bool Set(
const T &_value);
289 public:
bool GetAny(std::any &_anyVal)
const;
295 public:
template<
typename T>
296 bool Get(T &_value)
const;
302 public:
template<
typename T>
303 bool GetDefault(T &_value)
const;
307 public:
void SetDescription(
const std::string &_desc);
311 public: std::string GetDescription()
const;
315 public:
bool ValidateValue()
const;
329 private: std::unique_ptr<ParamPrivate> dataPtr;
361 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
363 ignition::math::Angle,
364 ignition::math::Color,
365 ignition::math::Vector2i,
366 ignition::math::Vector2d,
367 ignition::math::Vector3d,
368 ignition::math::Quaterniond,
400 const std::string &_typeName,
401 const std::string &_valueStr,
411 public:
bool StringFromValueImpl(
413 const std::string &_typeName,
415 std::string &_valueStr)
const;
426 public:
bool StringFromValueImpl(
428 const std::string &_typeName,
430 const std::optional<std::string> &_originalStr,
431 std::string &_valueStr)
const;
435 public:
template<
typename T>
436 std::string TypeToString()
const;
441 std::string ParamPrivate::TypeToString()
const
444 if constexpr (std::is_same_v<T, bool>)
446 else if constexpr (std::is_same_v<T, char>)
448 else if constexpr (std::is_same_v<T, std::string>)
450 else if constexpr (std::is_same_v<T, int>)
452 else if constexpr (std::is_same_v<T, std::uint64_t>)
454 else if constexpr (std::is_same_v<T, unsigned int>)
455 return "unsigned int";
456 else if constexpr (std::is_same_v<T, double>)
458 else if constexpr (std::is_same_v<T, float>)
460 else if constexpr (std::is_same_v<T, sdf::Time>)
462 else if constexpr (std::is_same_v<T, ignition::math::Angle>)
464 else if constexpr (std::is_same_v<T, ignition::math::Color>)
466 else if constexpr (std::is_same_v<T, ignition::math::Vector2i>)
468 else if constexpr (std::is_same_v<T, ignition::math::Vector2d>)
470 else if constexpr (std::is_same_v<T, ignition::math::Vector3d>)
472 else if constexpr (std::is_same_v<T, ignition::math::Quaterniond>)
474 else if constexpr (std::is_same_v<T, ignition::math::Pose3d>)
482 void Param::SetUpdateFunc(T _updateFunc)
484 this->dataPtr->updateFunc = _updateFunc;
489 bool Param::Set(
const T &_value)
493 std::stringstream ss;
495 return this->SetFromString(ss.str(),
true);
499 sdferr <<
"Unable to set parameter["
500 << this->dataPtr->key <<
"]."
501 <<
"Type used must have a stream input and output operator,"
502 <<
"which allows proper functioning of Param.\n";
509 bool Param::Get(T &_value)
const
511 T *value = std::get_if<T>(&this->dataPtr->value);
518 std::string typeStr = this->dataPtr->TypeToString<T>();
521 sdferr <<
"Unknown parameter type[" <<
typeid(T).name() <<
"]\n";
525 std::string valueStr = this->GetAsString();
527 bool success = this->dataPtr->ValueFromStringImpl(typeStr, valueStr, pv);
531 _value = std::get<T>(pv);
533 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
540 std::stringstream tmp;
541 if (valueStr ==
"true" || valueStr ==
"1")
558 bool Param::GetDefault(T &_value)
const
560 std::stringstream ss;
569 sdferr <<
"Unable to convert parameter["
570 << this->dataPtr->key <<
"] "
572 << this->dataPtr->typeName <<
"], to "
573 <<
"type[" <<
typeid(T).name() <<
"]\n";
581 template<
typename Type>
582 bool Param::IsType()
const
584 return std::holds_alternative<Type>(this->dataPtr->value);