18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
36 #include <gz/math/Angle.hh>
37 #include <gz/math/Color.hh>
38 #include <gz/math/Pose3.hh>
39 #include <gz/math/Quaternion.hh>
40 #include <gz/math/Vector2.hh>
41 #include <gz/math/Vector3.hh>
53 #pragma warning(disable: 4251)
59 inline namespace SDF_VERSION_NAMESPACE {
89 template<
typename ParamVariant>
96 if (s.
precision == std::numeric_limits<int>::max())
98 if constexpr (std::is_same_v<T, double>
99 || std::is_same_v<T, gz::math::Angle>
100 || std::is_same_v<T, gz::math::Vector2d>
101 || std::is_same_v<T, gz::math::Vector3d>
102 || std::is_same_v<T, gz::math::Quaterniond>
103 || std::is_same_v<T, gz::math::Pose3d>)
105 os << std::setprecision(std::numeric_limits<double>::max_digits10);
107 else if constexpr (std::is_same_v<T, float>
108 || std::is_same_v<T, gz::math::Color>)
110 os << std::setprecision(std::numeric_limits<float>::max_digits10);
122 template<
class... Ts>
126 std::visit([&os, &sv](
auto const &v)
145 public:
Param(
const std::string &_key,
const std::string &_typeName,
146 const std::string &_default,
bool _required,
147 const std::string &_description =
"");
157 public:
Param(
const std::string &_key,
const std::string &_typeName,
158 const std::string &_default,
bool _required,
160 const std::string &_description =
"");
172 public:
Param(
const std::string &_key,
const std::string &_typeName,
173 const std::string &_default,
bool _required,
174 const std::string &_minValue,
const std::string &_maxValue,
175 const std::string &_description =
"");
187 public:
Param(
const std::string &_key,
const std::string &_typeName,
188 const std::string &_default,
bool _required,
189 const std::string &_minValue,
const std::string &_maxValue,
191 const std::string &_description =
"");
200 public:
Param(
Param &&_param) noexcept =
default;
206 public:
Param &operator=(
const Param &_param);
211 public:
Param &operator=(
Param &&_param) noexcept =
default;
214 public:
virtual ~
Param();
219 public: std::string GetAsString(
226 public: std::string GetAsString(
233 public: std::string GetDefaultAsString(
240 public: std::string GetDefaultAsString(
249 public: std::optional<std::string> GetMinValueAsString(
258 public: std::optional<std::string> GetMinValueAsString(
267 public: std::optional<std::string> GetMaxValueAsString(
276 public: std::optional<std::string> GetMaxValueAsString(
285 public:
bool SetFromString(
const std::string &_value,
286 bool _ignoreParentAttributes);
294 public:
bool SetFromString(
const std::string &_value,
295 bool _ignoreParentAttributes,
300 public:
bool SetFromString(
const std::string &_value);
305 public:
bool SetFromString(
const std::string &_value,
318 public:
bool SetParentElement(
ElementPtr _parentElement);
326 public:
bool SetParentElement(
ElementPtr _parentElement,
330 public:
void Reset();
343 public:
bool Reparse();
361 public:
const std::string &GetKey()
const;
366 public:
template<
typename Type>
371 public:
const std::string &GetTypeName()
const;
375 public:
bool GetRequired()
const;
379 public:
bool GetSet()
const;
385 public:
bool IgnoresParentElementAttribute()
const;
394 public:
template<
typename T>
395 void SetUpdateFunc(T _updateFunc);
399 public:
void Update();
411 public:
template<
typename T>
412 bool Set(
const T &_value);
420 public:
template<
typename T>
421 bool Set(
const T &_value,
427 public:
bool GetAny(std::any &_anyVal)
const;
433 public:
bool GetAny(std::any &_anyVal,
sdf::Errors &_errors)
const;
439 public:
template<
typename T>
440 bool Get(T &_value)
const;
447 public:
template<
typename T>
455 public:
template<
typename T>
456 bool GetDefault(T &_value)
const;
463 public:
template<
typename T>
464 bool GetDefault(T &_value,
469 public:
void SetDescription(
const std::string &_desc);
473 public: std::string GetDescription()
const;
477 public:
bool ValidateValue()
const;
482 public:
bool ValidateValue(
sdf::Errors &_errors)
const;
496 private: std::unique_ptr<ParamPrivate> dataPtr;
529 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
536 gz::math::Quaterniond,
570 public:
void Init(
const std::string &_key,
const std::string &_typeName,
571 const std::string &_default,
bool _required,
573 const std::string &_description);
585 public:
void Init(
const std::string &_key,
const std::string &_typeName,
586 const std::string &_default,
bool _required,
587 const std::string &_minValue,
const std::string &_maxValue,
589 const std::string &_description);
598 const std::string &_typeName,
599 const std::string &_valueStr,
613 const std::string &_typeName,
615 std::string &_valueStr,
620 public:
template<
typename T>
629 if constexpr (std::is_same_v<T, bool>)
631 else if constexpr (std::is_same_v<T, char>)
633 else if constexpr (std::is_same_v<T, std::string>)
635 else if constexpr (std::is_same_v<T, int>)
637 else if constexpr (std::is_same_v<T, std::uint64_t>)
639 else if constexpr (std::is_same_v<T, unsigned int>)
640 return "unsigned int";
641 else if constexpr (std::is_same_v<T, double>)
643 else if constexpr (std::is_same_v<T, float>)
645 else if constexpr (std::is_same_v<T, sdf::Time>)
647 else if constexpr (std::is_same_v<T, gz::math::Angle>)
649 else if constexpr (std::is_same_v<T, gz::math::Color>)
651 else if constexpr (std::is_same_v<T, gz::math::Vector2i>)
653 else if constexpr (std::is_same_v<T, gz::math::Vector2d>)
655 else if constexpr (std::is_same_v<T, gz::math::Vector3d>)
657 else if constexpr (std::is_same_v<T, gz::math::Quaterniond>)
659 else if constexpr (std::is_same_v<T, gz::math::Pose3d>)
669 this->dataPtr->updateFunc = _updateFunc;
677 bool result = this->Set<T>(_value, errors);
689 std::stringstream ss;
690 ss << ParamStreamer<T>{_value, std::numeric_limits<int>::max()};
696 "Unable to set parameter["
697 + this->dataPtr->key +
"]."
698 +
"Type used must have a stream input and output operator,"
699 +
"which allows proper functioning of Param."});
709 bool result = this->Get<T>(_value, errors);
719 T *value = std::get_if<T>(&this->dataPtr->value);
726 std::string typeStr = this->dataPtr->TypeToString<T>();
730 "Unknown parameter type[" + std::string(
typeid(T).name()) +
"]"});
736 bool success = this->dataPtr->ValueFromStringImpl(
737 typeStr, valueStr, pv, _errors);
741 _value = std::get<T>(pv);
743 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
750 std::stringstream tmp;
751 if (valueStr ==
"true" || valueStr ==
"1")
771 bool result =
this>GetDefault<T>(_value, errors);
781 std::stringstream ss;
786 std::numeric_limits<int>::max()};
792 "Unable to convert parameter["
793 + this->dataPtr->key +
"] "
795 + this->dataPtr->typeName +
"], to "
796 +
"type[" +
typeid(T).name() +
"]"});
804 template<
typename Type>
807 return std::holds_alternative<Type>(this->dataPtr->value);