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 =
"");
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,
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);
bool set
True if the parameter is set.
Definition Param.hh:343
ParamVariant value
This parameter's value.
Definition Param.hh:372
ParamVariant defaultValue
This parameter's default value.
Definition Param.hh:386
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.
std::optional< std::string > strValue
This parameter's value that was provided as a string.
Definition Param.hh:380
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition Param.hh:392
std::string description
Description of the parameter.
Definition Param.hh:349
bool ValueFromStringImpl(const std::string &_typeName, const std::string &_valueStr, ParamVariant &_valueToSet) const
Method used to set the Param from a passed-in string.
std::string typeName
Definition Param.hh:346
bool required
True if the parameter is required.
Definition Param.hh:340
std::string defaultStrValue
This parameter's default value that was provided as a string.
Definition Param.hh:383
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
ElementWeakPtr parentElement
Parent element.
Definition Param.hh:352
std::string key
Key value.
Definition Param.hh:337
std::function< std::any()> updateFunc
Update function pointer.
Definition Param.hh:355
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition Param.hh:389
bool ignoreParentAttributes
True if the value has been parsed while ignoring its parent element's attributes, and will continue t...
Definition Param.hh:377
A parameter class.
Definition Param.hh:117
virtual ~Param()
Destructor.
bool IgnoresParentElementAttribute() const
Return true if the parameter ignores the parent element's attributes, or if the parameter has no pare...
const std::string & GetKey() const
Get the key value.
void Update()
Set the parameter's value using the updateFunc.
bool GetRequired() const
Return whether the parameter is required.
Param(Param &&_param) noexcept=default
Move constructor.
Param & operator=(Param &&_param) noexcept=default
Move assignment operator.
ElementPtr GetParentElement() const
Get the parent Element of this Param.
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, const std::string &_minValue, const std::string &_maxValue, const std::string &_description="")
Constructor with min and max values.
std::optional< std::string > GetMaxValueAsString(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.
Param & operator=(const Param &_param)
Copy assignment operator Note that the updateFunc member will not get copied.
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition Param.hh:321
bool GetAny(std::any &_anyVal) const
Get the value of the parameter as a std::any.
std::optional< std::string > GetMinValueAsString(const PrintConfig &_config=PrintConfig()) const
Get the minimum allowed value as a string.
bool Reparse()
Reparse the parameter value.
const std::string & GetTypeName() const
Get the type name value.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_description="")
Constructor.
void SetDescription(const std::string &_desc)
Set the description of the parameter.
void Reset()
Reset the parameter to the default value.
std::string GetDescription() const
Get the description of the parameter.
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
Param(const Param &_param)
Copy constructor Note that the updateFunc member does not get copied.
std::string GetAsString(const PrintConfig &_config=PrintConfig()) const
Get the value as a string.
bool SetFromString(const std::string &_value, bool _ignoreParentAttributes)
Set the parameter value from a string.
ParamPtr Clone() const
Clone the parameter.
std::string GetDefaultAsString(const PrintConfig &_config=PrintConfig()) const
Get the default value as a string.
bool GetSet() const
Return true if the parameter has been set.
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::weak_ptr< Element > ElementWeakPtr
Definition Element.hh:62
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition Param.hh:83
std::shared_ptr< Element > ElementPtr
Definition Element.hh:54
std::string lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
std::vector< ParamPtr > Param_V
Definition Param.hh:69
std::shared_ptr< Param > ParamPtr
Definition Param.hh:65
namespace for Simulation Description Format parser
Definition Actor.hh:34
const T & val
Definition Param.hh:77
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition system_util.hh:41