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 {
83 std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
90 inline std::ostream& operator<<(std::ostream &os, ParamStreamer<double> s)
92 os << std::setprecision(std::numeric_limits<double>::max_digits10) << s.val;
97 inline std::ostream& operator<<(std::ostream &os, ParamStreamer<float> s)
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);
211 public: ElementPtr GetParentElement()
const;
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;
266 public: ParamPtr Clone()
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,
362 unsigned int, double, float, sdf::Time,
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,
402 ParamVariant &_valueToSet)
const;
411 public:
bool StringFromValueImpl(
const PrintConfig &_config,
412 const std::string &_typeName,
413 const ParamVariant &_value,
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);
std::string description
Description of the parameter.
Definition: Param.hh:349
bool ignoreParentAttributes
True if the value has been parsed while ignoring its parent element's attributes, and will continue t...
Definition: Param.hh:377
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:321
ParamStreamer(T) -> ParamStreamer< T >
const T & val
Definition: Param.hh:77
std::string key
Key value.
Definition: Param.hh:337
std::shared_ptr< Element > ElementPtr
Definition: Element.hh:54
std::string lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
std::weak_ptr< Element > ElementWeakPtr
Definition: Element.hh:62
std::string typeName
Definition: Param.hh:346
std::optional< std::string > strValue
This parameter's value that was provided as a string.
Definition: Param.hh:380
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:355
This class contains configuration options for printing elements.
Definition: PrintConfig.hh:31
ParamVariant value
This parameter's value.
Definition: Param.hh:372
ElementWeakPtr parentElement
Parent element.
Definition: Param.hh:352
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:41
#define sdferr
Output an error message.
Definition: Console.hh:57
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition: Param.hh:389
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
std::string GetAsString(const PrintConfig &_config=PrintConfig()) const
Get the value as a string.
std::string defaultStrValue
This parameter's default value that was provided as a string.
Definition: Param.hh:383
std::ostream & operator<<(std::ostream &os, ParamStreamer< std::variant< Ts... >> sv)
Definition: Param.hh:104
namespace for Simulation Description Format parser
Definition: Actor.hh:33
std::vector< ParamPtr > Param_V
Definition: Param.hh:69
A parameter class.
Definition: Param.hh:116
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:65
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:386
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition: Param.hh:392