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 =
"");
286 bool _ignoreParentAttributes);
295 bool _ignoreParentAttributes,
361 public:
const std::string &
GetKey()
const;
366 public:
template<
typename Type>
394 public:
template<
typename T>
395 void SetUpdateFunc(T _updateFunc);
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;
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,
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);
SDF Element class.
Definition: Element.hh:76
std::variant< bool, char, std::string, int, std::uint64_t, unsigned int, double, float, sdf::Time, gz::math::Angle, gz::math::Color, gz::math::Vector2i, gz::math::Vector2d, gz::math::Vector3d, gz::math::Quaterniond, gz::math::Pose3d > ParamVariant
Definition: Param.hh:537
void Init(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, sdf::Errors &_errors, const std::string &_description)
Initializer function to help Param constructors.
static std::string TypeToString()
Data type to string mapping.
Definition: Param.hh:626
bool set
True if the parameter is set.
Definition: Param.hh:510
bool required
True if the parameter is required.
Definition: Param.hh:507
bool GZ_SDFORMAT_VISIBLE ValueFromStringImpl(const std::string &_typeName, const std::string &_valueStr, ParamVariant &_valueToSet, sdf::Errors &_errors) const
Method used to set the Param from a passed-in string.
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:554
ElementWeakPtr parentElement
Parent element.
Definition: Param.hh:519
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:522
std::string defaultStrValue
This parameter's default value that was provided as a string.
Definition: Param.hh:551
std::string typeName
Definition: Param.hh:513
void Init(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_minValue, const std::string &_maxValue, sdf::Errors &_errors, const std::string &_description)
Initializer function to help Param constructors.
bool StringFromValueImpl(const PrintConfig &_config, const std::string &_typeName, const ParamVariant &_value, std::string &_valueStr, sdf::Errors &_errors) const
Method used to get the string representation from a ParamVariant, or the string that was used to set ...
bool ignoreParentAttributes
True if the value has been parsed while ignoring its parent element's attributes, and will continue t...
Definition: Param.hh:545
ParamVariant value
This parameter's value.
Definition: Param.hh:540
std::string key
Key value.
Definition: Param.hh:504
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition: Param.hh:560
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition: Param.hh:557
std::optional< std::string > strValue
This parameter's value that was provided as a string.
Definition: Param.hh:548
std::string description
Description of the parameter.
Definition: Param.hh:516
A parameter class.
Definition: Param.hh:136
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.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_minValue, const std::string &_maxValue, sdf::Errors &_errors, const std::string &_description="")
Constructor with min and max values.
std::string GetAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the value as a string.
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:674
bool Reparse(sdf::Errors &_errors)
Reparse the parameter value.
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:667
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:488
std::string GetDefaultAsString(const PrintConfig &_config=PrintConfig()) const
Get the default value as a string.
bool SetFromString(const std::string &_value, bool _ignoreParentAttributes, sdf::Errors &_errors)
Set the parameter value from a string.
bool SetParentElement(ElementPtr _parentElement, sdf::Errors &_errors)
Set the parent Element of this Param.
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.
bool SetFromString(const std::string &_value, sdf::Errors &_errors)
Set the parameter value from a string.
bool GetAny(std::any &_anyVal, sdf::Errors &_errors) const
Get the value of the parameter as a std::any.
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.
std::string GetDefaultAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the default value as a string.
bool GetSet() const
Return true if the parameter has been set.
bool SetParentElement(ElementPtr _parentElement)
Set the parent Element of this Param.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, sdf::Errors &_errors, const std::string &_description="")
Constructor.
void Update(sdf::Errors &_errors)
Set the parameter's value using the updateFunc.
std::optional< std::string > GetMaxValueAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the maximum allowed value as a string.
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:706
bool Reparse()
Reparse the parameter value.
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:805
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.
std::optional< std::string > GetMinValueAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the minimum allowed value as a string.
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:768
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.
bool ValidateValue(sdf::Errors &_errors) const
Validate the value against minimum and maximum allowed values.
This class contains configuration options for printing elements.
Definition: PrintConfig.hh:33
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:107
#define sdferr
Output an error message.
Definition: Console.hh:57
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:70
std::vector< ParamPtr > Param_V
Definition: Param.hh:74
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:94
std::string GZ_SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
@ PARAMETER_ERROR
Generic error type for parameters (values of SDFormat elements or attributes).
@ UNKNOWN_PARAMETER_TYPE
The specified parameter (values of SDFormat elements or attributes) type is unknown.
ParamStreamer(const ParamVariant &_val, int _precision) -> ParamStreamer< ParamVariant >
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:95
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:63
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:55
namespace for Simulation Description Format parser
Definition: Actor.hh:35
const int precision
Definition: Param.hh:83
const T & val
Definition: Param.hh:82
ParamStreamer(const T &_val, int _precision=0)
Definition: Param.hh:84
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:25