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 {
91 if (s.
precision == std::numeric_limits<int>::max())
93 if constexpr (std::is_same_v<T, double>
94 || std::is_same_v<T, gz::math::Angle>
95 || std::is_same_v<T, gz::math::Vector2d>
96 || std::is_same_v<T, gz::math::Vector3d>
97 || std::is_same_v<T, gz::math::Quaterniond>
98 || std::is_same_v<T, gz::math::Pose3d>)
100 os << std::setprecision(std::numeric_limits<double>::max_digits10);
102 else if constexpr (std::is_same_v<T, float>
103 || std::is_same_v<T, gz::math::Color>)
105 os << std::setprecision(std::numeric_limits<float>::max_digits10);
117 template<
class... Ts>
121 std::visit([&os, &sv](
auto const &v)
140 public:
Param(
const std::string &_key,
const std::string &_typeName,
141 const std::string &_default,
bool _required,
142 const std::string &_description =
"");
152 public:
Param(
const std::string &_key,
const std::string &_typeName,
153 const std::string &_default,
bool _required,
155 const std::string &_description =
"");
167 public:
Param(
const std::string &_key,
const std::string &_typeName,
168 const std::string &_default,
bool _required,
169 const std::string &_minValue,
const std::string &_maxValue,
170 const std::string &_description =
"");
182 public:
Param(
const std::string &_key,
const std::string &_typeName,
183 const std::string &_default,
bool _required,
184 const std::string &_minValue,
const std::string &_maxValue,
186 const std::string &_description =
"");
195 public:
Param(
Param &&_param) noexcept =
default;
201 public:
Param &operator=(
const Param &_param);
206 public:
Param &operator=(
Param &&_param) noexcept =
default;
209 public:
virtual ~
Param();
214 public: std::string GetAsString(
221 public: std::string GetAsString(
228 public: std::string GetDefaultAsString(
235 public: std::string GetDefaultAsString(
244 public: std::optional<std::string> GetMinValueAsString(
253 public: std::optional<std::string> GetMinValueAsString(
262 public: std::optional<std::string> GetMaxValueAsString(
271 public: std::optional<std::string> GetMaxValueAsString(
280 public:
bool SetFromString(
const std::string &_value,
281 bool _ignoreParentAttributes);
289 public:
bool SetFromString(
const std::string &_value,
290 bool _ignoreParentAttributes,
295 public:
bool SetFromString(
const std::string &_value);
300 public:
bool SetFromString(
const std::string &_value,
313 public:
bool SetParentElement(
ElementPtr _parentElement);
321 public:
bool SetParentElement(
ElementPtr _parentElement,
325 public:
void Reset();
338 public:
bool Reparse();
356 public:
const std::string &GetKey()
const;
361 public:
template<
typename Type>
366 public:
const std::string &GetTypeName()
const;
370 public:
bool GetRequired()
const;
374 public:
bool GetSet()
const;
380 public:
bool IgnoresParentElementAttribute()
const;
389 public:
template<
typename T>
390 void SetUpdateFunc(T _updateFunc);
394 public:
void Update();
406 public:
template<
typename T>
407 bool Set(
const T &_value);
415 public:
template<
typename T>
416 bool Set(
const T &_value,
422 public:
bool GetAny(std::any &_anyVal)
const;
428 public:
bool GetAny(std::any &_anyVal,
sdf::Errors &_errors)
const;
434 public:
template<
typename T>
435 bool Get(T &_value)
const;
442 public:
template<
typename T>
450 public:
template<
typename T>
451 bool GetDefault(T &_value)
const;
458 public:
template<
typename T>
459 bool GetDefault(T &_value,
464 public:
void SetDescription(
const std::string &_desc);
468 public: std::string GetDescription()
const;
472 public:
bool ValidateValue()
const;
477 public:
bool ValidateValue(
sdf::Errors &_errors)
const;
491 private: std::unique_ptr<ParamPrivate> dataPtr;
524 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
531 gz::math::Quaterniond,
565 public:
void Init(
const std::string &_key,
const std::string &_typeName,
566 const std::string &_default,
bool _required,
568 const std::string &_description);
580 public:
void Init(
const std::string &_key,
const std::string &_typeName,
581 const std::string &_default,
bool _required,
582 const std::string &_minValue,
const std::string &_maxValue,
584 const std::string &_description);
593 const std::string &_typeName,
594 const std::string &_valueStr,
608 const std::string &_typeName,
610 std::string &_valueStr,
615 public:
template<
typename T>
624 if constexpr (std::is_same_v<T, bool>)
626 else if constexpr (std::is_same_v<T, char>)
628 else if constexpr (std::is_same_v<T, std::string>)
630 else if constexpr (std::is_same_v<T, int>)
632 else if constexpr (std::is_same_v<T, std::uint64_t>)
634 else if constexpr (std::is_same_v<T, unsigned int>)
635 return "unsigned int";
636 else if constexpr (std::is_same_v<T, double>)
638 else if constexpr (std::is_same_v<T, float>)
640 else if constexpr (std::is_same_v<T, sdf::Time>)
642 else if constexpr (std::is_same_v<T, gz::math::Angle>)
644 else if constexpr (std::is_same_v<T, gz::math::Color>)
646 else if constexpr (std::is_same_v<T, gz::math::Vector2i>)
648 else if constexpr (std::is_same_v<T, gz::math::Vector2d>)
650 else if constexpr (std::is_same_v<T, gz::math::Vector3d>)
652 else if constexpr (std::is_same_v<T, gz::math::Quaterniond>)
654 else if constexpr (std::is_same_v<T, gz::math::Pose3d>)
664 this->dataPtr->updateFunc = _updateFunc;
672 bool result = this->Set<T>(_value, errors);
684 std::stringstream ss;
685 ss << ParamStreamer<T>{_value, std::numeric_limits<int>::max()};
691 "Unable to set parameter["
692 + this->dataPtr->key +
"]."
693 +
"Type used must have a stream input and output operator,"
694 +
"which allows proper functioning of Param."});
704 bool result = this->Get<T>(_value, errors);
714 T *value = std::get_if<T>(&this->dataPtr->value);
721 std::string typeStr = this->dataPtr->TypeToString<T>();
725 "Unknown parameter type[" + std::string(
typeid(T).name()) +
"]"});
731 bool success = this->dataPtr->ValueFromStringImpl(
732 typeStr, valueStr, pv, _errors);
736 _value = std::get<T>(pv);
738 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
745 std::stringstream tmp;
746 if (valueStr ==
"true" || valueStr ==
"1")
766 bool result =
this>GetDefault<T>(_value, errors);
776 std::stringstream ss;
781 std::numeric_limits<int>::max()};
787 "Unable to convert parameter["
788 + this->dataPtr->key +
"] "
790 + this->dataPtr->typeName +
"], to "
791 +
"type[" +
typeid(T).name() +
"]"});
799 template<
typename Type>
802 return std::holds_alternative<Type>(this->dataPtr->value);