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 =
"");
202 bool _ignoreParentAttributes);
238 public:
const std::string &
GetKey()
const;
243 public:
template<
typename Type>
271 public:
template<
typename T>
272 void SetUpdateFunc(T _updateFunc);
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;
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,
413 const std::string &_typeName,
415 std::string &_valueStr)
const;
428 const std::string &_typeName,
430 const std::optional<std::string> &_originalStr,
431 std::string &_valueStr)
const;
435 public:
template<
typename T>
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>)
484 this->dataPtr->updateFunc = _updateFunc;
493 std::stringstream ss;
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";
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";
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")
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>
584 return std::holds_alternative<Type>(this->dataPtr->value);
SDF Element class.
Definition: Element.hh:75
bool set
True if the parameter is set.
Definition: Param.hh:343
bool required
True if the parameter is required.
Definition: Param.hh:340
std::variant< bool, char, std::string, int, std::uint64_t, unsigned int, double, float, sdf::Time, ignition::math::Angle, ignition::math::Color, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Vector3d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:369
bool StringFromValueImpl(const PrintConfig &_config, const std::string &_typeName, const ParamVariant &_value, std::string &_valueStr) const
Method used to get the string representation from a ParamVariant, or the string that was used to set ...
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:386
std::string TypeToString() const
Data type to string mapping.
Definition: Param.hh:441
ElementWeakPtr parentElement
Parent element.
Definition: Param.hh:352
bool IGNITION_SDFORMAT_VISIBLE ValueFromStringImpl(const std::string &_typeName, const std::string &_valueStr, ParamVariant &_valueToSet) const
Method used to set the Param from a passed-in string.
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:355
std::string defaultStrValue
This parameter's default value that was provided as a string.
Definition: Param.hh:383
std::string typeName
Definition: Param.hh:346
bool ignoreParentAttributes
True if the value has been parsed while ignoring its parent element's attributes, and will continue t...
Definition: Param.hh:377
ParamVariant value
This parameter's value.
Definition: Param.hh:372
std::string key
Key value.
Definition: Param.hh:337
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition: Param.hh:392
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition: Param.hh:389
std::optional< std::string > strValue
This parameter's value that was provided as a string.
Definition: Param.hh:380
std::string description
Description of the parameter.
Definition: Param.hh:349
bool StringFromValueImpl(const PrintConfig &_config, const std::string &_typeName, const ParamVariant &_value, const std::optional< std::string > &_originalStr, std::string &_valueStr) const
Method used to get the string representation from a ParamVariant, or the string that was used to set ...
A parameter class.
Definition: Param.hh:117
Param(Param &&_param) noexcept=default
Move constructor.
bool SetFromString(const std::string &_value, bool _ignoreParentAttributes)
Set the parameter value from a string.
ElementPtr GetParentElement() const
Get the parent Element of this Param.
std::optional< std::string > GetMaxValueAsString(const PrintConfig &_config=PrintConfig()) const
Get the maximum allowed value as a string.
const std::string & GetTypeName() const
Get the type name value.
const std::string & GetKey() const
Get the key value.
Param & operator=(Param &&_param) noexcept=default
Move assignment operator.
std::string GetAsString(const PrintConfig &_config=PrintConfig()) const
Get the value as a string.
void SetDescription(const std::string &_desc)
Set the description of the parameter.
virtual ~Param()
Destructor.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_minValue, const std::string &_maxValue, const std::string &_description="")
Constructor with min and max values.
void Update()
Set the parameter's value using the updateFunc.
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:489
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:482
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:321
std::string GetDefaultAsString(const PrintConfig &_config=PrintConfig()) const
Get the default value as a string.
Param & operator=(const Param &_param)
Copy assignment operator Note that the updateFunc member will not get copied.
ParamPtr Clone() const
Clone the parameter.
Param(const Param &_param)
Copy constructor Note that the updateFunc member does not get copied.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_description="")
Constructor.
std::string GetDescription() const
Get the description of the parameter.
bool GetSet() const
Return true if the parameter has been set.
bool SetParentElement(ElementPtr _parentElement)
Set the parent Element of this Param.
bool ValidateValue() const
Validate the value against minimum and maximum allowed values.
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:509
bool Reparse()
Reparse the parameter value.
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:582
bool GetAny(std::any &_anyVal) const
Get the value of the parameter as a std::any.
bool IgnoresParentElementAttribute() const
Return true if the parameter ignores the parent element's attributes, or if the parameter has no pare...
void Reset()
Reset the parameter to the default value.
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:558
bool GetRequired() const
Return whether the parameter is required.
std::optional< std::string > GetMinValueAsString(const PrintConfig &_config=PrintConfig()) const
Get the minimum allowed value as a string.
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
This class contains configuration options for printing elements.
Definition: PrintConfig.hh:32
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:118
#define sdferr
Output an error message.
Definition: Console.hh:57
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:65
std::vector< ParamPtr > Param_V
Definition: Param.hh:69
std::string IGNITION_SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:83
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:62
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:54
ParamStreamer(T) -> ParamStreamer< T >
namespace for Simulation Description Format parser
Definition: Actor.hh:35
const T & val
Definition: Param.hh:77
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:25