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;
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(
const PrintConfig &_config,
412 const std::string &_typeName,
414 std::string &_valueStr)
const;
418 public:
template<
typename T>
419 std::string TypeToString()
const;
424 std::string ParamPrivate::TypeToString()
const
427 if constexpr (std::is_same_v<T, bool>)
429 else if constexpr (std::is_same_v<T, char>)
431 else if constexpr (std::is_same_v<T, std::string>)
433 else if constexpr (std::is_same_v<T, int>)
435 else if constexpr (std::is_same_v<T, std::uint64_t>)
437 else if constexpr (std::is_same_v<T, unsigned int>)
438 return "unsigned int";
439 else if constexpr (std::is_same_v<T, double>)
441 else if constexpr (std::is_same_v<T, float>)
443 else if constexpr (std::is_same_v<T, sdf::Time>)
445 else if constexpr (std::is_same_v<T, ignition::math::Angle>)
447 else if constexpr (std::is_same_v<T, ignition::math::Color>)
449 else if constexpr (std::is_same_v<T, ignition::math::Vector2i>)
451 else if constexpr (std::is_same_v<T, ignition::math::Vector2d>)
453 else if constexpr (std::is_same_v<T, ignition::math::Vector3d>)
455 else if constexpr (std::is_same_v<T, ignition::math::Quaterniond>)
457 else if constexpr (std::is_same_v<T, ignition::math::Pose3d>)
465 void Param::SetUpdateFunc(T _updateFunc)
467 this->dataPtr->updateFunc = _updateFunc;
472 bool Param::Set(
const T &_value)
476 std::stringstream ss;
478 return this->SetFromString(ss.str(),
true);
482 sdferr <<
"Unable to set parameter["
483 << this->dataPtr->key <<
"]."
484 <<
"Type used must have a stream input and output operator,"
485 <<
"which allows proper functioning of Param.\n";
492 bool Param::Get(T &_value)
const
494 T *value = std::get_if<T>(&this->dataPtr->value);
501 std::string typeStr = this->dataPtr->TypeToString<T>();
504 sdferr <<
"Unknown parameter type[" <<
typeid(T).name() <<
"]\n";
508 std::string valueStr = this->GetAsString();
510 bool success = this->dataPtr->ValueFromStringImpl(typeStr, valueStr, pv);
514 _value = std::get<T>(pv);
516 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
523 std::stringstream tmp;
524 if (valueStr ==
"true" || valueStr ==
"1")
541 bool Param::GetDefault(T &_value)
const
543 std::stringstream ss;
552 sdferr <<
"Unable to convert parameter["
553 << this->dataPtr->key <<
"] "
555 << this->dataPtr->typeName <<
"], to "
556 <<
"type[" <<
typeid(T).name() <<
"]\n";
564 template<
typename Type>
565 bool Param::IsType()
const
567 return std::holds_alternative<Type>(this->dataPtr->value);