18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
36 #include <ignition/math.hh>
40 #include "sdf/sdf_config.h"
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;
331 private:
bool ValueFromString(
const std::string &_value);
334 private: std::unique_ptr<ParamPrivate> dataPtr;
366 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
368 ignition::math::Angle,
369 ignition::math::Color,
370 ignition::math::Vector2i,
371 ignition::math::Vector2d,
372 ignition::math::Vector3d,
373 ignition::math::Quaterniond,
405 const std::string &_typeName,
406 const std::string &_valueStr,
411 public:
template<
typename T>
412 std::string TypeToString()
const;
417 std::string ParamPrivate::TypeToString()
const
420 if constexpr (std::is_same_v<T, bool>)
422 else if constexpr (std::is_same_v<T, char>)
424 else if constexpr (std::is_same_v<T, std::string>)
426 else if constexpr (std::is_same_v<T, int>)
428 else if constexpr (std::is_same_v<T, std::uint64_t>)
430 else if constexpr (std::is_same_v<T, unsigned int>)
431 return "unsigned int";
432 else if constexpr (std::is_same_v<T, double>)
434 else if constexpr (std::is_same_v<T, float>)
436 else if constexpr (std::is_same_v<T, sdf::Time>)
438 else if constexpr (std::is_same_v<T, ignition::math::Angle>)
440 else if constexpr (std::is_same_v<T, ignition::math::Color>)
442 else if constexpr (std::is_same_v<T, ignition::math::Vector2i>)
444 else if constexpr (std::is_same_v<T, ignition::math::Vector2d>)
446 else if constexpr (std::is_same_v<T, ignition::math::Vector3d>)
448 else if constexpr (std::is_same_v<T, ignition::math::Quaterniond>)
450 else if constexpr (std::is_same_v<T, ignition::math::Pose3d>)
458 void Param::SetUpdateFunc(T _updateFunc)
460 this->dataPtr->updateFunc = _updateFunc;
465 bool Param::Set(
const T &_value)
469 std::stringstream ss;
471 return this->SetFromString(ss.str(),
true);
475 sdferr <<
"Unable to set parameter["
476 << this->dataPtr->key <<
"]."
477 <<
"Type used must have a stream input and output operator,"
478 <<
"which allows proper functioning of Param.\n";
485 bool Param::Get(T &_value)
const
487 T *value = std::get_if<T>(&this->dataPtr->value);
494 std::string typeStr = this->dataPtr->TypeToString<T>();
497 sdferr <<
"Unknown parameter type[" <<
typeid(T).name() <<
"]\n";
501 std::string valueStr = this->GetAsString();
503 bool success = this->dataPtr->ValueFromStringImpl(typeStr, valueStr, pv);
507 _value = std::get<T>(pv);
509 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
516 std::stringstream tmp;
517 if (valueStr ==
"true" || valueStr ==
"1")
534 bool Param::GetDefault(T &_value)
const
536 std::stringstream ss;
545 sdferr <<
"Unable to convert parameter["
546 << this->dataPtr->key <<
"] "
548 << this->dataPtr->typeName <<
"], to "
549 <<
"type[" <<
typeid(T).name() <<
"]\n";
557 template<
typename Type>
558 bool Param::IsType()
const
560 return std::holds_alternative<Type>(this->dataPtr->value);